114 lines
2.7 KiB
TypeScript
114 lines
2.7 KiB
TypeScript
import { useCallback, useMemo } from 'react';
|
|
import { useDocumentSelection } from './useDocumentSelection';
|
|
import { isDocumentEntry, isFolderEntry, getEntryId } from './entryKey';
|
|
|
|
interface SelectionEntry {
|
|
entryKey?: string;
|
|
// Legacy field for compatibility
|
|
rowKey?: string;
|
|
[key: string]: unknown;
|
|
}
|
|
|
|
interface WorkspaceSelectionOptions {
|
|
onDocumentActivate?: (id: string) => void;
|
|
onInspectFolder?: (id: string) => void;
|
|
}
|
|
|
|
const identity = <T,>(value: T) => value;
|
|
|
|
export const useWorkspaceSelection = ({
|
|
onDocumentActivate = identity,
|
|
onInspectFolder = identity,
|
|
}: WorkspaceSelectionOptions = {}) => {
|
|
const selection = useDocumentSelection();
|
|
|
|
const {
|
|
selectedEntries,
|
|
setSelectedEntries,
|
|
selectionOrder,
|
|
setSelectionOrder,
|
|
selectionOrderRef,
|
|
selectionAnchorRef,
|
|
selectionInitializedRef,
|
|
focusedDocumentId,
|
|
setFocusedDocumentId,
|
|
focusedRowKey,
|
|
setFocusedRowKey,
|
|
applySelection,
|
|
clearSelection,
|
|
handleEntrySelection,
|
|
promoteSelectionOrder,
|
|
configureSelectionEnvironment,
|
|
} = selection;
|
|
|
|
const selectedDocumentIds = useMemo(
|
|
() =>
|
|
selectedEntries
|
|
.filter((entry) => isDocumentEntry(entry))
|
|
.map((entry) => getEntryId(entry))
|
|
.filter(Boolean),
|
|
[selectedEntries],
|
|
);
|
|
|
|
const selectedFolderIds = useMemo(
|
|
() =>
|
|
selectedEntries
|
|
.filter((entry) => isFolderEntry(entry))
|
|
.map((entry) => getEntryId(entry))
|
|
.filter(Boolean),
|
|
[selectedEntries],
|
|
);
|
|
|
|
const selectEntry = useCallback(
|
|
(entry: SelectionEntry | string, event?: unknown) => {
|
|
const rowKey = entry && Object(entry) === entry
|
|
? (entry as SelectionEntry).rowKey ?? undefined
|
|
: (entry as string);
|
|
if (!rowKey) return;
|
|
handleEntrySelection(rowKey, event);
|
|
},
|
|
[handleEntrySelection],
|
|
);
|
|
|
|
const inspectDocument = useCallback(
|
|
(documentId?: string) => {
|
|
if (!documentId) return;
|
|
onDocumentActivate(documentId);
|
|
},
|
|
[onDocumentActivate],
|
|
);
|
|
|
|
const inspectFolder = useCallback(
|
|
(folderId?: string) => {
|
|
if (!folderId) return;
|
|
onInspectFolder(folderId);
|
|
},
|
|
[onInspectFolder],
|
|
);
|
|
|
|
return {
|
|
selectedEntries,
|
|
selectedDocumentIds,
|
|
selectedFolderIds,
|
|
selectionOrder,
|
|
selectionOrderRef,
|
|
selectionAnchorRef,
|
|
selectionInitializedRef,
|
|
focusedDocumentId,
|
|
setFocusedDocumentId,
|
|
focusedRowKey,
|
|
setFocusedRowKey,
|
|
applySelection,
|
|
clearSelection,
|
|
handleEntrySelection: selectEntry,
|
|
promoteSelectionOrder,
|
|
configureSelectionEnvironment,
|
|
setSelectedEntries,
|
|
setSelectionOrder,
|
|
inspectDocument,
|
|
inspectFolder,
|
|
};
|
|
};
|
|
|
|
export default useWorkspaceSelection;
|