refactor: Standardize document and folder identification with a new entryKey module.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||
import type { DragEvent } from 'react';
|
||||
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
||||
|
||||
type Identifier = string | number;
|
||||
type FolderIdentifier = string | 'root';
|
||||
@@ -30,8 +31,6 @@ interface UseDocumentDragHandlersOptions {
|
||||
documentLookup: Map<Identifier, DocumentLike>;
|
||||
setDraggedDocumentIds: (ids: Identifier[] | []) => void;
|
||||
setDraggedFolderId: (id: FolderIdentifier | null) => void;
|
||||
resolveDocumentRowKey: (id: Identifier) => string | null;
|
||||
resolveFolderRowKey: (id: FolderIdentifier) => string | null;
|
||||
documentsViewMode: string;
|
||||
}
|
||||
|
||||
@@ -44,8 +43,6 @@ const useDocumentDragHandlers = ({
|
||||
documentLookup,
|
||||
setDraggedDocumentIds,
|
||||
setDraggedFolderId,
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
documentsViewMode,
|
||||
}: UseDocumentDragHandlersOptions) => {
|
||||
const dragPreviewRef = useRef<HTMLDivElement | null>(null);
|
||||
@@ -213,7 +210,7 @@ const useDocumentDragHandlers = ({
|
||||
return;
|
||||
}
|
||||
|
||||
const documentKey = resolveDocumentRowKey(documentId);
|
||||
const documentKey = createDocumentEntryKey(documentId);
|
||||
if (!documentKey) {
|
||||
return;
|
||||
}
|
||||
@@ -279,7 +276,6 @@ const useDocumentDragHandlers = ({
|
||||
setDraggedFolderId,
|
||||
setDraggedDocumentIds,
|
||||
documentsViewMode,
|
||||
resolveDocumentRowKey,
|
||||
],
|
||||
);
|
||||
|
||||
@@ -300,7 +296,7 @@ const useDocumentDragHandlers = ({
|
||||
return;
|
||||
}
|
||||
event.stopPropagation();
|
||||
const folderKey = resolveFolderRowKey(normalizedFolderId);
|
||||
const folderKey = createFolderEntryKey(normalizedFolderId);
|
||||
const isAlreadySelected = folderKey ? selectedEntries.includes(folderKey) : false;
|
||||
|
||||
let effectiveFolderSelection: FolderIdentifier[] = normalizedFolderIds;
|
||||
@@ -309,7 +305,7 @@ const useDocumentDragHandlers = ({
|
||||
if (!isAlreadySelected && folderKey) {
|
||||
effectiveFolderSelection = [normalizedFolderId];
|
||||
effectiveDocumentSelection = [];
|
||||
handleEntrySelection(folderKey, { preventDefault: () => {} });
|
||||
handleEntrySelection(folderKey, { preventDefault: () => { } });
|
||||
}
|
||||
|
||||
const uniqueFolders = effectiveFolderSelection.length
|
||||
@@ -363,7 +359,6 @@ const useDocumentDragHandlers = ({
|
||||
setDraggedDocumentIds,
|
||||
documentLookup,
|
||||
createDragPreview,
|
||||
resolveFolderRowKey,
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useCallback } from 'react';
|
||||
|
||||
import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
|
||||
import { DEFAULT_FOLDER_NAME, getRowId, isDocumentRowKey } from '../../app/workspaceUtils';
|
||||
import { DEFAULT_FOLDER_NAME } from '../../app/workspaceUtils';
|
||||
import { getEntryId, isDocumentEntry } from '../../app/entryKey';
|
||||
import {
|
||||
addDocumentTags,
|
||||
createTag,
|
||||
@@ -275,10 +275,10 @@ const useDocumentMutations = ({
|
||||
|
||||
const pruneRow = (collection: string[]): string[] =>
|
||||
collection.filter((key) => {
|
||||
if (!isDocumentRowKey(key)) {
|
||||
if (!isDocumentEntry(key)) {
|
||||
return true;
|
||||
}
|
||||
const id = getRowId(key);
|
||||
const id = getEntryId(key);
|
||||
return id ? !uniqueIdSet.has(id as DocumentId) : true;
|
||||
});
|
||||
try {
|
||||
@@ -348,8 +348,8 @@ const useDocumentMutations = ({
|
||||
selectionOrderRef.current = nextSelectionOrder;
|
||||
if (
|
||||
selectionAnchorRef.current &&
|
||||
isDocumentRowKey(selectionAnchorRef.current) &&
|
||||
uniqueIdSet.has(getRowId(selectionAnchorRef.current) as DocumentId)
|
||||
isDocumentEntry(selectionAnchorRef.current) &&
|
||||
uniqueIdSet.has(getEntryId(selectionAnchorRef.current) as DocumentId)
|
||||
) {
|
||||
selectionAnchorRef.current = null;
|
||||
}
|
||||
@@ -358,8 +358,8 @@ const useDocumentMutations = ({
|
||||
}
|
||||
if (
|
||||
focusedRowKey &&
|
||||
isDocumentRowKey(focusedRowKey) &&
|
||||
uniqueIdSet.has(getRowId(focusedRowKey) as DocumentId)
|
||||
isDocumentEntry(focusedRowKey) &&
|
||||
uniqueIdSet.has(getEntryId(focusedRowKey) as DocumentId)
|
||||
) {
|
||||
setFocusedRowKey(null);
|
||||
}
|
||||
@@ -617,7 +617,7 @@ const useDocumentMutations = ({
|
||||
if (!source || source.id == null) {
|
||||
return null;
|
||||
}
|
||||
const labelText = `${source.label ?? ''}`.trim();
|
||||
const labelText = `${source.label ?? ''} `.trim();
|
||||
if (!labelText) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -29,17 +29,12 @@ import useDocumentsPanelProps from '../../documents/hooks/useDocumentsPanelProps
|
||||
import useDocumentPreview from '../../app/useDocumentPreview';
|
||||
import useSidebarProps from '../../sidebar/useSidebarProps';
|
||||
import {
|
||||
|
||||
DEFAULT_SORT_DIRECTION,
|
||||
DEFAULT_SORT_FIELD,
|
||||
createRootNode,
|
||||
getRowId,
|
||||
isDocumentRowKey,
|
||||
isFolderRowKey,
|
||||
mergeAssetIntoDocument,
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
} from '../../app/workspaceUtils';
|
||||
import { createDocumentEntryKey, createFolderEntryKey } from '../../app/entryKey';
|
||||
import useDocumentsSearch from '../../app/useDocumentsSearch';
|
||||
import useDocumentsStore from './store/useDocumentsStore';
|
||||
import useAuthManager from './useAuthManager';
|
||||
@@ -252,13 +247,7 @@ const useDocumentsWorkspace = ({
|
||||
}
|
||||
const tagManager = tagManagerRef.current;
|
||||
|
||||
const selection = useWorkspaceSelection({
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
isDocumentRowKey,
|
||||
isFolderRowKey,
|
||||
getRowId,
|
||||
});
|
||||
const selection = useWorkspaceSelection();
|
||||
|
||||
const {
|
||||
selectedEntries,
|
||||
@@ -415,7 +404,7 @@ const useDocumentsWorkspace = ({
|
||||
);
|
||||
|
||||
const visibleDocumentKeys = useMemo(
|
||||
() => visibleDocumentIds.map((id) => resolveDocumentRowKey(id)).filter(Boolean),
|
||||
() => visibleDocumentIds.map((id) => createDocumentEntryKey(id)).filter(Boolean),
|
||||
[visibleDocumentIds],
|
||||
);
|
||||
|
||||
@@ -424,7 +413,7 @@ const useDocumentsWorkspace = ({
|
||||
showingSearchResults
|
||||
? []
|
||||
: currentSubfolders
|
||||
.map((folder) => resolveFolderRowKey(folder.id))
|
||||
.map((folder) => createFolderEntryKey(folder.id))
|
||||
.filter(Boolean),
|
||||
[showingSearchResults, currentSubfolders],
|
||||
);
|
||||
@@ -599,8 +588,6 @@ const useDocumentsWorkspace = ({
|
||||
documentLookup,
|
||||
setDraggedDocumentIds,
|
||||
setDraggedFolderId,
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
documentsViewMode,
|
||||
});
|
||||
|
||||
@@ -827,8 +814,6 @@ const useDocumentsWorkspace = ({
|
||||
showingSearchResults,
|
||||
currentSubfolders,
|
||||
visibleDocuments: viewDocuments,
|
||||
resolveFolderRowKey,
|
||||
resolveDocumentRowKey,
|
||||
configureSelectionEnvironment,
|
||||
visibleRowKeySet,
|
||||
selectedEntries,
|
||||
@@ -840,7 +825,6 @@ const useDocumentsWorkspace = ({
|
||||
focusedDocumentId,
|
||||
setFocusedRowKey,
|
||||
focusedRowKey,
|
||||
isFolderRowKey,
|
||||
});
|
||||
const initializeAfterLogin = useCallback(async () => {
|
||||
await Promise.all([refreshTags(), refreshCorrespondents()]);
|
||||
@@ -1191,12 +1175,10 @@ const useDocumentsWorkspace = ({
|
||||
);
|
||||
|
||||
const handleEntryPointerCore = useEntryPointerCore({
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
onSelectEntry: (entry, event, { rowKey, modifierClick, primaryClick }) => {
|
||||
const { type, id } = entry;
|
||||
const key = rowKey
|
||||
|| (type === EntryType.document ? resolveDocumentRowKey(id) : resolveFolderRowKey(id));
|
||||
|| (type === EntryType.document ? createDocumentEntryKey(id) : createFolderEntryKey(id));
|
||||
if (key) {
|
||||
handleEntrySelection(key, event);
|
||||
}
|
||||
@@ -1238,7 +1220,6 @@ const useDocumentsWorkspace = ({
|
||||
selectedEntries,
|
||||
selectionAnchorRef,
|
||||
applySelection,
|
||||
resolveDocumentRowKey,
|
||||
showingSearchResults,
|
||||
searchQuery,
|
||||
activeTagFilters,
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import type { Dispatch, MutableRefObject, SetStateAction } from 'react';
|
||||
import { createRootNode, DEFAULT_FOLDER_NAME } from '../../app/workspaceUtils';
|
||||
import {
|
||||
DEFAULT_FOLDER_NAME,
|
||||
createRootNode,
|
||||
getRowId,
|
||||
isDocumentRowKey,
|
||||
isFolderRowKey,
|
||||
resolveDocumentRowKey,
|
||||
resolveFolderRowKey,
|
||||
} from '../../app/workspaceUtils';
|
||||
getEntryId,
|
||||
isDocumentEntry,
|
||||
isFolderEntry,
|
||||
createDocumentEntryKey,
|
||||
createFolderEntryKey,
|
||||
} from '../../app/entryKey';
|
||||
|
||||
type Identifier = string | number;
|
||||
type FolderId = Identifier | 'root';
|
||||
@@ -118,12 +117,12 @@ const useFolderTree = ({
|
||||
setCurrentFolder(folderInfo);
|
||||
|
||||
const availableDocKeys = docs
|
||||
.map((doc) => resolveDocumentRowKey(doc?.id as Identifier))
|
||||
.map((doc) => createDocumentEntryKey(doc?.id as Identifier))
|
||||
.filter(Boolean);
|
||||
const availableDocKeySet = new Set(availableDocKeys);
|
||||
const availableFolderKeys = new Set(
|
||||
subfolders
|
||||
.map((folder) => resolveFolderRowKey(folder?.id as Identifier))
|
||||
.map((folder) => createFolderEntryKey(folder?.id as Identifier))
|
||||
.filter(Boolean),
|
||||
);
|
||||
|
||||
@@ -132,22 +131,22 @@ const useFolderTree = ({
|
||||
|
||||
setSelectedEntries((previous) => {
|
||||
const previousFolderKeys = previous
|
||||
.filter(isFolderRowKey)
|
||||
.filter(isFolderEntry)
|
||||
.filter((key) => availableFolderKeys.has(key));
|
||||
const previousDocKeys = previous.filter(isDocumentRowKey);
|
||||
const previousDocKeys = previous.filter(isDocumentEntry);
|
||||
nextDocKeys = previousDocKeys.filter((key) => availableDocKeySet.has(key));
|
||||
mergedSelection = [...previousFolderKeys, ...nextDocKeys];
|
||||
return mergedSelection;
|
||||
});
|
||||
|
||||
const nextFocus = (() => {
|
||||
const currentFocusedKey = resolveDocumentRowKey(focusedDocumentId);
|
||||
const currentFocusedKey = createDocumentEntryKey(focusedDocumentId);
|
||||
if (currentFocusedKey && availableDocKeySet.has(currentFocusedKey)) {
|
||||
return focusedDocumentId;
|
||||
}
|
||||
if (nextDocKeys.length) {
|
||||
const lastDocKey = nextDocKeys[nextDocKeys.length - 1];
|
||||
return getRowId(lastDocKey) || null;
|
||||
return getEntryId(lastDocKey) || null;
|
||||
}
|
||||
return null;
|
||||
})();
|
||||
|
||||
@@ -1,16 +1,20 @@
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import type { MutableRefObject } from 'react';
|
||||
import { createDocumentEntryKey } from '../../app/entryKey';
|
||||
|
||||
type Identifier = string | number;
|
||||
|
||||
interface ApplySelectionFn {
|
||||
(keys: string[], options?: { anchor?: string | null; interactedKeys?: string[] }): unknown;
|
||||
}
|
||||
|
||||
interface UseWorkspaceDeskPropsArgs {
|
||||
viewDocuments: any[];
|
||||
inspectDocumentForDesk: (doc: any) => void;
|
||||
viewDocuments: unknown[];
|
||||
inspectDocumentForDesk?: (id: Identifier | null) => void;
|
||||
handleEntryPointer: (params: { rowKey?: string | null; id?: Identifier | null; type?: string; event?: any }) => void;
|
||||
selectedEntries: Array<string | number>;
|
||||
selectedEntries: string[];
|
||||
selectionAnchorRef: MutableRefObject<Identifier | string | null>;
|
||||
applySelection: (rowKeys: Array<string | number>, options?: { anchor?: Identifier | string | null; interactedKeys?: Array<string | number> }) => void;
|
||||
resolveDocumentRowKey: (id?: Identifier | null) => string | null;
|
||||
applySelection: ApplySelectionFn;
|
||||
showingSearchResults: boolean;
|
||||
searchQuery: string;
|
||||
activeTagFilters: Array<string | number>;
|
||||
@@ -32,7 +36,6 @@ const useWorkspaceDeskProps = ({
|
||||
selectedEntries,
|
||||
selectionAnchorRef,
|
||||
applySelection,
|
||||
resolveDocumentRowKey,
|
||||
showingSearchResults,
|
||||
searchQuery,
|
||||
activeTagFilters,
|
||||
@@ -53,7 +56,7 @@ const useWorkspaceDeskProps = ({
|
||||
}
|
||||
|
||||
const rowKeys = docIds
|
||||
.map((id) => resolveDocumentRowKey(id as Identifier))
|
||||
.map((id) => createDocumentEntryKey(id as Identifier))
|
||||
.filter((value): value is string => typeof value === 'string');
|
||||
|
||||
if (!rowKeys.length) {
|
||||
@@ -69,14 +72,14 @@ const useWorkspaceDeskProps = ({
|
||||
|
||||
const anchor = (rowKeys[0]
|
||||
|| selectionAnchorRef.current
|
||||
|| nextKeys[nextKeys.length - 1]) as Identifier | string | null;
|
||||
|| nextKeys[nextKeys.length - 1]) as string | null;
|
||||
|
||||
applySelection(nextKeys, {
|
||||
anchor,
|
||||
interactedKeys: rowKeys,
|
||||
});
|
||||
},
|
||||
[applySelection, resolveDocumentRowKey, selectedEntries, selectionAnchorRef],
|
||||
[applySelection, selectedEntries, selectionAnchorRef],
|
||||
);
|
||||
|
||||
const deskViewId = useMemo(() => {
|
||||
|
||||
Reference in New Issue
Block a user