mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-21 09:38:01 +02:00
39 lines
1.0 KiB
TypeScript
39 lines
1.0 KiB
TypeScript
|
import { useEffect, useRef, useState } from 'react';
|
||
|
import usePrevious from '@joplin/lib/hooks/usePrevious';
|
||
|
|
||
|
const useActiveDescendantId = (selectedFolderId: string, selectedNoteIds: string[]) => {
|
||
|
const selectedNoteIdsRef = useRef(selectedNoteIds);
|
||
|
selectedNoteIdsRef.current = selectedNoteIds;
|
||
|
|
||
|
const [activeNoteId, setActiveNoteId] = useState('');
|
||
|
useEffect(() => {
|
||
|
setActiveNoteId(selectedNoteIdsRef.current?.[0] ?? '');
|
||
|
}, [selectedFolderId]);
|
||
|
|
||
|
const previousNoteIdsRef = useRef<string[]>();
|
||
|
previousNoteIdsRef.current = usePrevious(selectedNoteIds);
|
||
|
|
||
|
useEffect(() => {
|
||
|
const previousNoteIds = previousNoteIdsRef.current ?? [];
|
||
|
|
||
|
setActiveNoteId(current => {
|
||
|
if (selectedNoteIds.includes(current)) {
|
||
|
return current;
|
||
|
} else {
|
||
|
// Prefer added items
|
||
|
for (const id of selectedNoteIds) {
|
||
|
if (!previousNoteIds.includes(id)) {
|
||
|
return id;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return selectedNoteIds[0] ?? '';
|
||
|
}
|
||
|
});
|
||
|
}, [selectedNoteIds]);
|
||
|
|
||
|
return { activeNoteId, setActiveNoteId };
|
||
|
};
|
||
|
|
||
|
export default useActiveDescendantId;
|