refactor: Standardize document and folder identification with a new entryKey module.

This commit is contained in:
2025-11-24 23:13:11 +01:00
parent 096c377897
commit 22f0c040a2
12 changed files with 146 additions and 202 deletions
+16 -31
View File
@@ -1,19 +1,15 @@
import { useCallback, useMemo } from 'react';
import { useDocumentSelection } from './useDocumentSelection';
type RowKey = string;
import { isDocumentEntry, isFolderEntry, getEntryId } from './entryKey';
interface SelectionEntry {
rowKey?: RowKey;
entryKey?: string;
// Legacy field for compatibility
rowKey?: string;
[key: string]: unknown;
}
interface WorkspaceSelectionOptions {
resolveDocumentRowKey?: (id: string | number) => RowKey | null;
resolveFolderRowKey?: (id: string | number) => RowKey | null;
isDocumentRowKey?: (key: RowKey | SelectionEntry) => boolean;
isFolderRowKey?: (key: RowKey | SelectionEntry) => boolean;
getRowId?: (key: RowKey | SelectionEntry) => string | number | null;
onDocumentActivate?: (id: string | number) => void;
onInspectFolder?: (id: string | number) => void;
}
@@ -21,21 +17,10 @@ interface WorkspaceSelectionOptions {
const identity = <T,>(value: T) => value;
export const useWorkspaceSelection = ({
resolveDocumentRowKey,
resolveFolderRowKey,
isDocumentRowKey = () => false,
isFolderRowKey = () => false,
getRowId = () => null,
onDocumentActivate = identity,
onInspectFolder = identity,
}: WorkspaceSelectionOptions = {}) => {
const selection = useDocumentSelection({
resolveDocumentRowKey,
resolveFolderRowKey,
isDocumentRowKey,
isFolderRowKey,
getRowId,
});
const selection = useDocumentSelection();
const {
selectedEntries,
@@ -59,26 +44,26 @@ export const useWorkspaceSelection = ({
const selectedDocumentIds = useMemo(
() =>
selectedEntries
.filter((entry) => isDocumentRowKey(entry))
.map((entry) => getRowId(entry))
.filter((entry) => isDocumentEntry(entry))
.map((entry) => getEntryId(entry))
.filter(Boolean),
[selectedEntries, isDocumentRowKey, getRowId],
[selectedEntries],
);
const selectedFolderIds = useMemo(
() =>
selectedEntries
.filter((entry) => isFolderRowKey(entry))
.map((entry) => getRowId(entry))
.filter((entry) => isFolderEntry(entry))
.map((entry) => getEntryId(entry))
.filter(Boolean),
[selectedEntries, isFolderRowKey, getRowId],
[selectedEntries],
);
const selectEntry = useCallback(
(entry: SelectionEntry | string | null, event?: unknown) => {
(entry: SelectionEntry | string, event?: unknown) => {
const rowKey = entry && Object(entry) === entry
? (entry as SelectionEntry).rowKey ?? null
: (entry as string | null);
? (entry as SelectionEntry).rowKey ?? undefined
: (entry as string);
if (!rowKey) return;
handleEntrySelection(rowKey, event);
},
@@ -86,7 +71,7 @@ export const useWorkspaceSelection = ({
);
const inspectDocument = useCallback(
(documentId?: string | number | null) => {
(documentId?: string | number) => {
if (!documentId) return;
onDocumentActivate(documentId);
},
@@ -94,7 +79,7 @@ export const useWorkspaceSelection = ({
);
const inspectFolder = useCallback(
(folderId?: string | number | null) => {
(folderId?: string | number) => {
if (!folderId) return;
onInspectFolder(folderId);
},