create-heresay 0.1.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.
Files changed (38) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/bin/create-heresay.mjs +9 -0
  4. package/package.json +38 -0
  5. package/release/VERSION +1 -0
  6. package/release/firebase.json +111 -0
  7. package/release/firestore.indexes.json +11 -0
  8. package/release/firestore.rules +10 -0
  9. package/release/functions/lib/core/handler.js +366 -0
  10. package/release/functions/lib/core/store.js +66 -0
  11. package/release/functions/lib/core/types.js +23 -0
  12. package/release/functions/lib/core/version.js +3 -0
  13. package/release/functions/lib/index.js +7 -0
  14. package/release/functions/lib/providers/firebase/admin.js +13 -0
  15. package/release/functions/lib/providers/firebase/api.js +55 -0
  16. package/release/functions/lib/providers/firebase/firestore-store.js +106 -0
  17. package/release/functions/package-lock.json +2993 -0
  18. package/release/functions/package.json +23 -0
  19. package/release/public/app/index.html +57 -0
  20. package/release/public/app-icon.svg +1 -0
  21. package/release/public/apple-touch-icon.png +0 -0
  22. package/release/public/dashboard.css +152 -0
  23. package/release/public/dashboard.js +580 -0
  24. package/release/public/demo.html +30 -0
  25. package/release/public/docs.html +152 -0
  26. package/release/public/favicon.svg +1 -0
  27. package/release/public/icon-192.png +0 -0
  28. package/release/public/index.html +166 -0
  29. package/release/public/logo-white.svg +1 -0
  30. package/release/public/logo.svg +1 -0
  31. package/release/public/mark.svg +1 -0
  32. package/release/public/sdk.js +380 -0
  33. package/release/public/site.css +150 -0
  34. package/src/cli.mjs +382 -0
  35. package/src/firebase.mjs +61 -0
  36. package/src/google.mjs +106 -0
  37. package/src/state.mjs +43 -0
  38. package/src/steps.mjs +306 -0
@@ -0,0 +1,580 @@
1
+ /* The team's side of a Heresay: sign in, add apps, read reports, accept or decline them. */
2
+ (function () {
3
+ 'use strict';
4
+ var auth = firebase.auth();
5
+ var LOCAL = location.hostname === 'localhost' || location.hostname === '127.0.0.1';
6
+ if (LOCAL) auth.useEmulator('http://127.0.0.1:9199');
7
+
8
+ var $ = function (id) { return document.getElementById(id); };
9
+ var TYPE_LABEL = { broken: 'Broken', confusing: 'Confusing', improvement: 'Could be better', idea: 'Idea' };
10
+ var STATUS_LABEL = { open: 'Open', accepted: 'Accepted', fixed: 'Fixed', declined: 'Declined' };
11
+ var SDK_URL = location.origin + '/sdk/v1.js';
12
+ var me = null, projects = [], timer = null;
13
+
14
+ // ---- plumbing --------------------------------------------------------------------------
15
+
16
+ function api(method, path, body) {
17
+ return auth.currentUser.getIdToken().then(function (tok) {
18
+ return fetch('/v1' + path, {
19
+ method: method,
20
+ headers: { authorization: 'Bearer ' + tok, 'content-type': 'application/json' },
21
+ body: body ? JSON.stringify(body) : undefined,
22
+ });
23
+ }).then(function (res) {
24
+ return res.json().catch(function () { return {}; }).then(function (b) {
25
+ if (!res.ok) { var e = new Error(b.error || ('HTTP ' + res.status)); e.status = res.status; e.code = b.code; throw e; }
26
+ return b;
27
+ });
28
+ });
29
+ }
30
+
31
+ function el(tag, attrs, kids) {
32
+ var n = document.createElement(tag);
33
+ for (var k in attrs || {}) {
34
+ if (attrs[k] == null || attrs[k] === false) continue;
35
+ if (k === 'text') n.textContent = attrs[k];
36
+ else if (k === 'html') n.innerHTML = attrs[k];
37
+ else if (k.slice(0, 2) === 'on') n.addEventListener(k.slice(2), attrs[k]);
38
+ else n.setAttribute(k, attrs[k] === true ? '' : attrs[k]);
39
+ }
40
+ (kids || []).forEach(function (c) { if (c) n.appendChild(typeof c === 'string' ? document.createTextNode(c) : c); });
41
+ return n;
42
+ }
43
+
44
+ function show(which) {
45
+ ['gate', 'blocked', 'app'].forEach(function (id) { $(id).hidden = id !== which; });
46
+ }
47
+
48
+ function copyButton(getText, label) {
49
+ var b = el('button', { class: 'btn', type: 'button', text: label || 'Copy' });
50
+ b.onclick = function () {
51
+ navigator.clipboard.writeText(getText()).then(function () {
52
+ b.textContent = 'Copied';
53
+ setTimeout(function () { b.textContent = label || 'Copy'; }, 1600);
54
+ });
55
+ };
56
+ return b;
57
+ }
58
+
59
+ function codeBlock(text) {
60
+ return el('div', { class: 'code' }, [el('pre', { text: text }), copyButton(function () { return text; })]);
61
+ }
62
+
63
+ function stopTimer() { if (timer) { clearInterval(timer); timer = null; } }
64
+ function every(ms, fn) {
65
+ stopTimer();
66
+ timer = setInterval(function () { if (!document.hidden) fn(); }, ms);
67
+ }
68
+
69
+ // ---- sign-in ---------------------------------------------------------------------------
70
+
71
+ function gateMsg(text, bad) { $('gate-msg').textContent = text; $('gate-msg').className = 'gate-msg' + (bad ? ' bad' : ''); }
72
+
73
+ $('google').onclick = function () {
74
+ gateMsg('');
75
+ auth.signInWithPopup(new firebase.auth.GoogleAuthProvider()).catch(function (e) {
76
+ if (e.code === 'auth/popup-closed-by-user' || e.code === 'auth/cancelled-popup-request') return;
77
+ if (e.code === 'auth/operation-not-allowed') gateMsg('Google sign-in is off for this Heresay. Use an email link instead.', true);
78
+ else if (e.code === 'auth/popup-blocked') gateMsg('Your browser blocked the sign-in window. Allow pop-ups for this site and try again.', true);
79
+ else gateMsg(e.message, true);
80
+ });
81
+ };
82
+
83
+ var finishingLink = auth.isSignInWithEmailLink(location.href);
84
+ $('link-form').onsubmit = function (e) {
85
+ e.preventDefault();
86
+ var email = $('link-email').value.trim();
87
+ if (finishingLink) return finishLink(email);
88
+ gateMsg('Sending…');
89
+ auth.sendSignInLinkToEmail(email, { url: location.origin + '/app/', handleCodeInApp: true })
90
+ .then(function () {
91
+ localStorage.setItem('heresay.email', email);
92
+ gateMsg('Check your inbox at ' + email + '. The link signs you in here. It can take a minute, and may land in spam.');
93
+ })
94
+ .catch(function (x) {
95
+ gateMsg(x.code === 'auth/operation-not-allowed'
96
+ ? 'Email links are off for this Heresay. Use Google instead.' : x.message, true);
97
+ });
98
+ };
99
+
100
+ function finishLink(email) {
101
+ auth.signInWithEmailLink(email, location.href)
102
+ .then(function () { localStorage.removeItem('heresay.email'); history.replaceState(null, '', '/app/'); finishingLink = false; })
103
+ .catch(function (x) {
104
+ gateMsg(x.code === 'auth/invalid-action-code'
105
+ ? 'That link has expired or was already used. Ask for a new one.' : x.message, true);
106
+ history.replaceState(null, '', '/app/'); finishingLink = false;
107
+ $('link-form').querySelector('button').textContent = 'Email me a sign-in link';
108
+ });
109
+ }
110
+ if (finishingLink) {
111
+ var saved = localStorage.getItem('heresay.email');
112
+ if (saved) finishLink(saved);
113
+ else {
114
+ // Opened on another device: we need the address again before Firebase will accept the link.
115
+ $('link-form').querySelector('button').textContent = 'Finish signing in';
116
+ gateMsg('Type the email the link was sent to, to finish signing in.');
117
+ }
118
+ }
119
+
120
+ $('signout').onclick = $('blocked-out').onclick = function () { auth.signOut(); };
121
+
122
+ auth.onAuthStateChanged(function (user) {
123
+ stopTimer();
124
+ if (!user) { me = null; show('gate'); return; }
125
+ api('GET', '/me').then(function (b) {
126
+ me = b;
127
+ $('who').textContent = b.email;
128
+ show('app');
129
+ route();
130
+ }).catch(function (x) {
131
+ show('blocked');
132
+ if (x.code === 'not_member') {
133
+ $('blocked-title').textContent = 'You’re not on this team yet';
134
+ $('blocked-text').textContent = (user.email || 'This account') + ' isn’t a member of this Heresay. Ask an owner to add you under Team, then sign in again.';
135
+ } else if (x.code === 'no_instance') {
136
+ $('blocked-title').textContent = 'Setup isn’t finished';
137
+ $('blocked-text').textContent = 'This Heresay has no owner yet. Run npx create-heresay again on the computer that set it up; it picks up where it stopped.';
138
+ } else {
139
+ $('blocked-title').textContent = 'Can’t reach this Heresay';
140
+ $('blocked-text').textContent = x.message;
141
+ }
142
+ });
143
+ });
144
+
145
+ // ---- routes ----------------------------------------------------------------------------
146
+
147
+ window.addEventListener('hashchange', function () { if (me) route(); });
148
+
149
+ function route() {
150
+ stopTimer();
151
+ var parts = location.hash.replace(/^#\/?/, '').split('/').filter(Boolean);
152
+ document.querySelectorAll('[data-nav]').forEach(function (a) {
153
+ a.setAttribute('aria-current', String((parts[0] === 'team') === (a.dataset.nav === 'team')));
154
+ });
155
+ var view = $('view');
156
+ view.textContent = '';
157
+ if (parts[0] === 'team') return teamView(view);
158
+ if (parts[0] === 'new') return newAppView(view, parts[1]);
159
+ if (parts[0] === 'apps' && parts[1]) {
160
+ return withProject(parts[1], function (p) {
161
+ if (parts[2] === 'setup') setupView(view, p);
162
+ else reportsView(view, p);
163
+ });
164
+ }
165
+ appsView(view);
166
+ }
167
+
168
+ function withProject(id, fn) {
169
+ var hit = projects.filter(function (p) { return p.id === id; })[0];
170
+ if (hit) return fn(hit);
171
+ api('GET', '/projects/' + id).then(function (b) { fn(b.project); })
172
+ .catch(function () { location.hash = '#/'; });
173
+ }
174
+
175
+ // ---- apps ------------------------------------------------------------------------------
176
+
177
+ var PLATFORMS = [
178
+ { id: 'web', label: 'Web', note: 'Any site or web app', ready: true,
179
+ icon: '<circle cx="12" cy="12" r="9"/><path d="M3 12h18M12 3c2.5 2.6 3.8 5.6 3.8 9s-1.3 6.4-3.8 9c-2.5-2.6-3.8-5.6-3.8-9S9.5 5.6 12 3z"/>' },
180
+ { id: 'ios', label: 'iOS', note: 'iPhone and iPad',
181
+ icon: '<rect x="6.5" y="2.5" width="11" height="19" rx="2.5"/><path d="M10.5 18.5h3"/>' },
182
+ { id: 'macos', label: 'macOS', note: 'Mac apps',
183
+ icon: '<rect x="3" y="4" width="18" height="12" rx="2"/><path d="M1.5 19.5h21"/>' },
184
+ { id: 'android', label: 'Android', note: 'Phones and tablets',
185
+ icon: '<rect x="5" y="8" width="14" height="11" rx="2"/><path d="M8 8a4 4 0 0 1 8 0M8.5 4.5l1 1.5M15.5 4.5l-1 1.5"/>' },
186
+ { id: 'react-native', label: 'React Native', note: 'One app, both stores',
187
+ icon: '<ellipse cx="12" cy="12" rx="10" ry="4"/><ellipse cx="12" cy="12" rx="10" ry="4" transform="rotate(60 12 12)"/><ellipse cx="12" cy="12" rx="10" ry="4" transform="rotate(120 12 12)"/>' },
188
+ { id: 'flutter', label: 'Flutter', note: 'Dart, any platform',
189
+ icon: '<path d="M14 3 5 12l3 3L20 3zM14 12l-5 5 5 5h6l-5-5 5-5z"/>' },
190
+ ];
191
+ var platformOf = function (id) { return PLATFORMS.filter(function (x) { return x.id === id; })[0] || PLATFORMS[0]; };
192
+ var icon = function (p) { return el('span', { class: 'picon', html: '<svg viewBox="0 0 24 24" aria-hidden="true">' + p.icon + '</svg>' }); };
193
+
194
+ function appsView(view) {
195
+ api('GET', '/projects').then(function (b) {
196
+ projects = b.projects;
197
+ if (!projects.length) {
198
+ view.appendChild(el('section', { class: 'hello' }, [
199
+ el('h1', { text: 'Add your first app' }),
200
+ el('p', { class: 'mute', text: 'Heresay puts a Report button in your app. When someone taps it, the report lands here, and they hear back when you answer.' }),
201
+ el('a', { class: 'btn primary', href: '#/new', text: 'Add an app' }),
202
+ ]));
203
+ return;
204
+ }
205
+ view.appendChild(el('div', { class: 'head' }, [
206
+ el('h1', { text: 'Apps' }),
207
+ el('a', { class: 'btn primary', href: '#/new', text: 'Add an app' }),
208
+ ]));
209
+ var grid = el('div', { class: 'apps' });
210
+ projects.forEach(function (p) {
211
+ var count = el('span', { class: 'count', text: ' ' });
212
+ grid.appendChild(el('a', { class: 'app-card', href: '#/apps/' + p.id }, [
213
+ icon(platformOf(p.platform)),
214
+ el('div', {}, [el('b', { text: p.name }), el('span', { class: 'mute', text: platformOf(p.platform).label + (p.framework ? ' · ' + frameworkLabel(p.framework) : '') })]),
215
+ count,
216
+ ]));
217
+ api('GET', '/projects/' + p.id + '/reports').then(function (r) {
218
+ var open = r.reports.filter(function (x) { return x.status === 'open'; }).length;
219
+ count.textContent = r.reports.length ? (open ? open + ' open' : 'All answered') : 'No reports yet';
220
+ if (open) count.className = 'count on';
221
+ });
222
+ });
223
+ view.appendChild(grid);
224
+ });
225
+ }
226
+
227
+ // ---- add an app ------------------------------------------------------------------------
228
+
229
+ // 'unknown' is sent as no framework: many apps are built by an agent and their owner may not know.
230
+ var FRAMEWORKS = [
231
+ ['unknown', 'Not sure'], ['html', 'Plain HTML'], ['react', 'React'], ['next', 'Next.js'], ['vue', 'Vue'], ['nuxt', 'Nuxt'],
232
+ ['svelte', 'SvelteKit'], ['angular', 'Angular'], ['other', 'Something else'],
233
+ ];
234
+ function frameworkLabel(id) { var f = FRAMEWORKS.filter(function (x) { return x[0] === id; })[0]; return f ? f[1] : id; }
235
+
236
+ function stepper(active) {
237
+ var steps = ['Platform', 'Details', 'Install', 'Test'];
238
+ return el('ol', { class: 'steps' }, steps.map(function (s, i) {
239
+ return el('li', { class: i + 1 < active ? 'done' : i + 1 === active ? 'now' : '' }, [el('span', { text: String(i + 1) }), s]);
240
+ }));
241
+ }
242
+
243
+ function newAppView(view, platform) {
244
+ if (!platform) {
245
+ view.appendChild(stepper(1));
246
+ view.appendChild(el('h1', { text: 'What are you adding Heresay to?' }));
247
+ var grid = el('div', { class: 'platforms' });
248
+ PLATFORMS.forEach(function (p) {
249
+ grid.appendChild(el(p.ready ? 'a' : 'div', {
250
+ class: 'platform' + (p.ready ? '' : ' soon'), href: p.ready ? '#/new/' + p.id : null,
251
+ 'aria-disabled': p.ready ? null : 'true',
252
+ }, [icon(p), el('b', { text: p.label }), el('span', { class: 'mute', text: p.note }), p.ready ? null : el('span', { class: 'tag', text: 'Soon' })]));
253
+ });
254
+ view.appendChild(grid);
255
+ view.appendChild(el('p', { class: 'mute small', text: 'Other platforms can already send reports through the API. See the docs, under Other platforms.' }));
256
+ return;
257
+ }
258
+
259
+ view.appendChild(stepper(2));
260
+ view.appendChild(el('h1', { text: 'About your web app' }));
261
+ var err = el('p', { class: 'err', role: 'alert' });
262
+ var fw = 'unknown';
263
+ var chips = el('div', { class: 'chips', role: 'radiogroup', 'aria-label': 'Framework' });
264
+ FRAMEWORKS.forEach(function (f) {
265
+ chips.appendChild(el('button', {
266
+ type: 'button', role: 'radio', 'aria-checked': String(f[0] === fw), text: f[1],
267
+ onclick: function (e) {
268
+ fw = f[0];
269
+ chips.querySelectorAll('button').forEach(function (b) { b.setAttribute('aria-checked', String(b === e.target)); });
270
+ },
271
+ }));
272
+ });
273
+ var form = el('form', { class: 'form' }, [
274
+ el('label', {}, ['Name', el('input', { name: 'name', required: true, maxlength: '80', placeholder: 'Acme web', autocomplete: 'off' })]),
275
+ el('div', { class: 'field' }, [el('span', { class: 'lbl' }, ['Built with ', el('span', { class: 'opt', text: 'optional' })]), chips,
276
+ el('p', { class: 'hint', text: 'Only changes the install steps we show. Not sure? Leave it: your coding agent can work it out, and the general steps work everywhere.' })]),
277
+ el('label', {}, ['Where it runs', el('textarea', { name: 'origins', rows: '3', placeholder: 'https://app.example.com\nhttp://localhost:3000' })]),
278
+ el('p', { class: 'hint', text: 'One address per line: your live site and anywhere you test it. Only these sites can send reports with this app’s key. Leave it empty to allow any site while you try things out.' }),
279
+ el('div', { class: 'row' }, [el('a', { class: 'btn ghost', href: '#/new', text: 'Back' }), el('button', { class: 'btn primary', text: 'Create app' })]),
280
+ err,
281
+ ]);
282
+ form.onsubmit = function (e) {
283
+ e.preventDefault();
284
+ err.textContent = '';
285
+ var origins = form.origins.value.split(/[\s,]+/).map(function (s) { return s.trim(); }).filter(Boolean);
286
+ api('POST', '/projects', { name: form.name.value, platform: platform, framework: fw === 'unknown' ? null : fw, allowed_origins: origins })
287
+ .then(function (b) { projects.push(b.project); location.hash = '#/apps/' + b.project.id + '/setup'; })
288
+ .catch(function (x) { err.textContent = x.message; });
289
+ };
290
+ view.appendChild(form);
291
+ form.name.focus();
292
+ }
293
+
294
+ // ---- install and test ------------------------------------------------------------------
295
+
296
+ function tag(p) { return '<script src="' + SDK_URL + '" data-key="' + p.key + '" defer></scr' + 'ipt>'; }
297
+
298
+ /** Where the tag goes for one framework: [where, code]. */
299
+ function placement(fw, p) {
300
+ if (fw === 'next') {
301
+ return ['In <code>app/layout.tsx</code>, inside <code>&lt;body&gt;</code> after <code>{children}</code>:',
302
+ "import Script from 'next/script';\n\n<Script src=\"" + SDK_URL + "\" data-key=\"" + p.key + "\" strategy=\"afterInteractive\" />"];
303
+ }
304
+ if (fw === 'nuxt') {
305
+ return ['In <code>nuxt.config.ts</code>:',
306
+ "export default defineNuxtConfig({\n app: { head: { script: [{ src: '" + SDK_URL + "', 'data-key': '" + p.key + "', defer: true }] } },\n});"];
307
+ }
308
+ return [{
309
+ html: 'Paste this before <code>&lt;/body&gt;</code> on every page:',
310
+ react: 'Paste this before <code>&lt;/body&gt;</code> in <code>index.html</code> (Vite) or <code>public/index.html</code> (Create React App):',
311
+ vue: 'Paste this before <code>&lt;/body&gt;</code> in <code>index.html</code>:',
312
+ svelte: 'Paste this before <code>&lt;/body&gt;</code> in <code>src/app.html</code>:',
313
+ angular: 'Paste this before <code>&lt;/body&gt;</code> in <code>src/index.html</code>:',
314
+ }[fw] || 'Load this once on every page, as late as you can, for example before <code>&lt;/body&gt;</code>:', tag(p)];
315
+ }
316
+
317
+ function byHand(p) {
318
+ var fw = p.framework;
319
+ if (fw && fw !== 'other') {
320
+ var one = placement(fw, p);
321
+ return [el('p', { html: one[0] }), codeBlock(one[1])];
322
+ }
323
+ // No framework given: the one tag that works anywhere, and where it goes for the common ones.
324
+ var where = el('details', { class: 'where' }, [el('summary', { text: 'Where does this go in my project?' })]);
325
+ FRAMEWORKS.forEach(function (f) {
326
+ if (f[0] === 'unknown' || f[0] === 'other') return;
327
+ var x = placement(f[0], p);
328
+ var same = x[1] === tag(p); // same tag as above: say where, don't repeat it
329
+ where.appendChild(el('div', { class: 'where-item' }, [el('b', { text: f[1] }),
330
+ el('p', { html: same ? x[0].replace(/^Paste this/, 'Paste the tag').replace(/:$/, '.') : x[0] }),
331
+ same ? null : codeBlock(x[1])]));
332
+ });
333
+ return [
334
+ el('p', { html: 'Load this once on every page, as late as you can, for example just before <code>&lt;/body&gt;</code>:' }),
335
+ codeBlock(tag(p)),
336
+ where,
337
+ ];
338
+ }
339
+
340
+ function agentPromptFor(p) {
341
+ return [
342
+ 'Add Heresay, an in-app feedback button, to this app.',
343
+ '',
344
+ '1. Load this script once on every page, as late as possible: before </body>, or the framework\'s equivalent (for Next.js, next/script with strategy="afterInteractive" in the root layout).',
345
+ '',
346
+ ' ' + tag(p),
347
+ '',
348
+ '2. If the app has signed-in users, call window.Heresay?.identify({ id: user.id, label: user.name }) after sign-in, and window.Heresay?.identify() after sign-out.',
349
+ '3. If the app has a version number (for example package.json "version"), add data-version="<version>" to the tag.',
350
+ '4. If screens change without the URL changing, call window.Heresay?.setScreen("<screen name>") on each change.',
351
+ '',
352
+ 'Change nothing else. Reference: ' + location.origin + '/docs.html',
353
+ ].join('\n');
354
+ }
355
+
356
+ function setupView(view, p) {
357
+ view.appendChild(stepper(3));
358
+ view.appendChild(el('div', { class: 'head' }, [
359
+ el('h1', { text: 'Put Heresay in ' + p.name }),
360
+ el('a', { class: 'btn ghost', href: '#/apps/' + p.id, text: 'Go to reports' }),
361
+ ]));
362
+
363
+ // Without a known framework, the agent is the better default: it can see the code.
364
+ var agentFirst = !p.framework || p.framework === 'other';
365
+ var panes = { hand: el('div', { hidden: agentFirst }, byHand(p)), agent: el('div', { hidden: !agentFirst }, [
366
+ el('p', { text: 'Paste this into Claude Code, Cursor or any coding agent, in your app’s folder:' }),
367
+ codeBlock(agentPromptFor(p)),
368
+ ]) };
369
+ var tabs = el('div', { class: 'seg', role: 'tablist' });
370
+ (agentFirst ? [['agent', 'Ask your coding agent'], ['hand', 'Add it yourself']] : [['hand', 'Add it yourself'], ['agent', 'Ask your coding agent']]).forEach(function (t, i) {
371
+ tabs.appendChild(el('button', {
372
+ type: 'button', role: 'tab', 'aria-selected': String(i === 0), text: t[1],
373
+ onclick: function (e) {
374
+ tabs.querySelectorAll('button').forEach(function (b) { b.setAttribute('aria-selected', String(b === e.target)); });
375
+ panes.hand.hidden = t[0] !== 'hand'; panes.agent.hidden = t[0] !== 'agent';
376
+ },
377
+ }));
378
+ });
379
+ view.appendChild(el('section', { class: 'card' }, [
380
+ tabs, panes.hand, panes.agent,
381
+ el('p', { class: 'hint', html: 'Your key is meant to be public: it only lets the sites you listed send reports. Optional extras (who is signed in, your app version, your brand colour) are in the <a href="/docs.html#options" target="_blank">docs</a>.' }),
382
+ el('div', { class: 'soon-note' }, [
383
+ el('b', { text: 'Coming next: ' }),
384
+ el('code', { text: 'npx heresay connect' }),
385
+ ' links your repo to this app, so your coding agent can pick up the reports you accept and mark them fixed.',
386
+ ]),
387
+ ]));
388
+
389
+ var status = el('div', { class: 'wait', role: 'status' });
390
+ var tryIt = el('a', { class: 'btn', href: '/demo.html?key=' + encodeURIComponent(p.key), target: '_blank', rel: 'noopener', text: 'Send a test report' });
391
+ view.appendChild(el('section', { class: 'card test' }, [
392
+ el('div', { class: 'test-head' }, [el('span', { class: 'stepnum', text: '4' }), el('h2', { text: 'Send your first report' })]),
393
+ el('p', { class: 'mute', text: 'Open your app, tap Report in the corner and send anything. Or use our test page. It shows up here within a few seconds.' }),
394
+ status,
395
+ el('div', { class: 'row' }, [tryIt]),
396
+ ]));
397
+
398
+ var seen = null;
399
+ function check() {
400
+ api('GET', '/projects/' + p.id + '/reports').then(function (b) {
401
+ if (seen === null) seen = b.reports.length;
402
+ var fresh = b.reports.length > seen;
403
+ if (!b.reports.length) {
404
+ status.className = 'wait';
405
+ status.textContent = 'Waiting for your first report…';
406
+ return;
407
+ }
408
+ var first = b.reports.slice().sort(function (a, c) { return a.created_at < c.created_at ? 1 : -1; })[0];
409
+ status.className = 'wait ok';
410
+ status.textContent = '';
411
+ status.appendChild(el('b', { text: fresh || seen === 0 ? 'It works. ' : 'Reports are coming in. ' }));
412
+ status.appendChild(document.createTextNode('Latest: “' + first.text.slice(0, 120) + '”'));
413
+ status.appendChild(el('a', { class: 'btn primary', href: '#/apps/' + p.id, text: 'See reports' }));
414
+ stopTimer();
415
+ }).catch(function () {});
416
+ }
417
+ check();
418
+ every(3000, check);
419
+ }
420
+
421
+ // ---- reports ---------------------------------------------------------------------------
422
+
423
+ var filter = 'open';
424
+
425
+ function reportsView(view, p) {
426
+ var reports = [];
427
+ var filters = el('nav', { class: 'filters', 'aria-label': 'Filter' });
428
+ var list = el('div', { class: 'reports' });
429
+ var empty = el('p', { class: 'empty' });
430
+ view.appendChild(el('div', { class: 'head' }, [
431
+ el('div', { class: 'title' }, [icon(platformOf(p.platform)), el('h1', { text: p.name })]),
432
+ el('a', { class: 'btn', href: '#/apps/' + p.id + '/setup', text: 'Install' }),
433
+ ]));
434
+ view.appendChild(filters);
435
+ view.appendChild(list);
436
+ view.appendChild(empty);
437
+
438
+ function load() {
439
+ return api('GET', '/projects/' + p.id + '/reports').then(function (b) { reports = b.reports; render(); });
440
+ }
441
+
442
+ function render() {
443
+ filters.textContent = '';
444
+ ['open', 'accepted', 'fixed', 'declined', 'all'].forEach(function (f) {
445
+ var n = f === 'all' ? reports.length : reports.filter(function (r) { return r.status === f; }).length;
446
+ filters.appendChild(el('button', {
447
+ 'aria-pressed': String(filter === f),
448
+ onclick: function () { filter = f; render(); },
449
+ }, [(f === 'all' ? 'All' : STATUS_LABEL[f]) + ' ', el('span', { class: 'n', text: String(n) })]));
450
+ });
451
+ list.textContent = '';
452
+ var shown = reports.filter(function (r) { return filter === 'all' || r.status === filter; });
453
+ empty.hidden = shown.length > 0;
454
+ empty.textContent = '';
455
+ if (!reports.length) {
456
+ empty.appendChild(document.createTextNode('No reports yet. '));
457
+ empty.appendChild(el('a', { href: '#/apps/' + p.id + '/setup', text: 'Check the install' }));
458
+ } else empty.textContent = filter === 'open' ? 'Nothing waiting. Every report has had its hearing.' : 'Nothing here.';
459
+ shown.forEach(function (r) { list.appendChild(row(p, r, load)); });
460
+ }
461
+
462
+ load();
463
+ // Reports arrive while you read. Don't refresh under someone typing a decline reason.
464
+ every(30000, function () { if (!list.querySelector('textarea')) load(); });
465
+ }
466
+
467
+ function ctxLine(c) {
468
+ return [c.route, c.app_version && ('v' + c.app_version), c.platform, c.os, c.browser, c.user_label || c.user_id]
469
+ .filter(Boolean).join(' · ');
470
+ }
471
+
472
+ function row(p, r, reload) {
473
+ var err = el('span', { class: 'err' });
474
+ var actions = el('div', { class: 'actions' });
475
+ var act = function (action, body) {
476
+ err.textContent = '';
477
+ return api('POST', '/projects/' + p.id + '/reports/' + r.id + '/' + action, body)
478
+ .then(reload)
479
+ .catch(function (x) { err.textContent = x.message; if (x.status === 409) reload(); });
480
+ };
481
+
482
+ function buttons() {
483
+ actions.textContent = '';
484
+ if (r.status === 'open') {
485
+ actions.appendChild(el('button', { class: 'btn primary', text: 'Accept', onclick: function () { act('accept'); } }));
486
+ actions.appendChild(el('button', { class: 'btn', text: 'Decline…', onclick: declineForm }));
487
+ }
488
+ if (r.status === 'accepted') {
489
+ actions.appendChild(el('button', {
490
+ class: 'btn primary', text: 'Copy prompt for agent',
491
+ onclick: function (e) {
492
+ var b = e.target;
493
+ api('GET', '/projects/' + p.id + '/reports/' + r.id + '/prompt')
494
+ .then(function (x) { return navigator.clipboard.writeText(x.prompt); })
495
+ .then(function () { b.textContent = 'Copied'; })
496
+ .catch(function (x) { err.textContent = x.message; });
497
+ },
498
+ }));
499
+ actions.appendChild(el('button', { class: 'btn', text: 'Mark fixed', onclick: function () { act('fixed'); } }));
500
+ }
501
+ actions.appendChild(err);
502
+ }
503
+
504
+ function declineForm() {
505
+ actions.textContent = '';
506
+ var reason = el('textarea', { placeholder: 'Why? The person who sent this will read it.', 'aria-label': 'Reason for declining' });
507
+ actions.appendChild(reason);
508
+ actions.appendChild(el('button', { class: 'btn', text: 'Decline', onclick: function () {
509
+ if (!reason.value.trim()) { err.textContent = 'Give a reason. They will see it.'; reason.focus(); return; }
510
+ act('decline', { reason: reason.value });
511
+ } }));
512
+ actions.appendChild(el('button', { class: 'btn ghost', text: 'Cancel', onclick: buttons }));
513
+ actions.appendChild(err);
514
+ reason.focus();
515
+ }
516
+ buttons();
517
+
518
+ return el('article', { class: 'report' }, [
519
+ el('div', { class: 'meta' }, [
520
+ el('span', { class: 'pill ' + r.type, text: TYPE_LABEL[r.type] }),
521
+ el('time', { datetime: r.created_at, text: new Date(r.created_at).toLocaleString() }),
522
+ el('span', { class: 'status s-' + r.status, text: STATUS_LABEL[r.status] }),
523
+ ]),
524
+ el('p', { class: 'text', text: r.text }),
525
+ el('div', { class: 'ctx', text: ctxLine(r.context) }),
526
+ r.decline_reason ? el('div', { class: 'reason', text: 'Declined: ' + r.decline_reason }) : null,
527
+ r.status === 'fixed' || r.status === 'declined' ? null : actions,
528
+ ]);
529
+ }
530
+
531
+ // ---- team ------------------------------------------------------------------------------
532
+
533
+ function teamView(view) {
534
+ var owner = me.role === 'owner';
535
+ var list = el('ul', { class: 'members' });
536
+ var err = el('p', { class: 'err', role: 'alert' });
537
+ view.appendChild(el('div', { class: 'head' }, [el('h1', { text: 'Team' })]));
538
+ view.appendChild(el('p', { class: 'mute', text: 'Everyone here can read and answer reports for every app. Owners can also change the team.' }));
539
+
540
+ function draw(members) {
541
+ list.textContent = '';
542
+ members.forEach(function (m) {
543
+ list.appendChild(el('li', {}, [
544
+ el('span', { class: 'avatar', text: m.email.charAt(0).toUpperCase() }),
545
+ el('div', {}, [el('b', { text: m.email + (m.email === me.email ? ' (you)' : '') }), el('span', { class: 'mute', text: m.role === 'owner' ? 'Owner' : 'Member' })]),
546
+ owner && m.email !== me.email ? el('button', {
547
+ class: 'btn ghost', text: 'Remove',
548
+ onclick: function () {
549
+ err.textContent = '';
550
+ api('DELETE', '/team/' + encodeURIComponent(m.email)).then(function (b) { draw(b.members); })
551
+ .catch(function (x) { err.textContent = x.message; });
552
+ },
553
+ }) : null,
554
+ ]));
555
+ });
556
+ }
557
+
558
+ view.appendChild(el('section', { class: 'card' }, [list, err]));
559
+ if (owner) {
560
+ var form = el('form', { class: 'invite' }, [
561
+ el('label', { class: 'sr', for: 'invite-email', text: 'Email' }),
562
+ el('input', { id: 'invite-email', name: 'email', type: 'email', required: true, placeholder: 'teammate@company.com' }),
563
+ el('select', { name: 'role', 'aria-label': 'Role' }, [el('option', { value: 'member', text: 'Member' }), el('option', { value: 'owner', text: 'Owner' })]),
564
+ el('button', { class: 'btn primary', text: 'Add' }),
565
+ ]);
566
+ form.onsubmit = function (e) {
567
+ e.preventDefault();
568
+ err.textContent = '';
569
+ api('POST', '/team', { email: form.email.value, role: form.role.value }).then(function (b) {
570
+ draw(b.members);
571
+ form.reset();
572
+ err.className = 'ok-msg';
573
+ err.textContent = 'Added. Send them this address; they sign in with that email: ' + location.origin + '/app/';
574
+ }).catch(function (x) { err.className = 'err'; err.textContent = x.message; });
575
+ };
576
+ view.appendChild(el('section', { class: 'card' }, [el('h2', { text: 'Add someone' }), form]));
577
+ }
578
+ api('GET', '/team').then(function (b) { draw(b.members); });
579
+ }
580
+ })();
@@ -0,0 +1,30 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head><meta charset="utf-8"><title>Heresay test page</title>
4
+ <meta name="viewport" content="width=device-width, initial-scale=1">
5
+ <link rel="icon" href="/favicon.svg" type="image/svg+xml">
6
+ <style>
7
+ body { margin:0; font:16px/1.6 system-ui, -apple-system, "Segoe UI", sans-serif; color:#15171c; background:#f5f6f7; }
8
+ main { max-width:560px; margin:14vh auto; padding:0 24px; }
9
+ h1 { font-size:26px; margin:0 0 8px; }
10
+ p { color:#5b6472; }
11
+ .arrow { margin-top:32px; font-weight:600; color:#0b5e57; }
12
+ </style></head>
13
+ <body>
14
+ <main>
15
+ <h1>Stand-in app</h1>
16
+ <p>This page plays the part of your app, so you can try Heresay before it's in your code. Reports sent from here go to the app whose key is in the address.</p>
17
+ <p class="arrow">Tap <b>Report</b> in the bottom-right corner and send anything. ↘</p>
18
+ </main>
19
+ <script>
20
+ // Only this page reads the key from the URL. A real app puts it in the tag.
21
+ var q = new URLSearchParams(location.search);
22
+ var s = document.createElement('script');
23
+ s.src = '/sdk/v1.js';
24
+ s.dataset.key = q.get('key') || '';
25
+ s.dataset.version = '0.0.1-demo';
26
+ if (q.get('accent')) s.dataset.accent = q.get('accent');
27
+ document.body.appendChild(s);
28
+ </script>
29
+ </body>
30
+ </html>