feat: Refactor frontend hooks to use useNavigate and useAppDispatch directly
This commit is contained in:
@@ -4,14 +4,12 @@ import type {
|
||||
MutableRefObject,
|
||||
SetStateAction,
|
||||
} from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import type { DocumentId } from '../types/identifiers';
|
||||
|
||||
type FolderId = DocumentId | 'root';
|
||||
|
||||
import type { Document } from '../types/documents';
|
||||
|
||||
type NavigateHandler = (path: string, options?: { replace?: boolean }) => void;
|
||||
|
||||
import useNotifyApiError from '../hooks/useNotifyApiError';
|
||||
|
||||
interface UseDocumentPreviewArgs {
|
||||
@@ -24,7 +22,6 @@ interface UseDocumentPreviewArgs {
|
||||
ingest: (docs: unknown[]) => { canonical: Document[]; changed: boolean };
|
||||
};
|
||||
selectedFolder?: FolderId | null;
|
||||
navigate: NavigateHandler;
|
||||
locationPathname: string;
|
||||
locationSearch: string;
|
||||
detailPanelControlRef: MutableRefObject<{
|
||||
@@ -45,7 +42,6 @@ const useDocumentPreview = ({
|
||||
routeDocumentId,
|
||||
documentsManager,
|
||||
selectedFolder,
|
||||
navigate,
|
||||
locationPathname,
|
||||
locationSearch,
|
||||
detailPanelControlRef,
|
||||
@@ -53,6 +49,7 @@ const useDocumentPreview = ({
|
||||
}: UseDocumentPreviewArgs): UseDocumentPreviewResult => {
|
||||
const previewReturnPathRef = useRef<string | null>(null);
|
||||
const notifyApiError = useNotifyApiError();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const resetPreviewState = useCallback(() => {
|
||||
previewReturnPathRef.current = null;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import type { Dispatch, SetStateAction } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { TAG_FILTER_UNTAGGED } from './workspaceUtils';
|
||||
import { listDocuments } from '../lib/api/apiClient';
|
||||
import type { Identifier } from '../types/identifiers';
|
||||
@@ -16,7 +17,6 @@ interface UseDocumentsSearchArgs {
|
||||
api: ApiClient;
|
||||
token?: string | null;
|
||||
selectedFolder?: Identifier | 'root' | null;
|
||||
navigate?: (path: string, options?: { replace?: boolean }) => void;
|
||||
locationPathname?: string;
|
||||
isDocumentsRoute?: boolean;
|
||||
searchIncludeDescendants?: boolean;
|
||||
@@ -67,7 +67,6 @@ const useDocumentsSearch = ({
|
||||
api,
|
||||
token,
|
||||
selectedFolder,
|
||||
navigate,
|
||||
locationPathname,
|
||||
isDocumentsRoute,
|
||||
searchIncludeDescendants,
|
||||
@@ -83,6 +82,7 @@ const useDocumentsSearch = ({
|
||||
const [searchLoading, setSearchLoading] = useState<boolean>(false);
|
||||
const [searchTrigger, setSearchTrigger] = useState<number>(0);
|
||||
const notifyApiError = useNotifyApiError();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const toggleTagFilter = useCallback((tagId: Identifier) => {
|
||||
if (!tagId) return;
|
||||
|
||||
@@ -371,7 +371,6 @@ const useDocumentsWorkspace = ({
|
||||
api: apiClient,
|
||||
token,
|
||||
selectedFolder,
|
||||
navigate,
|
||||
locationPathname: location.pathname,
|
||||
isDocumentsRoute,
|
||||
searchIncludeDescendants,
|
||||
@@ -444,7 +443,6 @@ const useDocumentsWorkspace = ({
|
||||
routeDocumentId: previewDocumentId,
|
||||
documentsManager,
|
||||
selectedFolder,
|
||||
navigate,
|
||||
locationPathname: location.pathname,
|
||||
locationSearch: location.search,
|
||||
detailPanelControlRef,
|
||||
@@ -756,7 +754,6 @@ const useDocumentsWorkspace = ({
|
||||
setFolderNodes,
|
||||
selectedFolder,
|
||||
setSelectedFolder,
|
||||
navigate,
|
||||
handleFileDrop,
|
||||
moveDocumentsToFolder,
|
||||
draggedDocumentIds,
|
||||
@@ -1080,14 +1077,12 @@ const useDocumentsWorkspace = ({
|
||||
}, [selectedFolder, folderNodes]);
|
||||
|
||||
const { handleTenantSelect } = useTenantManager({
|
||||
appDispatch,
|
||||
currentTenantId,
|
||||
resetWorkspaceState,
|
||||
refreshTags,
|
||||
refreshCorrespondents,
|
||||
loadFolder,
|
||||
handleDocumentsViewModeChange,
|
||||
navigate,
|
||||
tokenRef,
|
||||
tenantIdRef,
|
||||
});
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { MutableRefObject, useCallback } from 'react';
|
||||
import type { NavigateFunction } from 'react-router-dom';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
|
||||
import type { FolderId, TenantId } from '../../types/identifiers';
|
||||
import { useStatusToast } from '../../lib/context/StatusToastContext';
|
||||
import { useAppDispatch } from '../../lib/store/appState';
|
||||
|
||||
import { api, listTenants, switchTenant } from '../../lib/api/apiClient';
|
||||
|
||||
@@ -13,32 +15,30 @@ interface TenantOption {
|
||||
import useNotifyApiError from '../../hooks/useNotifyApiError';
|
||||
|
||||
interface UseTenantManagerOptions {
|
||||
appDispatch: (action: any) => void;
|
||||
currentTenantId: TenantId | null;
|
||||
resetWorkspaceState: () => void;
|
||||
refreshTags: () => Promise<void>;
|
||||
refreshCorrespondents: () => Promise<void>;
|
||||
loadFolder: (folderId: FolderId, options?: { preserveSearch?: boolean }) => Promise<void>;
|
||||
handleDocumentsViewModeChange: (mode: string) => void;
|
||||
navigate: NavigateFunction;
|
||||
tokenRef?: MutableRefObject<string | null>;
|
||||
tenantIdRef?: MutableRefObject<TenantId | null>;
|
||||
}
|
||||
|
||||
const useTenantManager = ({
|
||||
appDispatch,
|
||||
currentTenantId,
|
||||
resetWorkspaceState,
|
||||
refreshTags,
|
||||
refreshCorrespondents,
|
||||
loadFolder,
|
||||
handleDocumentsViewModeChange,
|
||||
navigate,
|
||||
tokenRef,
|
||||
tenantIdRef,
|
||||
}: UseTenantManagerOptions) => {
|
||||
const { showToast } = useStatusToast();
|
||||
const notifyApiError = useNotifyApiError();
|
||||
const navigate = useNavigate();
|
||||
const appDispatch = useAppDispatch();
|
||||
const handleTenantSelect = useCallback(
|
||||
async (tenantOption: TenantOption | null, { refreshOnly = false }: { refreshOnly?: boolean } = {}) => {
|
||||
const requestedTenantId = tenantOption?.id ?? null;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useCallback, useMemo } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import type { DragEvent } from 'react';
|
||||
import { useStatusToast } from '../../../lib/context/StatusToastContext';
|
||||
import { DEFAULT_FOLDER_NAME, hasFiles } from '../../../app/workspaceUtils';
|
||||
@@ -37,7 +38,6 @@ interface UseFolderTreeActionsOptions {
|
||||
setFolderNodes: (updater: (prev: Map<FolderKey, FolderNode>) => Map<FolderKey, FolderNode>) => void;
|
||||
selectedFolder: FolderKey;
|
||||
setSelectedFolder: (folderId: FolderKey) => void;
|
||||
navigate?: (path: string, options?: { replace?: boolean }) => void;
|
||||
handleFileDrop: (dataTransfer: DataTransfer, folderId: FolderKey) => Promise<void> | void;
|
||||
moveDocumentsToFolder: (docIds: FolderId[], folderId: FolderKey) => Promise<void>;
|
||||
draggedDocumentIds: FolderId[];
|
||||
@@ -54,7 +54,6 @@ const useFolderTreeActions = ({
|
||||
setFolderNodes,
|
||||
selectedFolder,
|
||||
setSelectedFolder,
|
||||
navigate,
|
||||
handleFileDrop,
|
||||
moveDocumentsToFolder,
|
||||
draggedDocumentIds,
|
||||
@@ -66,6 +65,7 @@ const useFolderTreeActions = ({
|
||||
}: UseFolderTreeActionsOptions) => {
|
||||
const { showToast } = useStatusToast();
|
||||
const notifyApiError = useNotifyApiError();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const moveFolder = useCallback(
|
||||
async (folderId: FolderKey, targetFolderId: FolderKey | null) => {
|
||||
|
||||
Reference in New Issue
Block a user