@ciridae/voice-conversations-react 0.0.0-stage → 0.1.0-nightly.0.bootstrap

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 (100) hide show
  1. package/README.md +30 -2
  2. package/components/BrowserTalkButton.tsx +96 -0
  3. package/components/CallBanner.tsx +43 -0
  4. package/components/CallCaptions.tsx +38 -0
  5. package/components/CallEntry.tsx +89 -0
  6. package/components/CallTranscript.tsx +58 -0
  7. package/components/RecentCallEntries.tsx +211 -0
  8. package/components/describeBrowserTalkError.ts +39 -0
  9. package/components/formatCallClock.ts +9 -0
  10. package/components/useEndPresentCall.ts +45 -0
  11. package/components/voiceCall.css +225 -0
  12. package/dist/MalformedVoiceResponseError.d.ts +6 -0
  13. package/dist/MalformedVoiceResponseError.js +9 -0
  14. package/dist/NumberToCallQrCode.d.ts +12 -0
  15. package/dist/NumberToCallQrCode.js +29 -0
  16. package/dist/PhoneNumberDialog.d.ts +25 -0
  17. package/dist/PhoneNumberDialog.js +333 -0
  18. package/dist/PhoneNumbersAdminPanel.d.ts +11 -0
  19. package/dist/PhoneNumbersAdminPanel.js +85 -0
  20. package/dist/VoiceApiError.d.ts +18 -0
  21. package/dist/VoiceApiError.js +19 -0
  22. package/dist/VoiceRequestTimedOutError.d.ts +6 -0
  23. package/dist/VoiceRequestTimedOutError.js +9 -0
  24. package/dist/averageSpeechBinsIntoBands.d.ts +6 -0
  25. package/dist/averageSpeechBinsIntoBands.js +20 -0
  26. package/dist/browserApisForBrowserTalk.d.ts +17 -0
  27. package/dist/browserApisForBrowserTalk.js +42 -0
  28. package/dist/browserTalkError.d.ts +36 -0
  29. package/dist/browserTalkError.js +1 -0
  30. package/dist/chooseCallToShowCaptionsOf.d.ts +22 -0
  31. package/dist/chooseCallToShowCaptionsOf.js +26 -0
  32. package/dist/createBrowserTalk.d.ts +56 -0
  33. package/dist/createBrowserTalk.js +414 -0
  34. package/dist/createBrowserTalkErrorFollower.d.ts +17 -0
  35. package/dist/createBrowserTalkErrorFollower.js +76 -0
  36. package/dist/createCallPresenceWatcher.d.ts +25 -0
  37. package/dist/createCallPresenceWatcher.js +66 -0
  38. package/dist/createLiveCallCaptionsPoller.d.ts +24 -0
  39. package/dist/createLiveCallCaptionsPoller.js +58 -0
  40. package/dist/createVisibleTabPoller.d.ts +13 -0
  41. package/dist/createVisibleTabPoller.js +56 -0
  42. package/dist/deriveCallPresence.d.ts +23 -0
  43. package/dist/deriveCallPresence.js +58 -0
  44. package/dist/describePhoneNumberError.d.ts +7 -0
  45. package/dist/describePhoneNumberError.js +29 -0
  46. package/dist/entries/index.d.ts +26 -0
  47. package/dist/entries/index.js +22 -0
  48. package/dist/followBrowserTalkErrorAgainstPresence.d.ts +23 -0
  49. package/dist/followBrowserTalkErrorAgainstPresence.js +52 -0
  50. package/dist/formatPhoneNumberAsTyped.d.ts +31 -0
  51. package/dist/formatPhoneNumberAsTyped.js +85 -0
  52. package/dist/generated/index.d.ts +1 -0
  53. package/dist/generated/index.js +2 -0
  54. package/dist/generated/types.gen.d.ts +795 -0
  55. package/dist/generated/types.gen.js +2 -0
  56. package/dist/getBrowserTalkForThisTab.d.ts +3 -0
  57. package/dist/getBrowserTalkForThisTab.js +13 -0
  58. package/dist/getCallPresenceWatcherForThisTab.d.ts +3 -0
  59. package/dist/getCallPresenceWatcherForThisTab.js +9 -0
  60. package/dist/mapStartBrowserTalkFailureToBrowserTalkError.d.ts +7 -0
  61. package/dist/mapStartBrowserTalkFailureToBrowserTalkError.js +60 -0
  62. package/dist/parseVoiceApiResponses.d.ts +11 -0
  63. package/dist/parseVoiceApiResponses.js +344 -0
  64. package/dist/readCallSummaryUntilItHasDuration.d.ts +12 -0
  65. package/dist/readCallSummaryUntilItHasDuration.js +45 -0
  66. package/dist/readTheBrowsersTimeZone.d.ts +8 -0
  67. package/dist/readTheBrowsersTimeZone.js +17 -0
  68. package/dist/readTimestampMilliseconds.d.ts +8 -0
  69. package/dist/readTimestampMilliseconds.js +10 -0
  70. package/dist/requestEndOfCall.d.ts +7 -0
  71. package/dist/requestEndOfCall.js +11 -0
  72. package/dist/startBrowserTalkErrorFollowerForThisTab.d.ts +6 -0
  73. package/dist/startBrowserTalkErrorFollowerForThisTab.js +12 -0
  74. package/dist/useAdminPhoneNumbers.d.ts +47 -0
  75. package/dist/useAdminPhoneNumbers.js +76 -0
  76. package/dist/useBrowserTalk.d.ts +10 -0
  77. package/dist/useBrowserTalk.js +23 -0
  78. package/dist/useBrowserTalkErrorWhileItApplies.d.ts +10 -0
  79. package/dist/useBrowserTalkErrorWhileItApplies.js +13 -0
  80. package/dist/useCallPresence.d.ts +5 -0
  81. package/dist/useCallPresence.js +34 -0
  82. package/dist/useCallSummaries.d.ts +9 -0
  83. package/dist/useCallSummaries.js +22 -0
  84. package/dist/useCallSummary.d.ts +15 -0
  85. package/dist/useCallSummary.js +21 -0
  86. package/dist/useFinePointerWithHover.d.ts +10 -0
  87. package/dist/useFinePointerWithHover.js +19 -0
  88. package/dist/useLiveCallCaptions.d.ts +8 -0
  89. package/dist/useLiveCallCaptions.js +27 -0
  90. package/dist/useLiveCallCaptionsOfLatestCall.d.ts +13 -0
  91. package/dist/useLiveCallCaptionsOfLatestCall.js +23 -0
  92. package/dist/usePhoneNumber.d.ts +45 -0
  93. package/dist/usePhoneNumber.js +125 -0
  94. package/dist/voiceApiModels.d.ts +85 -0
  95. package/dist/voiceApiModels.js +1 -0
  96. package/dist/voiceConversationsApi.d.ts +44 -0
  97. package/dist/voiceConversationsApi.js +227 -0
  98. package/dist/voiceConversationsSettings.d.ts +20 -0
  99. package/dist/voiceConversationsSettings.js +26 -0
  100. package/package.json +41 -4
@@ -0,0 +1,225 @@
1
+ /*
2
+ * Copy-paste styles for the voice call components. Map the variables to your kit's tokens; the
3
+ * fallbacks are the design's own values.
4
+ */
5
+ .voice-call {
6
+ --voice-call-surface: var(--color-surface, #ffffff);
7
+ --voice-call-surface-muted: var(--color-surface-muted, #f2f2f3);
8
+ --voice-call-text: var(--color-text, #1d1d1f);
9
+ --voice-call-text-muted: var(--color-text-muted, #6e6e73);
10
+ --voice-call-border: var(--color-border, #dcdce0);
11
+ --voice-call-primary: var(--color-primary, #1d1d1f);
12
+ --voice-call-on-primary: var(--color-on-primary, #ffffff);
13
+ --voice-call-live: var(--color-success, #2f8f6b);
14
+ --voice-call-danger: var(--color-danger, #e0533d);
15
+ --voice-call-on-danger: var(--color-on-danger, #ffffff);
16
+ --voice-call-member-bubble: var(--color-bubble-own, #dbe8f7);
17
+ --voice-call-radius: var(--radius-large, 14px);
18
+ color: var(--voice-call-text);
19
+ font: inherit;
20
+ }
21
+ .voice-call-hint,
22
+ .voice-call-error {
23
+ margin: 0;
24
+ font-size: 13px;
25
+ color: var(--voice-call-text-muted);
26
+ }
27
+ .voice-call-error {
28
+ color: var(--voice-call-danger);
29
+ }
30
+ .voice-call-button {
31
+ display: inline-flex;
32
+ align-items: center;
33
+ justify-content: center;
34
+ gap: 6px;
35
+ min-height: 36px;
36
+ padding: 0 14px;
37
+ border: 1px solid var(--voice-call-border);
38
+ border-radius: 999px;
39
+ background: var(--voice-call-surface);
40
+ color: var(--voice-call-text);
41
+ cursor: pointer;
42
+ }
43
+ .voice-call-button[data-variant="end"] {
44
+ border-color: var(--voice-call-danger);
45
+ background: var(--voice-call-danger);
46
+ color: var(--voice-call-on-danger);
47
+ }
48
+
49
+ .voice-call-status {
50
+ display: inline-flex;
51
+ align-items: center;
52
+ gap: 6px;
53
+ font-size: 12px;
54
+ color: var(--voice-call-live);
55
+ font-variant-numeric: tabular-nums;
56
+ }
57
+ .voice-call-dot {
58
+ width: 7px;
59
+ height: 7px;
60
+ border-radius: 50%;
61
+ background: var(--voice-call-live);
62
+ }
63
+
64
+ .voice-call-turns {
65
+ display: flex;
66
+ flex-direction: column;
67
+ align-self: stretch;
68
+ gap: var(--voice-call-turn-gap, 8px);
69
+ }
70
+ .voice-call-turn {
71
+ display: flex;
72
+ flex-direction: column;
73
+ max-width: 80%;
74
+ }
75
+ .voice-call-turn[data-speaker="caller"] {
76
+ align-self: flex-end;
77
+ align-items: flex-end;
78
+ }
79
+ .voice-call-turn[data-speaker="voice"] {
80
+ align-self: flex-start;
81
+ align-items: flex-start;
82
+ }
83
+ .voice-call-turn-text {
84
+ margin: 0;
85
+ padding: 8px 12px;
86
+ border-radius: var(--voice-call-radius);
87
+ background: var(--voice-call-surface-muted);
88
+ }
89
+ .voice-call-turn[data-speaker="caller"] .voice-call-turn-text {
90
+ background: var(--voice-call-member-bubble);
91
+ }
92
+
93
+ .voice-call-banner {
94
+ display: flex;
95
+ align-items: center;
96
+ justify-content: space-between;
97
+ gap: 12px;
98
+ padding: 6px 8px 6px 14px;
99
+ border: 1px solid var(--voice-call-border);
100
+ border-radius: 999px;
101
+ background: var(--voice-call-surface-muted);
102
+ font-size: 13px;
103
+ }
104
+ .voice-call-banner .voice-call-status {
105
+ font-size: 13px;
106
+ font-weight: 500;
107
+ }
108
+ .voice-call-transcript {
109
+ display: flex;
110
+ flex-direction: column;
111
+ gap: 16px;
112
+ }
113
+ .voice-call-transcript-header h2 {
114
+ margin: 0 0 4px;
115
+ font-size: 15px;
116
+ font-weight: 600;
117
+ }
118
+ .voice-call-turn-speaker {
119
+ margin-bottom: 2px;
120
+ font-size: 11px;
121
+ color: var(--voice-call-text-muted);
122
+ }
123
+ .voice-call-button[data-size="small"] {
124
+ min-height: 28px;
125
+ padding: 0 12px;
126
+ font-size: 12px;
127
+ }
128
+
129
+ /* Talk, in the composer: ghost while idle, solid primary from the press until the browser talk is over. */
130
+ .voice-call-browser-talk-button {
131
+ display: inline-flex;
132
+ flex-shrink: 0;
133
+ align-items: center;
134
+ justify-content: center;
135
+ width: 32px;
136
+ height: 32px;
137
+ padding: 0;
138
+ border: 1px solid transparent;
139
+ border-radius: 50%;
140
+ background: transparent;
141
+ color: var(--voice-call-text-muted);
142
+ cursor: pointer;
143
+ }
144
+ .voice-call-browser-talk-button[data-variant="primary"] {
145
+ border-color: var(--voice-call-primary);
146
+ background: var(--voice-call-primary);
147
+ color: var(--voice-call-on-primary);
148
+ }
149
+ .voice-call-browser-talk-button[aria-disabled="true"] {
150
+ cursor: default;
151
+ opacity: 0.7;
152
+ }
153
+ .voice-call-browser-talk-button:focus-visible,
154
+ .voice-call-entry-toggle:focus-visible {
155
+ outline: 2px solid var(--voice-call-primary);
156
+ outline-offset: 2px;
157
+ }
158
+
159
+ /* The latest ended call at the end of the chat's messages: "Call · 3:12", opening in place. */
160
+ .voice-call-entries {
161
+ display: flex;
162
+ flex-direction: column;
163
+ gap: 8px;
164
+ }
165
+ .voice-call-entry {
166
+ display: flex;
167
+ flex-direction: column;
168
+ gap: 10px;
169
+ }
170
+ .voice-call-entry-toggle {
171
+ display: inline-flex;
172
+ align-self: flex-start;
173
+ align-items: center;
174
+ gap: 6px;
175
+ min-height: 28px;
176
+ padding: 0 12px 0 10px;
177
+ border: 1px solid var(--voice-call-border);
178
+ border-radius: 999px;
179
+ background: var(--voice-call-surface-muted);
180
+ color: var(--voice-call-text-muted);
181
+ font: inherit;
182
+ font-size: 12px;
183
+ font-variant-numeric: tabular-nums;
184
+ cursor: pointer;
185
+ }
186
+ .voice-call-entry-chevron {
187
+ width: 6px;
188
+ height: 6px;
189
+ border-right: 1.5px solid currentColor;
190
+ border-bottom: 1.5px solid currentColor;
191
+ transform: rotate(-45deg);
192
+ transition: transform 120ms ease;
193
+ }
194
+ .voice-call-entry-toggle[aria-expanded="true"] .voice-call-entry-chevron {
195
+ transform: rotate(45deg);
196
+ }
197
+ .voice-call-entry-transcript {
198
+ display: flex;
199
+ flex-direction: column;
200
+ gap: 8px;
201
+ /* Bounded with its own scroll, so an open transcript never pushes the composer off-screen. */
202
+ max-height: min(40vh, 320px);
203
+ overflow-y: auto;
204
+ overscroll-behavior: contain;
205
+ padding-left: 12px;
206
+ border-left: 2px solid var(--voice-call-border);
207
+ }
208
+ .voice-call-entry-transcript[hidden] {
209
+ display: none;
210
+ }
211
+ /* Where the next call of a session took over, such as a phone call taking over a browser talk. */
212
+ .voice-call-entry-handover {
213
+ display: flex;
214
+ align-items: center;
215
+ gap: 8px;
216
+ margin: 4px 0;
217
+ color: var(--voice-call-text-muted);
218
+ font-size: 11px;
219
+ }
220
+ .voice-call-entry-handover::after {
221
+ flex: 1;
222
+ height: 1px;
223
+ background: var(--voice-call-border);
224
+ content: "";
225
+ }
@@ -0,0 +1,6 @@
1
+ /** A 2xx body that does not match the contract; handled as a failed call, never as success. */
2
+ export declare class MalformedVoiceResponseError extends Error {
3
+ readonly route: string;
4
+ readonly fieldPath: string;
5
+ constructor(route: string, fieldPath: string);
6
+ }
@@ -0,0 +1,9 @@
1
+ /** A 2xx body that does not match the contract; handled as a failed call, never as success. */
2
+ export class MalformedVoiceResponseError extends Error {
3
+ constructor(route, fieldPath) {
4
+ super(`Voice conversations ${route} returned a body whose ${fieldPath} does not match the contract`);
5
+ this.route = route;
6
+ this.fieldPath = fieldPath;
7
+ this.name = "MalformedVoiceResponseError";
8
+ }
9
+ }
@@ -0,0 +1,12 @@
1
+ export interface NumberToCallQrCodeProps {
2
+ /** The number to call in E.164 form; the code encodes `tel:` and this number. */
3
+ phoneNumberToCall: string;
4
+ /** The same number as shown on screen, for the accessible name. */
5
+ phoneNumberToCallDisplay: string;
6
+ }
7
+ /**
8
+ * A QR code that a phone's camera opens as a call to the assistant's number, drawn as an inline
9
+ * SVG: dark modules on white whatever the app's theme, since scanners need the contrast. It is an
10
+ * image to assistive technology; the `tel:` link beside it stays the control to use.
11
+ */
12
+ export declare function NumberToCallQrCode({ phoneNumberToCall, phoneNumberToCallDisplay, }: NumberToCallQrCodeProps): import("react").JSX.Element;
@@ -0,0 +1,29 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useMemo } from "react";
3
+ import { encode } from "uqr";
4
+ /** Modules of light margin around the code: the four the QR specification asks scanners to have. */
5
+ const QUIET_ZONE_MODULES = 4;
6
+ const RENDERED_SIZE_PIXELS = 128;
7
+ /**
8
+ * A QR code that a phone's camera opens as a call to the assistant's number, drawn as an inline
9
+ * SVG: dark modules on white whatever the app's theme, since scanners need the contrast. It is an
10
+ * image to assistive technology; the `tel:` link beside it stays the control to use.
11
+ */
12
+ export function NumberToCallQrCode({ phoneNumberToCall, phoneNumberToCallDisplay, }) {
13
+ const { size, darkModulesPath } = useMemo(() => {
14
+ const code = encode(`tel:${phoneNumberToCall}`, { ecc: "M", border: QUIET_ZONE_MODULES });
15
+ const squares = [];
16
+ code.data.forEach((row, y) => {
17
+ row.forEach((dark, x) => {
18
+ if (dark)
19
+ squares.push(`M${x} ${y}h1v1h-1z`);
20
+ });
21
+ });
22
+ return { size: code.size, darkModulesPath: squares.join("") };
23
+ }, [phoneNumberToCall]);
24
+ return (_jsxs("svg", { role: "img", "aria-label": `QR code to call ${phoneNumberToCallDisplay}`, viewBox: `0 0 ${size} ${size}`, width: RENDERED_SIZE_PIXELS, height: RENDERED_SIZE_PIXELS, shapeRendering: "crispEdges", style: qrCodeStyle, children: [_jsx("rect", { width: size, height: size, fill: "#ffffff" }), _jsx("path", { d: darkModulesPath, fill: "#000000" })] }));
25
+ }
26
+ const qrCodeStyle = {
27
+ flexShrink: 0,
28
+ borderRadius: "var(--radius-medium, 8px)",
29
+ };
@@ -0,0 +1,25 @@
1
+ export interface PhoneNumberDialogProps {
2
+ open: boolean;
3
+ onClose: () => void;
4
+ /** Called once a number is saved, with it in E.164 form; the dialog then shows the number to call. */
5
+ onSaved?: (phoneNumber: string) => void;
6
+ /** Added to the dialog panel, for the app's own styles. */
7
+ className?: string;
8
+ }
9
+ /**
10
+ * The member's "Phone number" dialog, through the package's `{prefix}/me/phone-number` routes.
11
+ * With a number saved, it leads with the number to call ("Call your assistant at …", when the app
12
+ * set `phone_number_to_call`), then the member's own number, with Change and Remove. On a laptop or
13
+ * desktop (a pointer that can hover) a QR code for the number to call sits beside it, for the
14
+ * member's phone camera; on a phone or tablet the number is a tap-to-call link alone. Without one,
15
+ * or on Change, it asks for the number: the backend reads one typed without `+` as a US number,
16
+ * so the field formats one as it is typed, `(415) 555-0123`, and leaves one starting with `+` as typed.
17
+ * Keep it, and its `open` state, outside the account menu: a menu that unmounts its items on
18
+ * selection would take the dialog with it. While open it keeps Tab and Shift+Tab inside itself,
19
+ * moves focus to the number field or to Done whenever it switches between asking for a number and
20
+ * showing the saved one, and on close returns focus to whatever had it before it opened.
21
+ * Colors read the same tokens as the copy-paste components
22
+ * (`--color-surface`, `--color-text`, `--color-primary`, ...), with neutral fallbacks, so it
23
+ * takes the app's theme when those are set.
24
+ */
25
+ export declare function PhoneNumberDialog({ open, onClose, onSaved, className }: PhoneNumberDialogProps): import("react").JSX.Element | null;
@@ -0,0 +1,333 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Fragment, useEffect, useId, useLayoutEffect, useRef, useState, } from "react";
3
+ import { formatPhoneNumberAsTyped } from "./formatPhoneNumberAsTyped.js";
4
+ import { NumberToCallQrCode } from "./NumberToCallQrCode.js";
5
+ import { usePhoneNumber } from "./usePhoneNumber.js";
6
+ import { useFinePointerWithHover } from "./useFinePointerWithHover.js";
7
+ /**
8
+ * The member's "Phone number" dialog, through the package's `{prefix}/me/phone-number` routes.
9
+ * With a number saved, it leads with the number to call ("Call your assistant at …", when the app
10
+ * set `phone_number_to_call`), then the member's own number, with Change and Remove. On a laptop or
11
+ * desktop (a pointer that can hover) a QR code for the number to call sits beside it, for the
12
+ * member's phone camera; on a phone or tablet the number is a tap-to-call link alone. Without one,
13
+ * or on Change, it asks for the number: the backend reads one typed without `+` as a US number,
14
+ * so the field formats one as it is typed, `(415) 555-0123`, and leaves one starting with `+` as typed.
15
+ * Keep it, and its `open` state, outside the account menu: a menu that unmounts its items on
16
+ * selection would take the dialog with it. While open it keeps Tab and Shift+Tab inside itself,
17
+ * moves focus to the number field or to Done whenever it switches between asking for a number and
18
+ * showing the saved one, and on close returns focus to whatever had it before it opened.
19
+ * Colors read the same tokens as the copy-paste components
20
+ * (`--color-surface`, `--color-text`, `--color-primary`, ...), with neutral fallbacks, so it
21
+ * takes the app's theme when those are set.
22
+ */
23
+ export function PhoneNumberDialog({ open, onClose, onSaved, className }) {
24
+ if (!open)
25
+ return null;
26
+ return _jsx(OpenPhoneNumberDialog, { onClose: onClose, onSaved: onSaved, className: className });
27
+ }
28
+ function OpenPhoneNumberDialog({ onClose, onSaved, className, }) {
29
+ const { phoneNumber, phoneNumberDisplay, phoneNumberToCall, phoneNumberToCallDisplay, loadStatus, change, error, save, remove, } = usePhoneNumber();
30
+ const pointer = useFinePointerWithHover();
31
+ const [typed, setTyped] = useState("");
32
+ // Typing before the first read answers, or pressing Change, keeps the number field up even
33
+ // once a saved number is known; a save or removal that succeeds ends it.
34
+ const [editing, setEditing] = useState(false);
35
+ // The number the backend last refused, while it is still what the field holds: only then is the
36
+ // field invalid. A failed read or a setup problem is not the member's typing.
37
+ const [refusedTyped, setRefusedTyped] = useState(null);
38
+ // The control pressed for the change under way. A browser moves focus to the body when the
39
+ // focused control is disabled (jsdom does not), so after a failed change focus goes back to it.
40
+ const pressed = useRef(null);
41
+ // Which view the last failed change belongs to, so its sentence never shows on the other view;
42
+ // `any_view` for a failed read, which concerns both; `no_view` once the member moved on with
43
+ // Change or Cancel, since the hook keeps its sentence until the next change succeeds.
44
+ const [errorBelongsTo, setErrorBelongsTo] = useState("any_view");
45
+ const titleId = useId();
46
+ const hintId = useId();
47
+ const input = useRef(null);
48
+ // Where the caret goes once the formatted number is in the field: React puts it at the end
49
+ // whenever it replaces a controlled input's value.
50
+ const caretAfterFormatting = useRef(null);
51
+ const done = useRef(null);
52
+ const saveButton = useRef(null);
53
+ const pressStartedOnBackdrop = useRef(false);
54
+ const pressEndedOnBackdrop = useRef(false);
55
+ const pressSeen = useRef(false);
56
+ const panel = useRef(null);
57
+ useEffect(() => {
58
+ // Whatever had focus before the dialog opened gets it back when the dialog closes, when it
59
+ // is still on the page (a menu item that unmounted with its menu is not). This effect runs
60
+ // before the one below moves focus into the dialog.
61
+ const focusedBeforeOpening = document.activeElement instanceof HTMLElement ? document.activeElement : null;
62
+ return () => {
63
+ if (focusedBeforeOpening?.isConnected)
64
+ focusedBeforeOpening.focus();
65
+ };
66
+ }, []);
67
+ const view = phoneNumber && !editing ? "saved_number" : "number_field";
68
+ const busy = change !== "none";
69
+ const focusedView = useRef(null);
70
+ useEffect(() => {
71
+ // Switching views removes the control that had focus (Remove, Change, Save), so focus would
72
+ // fall behind the overlay: it goes to the number field, or to Done. The field is enabled
73
+ // again only once a change is over. A change that fails leaves the view as it was, and focus
74
+ // stays, or returns to the control that was pressed.
75
+ if (busy)
76
+ return;
77
+ const active = document.activeElement;
78
+ const focusIsInside = panel.current !== null && active !== panel.current && panel.current.contains(active);
79
+ if (focusedView.current === view && focusIsInside) {
80
+ pressed.current = null;
81
+ return;
82
+ }
83
+ focusedView.current = view;
84
+ const pressedControl = pressed.current?.isConnected && !pressed.current.matches(":disabled") ? pressed.current : null;
85
+ pressed.current = null;
86
+ const target = pressedControl ?? (view === "number_field" ? input.current : done.current);
87
+ target?.focus();
88
+ }, [view, busy]);
89
+ useEffect(() => {
90
+ const keepTabInsideTheDialog = (event) => {
91
+ if (event.key !== "Tab" || panel.current === null)
92
+ return;
93
+ const focusable = findFocusableElements(panel.current);
94
+ const first = focusable[0];
95
+ const last = focusable[focusable.length - 1];
96
+ if (first === undefined || last === undefined) {
97
+ event.preventDefault();
98
+ return;
99
+ }
100
+ // The dialog moves focus itself on every Tab, through a list read fresh each time (controls
101
+ // turn on and off), rather than leaving any step to the browser: Safari leaves buttons out of
102
+ // its tab order, so a native step from the input would leave the dialog. Focus on anything
103
+ // not in the list (outside the dialog, or the panel itself) goes to the first or last control.
104
+ event.preventDefault();
105
+ const active = document.activeElement;
106
+ const activeIndex = active instanceof HTMLElement ? focusable.indexOf(active) : -1;
107
+ if (activeIndex === -1) {
108
+ if (event.shiftKey)
109
+ last.focus();
110
+ else
111
+ first.focus();
112
+ return;
113
+ }
114
+ const step = event.shiftKey ? -1 : 1;
115
+ const next = focusable[(activeIndex + step + focusable.length) % focusable.length];
116
+ next?.focus();
117
+ };
118
+ document.addEventListener("keydown", keepTabInsideTheDialog);
119
+ return () => document.removeEventListener("keydown", keepTabInsideTheDialog);
120
+ }, []);
121
+ useEffect(() => {
122
+ const closeOnEscape = (event) => {
123
+ if (event.key === "Escape")
124
+ onClose();
125
+ };
126
+ document.addEventListener("keydown", closeOnEscape);
127
+ return () => document.removeEventListener("keydown", closeOnEscape);
128
+ }, [onClose]);
129
+ useLayoutEffect(() => {
130
+ const caret = caretAfterFormatting.current;
131
+ caretAfterFormatting.current = null;
132
+ if (caret === null || input.current === null || document.activeElement !== input.current)
133
+ return;
134
+ input.current.setSelectionRange(caret, caret);
135
+ }, [typed]);
136
+ function changeTyped(event) {
137
+ const field = event.target;
138
+ const formatted = formatPhoneNumberAsTyped({
139
+ previousValue: typed,
140
+ value: field.value,
141
+ caret: field.selectionStart ?? field.value.length,
142
+ kind: readEditKind(event.nativeEvent),
143
+ });
144
+ // Formatting can give back the text already held (a formatting character removed and put
145
+ // back), so the state does not change and no render moves the caret: put both in now.
146
+ if (formatted.value === typed) {
147
+ field.value = formatted.value;
148
+ field.setSelectionRange(formatted.caret, formatted.caret);
149
+ }
150
+ else {
151
+ caretAfterFormatting.current = formatted.caret;
152
+ }
153
+ setTyped(formatted.value);
154
+ setRefusedTyped(null);
155
+ setEditing(true);
156
+ }
157
+ async function submit(event) {
158
+ event.preventDefault();
159
+ if (view !== "number_field" || typed.trim() === "")
160
+ return;
161
+ pressed.current = saveButton.current;
162
+ const saved = await save(typed);
163
+ if (saved.outcome !== "saved") {
164
+ // Only a refusal of the number itself makes the field wrong; a failure decided nothing.
165
+ if (saved.outcome === "number_refused")
166
+ setRefusedTyped(typed);
167
+ setErrorBelongsTo("number_field");
168
+ return;
169
+ }
170
+ setTyped("");
171
+ setEditing(false);
172
+ setErrorBelongsTo("any_view");
173
+ onSaved?.(saved.phoneNumber);
174
+ }
175
+ async function removeSavedNumber(removeButton) {
176
+ pressed.current = removeButton;
177
+ if (!(await remove())) {
178
+ setErrorBelongsTo("saved_number");
179
+ return;
180
+ }
181
+ setTyped("");
182
+ setRefusedTyped(null);
183
+ setEditing(false);
184
+ setErrorBelongsTo("any_view");
185
+ }
186
+ function startChanging() {
187
+ setRefusedTyped(null);
188
+ setErrorBelongsTo("no_view");
189
+ setEditing(true);
190
+ }
191
+ function stopChanging() {
192
+ setTyped("");
193
+ setRefusedTyped(null);
194
+ setErrorBelongsTo("no_view");
195
+ setEditing(false);
196
+ }
197
+ const callAt = phoneNumberToCall && phoneNumberToCallDisplay
198
+ ? { phoneNumberToCall, phoneNumberToCallDisplay }
199
+ : null;
200
+ const shownError = errorBelongsTo === "any_view" || errorBelongsTo === view ? error : null;
201
+ return (_jsx("div", { style: backdropStyle, onMouseDown: (event) => {
202
+ pressSeen.current = true;
203
+ pressStartedOnBackdrop.current = event.target === event.currentTarget;
204
+ }, onMouseUp: (event) => {
205
+ pressEndedOnBackdrop.current = event.target === event.currentTarget;
206
+ }, onClick: (event) => {
207
+ // Only a press that starts and ends on the backdrop closes. A drag between the panel and
208
+ // the backdrop, either way, keeps the typed number: the browser can send its click to the
209
+ // backdrop as their common ancestor, so the release target is checked on mouseup. A click
210
+ // with no press before it (assistive technology, a synthetic click) closes when it lands
211
+ // on the backdrop.
212
+ const onTheBackdrop = event.target === event.currentTarget;
213
+ const closes = pressSeen.current
214
+ ? pressStartedOnBackdrop.current && pressEndedOnBackdrop.current && onTheBackdrop
215
+ : onTheBackdrop;
216
+ pressSeen.current = false;
217
+ pressStartedOnBackdrop.current = false;
218
+ pressEndedOnBackdrop.current = false;
219
+ if (closes)
220
+ onClose();
221
+ }, children: _jsxs("form", { ref: panel, role: "dialog", "aria-modal": "true", "aria-labelledby": titleId, className: className, style: panelStyle, onClick: (event) => {
222
+ // The backdrop never sees this click, so its press tracking ends here: a later click
223
+ // with no press before it (assistive technology) is judged on its own.
224
+ event.stopPropagation();
225
+ pressSeen.current = false;
226
+ pressStartedOnBackdrop.current = false;
227
+ pressEndedOnBackdrop.current = false;
228
+ }, onSubmit: (event) => void submit(event), children: [_jsx("h2", { id: titleId, style: titleStyle, children: "Your phone number" }), view === "saved_number" ? (_jsxs(Fragment, { children: [callAt && pointer === "fine_pointer_with_hover" ? (_jsxs("div", { style: callAtWithQrCodeStyle, children: [_jsxs("div", { style: callAtTextStyle, children: [_jsx(CallYourAssistantAt, { ...callAt }), _jsx("p", { style: mutedTextStyle, children: "Scan with your phone's camera to call, or dial the number." })] }), _jsx(NumberToCallQrCode, { ...callAt })] })) : callAt ? (_jsx(CallYourAssistantAt, { ...callAt })) : null, _jsxs("p", { style: callAt ? mutedTextStyle : bodyTextStyle, children: ["Calls from your number, ", phoneNumberDisplay, ", reach your assistant.", callAt ? null : " Ask your admin for the number to call."] }), shownError ? (_jsx("p", { role: "alert", style: errorTextStyle, children: shownError })) : null, _jsxs("div", { style: actionsStyle, children: [_jsx("button", { type: "button", disabled: busy, onClick: (event) => void removeSavedNumber(event.currentTarget), style: buttonStyle(secondaryButtonStyle, busy), children: change === "removing" ? "Removing…" : "Remove" }), _jsx("span", { style: { flex: 1 } }), _jsx("button", { type: "button", disabled: busy, onClick: startChanging, style: buttonStyle(secondaryButtonStyle, busy), children: "Change" }), _jsx("button", { ref: done, type: "button", onClick: onClose, style: buttonStyle(primaryButtonStyle, false), children: "Done" })] })] }, "saved_number")) : (_jsxs(Fragment, { children: [_jsx("p", { style: mutedTextStyle, children: callAt
229
+ ? `Save the phone you will call from, then call ${callAt.phoneNumberToCallDisplay} to reach your assistant.`
230
+ : "Save the phone you will call from to reach your assistant." }), _jsxs("label", { style: labelStyle, children: ["Phone number", _jsx("input", { ref: input, type: "tel", autoComplete: "tel", inputMode: "tel", placeholder: "(415) 555-0123", value: typed, disabled: busy, "aria-invalid": refusedTyped !== null, "aria-describedby": hintId, onChange: changeTyped, style: inputStyle })] }), _jsx("p", { id: hintId, role: shownError ? "alert" : undefined, style: shownError ? errorTextStyle : mutedTextStyle, children: shownError ?? (loadStatus === "loading" ? "Loading your number…" : FORMAT_EXAMPLE) }), _jsxs("div", { style: actionsStyle, children: [_jsx("span", { style: { flex: 1 } }), _jsx("button", { type: "button", onClick: phoneNumber ? stopChanging : onClose, style: buttonStyle(secondaryButtonStyle, false), children: "Cancel" }), _jsx("button", { ref: saveButton, type: "submit", disabled: busy || typed.trim() === "", style: buttonStyle(primaryButtonStyle, busy || typed.trim() === ""), children: change === "saving" ? "Saving…" : "Save" })] })] }, "number_field"))] }) }));
231
+ }
232
+ /** The number to call, large, as a `tel:` link: the control that places the call on any device. */
233
+ function CallYourAssistantAt({ phoneNumberToCall, phoneNumberToCallDisplay, }) {
234
+ return (_jsxs("p", { style: leadTextStyle, children: ["Call your assistant at", " ", _jsx("a", { href: `tel:${phoneNumberToCall}`, style: phoneNumberToCallStyle, children: phoneNumberToCallDisplay })] }));
235
+ }
236
+ /** Backspace and Delete, from the browser's `inputType`; a paste, typing or anything else otherwise. */
237
+ function readEditKind(event) {
238
+ if (!(event instanceof InputEvent))
239
+ return "other_edit";
240
+ if (event.inputType === "deleteContentBackward")
241
+ return "delete_backward";
242
+ if (event.inputType === "deleteContentForward")
243
+ return "delete_forward";
244
+ return "other_edit";
245
+ }
246
+ const FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
247
+ /** The controls Tab moves between inside the dialog, in page order. */
248
+ function findFocusableElements(container) {
249
+ return Array.from(container.querySelectorAll(FOCUSABLE_SELECTOR));
250
+ }
251
+ const FORMAT_EXAMPLE = "A US number needs no country code. For another country, start with + and the country code.";
252
+ const backdropStyle = {
253
+ position: "fixed",
254
+ inset: 0,
255
+ display: "grid",
256
+ placeItems: "center",
257
+ padding: 16,
258
+ background: "rgb(0 0 0 / 32%)",
259
+ zIndex: 1000,
260
+ };
261
+ const panelStyle = {
262
+ display: "grid",
263
+ gap: 12,
264
+ width: "min(400px, 100%)",
265
+ padding: 20,
266
+ border: "1px solid var(--color-border, #dcdce0)",
267
+ borderRadius: "var(--radius-large, 14px)",
268
+ background: "var(--color-surface, #ffffff)",
269
+ color: "var(--color-text, #1d1d1f)",
270
+ boxShadow: "0 8px 24px rgb(0 0 0 / 16%)",
271
+ font: "inherit",
272
+ };
273
+ const titleStyle = { margin: 0, fontSize: 17, fontWeight: 600 };
274
+ const bodyTextStyle = { margin: 0, fontSize: 14 };
275
+ const leadTextStyle = { margin: 0, fontSize: 15 };
276
+ const phoneNumberToCallStyle = {
277
+ display: "block",
278
+ marginTop: 4,
279
+ fontSize: 22,
280
+ fontWeight: 600,
281
+ color: "inherit",
282
+ textDecoration: "none",
283
+ };
284
+ const callAtWithQrCodeStyle = {
285
+ display: "flex",
286
+ gap: 16,
287
+ alignItems: "center",
288
+ justifyContent: "space-between",
289
+ };
290
+ const callAtTextStyle = { display: "grid", gap: 8 };
291
+ const mutedTextStyle = {
292
+ margin: 0,
293
+ fontSize: 13,
294
+ color: "var(--color-text-muted, #6e6e73)",
295
+ };
296
+ const errorTextStyle = {
297
+ ...mutedTextStyle,
298
+ color: "var(--color-danger, #c0392b)",
299
+ };
300
+ const labelStyle = { display: "grid", gap: 6, fontSize: 13, fontWeight: 500 };
301
+ const inputStyle = {
302
+ padding: "8px 10px",
303
+ border: "1px solid var(--color-border, #dcdce0)",
304
+ borderRadius: "var(--radius-medium, 8px)",
305
+ background: "var(--color-surface, #ffffff)",
306
+ color: "inherit",
307
+ font: "inherit",
308
+ fontSize: 15,
309
+ };
310
+ const actionsStyle = { display: "flex", gap: 8, alignItems: "center" };
311
+ const buttonBaseStyle = {
312
+ padding: "8px 14px",
313
+ borderRadius: "var(--radius-medium, 8px)",
314
+ font: "inherit",
315
+ fontSize: 14,
316
+ cursor: "pointer",
317
+ };
318
+ const secondaryButtonStyle = {
319
+ ...buttonBaseStyle,
320
+ border: "1px solid var(--color-border, #dcdce0)",
321
+ background: "transparent",
322
+ color: "inherit",
323
+ };
324
+ const primaryButtonStyle = {
325
+ ...buttonBaseStyle,
326
+ border: "1px solid transparent",
327
+ background: "var(--color-primary, #1d1d1f)",
328
+ color: "var(--color-on-primary, #ffffff)",
329
+ };
330
+ /** A disabled button looks disabled, so a press that does nothing never looks like a broken one. */
331
+ function buttonStyle(base, disabled) {
332
+ return disabled ? { ...base, opacity: 0.5, cursor: "not-allowed" } : base;
333
+ }
@@ -0,0 +1,11 @@
1
+ export interface PhoneNumbersAdminPanelProps {
2
+ /** Added to the panel, for the app's own styles. */
3
+ className?: string;
4
+ }
5
+ /**
6
+ * The admin's "Phone numbers" panel: add one person's number by email, or upload a CSV with
7
+ * `email` and `phone_number` columns and see what happened to each row. People must be in the
8
+ * project; a pending invitee's number counts at once. Put it on a page only admins reach: the
9
+ * backend refuses anyone its `require_admin` dependency refuses.
10
+ */
11
+ export declare function PhoneNumbersAdminPanel({ className }: PhoneNumbersAdminPanelProps): import("react").JSX.Element;