@g9tm/app-diagnostic-lynx 0.4.0 → 0.5.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.
@@ -0,0 +1,224 @@
1
+ // The tester mode's default UI for a Lynx app (4.5.0; .agents/skills/g9-app-diagnostic/references/tester-mode.md «On
2
+ // Lynx»): the floating pill and the send form over @g9tm/app-diagnostic's TesterEngine — the same behaviour the first
3
+ // Lynx app built in its own theme (moved out of it on 2026-11-20 so every Lynx app has a tester mode). Plain Lynx elements,
4
+ // no design system: an app with one passes its own `style` or keeps its themed copy on top of the same engine.
5
+ //
6
+ // <TesterPanel engine={engine} language={() => 'fa'} /> the pill (gesture → screenshot → report)
7
+ // <TesterReports engine={engine} language={() => 'fa'} /> this device's reports and what waits in its outbox
8
+ //
9
+ // Never hidden while the screen records: a hidden Stop strands the tester until the 10-minute cap.
10
+ import { type ReactNode, useEffect, useState } from '@lynx-js/react';
11
+ import type { DiagLanguage, ReadableStore, TesterEngine, TesterFeed, TesterReportKind, TesterState } from '@g9tm/app-diagnostic';
12
+ import type { CSSProperties } from '@lynx-js/types';
13
+
14
+ /** The pill's and the form's own words (the engine's notices come translated from the package). */
15
+ const UI = {
16
+ fa: {
17
+ send: 'فرستادن', screenshot: 'عکس', record: 'ضبط صفحه', stop: 'توقف', voice: 'صدا', stopVoice: 'پایان صدا', attach: 'پیوست',
18
+ discard: 'حذف', confirmDiscard: 'گزارش و همهٔ شاهدهایش پاک شود؟', yes: 'پاک کن', no: 'نه', kind: 'نوع گزارش',
19
+ words: 'چه دیدی؟ چه انتظار داشتی؟', backup: 'پشتیبان دیتابیس هم برود', ai: 'هوش مصنوعی گزارش را بخواند', cancel: 'بستن',
20
+ review: 'فرستادن گزارش', evidence: 'شاهد', reports: 'گزارش‌های این دستگاه', queued: 'در صف ارسال', none: 'هنوز گزارشی نیست',
21
+ loadFailed: 'فهرست گزارش‌ها نیامد', refresh: 'تازه کن', start: 'گزارش تازه',
22
+ },
23
+ en: {
24
+ send: 'Send', screenshot: 'Shot', record: 'Record', stop: 'Stop', voice: 'Voice', stopVoice: 'End voice', attach: 'Attach',
25
+ discard: 'Delete', confirmDiscard: 'Delete the report and all its evidence?', yes: 'Delete', no: 'No', kind: 'Kind of report',
26
+ words: 'What did you see? What did you expect?', backup: 'Send the database backup too', ai: 'Let the AI read the report', cancel: 'Close',
27
+ review: 'Send the report', evidence: 'evidence', reports: 'This device\'s reports', queued: 'Waiting to be sent', none: 'No report yet',
28
+ loadFailed: 'The reports did not load', refresh: 'Refresh', start: 'New report',
29
+ },
30
+ } as const;
31
+
32
+ type Words = { readonly [K in keyof typeof UI.fa]: string };
33
+ const wordsOf = (language: () => DiagLanguage): Words => (language() === 'en' ? UI.en : UI.fa);
34
+
35
+ /** A store's value as React state (re-rendered on every change). */
36
+ function useStore<T>(store: ReadableStore<T>): T {
37
+ const [value, setValue] = useState<T>(store.get());
38
+ useEffect(() => store.subscribe(() => setValue(store.get())), [store]);
39
+ return value;
40
+ }
41
+
42
+ const clock = (ms: number) => {
43
+ const s = Math.max(0, Math.floor(ms / 1000));
44
+ return `${String(Math.floor(s / 60)).padStart(2, '0')}:${String(s % 60).padStart(2, '0')}`;
45
+ };
46
+
47
+ export interface TesterPanelProps {
48
+ readonly engine: TesterEngine;
49
+ readonly language: () => DiagLanguage;
50
+ /** Where the pill sits (default: the bottom end corner, above the home indicator). */
51
+ readonly style?: Record<string, string | number>;
52
+ /** The pill's colours: background, foreground, the recording red. */
53
+ readonly colors?: { readonly bg?: string; readonly fg?: string; readonly red?: string; readonly accent?: string };
54
+ }
55
+
56
+ function Button(p: { readonly label: string; readonly onTap: () => void; readonly strong?: boolean; readonly fg: string; readonly accent: string; readonly badge?: number }): ReactNode {
57
+ return (
58
+ <view bindtap={p.onTap} style={{ paddingLeft: '10px', paddingRight: '10px', height: '40px', borderRadius: '20px', display: 'flex', alignItems: 'center', justifyContent: 'center', backgroundColor: p.strong ? p.accent : 'transparent', marginLeft: '2px', marginRight: '2px' }}>
59
+ <text style={{ color: p.strong ? '#ffffff' : p.fg, fontSize: '14px', fontWeight: p.strong ? 'bold' : 'normal' }}>{p.badge ? `${p.label} ${p.badge}` : p.label}</text>
60
+ </view>
61
+ );
62
+ }
63
+
64
+ /** The floating capture pill: hidden with no report (the gesture starts one), the whole pill red with a clock while the screen records. */
65
+ export function TesterPanel(props: TesterPanelProps): ReactNode {
66
+ const { engine } = props;
67
+ const s: TesterState = useStore(engine.state);
68
+ const w = wordsOf(props.language);
69
+ const [form, setForm] = useState(false);
70
+ const [confirm, setConfirm] = useState(false);
71
+ const [now, setNow] = useState(Date.now());
72
+ const bg = props.colors?.bg ?? 'rgba(20,26,40,0.92)';
73
+ const fg = props.colors?.fg ?? '#e6eaf2';
74
+ const red = props.colors?.red ?? '#d8434a';
75
+ const accent = props.colors?.accent ?? '#0d8f84';
76
+ const recordingScreen = s.recording.screen;
77
+ // The clock ticks only while something records (Lynx's background thread has setTimeout through the runtime).
78
+ useEffect(() => {
79
+ if (!recordingScreen && !s.recording.voice) return undefined;
80
+ const id = setInterval(() => setNow(Date.now()), 1000);
81
+ return () => clearInterval(id);
82
+ }, [recordingScreen, s.recording.voice]);
83
+ if (!s.enabled) return null;
84
+ const draft = s.draft;
85
+ if (!draft && !s.showButton && s.gestureSupported) return null;
86
+ if (s.capturing) return null; // the tester documents the APP, not this pill
87
+ const box = { position: 'fixed', bottom: '96px', right: '16px', display: 'flex', flexDirection: 'row', alignItems: 'center', padding: '4px', borderRadius: '24px', ...(props.style ?? {}) } as Record<string, string | number>;
88
+ if (!draft) {
89
+ return (
90
+ <view style={{ ...box, backgroundColor: bg }}>
91
+ <Button label={`+ ${w.start}`} onTap={() => void engine.screenshot()} fg={fg} accent={accent} />
92
+ </view>
93
+ );
94
+ }
95
+ const elapsed = s.recordingStartedMs ? now - s.recordingStartedMs : 0;
96
+ if (recordingScreen) {
97
+ return (
98
+ <view style={{ ...box, backgroundColor: red }}>
99
+ <text style={{ color: '#ffffff', fontSize: '14px', fontWeight: 'bold', marginLeft: '12px', marginRight: '8px' }}>{`● ${clock(elapsed)}`}</text>
100
+ <Button label={`■ ${w.stop}`} onTap={() => void engine.toggleScreen()} fg="#ffffff" accent={accent} />
101
+ </view>
102
+ );
103
+ }
104
+ return (
105
+ <view>
106
+ <view style={{ ...box, backgroundColor: bg }}>
107
+ {s.recording.voice ? <text style={{ color: red, fontSize: '14px', fontWeight: 'bold', marginLeft: '10px', marginRight: '4px' }}>{`● ${clock(elapsed)}`}</text> : null}
108
+ <Button label={w.send} strong onTap={() => setForm(true)} fg={fg} accent={accent} badge={draft.attachments.length} />
109
+ <Button label={w.screenshot} onTap={() => void engine.screenshot()} fg={fg} accent={accent} />
110
+ {s.support.screen ? <Button label={w.record} onTap={() => void engine.toggleScreen()} fg={fg} accent={accent} /> : null}
111
+ {s.support.voice ? <Button label={s.recording.voice ? w.stopVoice : w.voice} onTap={() => void engine.toggleVoice()} fg={fg} accent={accent} /> : null}
112
+ <Button label={w.attach} onTap={() => void engine.attach()} fg={fg} accent={accent} />
113
+ <Button label={w.discard} onTap={() => setConfirm(true)} fg={fg} accent={accent} />
114
+ </view>
115
+ {confirm ? (
116
+ <Sheet onClose={() => setConfirm(false)}>
117
+ <text style={{ fontSize: '16px', marginBottom: '16px' }}>{w.confirmDiscard}</text>
118
+ <view style={{ display: 'flex', flexDirection: 'row' }}>
119
+ <Button label={w.yes} strong onTap={() => { setConfirm(false); void engine.discard(); }} fg={fg} accent={red} />
120
+ <Button label={w.no} onTap={() => setConfirm(false)} fg="#46526a" accent={accent} />
121
+ </view>
122
+ </Sheet>
123
+ ) : null}
124
+ {form ? <TesterForm engine={engine} language={props.language} onClose={() => setForm(false)} /> : null}
125
+ </view>
126
+ );
127
+ }
128
+
129
+ function Sheet(p: { readonly onClose: () => void; readonly children: ReactNode }): ReactNode {
130
+ return (
131
+ <view style={{ position: 'fixed', left: '0px', right: '0px', top: '0px', bottom: '0px', backgroundColor: 'rgba(0,0,0,0.45)', display: 'flex', flexDirection: 'column', justifyContent: 'flex-end' }} bindtap={p.onClose}>
132
+ <view catchtap={() => undefined} style={{ backgroundColor: '#ffffff', borderTopLeftRadius: '18px', borderTopRightRadius: '18px', padding: '20px', paddingBottom: '36px' }}>
133
+ {p.children}
134
+ </view>
135
+ </view>
136
+ );
137
+ }
138
+
139
+ /** The send form: the kind, the tester's own words (the one box), the database backup and — when the fleet asks — the AI. */
140
+ export function TesterForm(props: { readonly engine: TesterEngine; readonly language: () => DiagLanguage; readonly onClose: () => void }): ReactNode {
141
+ const { engine } = props;
142
+ const w = wordsOf(props.language);
143
+ const t = engine.translate;
144
+ const s = useStore(engine.state);
145
+ const kinds: readonly TesterReportKind[] = engine.kinds;
146
+ const [kind, setKind] = useState<string>(s.draft?.kind ?? kinds[0]?.key ?? 'bug');
147
+ const [words, setWords] = useState<string>(s.draft?.description ?? '');
148
+ const [backup, setBackup] = useState<boolean>(s.draft?.includeDatabaseBackup ?? false);
149
+ const [ai, setAi] = useState<boolean>(true);
150
+ const [sending, setSending] = useState(false);
151
+ const required = engine.descriptionRequired(s.draft) && words.trim().length === 0;
152
+ const label = (k: TesterReportKind) => (typeof k.label === 'function' ? k.label(t) : t(k.label));
153
+ const chip = (on: boolean): CSSProperties => ({ paddingLeft: '12px', paddingRight: '12px', height: '34px', borderRadius: '17px', marginRight: '6px', marginBottom: '6px', display: 'flex', alignItems: 'center', backgroundColor: on ? '#0d8f84' : '#eef1f6' });
154
+ const send = async () => {
155
+ if (sending || required) return;
156
+ setSending(true);
157
+ const ok = await engine.send({ words, kind, includeDatabaseBackup: backup, ...(engine.asksAboutAi ? { useAi: ai } : {}) });
158
+ setSending(false);
159
+ if (ok) props.onClose();
160
+ };
161
+ return (
162
+ <Sheet onClose={props.onClose}>
163
+ <text style={{ fontSize: '18px', fontWeight: 'bold', marginBottom: '12px' }}>{w.review}</text>
164
+ <text style={{ fontSize: '13px', color: '#46526a', marginBottom: '6px' }}>{w.kind}</text>
165
+ <view style={{ display: 'flex', flexDirection: 'row', flexWrap: 'wrap', marginBottom: '10px' }}>
166
+ {kinds.map((k) => (
167
+ <view key={k.key} bindtap={() => setKind(k.key)} style={chip(kind === k.key)}>
168
+ <text style={{ color: kind === k.key ? '#ffffff' : '#172033', fontSize: '14px' }}>{label(k)}</text>
169
+ </view>
170
+ ))}
171
+ </view>
172
+ <textarea
173
+ default-value={words}
174
+ placeholder={w.words}
175
+ bindinput={(e: { detail: { value: string } }) => setWords(e.detail.value)}
176
+ style={{ minHeight: '110px', borderWidth: '1px', borderColor: required ? '#c2283a' : '#d6dbe4', borderStyle: 'solid', borderRadius: '12px', padding: '10px', fontSize: '15px', marginBottom: '10px' }}
177
+ />
178
+ {required ? <text style={{ color: '#c2283a', fontSize: '13px', marginBottom: '8px' }}>{t('TesterPanelDescriptionRequired')}</text> : null}
179
+ <text style={{ fontSize: '13px', color: '#46526a', marginBottom: '10px' }}>{`${s.draft?.attachments.length ?? 0} ${w.evidence}`}</text>
180
+ {engine.offersDatabaseBackup ? (
181
+ <view bindtap={() => setBackup(!backup)} style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', marginBottom: '8px' }}>
182
+ <text style={{ fontSize: '15px' }}>{`${backup ? '☑' : '☐'} ${w.backup}`}</text>
183
+ </view>
184
+ ) : null}
185
+ {engine.asksAboutAi ? (
186
+ <view bindtap={() => setAi(!ai)} style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', marginBottom: '8px' }}>
187
+ <text style={{ fontSize: '15px' }}>{`${ai ? '☑' : '☐'} ${w.ai}`}</text>
188
+ </view>
189
+ ) : null}
190
+ <view style={{ display: 'flex', flexDirection: 'row', marginTop: '10px' }}>
191
+ <Button label={sending ? '…' : w.send} strong onTap={() => void send()} fg="#172033" accent={required ? '#9aa6b8' : '#0d8f84'} />
192
+ <Button label={w.cancel} onTap={props.onClose} fg="#46526a" accent="#0d8f84" />
193
+ </view>
194
+ </Sheet>
195
+ );
196
+ }
197
+
198
+ /** This device's reports (the gateway's feed) and what still waits in the outbox. */
199
+ export function TesterReports(props: { readonly engine: TesterEngine; readonly language: () => DiagLanguage }): ReactNode {
200
+ const w = wordsOf(props.language);
201
+ const [feed, setFeed] = useState<TesterFeed | null>(null);
202
+ const load = () => { void props.engine.feed().then(setFeed, () => setFeed({ status: 'failed', reports: [], queued: [] })); };
203
+ useEffect(load, [props.engine]);
204
+ const row = { paddingTop: '10px', paddingBottom: '10px', borderBottomWidth: '1px', borderBottomColor: '#e6eaf2', borderBottomStyle: 'solid' };
205
+ return (
206
+ <scroll-view scroll-orientation="vertical" style={{ flex: 1 }}>
207
+ <view style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', marginBottom: '8px' }}>
208
+ <text style={{ fontSize: '17px', fontWeight: 'bold' }}>{w.reports}</text>
209
+ <view bindtap={load}><text style={{ color: '#0d8f84', fontSize: '14px' }}>{w.refresh}</text></view>
210
+ </view>
211
+ {feed?.queued.map((title) => (
212
+ <view key={`q-${title}`} style={row}><text style={{ fontSize: '14px' }}>{`⏳ ${title}`}</text><text style={{ fontSize: '12px', color: '#7b8594' }}>{w.queued}</text></view>
213
+ ))}
214
+ {feed?.status === 'failed' ? <text style={{ color: '#c2283a' }}>{w.loadFailed}</text> : null}
215
+ {feed && feed.reports.length === 0 && feed.queued.length === 0 && feed.status !== 'failed' ? <text style={{ color: '#7b8594' }}>{w.none}</text> : null}
216
+ {feed?.reports.map((r) => (
217
+ <view key={r.reportId} style={row}>
218
+ <text style={{ fontSize: '14px', fontWeight: 'bold' }}>{`${r.code} · ${r.title}`}</text>
219
+ <text style={{ fontSize: '12px', color: '#7b8594' }}>{[r.state, r.outcome, r.jiraKey, r.appVersion].filter(Boolean).join(' · ')}</text>
220
+ </view>
221
+ ))}
222
+ </scroll-view>
223
+ );
224
+ }