mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-21 06:59:09 +00:00
refactor: extract utility hook
This commit is contained in:
committed by
Carlos Valente
parent
5d6345717f
commit
c5a5eab225
@@ -1,3 +1,4 @@
|
|||||||
|
import { useMemo } from 'react';
|
||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { ProjectFileListResponse } from 'ontime-types';
|
import { ProjectFileListResponse } from 'ontime-types';
|
||||||
|
|
||||||
@@ -22,3 +23,23 @@ export function useProjectList() {
|
|||||||
});
|
});
|
||||||
return { data: data ?? placeholderProjectList, status, refetch };
|
return { data: data ?? placeholderProjectList, status, refetch };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useOrderedProjectList() {
|
||||||
|
const response = useProjectList();
|
||||||
|
const { files, lastLoadedProject } = response.data;
|
||||||
|
|
||||||
|
const reorderedProjectFiles = useMemo(() => {
|
||||||
|
if (!files.length) return [];
|
||||||
|
|
||||||
|
const currentlyLoadedIndex = files.findIndex((project) => project.filename === lastLoadedProject);
|
||||||
|
|
||||||
|
if (currentlyLoadedIndex === -1) return files;
|
||||||
|
|
||||||
|
const projectFiles = [...files];
|
||||||
|
const current = projectFiles.splice(currentlyLoadedIndex, 1)[0];
|
||||||
|
|
||||||
|
return [current, ...projectFiles];
|
||||||
|
}, [files, lastLoadedProject]);
|
||||||
|
|
||||||
|
return { ...response, data: { reorderedProjectFiles, lastLoadedProject: response.data.lastLoadedProject } };
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMemo, useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
|
||||||
import { useProjectList } from '../../../../common/hooks-query/useProjectList';
|
import { useOrderedProjectList } from '../../../../common/hooks-query/useProjectList';
|
||||||
import * as Panel from '../../panel-utils/PanelUtils';
|
import * as Panel from '../../panel-utils/PanelUtils';
|
||||||
|
|
||||||
import ProjectListItem, { EditMode } from './ProjectListItem';
|
import ProjectListItem, { EditMode } from './ProjectListItem';
|
||||||
@@ -8,8 +8,7 @@ import ProjectListItem, { EditMode } from './ProjectListItem';
|
|||||||
import style from './ProjectPanel.module.scss';
|
import style from './ProjectPanel.module.scss';
|
||||||
|
|
||||||
export default function ProjectList() {
|
export default function ProjectList() {
|
||||||
const { data, refetch } = useProjectList();
|
const { data, refetch } = useOrderedProjectList();
|
||||||
const { files, lastLoadedProject } = data;
|
|
||||||
|
|
||||||
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);
|
||||||
@@ -28,16 +27,6 @@ export default function ProjectList() {
|
|||||||
await refetch();
|
await refetch();
|
||||||
};
|
};
|
||||||
|
|
||||||
const reorderedProjectFiles = useMemo(() => {
|
|
||||||
if (!data.files?.length) return [];
|
|
||||||
|
|
||||||
const currentlyLoadedIndex = files.findIndex((project) => project.filename === lastLoadedProject);
|
|
||||||
const projectFiles = [...files];
|
|
||||||
const current = projectFiles.splice(currentlyLoadedIndex, 1)?.[0];
|
|
||||||
|
|
||||||
return [current, ...projectFiles];
|
|
||||||
}, [data.files?.length, files, lastLoadedProject]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel.Table>
|
<Panel.Table>
|
||||||
<thead>
|
<thead>
|
||||||
@@ -48,7 +37,7 @@ export default function ProjectList() {
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{reorderedProjectFiles.map((project) => (
|
{data.reorderedProjectFiles.map((project) => (
|
||||||
<ProjectListItem
|
<ProjectListItem
|
||||||
key={project.filename}
|
key={project.filename}
|
||||||
filename={project.filename}
|
filename={project.filename}
|
||||||
@@ -58,7 +47,7 @@ export default function ProjectList() {
|
|||||||
onRefetch={handleRefetch}
|
onRefetch={handleRefetch}
|
||||||
editingFilename={editingFilename}
|
editingFilename={editingFilename}
|
||||||
editingMode={editingMode}
|
editingMode={editingMode}
|
||||||
current={project.filename === lastLoadedProject}
|
current={project.filename === data.lastLoadedProject}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|||||||
Reference in New Issue
Block a user