okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0213-preview (68bead108) into f-0213-inte82c70698

main
3.7 KiB74 linesraw
1/**
2 * Which copy of a file the sidebar shows (#5180 follow-up: the preview kept a file's first content after the file changed).
3 *
4 * A preview tab can come from two places: the person (or a finished run) opening a file, which reads it from the host now, and the
5 * message the file was made in, which carries the content it had then. They share one id (`file:<path>`). The rules, as pure functions:
6 *   - opening a file replaces the tab in place when the content differs, and keeps the very same array when it does not;
7 *   - a message's artifact only adds a tab that is missing, or replaces one that is OLDER than the message and different; it never
8 *     overwrites a copy that was read later;
9 *   - both return the array they were given when nothing changed, so no state update, no render and no effect follows: there is no loop to
10 *     start however often they are called (the same artifacts are handed over again whenever a message arrives).
11 * The panel reloads when `previewVersion` changes, and only then.
12 */
13import type { PreviewArtifact } from './artifacts';
14import { textRevision } from './preview-render';
15
16/** What identifies the content of a preview: the host's version for a file (modified time and size), else a hash of the text, else the address. */
17export function previewVersion(a: Pick<PreviewArtifact, 'version' | 'content' | 'url'>): string {
18    if (a.content !== undefined)
19        return 'c' + textRevision(a.content);
20    return a.version ? 'v' + a.version : 'u' + (a.url ?? '');
21}
22
23const sameContent = (a: PreviewArtifact, b: PreviewArtifact) => a.kind === b.kind && a.title === b.title && previewVersion(a) === previewVersion(b);
24
25/** A file was opened (read from the host just now): it takes the place of its tab when it differs. */
26export function openItem(old: PreviewArtifact[], item: PreviewArtifact): PreviewArtifact[] {
27    const at = old.findIndex(a => a.id === item.id);
28    if (at < 0)
29        return [...old, item];
30    if (sameContent(old[at], item))
31        return old;
32    const next = old.slice();
33    next[at] = item;
34    return next;
35}
36
37/**
38 * The artifacts the messages carry, merged into the tabs. `items` are in message order, each stamped with the time of its message (`at`);
39 * `closed` are the ids the person closed.
40 */
41export function syncArtifacts(old: PreviewArtifact[], items: PreviewArtifact[], closed: readonly string[] | undefined = []): PreviewArtifact[] {
42    let next = old;
43    const edit = () => { if (next === old) next = old.slice(); };
44    for (const item of items) {
45        const at = next.findIndex(a => a.id === item.id);
46        if (closed?.includes(item.id)) {
47            if (at >= 0) { edit(); next.splice(at, 1); }
48            continue;
49        }
50        if (at < 0) {
51            edit();
52            next.push(item);
53            continue;
54        }
55        const have = next[at];
56        // only a message newer than the copy on show, with different content, replaces it
57        if (item.at !== undefined && have.at !== undefined && item.at > have.at && !sameContent(have, item)) {
58            edit();
59            next[at] = item;
60        }
61    }
62    return next;
63}
64
65/** The message artifacts of a session, in order and de-duplicated by id (the last of an id wins), each stamped with its message's time. */
66export function messageArtifacts(messages: ReadonlyArray<{ at?: string; artifacts?: PreviewArtifact[] }> | undefined): PreviewArtifact[] {
67    const byId = new Map<string, PreviewArtifact>();
68    for (const m of messages ?? []) {
69        const at = m.at ? Date.parse(m.at) : NaN;
70        for (const a of m.artifacts ?? [])
71            byId.set(a.id, Number.isFinite(at) && a.at === undefined ? { ...a, at } : a);
72    }
73    return [...byId.values()];
74}