Repository navigation
fix(overview): suggest a filename that is not taken yet - #210
seecker-ionos wants to merge 2 commits into
Conversation
6e00b19 to
46bd0a0
Compare
moodyjmz
left a comment
There was a problem hiding this comment.
Thanks for this, and for keeping the probing bounded and the dialog opening straight away rather than waiting on the network.
Pressing Enter before the free-name check answers looks like it still sends the plain default, which is the "already exists" error this PR sets out to remove. Two smaller points inline: the late answer pulls focus back into the filename field, and the probes keep running after the dialog closes. Happy to be corrected if I've misread the flow.
Tests that would pin these: Enter before the answer creates the free name; no focus change when the input isn't focused; closing the dialog aborts the signal passed to nameExists.
With #211: the two PRs conflict in src/services/folders.ts (both add it), OfficeOverview.vue and both spec files, whichever lands first. Once both are in, the suggestion needs to follow the chosen folder: run after the remembered-folder check, re-run when the folder changes, and only while the name is untouched. The AbortController suggested inline is the piece that makes that re-run cheap.
| const freeName = await suggestFreeName(defaultName) | ||
| // A later click, a closed dialog, a create already under way or a name the | ||
| // user typed all win over this answer. | ||
| if (request !== suggestionRequest || !showCreateDialog.value || creating.value |
There was a problem hiding this comment.
The input is focused with the basename selected, so open + Enter is the fast path, and it can easily beat the PROPFIND. doCreateFromTemplate then looks like it reads newFileName while it is still the default, and the late answer appears to be dropped here by the creating check.
Suggestion: keep the in-flight suggestion as a promise, and in doCreateFromTemplate set creating first, then wait for it if the name is still the suggested one, then validate and send. The field and Create button are already disabled while creating, so the wait looks like the normal busy state, and the creating condition here should no longer be needed.
There was a problem hiding this comment.
You read it right. Done as you suggested: doCreateFromTemplate now sets creating first, waits for the in-flight suggestion if the field still holds the suggested name, then validates and sends. The creating check in the late-answer path is gone. If the dialog is closed while it waits, nothing is created. Covered by "creates the free name when Create is pressed before the check answers" and "does not create a file when the dialog is closed while Create waits for the check". Fixed in 4ef25a9.
| } | ||
| suggestedName.value = freeName | ||
| newFileName.value = freeName | ||
| focusFileNameInput(creator.extension) |
There was a problem hiding this comment.
If the user has already tabbed to Create (or anywhere else) when the answer arrives, focus and selection jump back to the field, and a screen reader may re-announce it. Suggestion: only reselect the basename when the input is still document.activeElement; otherwise just update the value.
Non-blocking: suggestedName is only read in script, so it can be a plain variable.
There was a problem hiding this comment.
The late answer now only reselects the basename while the input is still document.activeElement. Otherwise it just updates the value and never calls focus(). Tested both ways. suggestedName is now a plain variable.
| maxProbes = MAX_NAME_PROBES, | ||
| ): Promise<string> { | ||
| const taken: string[] = [] | ||
| for (let probe = 0; probe < maxProbes; probe++) { |
There was a problem hiding this comment.
Closing the dialog or clicking another template doesn't stop this loop; the result is only discarded at the end. exists() accepts { signal } (webdav 5.11 WebDAVMethodOptions), so an AbortController per dialog, aborted on a new click and when showCreateDialog goes false, stops the remaining requests. An aborted request should reject inside isTaken, which already ends the loop, so this file probably needs no change, and signal.aborted could replace the suggestionRequest counter and the !showCreateDialog check in the view.
Non-blocking: at the cap, returning the default (known to be taken) vs the next unprobed candidate is a toss-up either way.
There was a problem hiding this comment.
Added one AbortController per dialog. It is aborted on a new click, when showCreateDialog goes false, and on unmount, and its signal goes through nameExists to exists(). It replaces the suggestionRequest counter and the !showCreateDialog check. As you expected, freeFileName.ts is unchanged, because the aborted request rejects inside isTaken. On the cap, I kept returning the default.
| // depth-0 PROPFIND; a missing entry is the 404 that exists() turns into false, | ||
| // any other failure is thrown. | ||
| export async function nameExists(folder: string, name: string): Promise<boolean> { | ||
| return getClient().exists(davPath(folder, name)) |
There was a problem hiding this comment.
In @nextcloud/files 4.1, getClient() builds a new client and subscribes it to request-token updates on every call, without unsubscribing, so this adds up to 20 per dialog open. A single lazily created client avoids that:
let client: ReturnType<typeof getClient> | undefined
function davClient(): ReturnType<typeof getClient> {
client ??= getClient()
return client
}There was a problem hiding this comment.
Done. There is now one lazily created client, with a test that getClient() runs once across several checks.
The create dialog always pre-filled the creator's default name, e.g. "New document.docx". Files are created in the user's root folder, so once that file exists every further attempt fails with "File already exists" and the user has to rename by hand. Suggest the first free one of "New document.docx", "New document (1).docx", ... instead, numbered by getUniqueName() like the Files app's "New" menu. Each candidate is checked with one depth-0 PROPFIND, capped at 20, so the work does not grow with the size of the root folder. The dialog still opens at once with the plain default and the free name replaces it when the checks are done, unless the user has typed a name, opened another creator meanwhile or already started creating. If a check fails, the plain default stays; the server still rejects a taken name. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Sebastian Ecker <sebastian.ecker@world4you.com>
…ecks Pressing Enter right after the create dialog opens could beat the existence checks, so the plain default, which is already taken, was sent and the server answered "already exists". Creating now marks the dialog busy first, waits for a pending suggestion while the field still holds the suggested name, and only then validates and sends. A late answer is no longer dropped because creating has started; if the dialog is closed while waiting, nothing is created. The checks share one AbortController per dialog, aborted when another creator is picked, the dialog closes or the view unmounts, so the remaining requests stop instead of running to the end. This replaces the request counter. A late answer only reselects the basename while the input still has focus, so it no longer pulls focus back from the Create button or elsewhere. nameExists() reuses one lazily created DAV client: getClient() builds a new client and subscribes it to request-token updates on every call. Assisted-by: ClaudeCode:claude-opus-5-5 Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: seecker-ionos <sebastian.ecker@world4you.com>
46bd0a0 to
4ef25a9
Compare
|
Thanks for the careful read. All points are addressed in 4ef25a9, and the branch is rebased on current main. About #211: whichever of the two lands second, I'll rebase it and move the suggestion behind the remembered-folder check. When the folder changes and the name is still untouched, it aborts the running check and starts a new one. The AbortController makes that cheap, as you said. |
Problem
Creating a new document from the Office overview always pre-fills the dialog with the creator's default name (e.g.
New document.docx). Files are created in the user's root folder, so as soon as that file exists, every further attempt fails with "File already exists" and the user has to rename by hand.The Files app's "New" menu avoids this: it suggests
New document (1).docxviagetUniqueName()from@nextcloud/files.Fix
src/utils/freeFileName.ts:findFreeFileName()walksNew document.docx,New document (1).docx, … — candidates generated bygetUniqueName(), so the numbering matches Files — and returns the first one that is not taken. At most 20 candidates are checked; past that the plain default is kept.src/services/folders.ts:nameExists(folder, name, signal?)— one depth-0 PROPFIND via the@nextcloud/files/davclient'sexists()(404 → free), abortable, on one shared client (getClient()subscribes a new client to request-token updates on every call).OfficeOverview.vue: the dialog still opens immediately with the plain default; the free name replaces it once the checks are done, unless the user has typed a name. Pressing Create or Enter before the answer waits for the free name (field and button show the busy state) instead of sending the taken default. Closing the dialog or picking another creator aborts the remaining checks (oneAbortControllerper dialog). A late answer only reselects the basename while the field still has focus. If a check fails, the default stays — the server still rejects a taken name, so nothing gets overwritten.Reuse
Rung 2:
getUniqueName()from@nextcloud/files(already a dependency) for the naming scheme, and the existing@nextcloud/files/davclient.Contestable call, flagged deliberately: rung 1 would be the server's
OCP\Files\Folder::getNonExistingName(), but this app has no endpoint exposing it, and adding a controller only for a name suggestion felt larger than the problem. Happy to switch if you prefer that.Bound work
The cost depends on how many copies of the name already exist, not on the folder size: typically one request, at most 20. I first listed the root folder (one depth-1 PROPFIND), but that grows with the number of entries in the root, which "No unbounded reads" rules out.
Tests
freeFileName.spec.ts: free default after one check,(1), skipping taken copies one at a time, dots in the base name, probe limit, error propagation.folders.spec.ts: DAV path for root and nested folders, error propagation, abort signal passed on, one client reused across checks.OfficeOverview.spec.ts: free/taken/failed check; dialog opens before the check answers; a late answer for an earlier click is ignored and its checks aborted; a typed name is kept; Create/Enter before the answer creates the free name; closing while Create waits creates nothing; closing aborts the checks; focus/selection are only touched while the field is focused. All review follow-up tests fail against the first commit.npm run test:unit,npm run lint,npm run stylelint,npm run buildpass locally.vue-tsc --noEmitreports the same 4 errors as onmain, none in the changed code. Bundle size unchanged (+0.3 KB).New document.docx, then switches toNew document (1).docx(two depth-0 PROPFINDs); after creating it,New document (2).docx(three). The free candidate's 404 shows up as a failed request in the browser console — expected, and the same as anyexists()check.js/andcss/are not committed — please/compile.Closes nothing on its own; #205 (choose the target folder) builds on this in a follow-up PR.