mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-20 06:29:07 +00:00
refactor: useSuspenseQuery for project list
This commit is contained in:
@@ -1,24 +1,18 @@
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useSuspenseQuery } from '@tanstack/react-query';
|
||||||
import { ProjectFile, ProjectFileList, ProjectFileListResponse } from 'ontime-types';
|
import { ProjectFile, ProjectFileList } from 'ontime-types';
|
||||||
|
import { MILLIS_PER_HOUR } from 'ontime-utils';
|
||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { queryRefetchIntervalSlow } from '../../ontimeConfig';
|
|
||||||
import { PROJECT_LIST } from '../api/constants';
|
import { PROJECT_LIST } from '../api/constants';
|
||||||
import { getProjects } from '../api/db';
|
import { getProjects } from '../api/db';
|
||||||
|
|
||||||
const placeholderProjectList: ProjectFileListResponse = {
|
export function useProjectList() {
|
||||||
files: [],
|
const { data, status, refetch } = useSuspenseQuery({
|
||||||
lastLoadedProject: '',
|
|
||||||
};
|
|
||||||
|
|
||||||
function useProjectList() {
|
|
||||||
const { data, status, refetch } = useQuery({
|
|
||||||
queryKey: PROJECT_LIST,
|
queryKey: PROJECT_LIST,
|
||||||
queryFn: ({ signal }) => getProjects({ signal }),
|
queryFn: ({ signal }) => getProjects({ signal }),
|
||||||
placeholderData: (previousData, _previousQuery) => previousData,
|
staleTime: MILLIS_PER_HOUR,
|
||||||
refetchInterval: queryRefetchIntervalSlow,
|
|
||||||
});
|
});
|
||||||
return { data: data ?? placeholderProjectList, status, refetch };
|
return { data, status, refetch };
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ProjectSortMode = 'alphabetical-asc' | 'alphabetical-desc' | 'modified-asc' | 'modified-desc';
|
export type ProjectSortMode = 'alphabetical-asc' | 'alphabetical-desc' | 'modified-asc' | 'modified-desc';
|
||||||
|
|||||||
@@ -216,6 +216,9 @@ export const connectSocket = () => {
|
|||||||
case RefetchKey.ProjectRundowns:
|
case RefetchKey.ProjectRundowns:
|
||||||
ontimeQueryClient.invalidateQueries({ queryKey: PROJECT_RUNDOWNS });
|
ontimeQueryClient.invalidateQueries({ queryKey: PROJECT_RUNDOWNS });
|
||||||
break;
|
break;
|
||||||
|
case RefetchKey.ProjectFiles:
|
||||||
|
ontimeQueryClient.invalidateQueries({ queryKey: PROJECT_RUNDOWNS });
|
||||||
|
break;
|
||||||
default: {
|
default: {
|
||||||
target satisfies never;
|
target satisfies never;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { useState } from 'react';
|
import { Suspense, useState } from 'react';
|
||||||
import { IoArrowDown, IoArrowUp } from 'react-icons/io5';
|
import { IoArrowDown, IoArrowUp } from 'react-icons/io5';
|
||||||
|
|
||||||
import Info from '../../../../common/components/info/Info';
|
import Info from '../../../../common/components/info/Info';
|
||||||
@@ -11,11 +11,25 @@ import style from './ProjectPanel.module.scss';
|
|||||||
type SortParameter = 'alphabetical' | 'modified';
|
type SortParameter = 'alphabetical' | 'modified';
|
||||||
|
|
||||||
export default function ProjectList() {
|
export default function ProjectList() {
|
||||||
|
return (
|
||||||
|
<Suspense
|
||||||
|
fallback={
|
||||||
|
<div className={style.empty}>
|
||||||
|
<Panel.Loader isLoading />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<ProjectListSuspend />
|
||||||
|
</Suspense>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ProjectListSuspend() {
|
||||||
const [editingMode, setEditingMode] = useState<EditMode | null>(null);
|
const [editingMode, setEditingMode] = useState<EditMode | null>(null);
|
||||||
const [editingFilename, setEditingFilename] = useState<string | null>(null);
|
const [editingFilename, setEditingFilename] = useState<string | null>(null);
|
||||||
const [sortMode, setSortMode] = useState<ProjectSortMode>('modified-desc');
|
const [sortMode, setSortMode] = useState<ProjectSortMode>('modified-desc');
|
||||||
|
|
||||||
const { data, refetch, status } = useOrderedProjectList(sortMode);
|
const { data, refetch } = useOrderedProjectList(sortMode);
|
||||||
|
|
||||||
const handleToggleEditMode = (editMode: EditMode, filename: string | null) => {
|
const handleToggleEditMode = (editMode: EditMode, filename: string | null) => {
|
||||||
setEditingMode((prev) => (prev === editMode && filename === editingFilename ? null : editMode));
|
setEditingMode((prev) => (prev === editMode && filename === editingFilename ? null : editMode));
|
||||||
@@ -38,14 +52,6 @@ export default function ProjectList() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (status === 'pending') {
|
|
||||||
return (
|
|
||||||
<div className={style.empty}>
|
|
||||||
<Panel.Loader isLoading />
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const numProjects = data.reorderedProjectFiles.length;
|
const numProjects = data.reorderedProjectFiles.length;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import { copyFile } from 'fs/promises';
|
import { copyFile } from 'fs/promises';
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
|
|
||||||
import { DatabaseModel, LogOrigin, ProjectFileListResponse } from 'ontime-types';
|
import { DatabaseModel, LogOrigin, ProjectFileListResponse, RefetchKey } from 'ontime-types';
|
||||||
import { getErrorMessage, getFirstRundown } from 'ontime-utils';
|
import { getErrorMessage, getFirstRundown } from 'ontime-utils';
|
||||||
|
|
||||||
|
import { sendRefetch } from '../../adapters/WebsocketAdapter.js';
|
||||||
import { parseCustomFields } from '../../api-data/custom-fields/customFields.parser.js';
|
import { parseCustomFields } from '../../api-data/custom-fields/customFields.parser.js';
|
||||||
import { parseDatabaseModel } from '../../api-data/db/db.parser.js';
|
import { parseDatabaseModel } from '../../api-data/db/db.parser.js';
|
||||||
import { getCurrentRundown } from '../../api-data/rundown/rundown.dao.js';
|
import { getCurrentRundown } from '../../api-data/rundown/rundown.dao.js';
|
||||||
@@ -72,6 +73,9 @@ export async function getCurrentProject(): Promise<{ filename: string; pathToFil
|
|||||||
// we know the project is loaded since we force initialisation above
|
// we know the project is loaded since we force initialisation above
|
||||||
const pathToFile = getPathToProject(currentProjectState.currentProjectName as string);
|
const pathToFile = getPathToProject(currentProjectState.currentProjectName as string);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return { filename: currentProjectState.currentProjectName as string, pathToFile };
|
return { filename: currentProjectState.currentProjectName as string, pathToFile };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +109,9 @@ async function loadProject(projectData: DatabaseModel, fileName: string, rundown
|
|||||||
currentProjectName: fileName,
|
currentProjectName: fileName,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +120,9 @@ async function loadProject(projectData: DatabaseModel, fileName: string, rundown
|
|||||||
*/
|
*/
|
||||||
export async function loadDemoProject(): Promise<string> {
|
export async function loadDemoProject(): Promise<string> {
|
||||||
populateOntimeLogo();
|
populateOntimeLogo();
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return createProject(config.demoProject, demoDb);
|
return createProject(config.demoProject, demoDb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -122,6 +132,9 @@ export async function loadDemoProject(): Promise<string> {
|
|||||||
*/
|
*/
|
||||||
async function loadNewProject(): Promise<string> {
|
async function loadNewProject(): Promise<string> {
|
||||||
const emptyProject = makeNewProject();
|
const emptyProject = makeNewProject();
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return createProject(config.newProject, emptyProject);
|
return createProject(config.newProject, emptyProject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,6 +155,9 @@ async function handleCorruptedFile(filePath: string, fileName: string): Promise<
|
|||||||
// and make a new file with the recovered data
|
// and make a new file with the recovered data
|
||||||
const newPath = appendToName(filePath, '(recovered)');
|
const newPath = appendToName(filePath, '(recovered)');
|
||||||
await dockerSafeRename(filePath, newPath);
|
await dockerSafeRename(filePath, newPath);
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return getFileNameFromPath(newPath);
|
return getFileNameFromPath(newPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,6 +178,9 @@ async function handleMigratedFile(filePath: string, fileName: string): Promise<s
|
|||||||
// and make a new file with the migrated data
|
// and make a new file with the migrated data
|
||||||
const newPath = appendToName(filePath, '(migrated)');
|
const newPath = appendToName(filePath, '(migrated)');
|
||||||
await dockerSafeRename(filePath, newPath);
|
await dockerSafeRename(filePath, newPath);
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return getFileNameFromPath(newPath);
|
return getFileNameFromPath(newPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,6 +219,9 @@ export async function initialiseProject(): Promise<string> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return loadNewProject();
|
return loadNewProject();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,6 +253,10 @@ export async function loadProjectFile(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const projectName = await loadProject(result.data, parsedFileName, options?.rundownId);
|
const projectName = await loadProject(result.data, parsedFileName, options?.rundownId);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return projectName;
|
return projectName;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -241,6 +267,9 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
|
|||||||
const files = await getProjectFiles();
|
const files = await getProjectFiles();
|
||||||
const lastLoaded = await getLastLoaded();
|
const lastLoaded = await getLastLoaded();
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
files,
|
files,
|
||||||
lastLoadedProject: lastLoaded?.projectName ? removeFileExtension(lastLoaded?.projectName) : '',
|
lastLoadedProject: lastLoaded?.projectName ? removeFileExtension(lastLoaded?.projectName) : '',
|
||||||
@@ -263,6 +292,10 @@ export async function duplicateProjectFile(originalFile: string, newFilename: st
|
|||||||
|
|
||||||
const pathToDuplicate = getPathToProject(newFilename);
|
const pathToDuplicate = getPathToProject(newFilename);
|
||||||
await copyFile(projectFilePath, pathToDuplicate);
|
await copyFile(projectFilePath, pathToDuplicate);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,8 +324,16 @@ export async function renameProjectFile(originalFile: string, newFilename: strin
|
|||||||
const projectData = parseDatabaseModel(fileData);
|
const projectData = parseDatabaseModel(fileData);
|
||||||
|
|
||||||
const newFileName = await loadProject(projectData.data, newFilename);
|
const newFileName = await loadProject(projectData.data, newFilename);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return newFileName;
|
return newFileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return newFilename;
|
return newFilename;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,6 +345,10 @@ export async function renameProjectFile(originalFile: string, newFilename: strin
|
|||||||
export async function createProject(fileName: string, initialData: DatabaseModel): Promise<string> {
|
export async function createProject(fileName: string, initialData: DatabaseModel): Promise<string> {
|
||||||
const fileNameWithExtension = generateUniqueFileName(publicDir.projectsDir, ensureJsonExtension(fileName));
|
const fileNameWithExtension = generateUniqueFileName(publicDir.projectsDir, ensureJsonExtension(fileName));
|
||||||
await loadProject(initialData, fileNameWithExtension);
|
await loadProject(initialData, fileNameWithExtension);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return fileNameWithExtension;
|
return fileNameWithExtension;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -315,6 +360,10 @@ export async function createProject(fileName: string, initialData: DatabaseModel
|
|||||||
export async function createProjectWithPatch(fileName: string, initialData: Partial<DatabaseModel>): Promise<string> {
|
export async function createProjectWithPatch(fileName: string, initialData: Partial<DatabaseModel>): Promise<string> {
|
||||||
const newProject = makeNewProject();
|
const newProject = makeNewProject();
|
||||||
const sanitisedData = safeMerge(newProject, initialData);
|
const sanitisedData = safeMerge(newProject, initialData);
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return createProject(fileName, sanitisedData);
|
return createProject(fileName, sanitisedData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -331,6 +380,9 @@ export async function deleteProjectFile(filename: string) {
|
|||||||
throw new Error('Project file not found');
|
throw new Error('Project file not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
await deleteFile(projectFilePath);
|
await deleteFile(projectFilePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,5 +427,9 @@ export async function patchCurrentProject(data: Partial<DatabaseModel>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const updatedData = await getDataProvider().getData();
|
const updatedData = await getDataProvider().getData();
|
||||||
|
|
||||||
|
setImmediate(() => {
|
||||||
|
sendRefetch(RefetchKey.ProjectFiles);
|
||||||
|
});
|
||||||
return updatedData;
|
return updatedData;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export enum RefetchKey {
|
export enum RefetchKey {
|
||||||
All = 'all',
|
All = 'all',
|
||||||
CustomFields = 'custom-fields',
|
CustomFields = 'custom-fields',
|
||||||
|
ProjectFiles = 'project-files',
|
||||||
ProjectData = 'project-data',
|
ProjectData = 'project-data',
|
||||||
ProjectRundowns = 'project-rundowns',
|
ProjectRundowns = 'project-rundowns',
|
||||||
Report = 'report',
|
Report = 'report',
|
||||||
|
|||||||
Reference in New Issue
Block a user