Simplify sorting logic in App.tsx
This commit is contained in:
22
src/App.tsx
22
src/App.tsx
@@ -182,18 +182,8 @@ export default function App() {
|
|||||||
const showLineNumbersActive = showLineNumbers && !markdownPreview;
|
const showLineNumbersActive = showLineNumbers && !markdownPreview;
|
||||||
const hasSearch = search.trim().length > 0;
|
const hasSearch = search.trim().length > 0;
|
||||||
|
|
||||||
const sortByOrderThenUpdated = useCallback(
|
const sortByUpdated = useCallback(
|
||||||
<T extends { sort_order: number | null; updated_at: number }>(a: T, b: T) => {
|
<T extends { updated_at: number }>(a: T, b: T) => b.updated_at - a.updated_at,
|
||||||
const aHasOrder = a.sort_order !== null && a.sort_order !== undefined;
|
|
||||||
const bHasOrder = b.sort_order !== null && b.sort_order !== undefined;
|
|
||||||
if (aHasOrder && bHasOrder) {
|
|
||||||
return (a.sort_order ?? 0) - (b.sort_order ?? 0);
|
|
||||||
}
|
|
||||||
if (aHasOrder !== bHasOrder) {
|
|
||||||
return aHasOrder ? -1 : 1;
|
|
||||||
}
|
|
||||||
return b.updated_at - a.updated_at;
|
|
||||||
},
|
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -217,10 +207,10 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const [key, list] of map.entries()) {
|
for (const [key, list] of map.entries()) {
|
||||||
map.set(key, [...list].sort(sortByOrderThenUpdated));
|
map.set(key, [...list].sort(sortByUpdated));
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}, [folders, sortByOrderThenUpdated]);
|
}, [folders, sortByUpdated]);
|
||||||
|
|
||||||
const textsByFolder = useMemo(() => {
|
const textsByFolder = useMemo(() => {
|
||||||
const map = new Map<string | null, Text[]>();
|
const map = new Map<string | null, Text[]>();
|
||||||
@@ -234,10 +224,10 @@ export default function App() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const [key, list] of map.entries()) {
|
for (const [key, list] of map.entries()) {
|
||||||
map.set(key, [...list].sort(sortByOrderThenUpdated));
|
map.set(key, [...list].sort(sortByUpdated));
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}, [texts, sortByOrderThenUpdated]);
|
}, [texts, sortByUpdated]);
|
||||||
|
|
||||||
const visibleFolderIds = useMemo(() => {
|
const visibleFolderIds = useMemo(() => {
|
||||||
if (!hasSearch) return null;
|
if (!hasSearch) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user