feat: Refactor frontend hooks to use useNavigate and useAppDispatch directly

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