fix: debounce folder keyboard-reorder persistence
Holding Up/Down on a folder's drag handle fired a reorder_folders DB write per keystroke. Update the local order immediately for responsiveness, but debounce the persist (400ms) with cleanup on unmount.
This commit is contained in:
@@ -358,6 +358,14 @@ export function Sidebar() {
|
|||||||
const customFoldersRef = useRef(folders);
|
const customFoldersRef = useRef(folders);
|
||||||
const pointerYRef = useRef(0);
|
const pointerYRef = useRef(0);
|
||||||
const autoScrollFrameRef = useRef<number | null>(null);
|
const autoScrollFrameRef = useRef<number | null>(null);
|
||||||
|
const keyboardPersistRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
if (keyboardPersistRef.current) clearTimeout(keyboardPersistRef.current);
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (draggedFolderId !== null) return;
|
if (draggedFolderId !== null) return;
|
||||||
@@ -442,7 +450,13 @@ export function Sidebar() {
|
|||||||
[next[currentIndex], next[nextIndex]] = [next[nextIndex], next[currentIndex]];
|
[next[currentIndex], next[nextIndex]] = [next[nextIndex], next[currentIndex]];
|
||||||
customFoldersRef.current = next;
|
customFoldersRef.current = next;
|
||||||
setCustomFolders(next);
|
setCustomFolders(next);
|
||||||
void reorderFolders(next.map((folder) => folder.id));
|
// Debounce the DB write so a held arrow key doesn't fire one per keystroke;
|
||||||
|
// the local order updates immediately, only the persist waits to settle.
|
||||||
|
if (keyboardPersistRef.current) clearTimeout(keyboardPersistRef.current);
|
||||||
|
keyboardPersistRef.current = setTimeout(() => {
|
||||||
|
keyboardPersistRef.current = null;
|
||||||
|
void reorderFolders(customFoldersRef.current.map((folder) => folder.id));
|
||||||
|
}, 400);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAddFolder = async () => {
|
const handleAddFolder = async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user