* refactor(thumbnails): one resolver for where previews live on disk (#320) Add py/api/preview_store.py: the store of a source, a preview's path, URL parts to disk, containment (owns), a write generation and the lock around every preview's final rename. Every preview reader, writer and remover now asks it instead of joining <source>/thumbnails by hand: serve, on-demand, capture (its path resolved when the write starts), the after-scan queue, the thumbnail plans and job payloads, clear, the temp sweep, History covers, the auto-tag listing and the owned-preview lookups. Source mode only: every path is the one built before, so nothing changes on disk or on the wire. A structural test keeps new thumbnails joins out of the backend. * refactor(thumbnails): keep the after-scan queue's store rule and pin the write lock (#320) A planned target keeps the plan's own containment rule (inside the store, as the thumbnail job writes it); captures keep the serve rule. The structural test also covers PREVIEW_FOLDER joins and the repo-root modules; the write-lock test asserts the lock instead of waiting on a timer; new tests cover nested roots in the after-scan queue and a legacy upper-case preview served through the resolver. * feat(thumbnails): keep previews in one folder, set from config.json (#320) thumbnails.location in config.json can now name one folder for every source's previews: <folder>/<source key>/<rel>_thumb<ext>. A source key is a readable slug plus a short hash of the source's identity, stored in the folder's marker (.promptmanager-thumbnails.json) with the install that made it. Serving, on-demand and capture writes, listings, clear, delete, duplicates, the temp sweep and History covers all follow it; a leftover in <source>/thumbnails is never served and other text under thumbnails/ is refused. A missing, unplugged or foreign folder is unavailable (a probe checks it every 5 s within 2 s; a stalled or failing store call flips it at once): originals are served, nothing is written anywhere, the folder is never re-created, and generate, convert and clear answer 409 location-unavailable. A bad config value is unavailable too, never a fall-back to the sources. No UI yet: POST /settings refuses thumbnail_location (409 location-needs-move); GET /settings reports it with its path, the thumbnail status without one. Sources inside or around the folder and a library move into it are refused. * fix(thumbnails): never follow a marker or a link out of the thumbnail folder (#320) Marker keys are data, not paths: only a single <slug>-<hex> folder name is ever joined onto the thumbnail folder; anything else is dropped and logged. Every preview write and delete passes one ownership check (below a key folder, lexically and resolved, no link, junction or reparse point on the way); make_parent refuses anything else and writes nothing while the folder lacks this install's marker (an unmounted mount point). Clear never descends into a junction. The configured location is in force before any request (unchecked folder mode is unavailable), and a startup check that fails leaves it invalid, never the sources. Older listings that run on the event loop no longer touch the store in folder mode. A leftover under another case of thumbnails/ on a case-insensitive disk is refused too. * test(memory): let the musl test fake os.confstr on Windows (#523) The portability run on main fails since #523: Windows has no os.confstr, so patching it without create=True raises before the test runs. * feat(thumbnails): move thumbnails between the sources and one folder (#320) POST /images/thumbnails/plan reviews a move (what is copied, removed and left, free space, too-long Windows paths, sources no longer listed) and hands out a token; POST /jobs/thumbnails {mode: move} runs it as a thumbnails job: hold automatic previews, copy and check every preview on the job pool, switch with one config commit that records the old copies, then remove them. Per-file errors still switch; a folder-wide error, a cancel before the switch or a crash leaves nothing changed (a journal is undone at the next start); old copies that could not go are removed by a finishing job at the next start. Leaving a folder removes the previews of this install's sources that are no longer listed (Q320-8). Recreate folder and Use this folder answer an unavailable folder; the Thumbnail maintenance scope prune removes the thumbnails of removed sources. * feat(system): choose where thumbnails are kept and move them (#320) System › Sources & scans gains the Thumbnail location row (D196): Inside each source or One folder, the folder path, the status line and Review move…, whose review (D197) lists what is copied, removed and left before Move thumbnails starts; a refused folder is explained under the field, a folder of your own asks for a confirmation first. Activity shows a move and a prune with their own tiles and step line (D198). An unavailable folder shows a notice on Overview and Sources & scans with Recreate folder or Use this folder (D199). Thumbnail maintenance offers Remove thumbnails of removed sources in folder mode (D200), and Move library's review says where the thumbnails stay (D201). Closes #320 * fix(thumbnails): keep a move's own previews and never walk out of a store (#320) A cleanup that waited from an earlier move never names a key folder the new move just filled, so moving back keeps the previews. Removing empty folders walks top-down and never into a link or junction. Any error while copying undoes the copies; once the switch is committed, a journal that cannot be removed no longer undoes it. Hand-edited cleanup entries and marker keys never name a folder outside the stores; removal stops at interpreter exit. The path field carries aria-invalid with its reason, and a review that went stale reviews again. * test(thumbnails): cover the prune scope, the Overview notice and the space refusal (#320) The frontend coverage gate counted the new branches of thumbnail maintenance, the Activity views, the notices' Change location and the api-client's space refusal as uncovered; page and unit tests now drive them. * fix(thumbnails): report removals that fail and explain the quiet excepts (#320) An old thumbnail the finishing move cannot remove (held open on Windows) is now an error row of the job with its path, and logged; a failed copy's temp that cannot go is logged with its path. Every except that only passes says why, and the thread runners say their caller re-raises. * fix(thumbnails): never plan or delete through a linked key folder (#320) Review finding P1 on #591: in folder mode a source key folder that is a link or junction to a folder outside was still handed to the planner, so Convert outdated could remove an older-format file outside the thumbnail folder. ensure_store now refuses a linked store before any planning, and each removal of an old preview passes may_write at the moment it runs. * fix(thumbnails): the temp sweep never walks into a link (#320) Review finding P2 on #591: Clear thumbnails and the startup sweep removed crashed-write temps with a plain os.walk, which on Windows walks into a junction even without followlinks. The sweeper now skips a folder that is a link, junction or reparse point (the top one too), and each temp passes may_write before it is removed. * fix(thumbnails): lock the folder marker across processes (#320) Review finding P2 on #591: two installs sharing one thumbnail folder could each read the marker, add a key and replace it, so the last writer dropped the other's key. write_marker now re-reads, changes and replaces the marker under a lock file made with O_CREAT | O_EXCL beside it (atomic on shares, unlike flock); a lock older than 30 s, left by a writer that died, is taken over, and a wait past 1.5 s fails as a TimeoutError like an unreachable folder. Tested with three processes adding keys at once. * test(thumbnails): cover the space refusal, Overview's Use this folder and the prune review (#320) The frontend coverage floors of activity-kinds, system-overview and thumbnail-review: a disk without the space shows the review but cannot move (a fixture route fakes the free space), Use this folder on Overview reloads the host values, the prune review's text and rows are pure functions with unit tests, and Overview takes the System drafts it is always mounted with. * fix(thumbnails): never copy through a linked folder; refuse a target inside the folder in use (#320) Moving back to Inside each source followed an existing linked folder in the destination and could replace a file outside the source (Codex P1). A target nested inside the thumbnail folder in use was accepted, and the old-copy cleanup then removed the previews just copied there (Codex P2). Every copy now checks the whole destination chain for links, also when the folders exist, and the move refuses a folder inside the one in use with location-inside-current. The cancelled branches no longer mix explicit and implicit returns (code-quality note). * fix(system): one full stop in the move-back review (#320) * fix(thumbnails): a folder check answers for the folder as it is now (#320) Probe.check() joined a check already running, or a finished one nobody had collected. A folder deleted right after configure() woke the probe therefore stayed available until the next probe, which failed the Overview notice spec in the merge queue. It now waits for a running check and starts a new one, still never two at once, and a stuck check still times out. The spec also asserts the fixture's removal took effect.
413 lines
20 KiB
JavaScript
413 lines
20 KiB
JavaScript
/**
|
||
* Thumbnail location (#320, beyond the concept: D196–D199, approved static mock `320-mock`): where previews are kept.
|
||
*
|
||
* - D196 `locationBlock`: a `.setting-block` in System › Sources & scans after "Thumbnail generation": the radios
|
||
* "Inside each source" / "One folder", a "Folder path" field (enabled with One folder, prefilled with the folder in
|
||
* use or the server's `default_path`), the status line and "Review move…" (enabled when the choice differs from the
|
||
* location in use). Not a host draft: the Save row never saves it; the Move thumbnails job is the only writer (D4B).
|
||
* A refusal of the review shows in place under the field (`aria-invalid`), in the server's words.
|
||
* - D197 `openMoveReview`: the shared review (Move library's shape) over the server's plan
|
||
* (`POST /images/thumbnails/plan`): From, To, Thumbnails, Free space, Too long for Windows, Removed after the move,
|
||
* Left in place, Links skipped; a folder that is not the default asks for the confirmation checkbox first. "Move
|
||
* thumbnails" starts the job (`POST /jobs/thumbnails {mode: "move", token, confirmed}`) and opens it in Activity.
|
||
* - D199 `locationNotice`: while the thumbnail folder is unavailable, a `.notice` on Overview and Sources & scans
|
||
* says why, with "Recreate folder" (a missing folder, after a confirmation) or "Use this folder" (another folder's
|
||
* marker), and "Change location".
|
||
*
|
||
* The location comes from GET /settings' `thumbnail_location` (`{mode, path, default_path, available, problem,
|
||
* cleanup_pending}`), read with the host settings.
|
||
*/
|
||
import { createButton } from '../shared/toolkit/core.js';
|
||
import { closeWorkspaceDialog, openContentDialog, openReviewDialog } from './workspace-dialogs.mjs';
|
||
import { busyToast } from './activity-scan.mjs';
|
||
import { showWorkspaceToast } from './toast.mjs';
|
||
import { navigateTo } from './state.mjs';
|
||
import { noun } from './plural.mjs';
|
||
|
||
export const PLAN_URL = '/prompt_manager/images/thumbnails/plan';
|
||
export const RECREATE_URL = '/prompt_manager/images/thumbnails/location/recreate';
|
||
export const ADOPT_URL = '/prompt_manager/images/thumbnails/location/adopt';
|
||
export const LOCATION_HINT = 'Where previews are kept. Originals are never moved. Avoid cloud-synced folders.';
|
||
export const SENTENCE = 'Every thumbnail is copied and checked before Prompt Manager switches. Scans, deletes and '
|
||
+ 'tagging wait until it finishes; closing this page does not stop it. Originals are never touched.';
|
||
export const REVIEW_FAILED = 'The move could not be reviewed. Try again.';
|
||
export const START_FAILED = 'Move thumbnails could not start. Try again.';
|
||
export const NOTICE_TITLE = 'The thumbnail folder is unavailable';
|
||
const BACK = 'Originals are shown and no thumbnails are written until it is back.';
|
||
const CHECK = 'If its drive is unplugged, connect it; Prompt Manager checks again every few seconds.';
|
||
|
||
const number = value => Number(value || 0).toLocaleString('en-US');
|
||
const isFolder = location => location?.mode === 'folder';
|
||
|
||
/** "1.2 GB" (binary units, one decimal, as the server writes them). */
|
||
export function sizeText(bytes) {
|
||
let size = Number(bytes) || 0;
|
||
for (const unit of ['B', 'KB', 'MB', 'GB']) {
|
||
if (size < 1024 || unit === 'GB') return unit === 'B' ? `${size} B` : `${size.toFixed(1)} ${unit}`;
|
||
size /= 1024;
|
||
}
|
||
return `${size.toFixed(1)} GB`;
|
||
}
|
||
|
||
export const SUGGESTED = 'The suggested folder is next to your library; Review move… shows what happens before anything '
|
||
+ 'is copied.';
|
||
|
||
/** The status line under the row; `choice` (`{mode, path}`) adds the suggestion while the default folder is chosen. */
|
||
export function statusText(location, choice = null) {
|
||
if (!location) return '';
|
||
if (!isFolder(location) && choice?.mode === 'folder' && location.default_path
|
||
&& choice.path?.trim() === location.default_path) {
|
||
return `Now: inside each source. ${SUGGESTED}`;
|
||
}
|
||
let text = isFolder(location) ? `Now: one folder, ${location.path}` : 'Now: inside each source';
|
||
if (isFolder(location) && location.available === false) return `${text} · unavailable. Originals are shown.`;
|
||
text += '.';
|
||
if (isFolder(location) && location.path && location.path === location.default_path) {
|
||
text += ' Using the default folder next to your library.';
|
||
}
|
||
if (location.cleanup_pending > 0) {
|
||
text += ` Old thumbnails in ${number(location.cleanup_pending)} ${noun(location.cleanup_pending, 'place')} wait to `
|
||
+ 'be removed until it is connected.';
|
||
}
|
||
return text;
|
||
}
|
||
|
||
/** Whether the row's choice (`{mode, path}`) differs from the location in use (Review move… enabled). */
|
||
export function choiceChanged(location, choice) {
|
||
if (!location) return false;
|
||
if (choice.mode !== 'folder') return isFolder(location) || Boolean(location.problem);
|
||
const path = (choice.path || '').trim();
|
||
return Boolean(path) && (!isFolder(location) || path !== location.path);
|
||
}
|
||
|
||
/** The D197 review rows (`[term, value]`, a value may be `[lead, items]`) for the server's plan. */
|
||
export function reviewRows(plan) {
|
||
const totals = plan.totals ?? {};
|
||
const sources = totals.sources ?? plan.sources?.length ?? 0;
|
||
const inside = `Inside each source (${number(sources)} ${noun(sources, 'source')})`;
|
||
const from = plan.from?.mode === 'folder' ? plan.from.path : inside;
|
||
let to = inside;
|
||
if (plan.to?.mode === 'folder') {
|
||
const state = plan.to.default ? (plan.to.will_create ? 'new, next to your library' : 'next to your library')
|
||
: (plan.to.will_create ? 'new folder' : 'empty folder');
|
||
to = `${plan.to.path} (${state})`;
|
||
}
|
||
const rows = [['From', from], ['To', to],
|
||
['Thumbnails', `${number(totals.files)} in ${number(sources)} ${noun(sources, 'source')} · ${sizeText(totals.bytes)}`]];
|
||
if (Number.isFinite(plan.free_bytes)) rows.push(['Free space', `${sizeText(plan.free_bytes)} free`]);
|
||
if (totals.too_long > 0) {
|
||
const more = totals.too_long - (plan.too_long_listed?.length ?? 0);
|
||
rows.push(['Too long for Windows', [`${number(totals.too_long)} ${noun(totals.too_long, 'thumbnail')} cannot be `
|
||
+ 'stored there. They show the original instead; choose a shorter folder to keep them.',
|
||
[...(plan.too_long_listed ?? []), ...(more > 0 ? [`and ${number(more)} more`] : [])]]]);
|
||
}
|
||
const removed = [];
|
||
if (totals.orphans > 0) removed.push(`${number(totals.orphans)} ${noun(totals.orphans, 'thumbnail')} whose original is gone`);
|
||
if (totals.temps > 0) removed.push(`${number(totals.temps)} unfinished temporary ${noun(totals.temps, 'file')}`);
|
||
for (const source of plan.unlisted ?? []) {
|
||
removed.push(`1 source no longer listed: ${number(source.files)} ${noun(source.files, 'thumbnail')} are removed; `
|
||
+ 'they are made again if it returns');
|
||
}
|
||
const oldCopies = plan.from?.mode === 'folder'
|
||
? 'The thumbnail folders of these sources inside it. The folder itself is removed once empty if Prompt Manager created it'
|
||
: 'The old copies in each source’s thumbnails folder';
|
||
rows.push(['Removed after the move', [removed.length ? `${oldCopies}, and:` : `${oldCopies}.`, removed]]);
|
||
if (totals.left_in_place > 0) {
|
||
rows.push(['Left in place', [`${number(totals.left_in_place)} ${noun(totals.left_in_place, 'file')} that `
|
||
+ `${totals.left_in_place === 1 ? 'is' : 'are'} not ${totals.left_in_place === 1 ? 'a thumbnail' : 'thumbnails'}:`,
|
||
plan.left_listed ?? []]]);
|
||
}
|
||
if (totals.links > 0) {
|
||
rows.push(['Links skipped', `${number(totals.links)} ${noun(totals.links, 'link')} inside a thumbnails folder `
|
||
+ `${totals.links === 1 ? 'is' : 'are'} not followed`]);
|
||
}
|
||
return rows;
|
||
}
|
||
|
||
/** The notice text for an unavailable `location` (D199). */
|
||
export function noticeText(location) {
|
||
const where = location?.path || 'The thumbnail folder';
|
||
switch (location?.problem) {
|
||
case 'missing': return `${where} is missing. ${BACK} ${CHECK}`;
|
||
case 'unreachable': return `${where} can’t be reached. ${BACK} ${CHECK}`;
|
||
case 'marker-mismatch': return `${where} holds another thumbnail folder. ${BACK} Use it if it is yours, or change `
|
||
+ 'the location.';
|
||
case 'invalid': return `The thumbnail location in config.json can’t be used. ${BACK} Change the location.`;
|
||
default: return `${where} is being checked. ${BACK}`;
|
||
}
|
||
}
|
||
|
||
function el(tag, text, className) {
|
||
const node = document.createElement(tag);
|
||
if (text !== undefined) node.textContent = text;
|
||
if (className) node.className = className;
|
||
return node;
|
||
}
|
||
|
||
function radio(value, label, hint) {
|
||
const node = el('label');
|
||
const input = el('input');
|
||
input.type = 'radio';
|
||
input.name = 'thumb-location';
|
||
input.value = value;
|
||
input.id = `loc-${value}`;
|
||
node.append(input, ` ${label} `, el('span', hint, 'small'));
|
||
return { node, input };
|
||
}
|
||
|
||
/** Start the move with the plan's `token`; opens it in Activity, or says why not. */
|
||
export async function startMove({ store, scope, token, confirmed, notify = showWorkspaceToast, navigate = navigateTo,
|
||
onStale = null }) {
|
||
try {
|
||
const result = await store.start('thumbnails', { mode: 'move', token, confirmed });
|
||
if (scope.signal.aborted) return null;
|
||
if (result.busy !== undefined) {
|
||
notify(busyToast(result.busy));
|
||
return null;
|
||
}
|
||
navigate(`#/activity/${result.job.id}`);
|
||
return result.job;
|
||
} catch (error) {
|
||
if (scope.signal.aborted) return null;
|
||
// The sources or the location changed since the review: review again with what is there now.
|
||
if (error?.code === 'plan-stale' && onStale) {
|
||
onStale();
|
||
return null;
|
||
}
|
||
notify(typeof error?.code === 'string' && error.message ? error.message : START_FAILED);
|
||
return null;
|
||
}
|
||
}
|
||
|
||
/** Open the D197 review over `plan`; a plan the server refused for space keeps Move thumbnails unavailable. */
|
||
export function openMoveReview({ scope, store, plan, refusal = null, trigger = null, notify, navigate, onStale = null }) {
|
||
const options = [];
|
||
let box = null;
|
||
if (plan.confirm_required && !refusal) {
|
||
const label = el('label', undefined, 'review-confirm');
|
||
box = el('input');
|
||
box.type = 'checkbox';
|
||
box.id = 'review-confirmed';
|
||
label.append(box, el('span', `Prompt Manager may create its own folders and a .promptmanager-thumbnails.json file in ${plan.to.path}.`));
|
||
options.push(label);
|
||
}
|
||
if (refusal) options.push(el('p', refusal, 'small review-refusal'));
|
||
const review = openReviewDialog({ scope, trigger, title: 'Move thumbnails', grid: reviewRows(plan), options,
|
||
sentence: refusal ? null : SENTENCE, cancelLabel: 'Cancel', primary: { label: 'Move thumbnails' },
|
||
onStart: () => { startMove({ store, scope, token: plan.token, confirmed: Boolean(box?.checked), notify, navigate,
|
||
onStale }); } });
|
||
const locked = () => Boolean(refusal) || (box !== null && !box.checked);
|
||
review.setPrimary({ label: 'Move thumbnails', disabled: locked() });
|
||
if (box) scope.listen(box, 'change', () => review.setPrimary({ label: 'Move thumbnails', disabled: locked() }));
|
||
return review;
|
||
}
|
||
|
||
/**
|
||
* The D196 row; `location()` gives the server's `thumbnail_location` (undefined while read). Returns `{node, paint,
|
||
* focus}`; `paint()` follows a new location.
|
||
*/
|
||
export function locationBlock({ api, store, scope, location, notify = showWorkspaceToast, navigate = navigateTo }) {
|
||
const block = el('div', undefined, 'setting-block');
|
||
block.id = 'location-block';
|
||
const text = el('div', undefined, 'setting-block-text');
|
||
const label = el('span', 'Thumbnail location', 'setting-block-label');
|
||
label.id = 'location-label';
|
||
text.append(label, el('p', LOCATION_HINT));
|
||
const choice = el('div', undefined, 'location-choice');
|
||
choice.setAttribute('role', 'radiogroup');
|
||
choice.setAttribute('aria-labelledby', 'location-label');
|
||
const source = radio('source', 'Inside each source', '<source>/thumbnails');
|
||
const folder = radio('folder', 'One folder', 'one subfolder per source');
|
||
choice.append(source.node, folder.node);
|
||
const pathField = el('div', undefined, 'location-path field');
|
||
const pathLabel = el('label', 'Folder path');
|
||
pathLabel.htmlFor = 'location-path';
|
||
const input = el('input');
|
||
input.id = 'location-path';
|
||
input.autocomplete = 'off';
|
||
input.spellcheck = false;
|
||
const problem = el('p', '', 'small location-problem');
|
||
problem.id = 'location-problem';
|
||
problem.setAttribute('role', 'alert');
|
||
pathField.append(pathLabel, input, problem);
|
||
const status = el('p', '', 'small location-status');
|
||
status.id = 'location-status';
|
||
status.setAttribute('role', 'status');
|
||
const actions = el('div', undefined, 'row-actions');
|
||
const review = createButton('Review move…');
|
||
review.id = 'review-move';
|
||
actions.append(review);
|
||
block.append(text, choice, pathField, status, actions);
|
||
|
||
let shown;
|
||
const setProblem = message => {
|
||
problem.textContent = message || '';
|
||
pathField.setAttribute('aria-invalid', String(Boolean(message)));
|
||
// The field itself carries the state, so a screen reader announces it with the reason.
|
||
if (message) {
|
||
input.setAttribute('aria-invalid', 'true');
|
||
input.setAttribute('aria-describedby', 'location-problem');
|
||
} else {
|
||
input.removeAttribute('aria-invalid');
|
||
input.removeAttribute('aria-describedby');
|
||
}
|
||
};
|
||
const current = () => ({ mode: folder.input.checked ? 'folder' : 'source', path: input.value });
|
||
const repaintButton = () => {
|
||
const now = location();
|
||
input.disabled = !folder.input.checked || !now;
|
||
source.input.disabled = folder.input.disabled = !now;
|
||
const changed = choiceChanged(now, current());
|
||
status.textContent = now ? statusText(now, current()) : '';
|
||
review.setAttribute('aria-disabled', String(!changed));
|
||
review.classList.toggle('ca-primary', changed);
|
||
};
|
||
const paint = () => {
|
||
const now = location();
|
||
if (now && now !== shown) {
|
||
shown = now;
|
||
(isFolder(now) ? folder : source).input.checked = true;
|
||
input.value = (isFolder(now) ? now.path : now.default_path) || '';
|
||
setProblem(null);
|
||
}
|
||
repaintButton();
|
||
};
|
||
for (const control of [source.input, folder.input]) {
|
||
scope.listen(control, 'change', () => {
|
||
if (folder.input.checked && !input.value) input.value = location()?.default_path || '';
|
||
setProblem(null);
|
||
repaintButton();
|
||
});
|
||
}
|
||
scope.listen(input, 'input', () => { setProblem(null); repaintButton(); });
|
||
scope.listen(review, 'click', async () => {
|
||
if (review.getAttribute('aria-disabled') === 'true') return;
|
||
const chosen = current();
|
||
const body = { mode: 'move', location: chosen.mode === 'folder' ? { mode: 'folder', path: chosen.path.trim() }
|
||
: { mode: 'source' } };
|
||
review.setAttribute('aria-disabled', 'true');
|
||
try {
|
||
const plan = await api.post(PLAN_URL, body, { signal: scope.signal });
|
||
if (!scope.signal.aborted) {
|
||
openMoveReview({ scope, store, plan, trigger: review, notify, navigate, onStale: () => review.click() });
|
||
}
|
||
} catch (error) {
|
||
if (scope.signal.aborted) return;
|
||
if (error?.status === 507 && error?.plan) {
|
||
openMoveReview({ scope, store, plan: error.plan, refusal: error.plan.insufficient?.text || error.message,
|
||
trigger: review });
|
||
} else if (error?.status === 409 && error?.code === 'busy') {
|
||
notify(busyToast(error.running ?? null));
|
||
} else if (typeof error?.code === 'string' && error.code.startsWith('location-') && error.message) {
|
||
setProblem(error.message);
|
||
input.focus();
|
||
} else {
|
||
notify(typeof error?.message === 'string' && error?.code ? error.message : REVIEW_FAILED);
|
||
}
|
||
} finally {
|
||
repaintButton();
|
||
}
|
||
});
|
||
paint();
|
||
return {
|
||
node: block,
|
||
paint,
|
||
focus() {
|
||
folder.input.checked = true;
|
||
repaintButton();
|
||
input.focus();
|
||
block.scrollIntoView?.({ block: 'center' });
|
||
},
|
||
};
|
||
}
|
||
|
||
/** Open the Recreate confirmation for `location`; resolves once it closed. */
|
||
export function openRecreate({ api, scope, location, trigger = null, onDone, notify = showWorkspaceToast }) {
|
||
const grid = el('dl', undefined, 'meta-grid');
|
||
for (const [term, value] of [['Folder', location.path], ['Made', 'An empty folder with a new .promptmanager-thumbnails.json'],
|
||
['Thumbnails', 'Made again as you browse, or with Thumbnail maintenance']]) {
|
||
const entry = el('div');
|
||
entry.append(el('dt', term), el('dd', value));
|
||
grid.append(entry);
|
||
}
|
||
const why = el('p', '', 'small location-problem');
|
||
const cancel = createButton('Cancel');
|
||
const go = createButton('Recreate folder', 'ca-primary');
|
||
const actions = el('div', undefined, 'dialog-actions');
|
||
actions.append(cancel, go);
|
||
scope.listen(cancel, 'click', () => closeWorkspaceDialog());
|
||
scope.listen(go, 'click', async () => {
|
||
try {
|
||
const result = await api.post(RECREATE_URL, {}, { signal: scope.signal });
|
||
if (scope.signal.aborted) return;
|
||
closeWorkspaceDialog();
|
||
onDone?.(result.location);
|
||
} catch (error) {
|
||
if (scope.signal.aborted) return;
|
||
why.textContent = error?.message || 'The folder could not be recreated.';
|
||
if (!error?.message) notify(why.textContent);
|
||
}
|
||
});
|
||
openContentDialog({ scope, title: 'Recreate the thumbnail folder?', trigger, nodes: [grid,
|
||
el('p', 'If the folder is on a drive that is not connected, connect it instead: recreating would put thumbnails on '
|
||
+ 'another disk.', 'small'), why, actions] });
|
||
}
|
||
|
||
/**
|
||
* The D199 notice for `location` (null while it is available): Recreate folder for a missing folder, Use this folder
|
||
* for another folder's marker, and Change location (`onChange`).
|
||
*/
|
||
export function locationNotice({ api, scope, location, onChange, onDone, notify = showWorkspaceToast }) {
|
||
if (!isFolder(location) || location.available !== false) return null;
|
||
const notice = el('div', undefined, 'notice');
|
||
notice.id = 'unavailable-notice';
|
||
const text = el('div');
|
||
text.append(el('h2', NOTICE_TITLE), el('p', noticeText(location)));
|
||
const actions = el('div', undefined, 'row-actions');
|
||
if (location.problem === 'missing') {
|
||
const recreate = createButton('Recreate folder');
|
||
scope.listen(recreate, 'click', () => openRecreate({ api, scope, location, trigger: recreate, onDone, notify }));
|
||
actions.append(recreate);
|
||
} else if (location.problem === 'marker-mismatch') {
|
||
const adopt = createButton('Use this folder');
|
||
scope.listen(adopt, 'click', async () => {
|
||
try {
|
||
const result = await api.post(ADOPT_URL, {}, { signal: scope.signal });
|
||
if (!scope.signal.aborted) onDone?.(result.location);
|
||
} catch (error) {
|
||
if (!scope.signal.aborted) notify(error?.message || 'The folder could not be used.');
|
||
}
|
||
});
|
||
actions.append(adopt);
|
||
}
|
||
const change = createButton('Change location', 'ca-primary');
|
||
scope.listen(change, 'click', () => onChange?.());
|
||
actions.append(change);
|
||
notice.append(text, actions);
|
||
return notice;
|
||
}
|
||
|
||
/**
|
||
* Where a section shows the D199 notice: a comment `anchor` (no element, so the concept comparisons see nothing while the
|
||
* folder is available) after which `paint()` keeps the notice for `location()`.
|
||
*/
|
||
export function noticeSlot({ api, scope, location, onChange, onDone, notify = showWorkspaceToast }) {
|
||
const anchor = document.createComment('thumbnail-location-notice');
|
||
let shownFor;
|
||
let notice = null;
|
||
const paint = () => {
|
||
const now = location();
|
||
// Not mounted yet: the next paint (a microtask, or the next host read) places it.
|
||
if (!anchor.parentNode) {
|
||
queueMicrotask(() => { if (anchor.parentNode && !scope.signal.aborted) paint(); });
|
||
return;
|
||
}
|
||
if (now === shownFor) return;
|
||
shownFor = now;
|
||
notice?.remove();
|
||
notice = locationNotice({ api, scope, location: now, onChange, onDone, notify });
|
||
if (notice) anchor.after(notice);
|
||
};
|
||
return { anchor, paint };
|
||
}
|