diff --git a/app/cypress/e2e/files/footer-rename.cy.js b/app/cypress/e2e/files/footer-rename.cy.js index 843397cd..e9d8f199 100644 --- a/app/cypress/e2e/files/footer-rename.cy.js +++ b/app/cypress/e2e/files/footer-rename.cy.js @@ -108,22 +108,29 @@ import { selectRow('fb-alpha'); footerButton('Rename').click(); - drainQueuedRefresh(() => { + // A directory rename is a file-worker move job; the request returns once it is queued, and the job + // reports when it is done. No refresh is queued. + cy.serverNow().then((since) => { dialog().within(() => { cy.get('input').clear().type('fb-alpha-renamed'); cy.contains('button', 'Rename').click(); }); - - // A directory rename runs as a file-worker move job; the modal stays open until - // the job finishes, which takes longer than the default timeout. - cy.get('[role="dialog"]', {timeout: 30000}).should('not.exist'); - row('fb-alpha-renamed').should('be.visible'); - // The folder row's text is just the name, so anchoring excludes 'fb-alpha-renamed'. - cy.contains('tr', /fb-alpha$/).should('not.exist'); - - cy.task('fb:readdir', 'fb-alpha-renamed') - .should('deep.equal', ['fb-one.txt', 'fb-two.txt']); - cy.task('fb:exists', 'fb-alpha').should('be.false'); + dialog().should('not.exist'); + cy.waitForEvent({ + since, + event: 'file_move_completed', + message: 'Renamed fb-alpha to fb-alpha-renamed', + failureEvent: 'file_move_failed', + }); }); + + // The table is fetched again once the file worker is idle. + cy.contains('tr', 'fb-alpha-renamed', {timeout: 10000}).should('be.visible'); + // The folder row's text is just the name, so anchoring excludes 'fb-alpha-renamed'. + cy.contains('tr', /fb-alpha$/).should('not.exist'); + + cy.task('fb:readdir', 'fb-alpha-renamed') + .should('deep.equal', ['fb-one.txt', 'fb-two.txt']); + cy.task('fb:exists', 'fb-alpha').should('be.false'); }); }); diff --git a/app/src/api.js b/app/src/api.js index 527ad696..1ccf5275 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1927,13 +1927,31 @@ export async function searchDirectories(path) { } } +/** + * Returns the file worker job id of a queued rename (a directory), or null when the rename is already done. + */ export async function renamePath(path, newName) { const body = {path, new_name: newName}; const response = await apiPost(`${API_URI}/files/rename`, body); if (!response.ok) { const message = await getErrorMessage(response, 'Failed to rename!'); toast({type: 'error', title: 'Error', description: message, time: 5000}); + return null; } + const json = await response.json(); + return json['job_id'] || null; +} + +/** + * Returns a file worker job's status ('pending', 'complete', or 'failed'), or null if it is unknown. + */ +export async function fetchFileWorkerJobStatus(jobId) { + const response = await apiGet(`${API_URI}/files/jobs/${encodeURIComponent(jobId)}`); + if (!response.ok) { + return null; + } + const json = await response.json(); + return json['status']; } export async function movePaths(destination, paths) { diff --git a/app/src/components/FileBrowser.js b/app/src/components/FileBrowser.js index cd267f67..e4cce687 100644 --- a/app/src/components/FileBrowser.js +++ b/app/src/components/FileBrowser.js @@ -23,7 +23,15 @@ import { import React, {useContext, useEffect, useState} from "react"; import {IconArrowsMove, IconChevronsUp, IconCursorText, IconTags} from "@tabler/icons-react"; import {useHotkeys} from "react-hotkeys-hook"; -import {deleteFile, ignoreDirectory, makeDirectory, movePaths, renamePath, unignoreDirectory} from "../api"; +import { + deleteFile, + fetchFileWorkerJobStatus, + ignoreDirectory, + makeDirectory, + movePaths, + renamePath, + unignoreDirectory +} from "../api"; import _ from 'lodash'; import {SortableTable} from "./SortableTable"; import {useBrowseFiles, useElementWidth, useMediaDirectory, useWROLMode} from "../hooks/customHooks"; @@ -34,6 +42,7 @@ import {BulkTagModal} from "./BulkTagModal"; import {InlineErrorBoundary} from "./ErrorBoundary"; import {TaggedDeleteConfirmModal} from "./TaggedDeleteConfirmModal"; import {UploadModal} from "./Upload"; +import {useFileWorkerStatus} from "../contexts/FileWorkerStatusContext"; function depthIndentation(path) { // Repeated spaces for every folder a path is in. @@ -228,6 +237,8 @@ function SkeletonFileRow({depth}) { // Below this footer width (px) the action buttons are icon-only; at or above it they expand to show labels. // The eight labeled buttons need ~1020px to fit on a single row; this leaves a small buffer. const FOOTER_LABELS_MIN_WIDTH = 1050; +// How often a queued rename's job is checked. +const RENAME_JOB_POLL_MS = 500; export function FileBrowser() { const [selectedPaths, setSelectedPaths] = React.useState([]); @@ -328,6 +339,46 @@ export function FileBrowser() { } }, [browseFiles]); + // A move finishes in the file worker after its request returns (the Files page shows its progress), so fetch + // the files again when the file worker is done. + const {status: fileWorkerStatus, setFastPolling} = useFileWorkerStatus(); + const fileWorkerBusy = !!fileWorkerStatus && fileWorkerStatus.status !== 'idle'; + const fileWorkerWasBusy = React.useRef(false); + useEffect(() => { + if (fileWorkerWasBusy.current && !fileWorkerBusy) { + fetchFiles(); + } + fileWorkerWasBusy.current = fileWorkerBusy; + }, [fileWorkerBusy]); + + // A queued rename (a directory) returns its job; wait for that job, rather than inferring it from the file + // worker's status (a short job can start and finish between two polls). + const mountedRef = React.useRef(true); + useEffect(() => () => { + mountedRef.current = false; + }, []); + const afterRename = async (jobId) => { + if (!jobId) { + await reset(); + return; + } + setSelectedPaths([]); + setFastPolling(true); // Update the progress bar while the rename runs. + try { + while (mountedRef.current) { + await new Promise(resolve => setTimeout(resolve, RENAME_JOB_POLL_MS)); + if (await fetchFileWorkerJobStatus(jobId) !== 'pending') { + break; + } + } + } finally { + setFastPolling(false); + } + if (mountedRef.current) { + await reset(); + } + } + // Clear loading states when file data updates useEffect(() => { if (loadingFolders.size > 0) { @@ -413,7 +464,7 @@ export function FileBrowser() { onClose={() => setRenameOpen(false)} onPending={setPending} path={selectedPaths[0]} - onSubmit={reset} + onSubmit={afterRename} /> : null}