@vitrinka/web 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +46 -0
- package/README.md +73 -21
- package/build/hud.d.ts +9 -0
- package/build/hud.iife.js +411 -0
- package/build/hud.js +8 -0
- package/build/recorder/api.d.ts +2 -0
- package/build/recorder/block.d.ts +7 -0
- package/build/recorder/block.js +7 -0
- package/build/recorder/capture/click.d.ts +2 -4
- package/build/recorder/capture/click.js +2 -31
- package/build/recorder/capture/rrweb.d.ts +2 -2
- package/build/recorder/capture/rrweb.js +2 -2
- package/build/recorder/capture/selector.d.ts +8 -0
- package/build/recorder/capture/selector.js +35 -0
- package/build/recorder/hud/AnnotateOverlay.d.ts +7 -1
- package/build/recorder/hud/AnnotateOverlay.js +51 -17
- package/build/recorder/hud/Hud.d.ts +16 -1
- package/build/recorder/hud/Hud.js +251 -83
- package/build/recorder/hud/LinkSheet.d.ts +3 -3
- package/build/recorder/hud/LinkSheet.js +1 -1
- package/build/recorder/hud/Menu.d.ts +30 -0
- package/build/recorder/hud/Menu.js +73 -0
- package/build/recorder/hud/RecorderPill.d.ts +36 -27
- package/build/recorder/hud/RecorderPill.js +212 -123
- package/build/recorder/hud/Sheet.d.ts +1 -1
- package/build/recorder/hud/Sheet.js +1 -1
- package/build/recorder/hud/Tooltip.d.ts +19 -0
- package/build/recorder/hud/Tooltip.js +94 -0
- package/build/recorder/hud/controller.d.ts +148 -0
- package/build/recorder/hud/controller.js +17 -0
- package/build/recorder/hud/flow.d.ts +54 -0
- package/build/recorder/hud/flow.js +39 -0
- package/build/recorder/hud/host.js +1 -1
- package/build/recorder/hud/icons.d.ts +7 -0
- package/build/recorder/hud/icons.js +19 -0
- package/build/recorder/hud/iife.d.ts +13 -0
- package/build/recorder/hud/iife.js +11 -0
- package/build/recorder/hud/layer.d.ts +29 -6
- package/build/recorder/hud/layer.js +67 -33
- package/build/recorder/hud/mount.d.ts +13 -0
- package/build/recorder/hud/mount.js +27 -0
- package/build/recorder/hud/place.d.ts +43 -0
- package/build/recorder/hud/place.js +63 -0
- package/build/recorder/hud/spots.d.ts +45 -0
- package/build/recorder/hud/spots.js +107 -0
- package/build/recorder/hud/status.d.ts +31 -0
- package/build/recorder/hud/status.js +67 -0
- package/build/recorder/hud/styles.d.ts +28 -18
- package/build/recorder/hud/styles.js +295 -122
- package/build/recorder/hud/useDock.d.ts +9 -8
- package/build/recorder/hud/useDock.js +41 -20
- package/build/recorder/index.d.ts +2 -2
- package/build/recorder/index.js +9 -18
- package/build/recorder/link.d.ts +1 -1
- package/build/recorder/link.js +8 -2
- package/build/recorder/me.d.ts +34 -0
- package/build/recorder/me.js +172 -0
- package/build/recorder/page-controller.d.ts +12 -0
- package/build/recorder/page-controller.js +100 -0
- package/build/recorder/queue.d.ts +4 -0
- package/build/recorder/queue.js +2 -0
- package/build/recorder/recents.d.ts +23 -0
- package/build/recorder/recents.js +80 -0
- package/build/recorder/session.d.ts +2 -2
- package/build/recorder/session.js +15 -1
- package/build/recorder/storage/index.js +2 -1
- package/package.json +9 -3
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where the HUD rests and how it moves (recorder-hud-subtle D2). The dock
|
|
3
|
-
* sits on one of
|
|
3
|
+
* sits on one of eight spots by CSS insets (see `.dock` in styles.ts), so a
|
|
4
4
|
* resize or rotation needs no JS; a drag moves it by `transform` only and a
|
|
5
|
-
* release settles through
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* release settles through `./spots` — a flick lands where it is aimed, a
|
|
6
|
+
* push past a side edge tucks it into a tab. The move to the new spot is a
|
|
7
|
+
* FLIP: the dock re-anchors, then springs from where it was let go. Arrow
|
|
8
|
+
* keys on a focused handle are the non-drag alternative. The place is
|
|
9
|
+
* remembered per origin in the HUD's storage.
|
|
10
10
|
*/
|
|
11
|
-
import { colOf, neighbour, parsePlace, rowOf, settle, untuck } from '@vitrinka/link/dock';
|
|
12
11
|
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
13
|
-
import {
|
|
12
|
+
import { colOf, neighbour, parsePlace, rowOf, settle, untuck } from './spots';
|
|
14
13
|
const DOCK_KEY = 'dock';
|
|
15
14
|
/** Travel before a press becomes a drag — below it a press stays a tap. */
|
|
16
15
|
const DRAG_SLOP = 4;
|
|
@@ -23,15 +22,35 @@ function reducedMotion() {
|
|
|
23
22
|
function margin() {
|
|
24
23
|
return globalThis.matchMedia?.('(pointer: coarse)').matches ? 12 : 16;
|
|
25
24
|
}
|
|
26
|
-
/**
|
|
25
|
+
/**
|
|
26
|
+
* The point of `r` a place is anchored by: its corner, its middle on the c
|
|
27
|
+
* column, its top edge on the middle row (the handle's middle sits at 50%),
|
|
28
|
+
* its mid-height when tucked.
|
|
29
|
+
*/
|
|
27
30
|
function anchorPoint(r, place) {
|
|
28
31
|
const spot = untuck(place);
|
|
29
32
|
const x = colOf(spot) === 'l' ? r.left : colOf(spot) === 'r' ? r.right : r.left + r.width / 2;
|
|
30
|
-
const
|
|
33
|
+
const row = rowOf(spot);
|
|
34
|
+
const y = 'tuck' in place ? r.top + r.height / 2 : row === 'b' ? r.bottom : r.top;
|
|
31
35
|
return { x, y };
|
|
32
36
|
}
|
|
33
|
-
|
|
34
|
-
|
|
37
|
+
/**
|
|
38
|
+
* The centre column anchors the handle's middle (`--hw` = its width); the
|
|
39
|
+
* middle row anchors the handle's middle too (`--hh` = its height when the
|
|
40
|
+
* pill stands up, the pill's height otherwise).
|
|
41
|
+
*/
|
|
42
|
+
function measureAnchor(el) {
|
|
43
|
+
const h = el.querySelector('.seg.on .handle, .seg.on .puck, .tab');
|
|
44
|
+
const pill = el.querySelector('.pill');
|
|
45
|
+
if (h)
|
|
46
|
+
el.style.setProperty('--hw', `${h.offsetWidth}px`);
|
|
47
|
+
const v = pill?.dataset.orient === 'v';
|
|
48
|
+
const hh = v ? h?.offsetHeight : (pill ?? h)?.offsetHeight;
|
|
49
|
+
if (hh)
|
|
50
|
+
el.style.setProperty('--hh', `${hh}px`);
|
|
51
|
+
}
|
|
52
|
+
export function useDock(onMoveStart, storage) {
|
|
53
|
+
const [place, setPlace] = useState(() => parsePlace(storage.getString(DOCK_KEY)));
|
|
35
54
|
const [dragging, setDragging] = useState(false);
|
|
36
55
|
const ref = useRef(null);
|
|
37
56
|
const from = useRef(null);
|
|
@@ -45,12 +64,18 @@ export function useDock(onMoveStart) {
|
|
|
45
64
|
from.current = el.getBoundingClientRect();
|
|
46
65
|
setPlace(next);
|
|
47
66
|
try {
|
|
48
|
-
|
|
67
|
+
storage.set(DOCK_KEY, JSON.stringify(next));
|
|
49
68
|
}
|
|
50
69
|
catch (e) {
|
|
51
70
|
console.warn('vitrinka: could not remember the HUD position', e);
|
|
52
71
|
}
|
|
53
|
-
}, []);
|
|
72
|
+
}, [storage]);
|
|
73
|
+
// Every commit can swap the face or the orientation: re-measure the anchor
|
|
74
|
+
// before the FLIP below reads where the dock now rests.
|
|
75
|
+
useLayoutEffect(() => {
|
|
76
|
+
if (ref.current)
|
|
77
|
+
measureAnchor(ref.current);
|
|
78
|
+
});
|
|
54
79
|
// FLIP: the dock has re-anchored; spring it from where it was let go.
|
|
55
80
|
useLayoutEffect(() => {
|
|
56
81
|
const el = ref.current;
|
|
@@ -76,16 +101,12 @@ export function useDock(onMoveStart) {
|
|
|
76
101
|
const done = () => el.classList.remove('settling');
|
|
77
102
|
el.addEventListener('transitionend', done, { once: true });
|
|
78
103
|
}, [place]);
|
|
79
|
-
//
|
|
104
|
+
// …and between commits, while a label or the tray tweens.
|
|
80
105
|
useEffect(() => {
|
|
81
106
|
const el = ref.current;
|
|
82
107
|
if (!el || typeof ResizeObserver !== 'function')
|
|
83
108
|
return;
|
|
84
|
-
const ro = new ResizeObserver(() =>
|
|
85
|
-
const h = el.querySelector('.handle, .puck, .tab');
|
|
86
|
-
if (h)
|
|
87
|
-
el.style.setProperty('--hw', `${h.offsetWidth}px`);
|
|
88
|
-
});
|
|
109
|
+
const ro = new ResizeObserver(() => measureAnchor(el));
|
|
89
110
|
ro.observe(el);
|
|
90
111
|
return () => ro.disconnect();
|
|
91
112
|
}, []);
|
|
@@ -42,7 +42,7 @@ export interface VitrinkaRecorderPillProps {
|
|
|
42
42
|
}
|
|
43
43
|
/**
|
|
44
44
|
* The HUD — mount it anywhere under the root (it renders into its own shadow
|
|
45
|
-
* host on `<html>`, never into the app's DOM)
|
|
46
|
-
* inert.
|
|
45
|
+
* host on `<html>`, never into the app's DOM), driven by the in-page
|
|
46
|
+
* recorder's controller. Renders null when the root is inert.
|
|
47
47
|
*/
|
|
48
48
|
export declare function VitrinkaRecorderPill(props: VitrinkaRecorderPillProps): null;
|
package/build/recorder/index.js
CHANGED
|
@@ -9,11 +9,11 @@
|
|
|
9
9
|
* outside an allowed lane.
|
|
10
10
|
*/
|
|
11
11
|
import { createElement, Fragment, useEffect, useRef } from 'react';
|
|
12
|
-
import { createRoot } from 'react-dom/client';
|
|
13
12
|
import { envConfig, vitrinkaConfigured } from './config';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
13
|
+
import { mountRecorderHud } from './hud/mount';
|
|
14
|
+
import { createPageController } from './page-controller';
|
|
16
15
|
import { RecorderProvider, useRecorderRoute } from './RecorderProvider';
|
|
16
|
+
import { getRecorderStorage } from './storage';
|
|
17
17
|
export { useRecorderRoute };
|
|
18
18
|
export { configureRecorderStorage } from './storage';
|
|
19
19
|
/** Wrap the app root. Starts the capture lanes only when `url` + `key` are present. */
|
|
@@ -41,28 +41,19 @@ function RouteFeed({ route }) {
|
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* The HUD — mount it anywhere under the root (it renders into its own shadow
|
|
44
|
-
* host on `<html>`, never into the app's DOM)
|
|
45
|
-
* inert.
|
|
44
|
+
* host on `<html>`, never into the app's DOM), driven by the in-page
|
|
45
|
+
* recorder's controller. Renders null when the root is inert.
|
|
46
46
|
*/
|
|
47
47
|
export function VitrinkaRecorderPill(props) {
|
|
48
|
-
const rootRef = useRef(null);
|
|
49
48
|
const titleRef = useRef(props.title);
|
|
50
49
|
titleRef.current = props.title;
|
|
51
50
|
useEffect(() => {
|
|
52
51
|
if (!vitrinkaConfigured())
|
|
53
52
|
return;
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
hostMount: host.mount,
|
|
59
|
-
defaultTitle: () => titleRef.current ?? document.title,
|
|
60
|
-
}));
|
|
61
|
-
return () => {
|
|
62
|
-
root.unmount();
|
|
63
|
-
host.destroy();
|
|
64
|
-
rootRef.current = null;
|
|
65
|
-
};
|
|
53
|
+
return mountRecorderHud(createPageController(), {
|
|
54
|
+
title: () => titleRef.current ?? document.title,
|
|
55
|
+
storage: getRecorderStorage(),
|
|
56
|
+
});
|
|
66
57
|
}, []);
|
|
67
58
|
return null;
|
|
68
59
|
}
|
package/build/recorder/link.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface DeviceLink {
|
|
|
16
16
|
* page, and a token approved into any other one is discarded unstored.
|
|
17
17
|
*/
|
|
18
18
|
export declare function linkDevice(): Promise<DeviceLink>;
|
|
19
|
-
/** Forget the stored link; a live session ends locally (its tail is dropped). */
|
|
19
|
+
/** Forget the stored link and its account; a live session ends locally (its tail is dropped). */
|
|
20
20
|
export declare function forgetLink(): void;
|
|
21
21
|
/** Register the 401 → unlinked transition. Returns the uninstaller. */
|
|
22
22
|
export declare function installUnauthorizedHandler(): () => void;
|
package/build/recorder/link.js
CHANGED
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
import { linkWorkspace, pollLink, startLink } from '@vitrinka/link';
|
|
6
6
|
import { onUnauthorized } from './api';
|
|
7
7
|
import { clearLink, defaultLinkLabel, recorderConfig, storeLink } from './config';
|
|
8
|
+
import { clearAccount } from './me';
|
|
8
9
|
import { getState, resetQueues, setState } from './queue';
|
|
10
|
+
import { updateRecent } from './recents';
|
|
11
|
+
import { elapsedOf } from './session';
|
|
9
12
|
import { notify } from './state';
|
|
10
13
|
export { LinkExpired } from '@vitrinka/link';
|
|
11
14
|
/**
|
|
@@ -24,10 +27,13 @@ export async function linkDevice() {
|
|
|
24
27
|
});
|
|
25
28
|
return { start, linked, cancel: () => ac.abort() };
|
|
26
29
|
}
|
|
27
|
-
/** Forget the stored link; a live session ends locally (its tail is dropped). */
|
|
30
|
+
/** Forget the stored link and its account; a live session ends locally (its tail is dropped). */
|
|
28
31
|
export function forgetLink() {
|
|
29
32
|
clearLink();
|
|
30
|
-
|
|
33
|
+
clearAccount();
|
|
34
|
+
const rec = getState();
|
|
35
|
+
if (rec) {
|
|
36
|
+
updateRecent(rec.sessionId, { status: 'unsaved', durationMs: elapsedOf(rec) });
|
|
31
37
|
setState(null);
|
|
32
38
|
resetQueues();
|
|
33
39
|
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who the token belongs to, and the HUD preferences that follow the user.
|
|
3
|
+
*
|
|
4
|
+
* GET /api/v1/recorder/me → {kind, workspace, user, project, label, prefs}
|
|
5
|
+
* PATCH /api/v1/recorder/me {prefs: {size?, verbose?}} → the same envelope
|
|
6
|
+
*
|
|
7
|
+
* Prefs live in the recorder storage first (`prefs`): the first paint, an
|
|
8
|
+
* offline page and a key build (`prefs: null`, PATCH answers 409) all read the
|
|
9
|
+
* local copy. A linked device's server prefs win whenever `/me` answers. A 404
|
|
10
|
+
* or a network error means a server too old for the route or an offline one:
|
|
11
|
+
* the local copy stands, said once. A 401 keeps the session doors' semantics
|
|
12
|
+
* (api.ts forgets the link).
|
|
13
|
+
*/
|
|
14
|
+
import { type HudAccount, type HudPrefs } from './hud/controller';
|
|
15
|
+
/** The `/recorder/me` envelope. */
|
|
16
|
+
export interface RecorderMe extends HudAccount {
|
|
17
|
+
prefs: HudPrefs | null;
|
|
18
|
+
}
|
|
19
|
+
/** The local prefs (defaults when never set). */
|
|
20
|
+
export declare function cachedPrefs(): HudPrefs;
|
|
21
|
+
/** The last account `/me` answered for this token (null when unknown). */
|
|
22
|
+
export declare function cachedAccount(): HudAccount | null;
|
|
23
|
+
/** Forget the account (unlink, 401); prefs stay with the device. */
|
|
24
|
+
export declare function clearAccount(): void;
|
|
25
|
+
/** Ask the server who this token is; null when unlinked, unavailable or refused. Never rejects. */
|
|
26
|
+
export declare function fetchMe(): Promise<HudAccount | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Apply a prefs change locally at once (the HUD re-renders), then persist it
|
|
29
|
+
* on the server for a linked device. A key build (409, or a cached `key`
|
|
30
|
+
* account) and an unavailable server keep it local. Never rejects.
|
|
31
|
+
*/
|
|
32
|
+
export declare function savePrefs(patch: Partial<HudPrefs>): Promise<HudPrefs>;
|
|
33
|
+
/** Test-only: drop the caches and the unavailable latch. */
|
|
34
|
+
export declare function __resetMeForTests(): void;
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who the token belongs to, and the HUD preferences that follow the user.
|
|
3
|
+
*
|
|
4
|
+
* GET /api/v1/recorder/me → {kind, workspace, user, project, label, prefs}
|
|
5
|
+
* PATCH /api/v1/recorder/me {prefs: {size?, verbose?}} → the same envelope
|
|
6
|
+
*
|
|
7
|
+
* Prefs live in the recorder storage first (`prefs`): the first paint, an
|
|
8
|
+
* offline page and a key build (`prefs: null`, PATCH answers 409) all read the
|
|
9
|
+
* local copy. A linked device's server prefs win whenever `/me` answers. A 404
|
|
10
|
+
* or a network error means a server too old for the route or an offline one:
|
|
11
|
+
* the local copy stands, said once. A 401 keeps the session doors' semantics
|
|
12
|
+
* (api.ts forgets the link).
|
|
13
|
+
*/
|
|
14
|
+
import { DEFAULT_PREFS } from './hud/controller';
|
|
15
|
+
import { api, VitrinkaApiError } from './api';
|
|
16
|
+
import { vitrinkaLinked } from './config';
|
|
17
|
+
import { notify } from './state';
|
|
18
|
+
import { getRecorderStorage } from './storage';
|
|
19
|
+
const PREFS_KEY = 'prefs';
|
|
20
|
+
const ME_KEY = 'me';
|
|
21
|
+
const SIZES = ['sm', 'md', 'lg'];
|
|
22
|
+
function parsePrefs(raw) {
|
|
23
|
+
if (!raw || typeof raw !== 'object')
|
|
24
|
+
return null;
|
|
25
|
+
const o = raw;
|
|
26
|
+
return {
|
|
27
|
+
size: SIZES.includes(o.size) ? o.size : DEFAULT_PREFS.size,
|
|
28
|
+
verbose: typeof o.verbose === 'boolean' ? o.verbose : DEFAULT_PREFS.verbose,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function readJson(key) {
|
|
32
|
+
const raw = getRecorderStorage().getString(key);
|
|
33
|
+
if (!raw)
|
|
34
|
+
return null;
|
|
35
|
+
try {
|
|
36
|
+
return JSON.parse(raw);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function write(key, value) {
|
|
43
|
+
try {
|
|
44
|
+
getRecorderStorage().set(key, JSON.stringify(value));
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
console.warn(`vitrinka: could not store ${key}`, e);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
let prefsCache;
|
|
51
|
+
let meCache;
|
|
52
|
+
/** The local prefs (defaults when never set). */
|
|
53
|
+
export function cachedPrefs() {
|
|
54
|
+
if (prefsCache === undefined)
|
|
55
|
+
prefsCache = parsePrefs(readJson(PREFS_KEY)) ?? DEFAULT_PREFS;
|
|
56
|
+
return prefsCache;
|
|
57
|
+
}
|
|
58
|
+
function storePrefs(p) {
|
|
59
|
+
prefsCache = p;
|
|
60
|
+
write(PREFS_KEY, p);
|
|
61
|
+
}
|
|
62
|
+
/** The last account `/me` answered for this token (null when unknown). */
|
|
63
|
+
export function cachedAccount() {
|
|
64
|
+
if (meCache === undefined) {
|
|
65
|
+
const o = readJson(ME_KEY);
|
|
66
|
+
meCache = o && typeof o === 'object' ? o : null;
|
|
67
|
+
}
|
|
68
|
+
return meCache;
|
|
69
|
+
}
|
|
70
|
+
/** Forget the account (unlink, 401); prefs stay with the device. */
|
|
71
|
+
export function clearAccount() {
|
|
72
|
+
meCache = null;
|
|
73
|
+
getRecorderStorage().remove(ME_KEY);
|
|
74
|
+
}
|
|
75
|
+
/** A 404 said the server predates the route: not asked again this document. */
|
|
76
|
+
let unavailable = false;
|
|
77
|
+
let warned = false;
|
|
78
|
+
/** 404 (route unknown) and transport failures mean "too old or offline", never an error to show. */
|
|
79
|
+
function soft(e) {
|
|
80
|
+
return !(e instanceof VitrinkaApiError) || e.status === 404;
|
|
81
|
+
}
|
|
82
|
+
function noteUnavailable(e) {
|
|
83
|
+
if (e instanceof VitrinkaApiError)
|
|
84
|
+
unavailable = true;
|
|
85
|
+
if (warned)
|
|
86
|
+
return;
|
|
87
|
+
warned = true;
|
|
88
|
+
console.warn('vitrinka: /recorder/me unavailable — HUD prefs stay on this device', e);
|
|
89
|
+
}
|
|
90
|
+
function isMe(o) {
|
|
91
|
+
if (!o || typeof o !== 'object')
|
|
92
|
+
return false;
|
|
93
|
+
const m = o;
|
|
94
|
+
const ws = m.workspace;
|
|
95
|
+
return (m.kind === 'linked' || m.kind === 'key') && typeof ws?.slug === 'string' && typeof ws.name === 'string';
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Local edits so far. A response carries the count from when its request
|
|
99
|
+
* left, and its prefs count only if no edit happened since: a menu GET that
|
|
100
|
+
* overlaps a size change, or two PATCHes answering out of order, never
|
|
101
|
+
* revert the newest choice. The account is always taken.
|
|
102
|
+
*/
|
|
103
|
+
let edits = 0;
|
|
104
|
+
function adopt(me, gen) {
|
|
105
|
+
if (!isMe(me)) {
|
|
106
|
+
console.warn('vitrinka: /recorder/me answered an unexpected shape — ignored');
|
|
107
|
+
return cachedAccount();
|
|
108
|
+
}
|
|
109
|
+
const account = {
|
|
110
|
+
kind: me.kind,
|
|
111
|
+
workspace: me.workspace,
|
|
112
|
+
user: me.user,
|
|
113
|
+
project: me.project,
|
|
114
|
+
label: me.label,
|
|
115
|
+
};
|
|
116
|
+
meCache = account;
|
|
117
|
+
write(ME_KEY, account);
|
|
118
|
+
const server = parsePrefs(me.prefs);
|
|
119
|
+
if (server && gen === edits)
|
|
120
|
+
storePrefs(server);
|
|
121
|
+
notify();
|
|
122
|
+
return account;
|
|
123
|
+
}
|
|
124
|
+
/** Ask the server who this token is; null when unlinked, unavailable or refused. Never rejects. */
|
|
125
|
+
export async function fetchMe() {
|
|
126
|
+
if (!vitrinkaLinked() || unavailable)
|
|
127
|
+
return cachedAccount();
|
|
128
|
+
const gen = edits;
|
|
129
|
+
try {
|
|
130
|
+
return adopt(await api('GET', '/api/v1/recorder/me'), gen);
|
|
131
|
+
}
|
|
132
|
+
catch (e) {
|
|
133
|
+
if (soft(e))
|
|
134
|
+
noteUnavailable(e);
|
|
135
|
+
else
|
|
136
|
+
console.warn('vitrinka: /recorder/me failed', e);
|
|
137
|
+
return cachedAccount();
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Apply a prefs change locally at once (the HUD re-renders), then persist it
|
|
142
|
+
* on the server for a linked device. A key build (409, or a cached `key`
|
|
143
|
+
* account) and an unavailable server keep it local. Never rejects.
|
|
144
|
+
*/
|
|
145
|
+
export async function savePrefs(patch) {
|
|
146
|
+
const next = { ...cachedPrefs(), ...patch };
|
|
147
|
+
storePrefs(next);
|
|
148
|
+
const gen = ++edits;
|
|
149
|
+
notify();
|
|
150
|
+
if (!vitrinkaLinked() || unavailable || cachedAccount()?.kind === 'key')
|
|
151
|
+
return next;
|
|
152
|
+
try {
|
|
153
|
+
adopt(await api('PATCH', '/api/v1/recorder/me', { prefs: patch }), gen);
|
|
154
|
+
}
|
|
155
|
+
catch (e) {
|
|
156
|
+
if (e instanceof VitrinkaApiError && e.status === 409)
|
|
157
|
+
return next;
|
|
158
|
+
if (soft(e))
|
|
159
|
+
noteUnavailable(e);
|
|
160
|
+
else
|
|
161
|
+
console.warn('vitrinka: saving HUD prefs failed — kept on this device', e);
|
|
162
|
+
}
|
|
163
|
+
return cachedPrefs();
|
|
164
|
+
}
|
|
165
|
+
/** Test-only: drop the caches and the unavailable latch. */
|
|
166
|
+
export function __resetMeForTests() {
|
|
167
|
+
prefsCache = undefined;
|
|
168
|
+
meCache = undefined;
|
|
169
|
+
unavailable = false;
|
|
170
|
+
warned = false;
|
|
171
|
+
edits = 0;
|
|
172
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The in-page recorder as a `HudController` — the HUD's view of this
|
|
3
|
+
* document's session, link, account, prefs and recents, and its doors into
|
|
4
|
+
* them. Another host (the browser extension) supplies its own controller.
|
|
5
|
+
*
|
|
6
|
+
* The snapshot is rebuilt on every read and handed back as the SAME object
|
|
7
|
+
* while nothing in it changed (a structural key), so `useSyncExternalStore`
|
|
8
|
+
* stays quiet between changes even for values no `notify()` announces (the
|
|
9
|
+
* event count, the offline threshold) — the HUD's clock tick picks those up.
|
|
10
|
+
*/
|
|
11
|
+
import type { HudController } from './hud/controller';
|
|
12
|
+
export declare function createPageController(): HudController;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { readLink, recorderConfig, vitrinkaLinked } from './config';
|
|
2
|
+
import { forgetLink, linkDevice } from './link';
|
|
3
|
+
import { cachedAccount, cachedPrefs, fetchMe, savePrefs } from './me';
|
|
4
|
+
import { getState, health } from './queue';
|
|
5
|
+
import { readRecents, refreshRecents } from './recents';
|
|
6
|
+
import { addAnnotation, addNote, RECORDER_ID, startSession, stopSession, togglePause } from './session';
|
|
7
|
+
import { annotateState, setAnnotating, subscribe } from './state';
|
|
8
|
+
function build() {
|
|
9
|
+
const rec = getState();
|
|
10
|
+
const cfg = recorderConfig();
|
|
11
|
+
let recording = null;
|
|
12
|
+
if (rec) {
|
|
13
|
+
const h = health();
|
|
14
|
+
recording = {
|
|
15
|
+
sessionId: rec.sessionId,
|
|
16
|
+
title: rec.title,
|
|
17
|
+
...(rec.boardUrl ? { boardUrl: rec.boardUrl } : {}),
|
|
18
|
+
paused: rec.paused,
|
|
19
|
+
dead: Boolean(rec.dead),
|
|
20
|
+
activeMs: rec.activeMs || 0,
|
|
21
|
+
resumedAt: !rec.paused && rec.resumeAt ? Date.parse(rec.resumeAt) : null,
|
|
22
|
+
sync: {
|
|
23
|
+
state: h.state === 'idle' ? 'ok' : h.state,
|
|
24
|
+
synced: h.synced,
|
|
25
|
+
queued: h.queued,
|
|
26
|
+
chunks: h.chunks,
|
|
27
|
+
failures: h.failures,
|
|
28
|
+
error: h.error,
|
|
29
|
+
lastSyncAt: h.lastSyncAt,
|
|
30
|
+
events: h.localSeq,
|
|
31
|
+
serverMaxSeq: h.serverMaxSeq,
|
|
32
|
+
deadReason: h.deadReason,
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
linked: vitrinkaLinked(),
|
|
38
|
+
canUnlink: !cfg.key && readLink() !== null,
|
|
39
|
+
annotating: annotateState.active,
|
|
40
|
+
recording,
|
|
41
|
+
account: vitrinkaLinked() ? cachedAccount() : null,
|
|
42
|
+
prefs: cachedPrefs(),
|
|
43
|
+
// A "recording" that is not this document's live session was never
|
|
44
|
+
// stopped (its tab closed, or another tab holds it).
|
|
45
|
+
recents: readRecents().map((r) => r.status === 'recording' && r.sessionId !== rec?.sessionId ? { ...r, status: 'unsaved' } : r),
|
|
46
|
+
workspaceUrl: cfg.url,
|
|
47
|
+
version: RECORDER_ID,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
export function createPageController() {
|
|
51
|
+
let last = null;
|
|
52
|
+
let lastKey = '';
|
|
53
|
+
return {
|
|
54
|
+
getSnapshot() {
|
|
55
|
+
const next = build();
|
|
56
|
+
const key = JSON.stringify(next);
|
|
57
|
+
if (last && key === lastKey)
|
|
58
|
+
return last;
|
|
59
|
+
last = next;
|
|
60
|
+
lastKey = key;
|
|
61
|
+
return next;
|
|
62
|
+
},
|
|
63
|
+
subscribe,
|
|
64
|
+
async start({ title }) {
|
|
65
|
+
await startSession({ title });
|
|
66
|
+
},
|
|
67
|
+
async togglePause() {
|
|
68
|
+
await togglePause();
|
|
69
|
+
},
|
|
70
|
+
async stop() {
|
|
71
|
+
const boardUrl = getState()?.boardUrl;
|
|
72
|
+
const live = getState() !== null;
|
|
73
|
+
const done = await stopSession();
|
|
74
|
+
// null for a live session = the server refused to close it (ended locally, not saved).
|
|
75
|
+
if (live && done === null)
|
|
76
|
+
throw new Error('the server refused to close this session — it ended locally, not saved');
|
|
77
|
+
const url = done?.board?.url ?? boardUrl;
|
|
78
|
+
return url ? { boardUrl: url } : {};
|
|
79
|
+
},
|
|
80
|
+
note: (text) => addNote(text),
|
|
81
|
+
annotate: ({ text, rect, selector, task }) => addAnnotation(text, rect, selector, { task }),
|
|
82
|
+
setAnnotating,
|
|
83
|
+
async link() {
|
|
84
|
+
const flow = await linkDevice();
|
|
85
|
+
return {
|
|
86
|
+
start: flow.start,
|
|
87
|
+
linked: flow.linked.then(() => {
|
|
88
|
+
void fetchMe();
|
|
89
|
+
}),
|
|
90
|
+
cancel: flow.cancel,
|
|
91
|
+
};
|
|
92
|
+
},
|
|
93
|
+
unlink: forgetLink,
|
|
94
|
+
getMe: fetchMe,
|
|
95
|
+
async setPrefs(patch) {
|
|
96
|
+
await savePrefs(patch);
|
|
97
|
+
},
|
|
98
|
+
refreshRecents: () => refreshRecents(getState()?.sessionId ?? null),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -74,9 +74,13 @@ export type RecorderHealthState = 'idle' | 'ok' | 'backlog' | 'offline' | 'dead'
|
|
|
74
74
|
export interface RecorderHealth {
|
|
75
75
|
state: RecorderHealthState;
|
|
76
76
|
queued: number;
|
|
77
|
+
/** Of `queued`, the rrweb chunks still pending upload. */
|
|
78
|
+
chunks: number;
|
|
77
79
|
failures: number;
|
|
78
80
|
error: string;
|
|
79
81
|
sinceSyncMs: number | null;
|
|
82
|
+
/** Epoch ms of the last confirmed delivery or reconcile (null before any). */
|
|
83
|
+
lastSyncAt: number | null;
|
|
80
84
|
localSeq: number;
|
|
81
85
|
serverMaxSeq: number;
|
|
82
86
|
/** The reconciliation itself: the server accounts for every allocated seq. */
|
package/build/recorder/queue.js
CHANGED
|
@@ -191,9 +191,11 @@ export function health() {
|
|
|
191
191
|
return {
|
|
192
192
|
state,
|
|
193
193
|
queued,
|
|
194
|
+
chunks: getChunks().length,
|
|
194
195
|
failures,
|
|
195
196
|
error: lastError,
|
|
196
197
|
sinceSyncMs: sinceSync,
|
|
198
|
+
lastSyncAt: lastSyncAt || null,
|
|
197
199
|
localSeq: rec?.seq ?? 0,
|
|
198
200
|
serverMaxSeq,
|
|
199
201
|
synced: rec !== null && !rec.dead && serverMaxSeq >= rec.seq && queued === 0,
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The device's last five recordings (`recents` in the recorder storage),
|
|
3
|
+
* newest first. The session lifecycle writes them (start → recording, stop →
|
|
4
|
+
* saved with the board link and duration, a local end → unsaved); the HUD's
|
|
5
|
+
* menu lists them. A recorder token cannot LIST sessions, only read its own by
|
|
6
|
+
* id, so a recent without a board link is refreshed through that reconcile
|
|
7
|
+
* read (`GET /api/v1/sessions/:id`).
|
|
8
|
+
*/
|
|
9
|
+
import type { HudRecent } from './hud/controller';
|
|
10
|
+
export declare const RECENTS_KEY = "recents";
|
|
11
|
+
export declare const MAX_RECENTS = 5;
|
|
12
|
+
export declare function readRecents(): readonly HudRecent[];
|
|
13
|
+
/** Add (or move to the front) a recording. */
|
|
14
|
+
export declare function noteRecent(entry: HudRecent): void;
|
|
15
|
+
/** Patch a recording in place; a no-op for one not listed. */
|
|
16
|
+
export declare function updateRecent(sessionId: string, patch: Partial<Omit<HudRecent, 'sessionId'>>): void;
|
|
17
|
+
/**
|
|
18
|
+
* Fill the board links the list lacks — `live` (the recording in progress)
|
|
19
|
+
* is skipped, its link arrives with its stop. Never rejects.
|
|
20
|
+
*/
|
|
21
|
+
export declare function refreshRecents(live: string | null): Promise<void>;
|
|
22
|
+
/** Test-only: forget the in-memory list (storage stays). */
|
|
23
|
+
export declare function __resetRecentsForTests(): void;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { api, VitrinkaApiError } from './api';
|
|
2
|
+
import { notify } from './state';
|
|
3
|
+
import { getRecorderStorage } from './storage';
|
|
4
|
+
export const RECENTS_KEY = 'recents';
|
|
5
|
+
export const MAX_RECENTS = 5;
|
|
6
|
+
const STATUSES = ['recording', 'saved', 'unsaved', 'deleted'];
|
|
7
|
+
function valid(r) {
|
|
8
|
+
if (!r || typeof r !== 'object')
|
|
9
|
+
return false;
|
|
10
|
+
const o = r;
|
|
11
|
+
return (typeof o.sessionId === 'string' &&
|
|
12
|
+
typeof o.title === 'string' &&
|
|
13
|
+
typeof o.startedAt === 'number' &&
|
|
14
|
+
STATUSES.includes(o.status));
|
|
15
|
+
}
|
|
16
|
+
let cache;
|
|
17
|
+
export function readRecents() {
|
|
18
|
+
if (cache === undefined) {
|
|
19
|
+
try {
|
|
20
|
+
const raw = JSON.parse(getRecorderStorage().getString(RECENTS_KEY) ?? '[]');
|
|
21
|
+
cache = Array.isArray(raw) ? raw.filter(valid).slice(0, MAX_RECENTS) : [];
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
cache = [];
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return cache;
|
|
28
|
+
}
|
|
29
|
+
function write(list) {
|
|
30
|
+
cache = list.slice(0, MAX_RECENTS);
|
|
31
|
+
try {
|
|
32
|
+
getRecorderStorage().set(RECENTS_KEY, JSON.stringify(cache));
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
console.warn('vitrinka: could not store recent recordings', e);
|
|
36
|
+
}
|
|
37
|
+
notify();
|
|
38
|
+
}
|
|
39
|
+
/** Add (or move to the front) a recording. */
|
|
40
|
+
export function noteRecent(entry) {
|
|
41
|
+
write([entry, ...readRecents().filter((r) => r.sessionId !== entry.sessionId)]);
|
|
42
|
+
}
|
|
43
|
+
/** Patch a recording in place; a no-op for one not listed. */
|
|
44
|
+
export function updateRecent(sessionId, patch) {
|
|
45
|
+
const list = readRecents();
|
|
46
|
+
if (!list.some((r) => r.sessionId === sessionId))
|
|
47
|
+
return;
|
|
48
|
+
write(list.map((r) => (r.sessionId === sessionId ? { ...r, ...patch } : r)));
|
|
49
|
+
}
|
|
50
|
+
/** Ids already asked this document — a refused read is not retried on every menu open. */
|
|
51
|
+
const asked = new Set();
|
|
52
|
+
/**
|
|
53
|
+
* Fill the board links the list lacks — `live` (the recording in progress)
|
|
54
|
+
* is skipped, its link arrives with its stop. Never rejects.
|
|
55
|
+
*/
|
|
56
|
+
export async function refreshRecents(live) {
|
|
57
|
+
const missing = readRecents().filter((r) => !r.boardUrl && r.status !== 'deleted' && r.sessionId !== live && !asked.has(r.sessionId));
|
|
58
|
+
for (const r of missing) {
|
|
59
|
+
asked.add(r.sessionId);
|
|
60
|
+
try {
|
|
61
|
+
const s = await api('GET', `/api/v1/sessions/${r.sessionId}`);
|
|
62
|
+
const boardUrl = s.boardUrl ?? s.board?.url;
|
|
63
|
+
if (s.deletedAt)
|
|
64
|
+
updateRecent(r.sessionId, { status: 'deleted' });
|
|
65
|
+
else if (boardUrl)
|
|
66
|
+
updateRecent(r.sessionId, { boardUrl, ...(s.status === 'done' ? { status: 'saved' } : {}) });
|
|
67
|
+
}
|
|
68
|
+
catch (e) {
|
|
69
|
+
if (e instanceof VitrinkaApiError && e.status === 404)
|
|
70
|
+
updateRecent(r.sessionId, { status: 'deleted' });
|
|
71
|
+
else
|
|
72
|
+
console.warn(`vitrinka: could not refresh recent session ${r.sessionId}`, e);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/** Test-only: forget the in-memory list (storage stays). */
|
|
77
|
+
export function __resetRecentsForTests() {
|
|
78
|
+
cache = undefined;
|
|
79
|
+
asked.clear();
|
|
80
|
+
}
|
|
@@ -17,8 +17,8 @@ import { type SessionState } from './queue';
|
|
|
17
17
|
export { currentRoute, notify, subscribe } from './state';
|
|
18
18
|
export type { SessionDone } from '../protocol';
|
|
19
19
|
/** Sent as `meta.recorder`; bumped with the package version. */
|
|
20
|
-
export declare const RECORDER_VERSION = "0.
|
|
21
|
-
export declare const RECORDER_ID = "web/0.
|
|
20
|
+
export declare const RECORDER_VERSION = "0.2.0";
|
|
21
|
+
export declare const RECORDER_ID = "web/0.2.0";
|
|
22
22
|
/** What `POST /api/v1/sessions` answers (the fields this recorder keeps). */
|
|
23
23
|
export interface SessionOut {
|
|
24
24
|
id: string;
|