@joinskips/common 0.0.0-stage → 0.1.0-develop.144
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/README.md +104 -2
- package/dist/chat/SkipsChat.d.ts +37 -0
- package/dist/chat/SkipsChat.js +103 -0
- package/dist/chat/client.d.ts +53 -0
- package/dist/chat/client.js +178 -0
- package/dist/chat/connection.d.ts +4 -0
- package/dist/chat/connection.js +6 -0
- package/dist/chat/generated/skips-app/index.d.ts +1 -0
- package/dist/chat/generated/skips-app/index.js +2 -0
- package/dist/chat/generated/skips-app/types.gen.d.ts +733 -0
- package/dist/chat/generated/skips-app/types.gen.js +2 -0
- package/dist/chat/index.d.ts +3 -0
- package/dist/chat/index.js +2 -0
- package/dist/chat/platform.d.ts +4 -0
- package/dist/chat/platform.js +46 -0
- package/dist/chat/session.d.ts +19 -0
- package/dist/chat/session.js +95 -0
- package/dist/chat/useSkipsChat.d.ts +79 -0
- package/dist/chat/useSkipsChat.js +507 -0
- package/dist/chat/verify.d.ts +35 -0
- package/dist/chat/verify.js +84 -0
- package/dist/elements/skips-elements.js +18521 -0
- package/dist/helpers/attachments.d.ts +12 -0
- package/dist/helpers/attachments.js +34 -0
- package/dist/helpers/cable.d.ts +34 -0
- package/dist/helpers/cable.js +88 -0
- package/dist/helpers/cableFrames.d.ts +40 -0
- package/dist/helpers/cableFrames.js +70 -0
- package/dist/helpers/cardNote.d.ts +79 -0
- package/dist/helpers/cardNote.js +152 -0
- package/dist/helpers/commandNote.d.ts +4 -0
- package/dist/helpers/commandNote.js +23 -0
- package/dist/helpers/index.d.ts +19 -0
- package/dist/helpers/index.js +19 -0
- package/dist/helpers/markdown.d.ts +47 -0
- package/dist/helpers/markdown.js +129 -0
- package/dist/helpers/medicationOrder.d.ts +333 -0
- package/dist/helpers/medicationOrder.js +383 -0
- package/dist/helpers/message.d.ts +46 -0
- package/dist/helpers/message.js +12 -0
- package/dist/helpers/noteView.d.ts +35 -0
- package/dist/helpers/noteView.js +65 -0
- package/dist/helpers/orderForm.d.ts +126 -0
- package/dist/helpers/orderForm.js +271 -0
- package/dist/helpers/ordersMenu.d.ts +56 -0
- package/dist/helpers/ordersMenu.js +129 -0
- package/dist/helpers/regimenChoice.d.ts +42 -0
- package/dist/helpers/regimenChoice.js +73 -0
- package/dist/helpers/scrollBox.d.ts +5 -0
- package/dist/helpers/scrollBox.js +15 -0
- package/dist/helpers/skipTyping.d.ts +26 -0
- package/dist/helpers/skipTyping.js +29 -0
- package/dist/helpers/skipsMessage.d.ts +55 -0
- package/dist/helpers/skipsMessage.js +130 -0
- package/dist/helpers/stageRefill.d.ts +33 -0
- package/dist/helpers/stageRefill.js +41 -0
- package/dist/helpers/thread.d.ts +54 -0
- package/dist/helpers/thread.js +122 -0
- package/dist/helpers/typing.d.ts +13 -0
- package/dist/helpers/typing.js +68 -0
- package/dist/helpers/usrxNote.d.ts +22 -0
- package/dist/helpers/usrxNote.js +26 -0
- package/dist/helpers/visibleArea.d.ts +15 -0
- package/dist/helpers/visibleArea.js +11 -0
- package/dist/helpers/welcome.d.ts +25 -0
- package/dist/helpers/welcome.js +51 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/index.js +8 -0
- package/dist/hooks/useCardNoteActions.d.ts +82 -0
- package/dist/hooks/useCardNoteActions.js +210 -0
- package/dist/hooks/useCodeEntry.d.ts +6 -0
- package/dist/hooks/useCodeEntry.js +14 -0
- package/dist/hooks/useComposer.d.ts +20 -0
- package/dist/hooks/useComposer.js +40 -0
- package/dist/hooks/useOrderForm.d.ts +80 -0
- package/dist/hooks/useOrderForm.js +403 -0
- package/dist/hooks/useOrdersMenu.d.ts +114 -0
- package/dist/hooks/useOrdersMenu.js +337 -0
- package/dist/hooks/useRegimenChoice.d.ts +23 -0
- package/dist/hooks/useRegimenChoice.js +50 -0
- package/dist/hooks/useResendCountdown.d.ts +2 -0
- package/dist/hooks/useResendCountdown.js +19 -0
- package/dist/hooks/useStepField.d.ts +8 -0
- package/dist/hooks/useStepField.js +17 -0
- package/dist/hooks/useTypingCue.d.ts +11 -0
- package/dist/hooks/useTypingCue.js +41 -0
- package/dist/hooks/useWelcomeTyping.d.ts +2 -0
- package/dist/hooks/useWelcomeTyping.js +25 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/staff/CardNoteActions.d.ts +10 -0
- package/dist/staff/CardNoteActions.js +16 -0
- package/dist/staff/OrderFormNote.d.ts +8 -0
- package/dist/staff/OrderFormNote.js +8 -0
- package/dist/staff/OrdersMenuNote.d.ts +8 -0
- package/dist/staff/OrdersMenuNote.js +8 -0
- package/dist/staff/RegimenChoiceNote.d.ts +7 -0
- package/dist/staff/RegimenChoiceNote.js +8 -0
- package/dist/staff/features.d.ts +9 -0
- package/dist/staff/features.js +8 -0
- package/dist/staff/index.d.ts +11 -0
- package/dist/staff/index.js +7 -0
- package/dist/ui/Bubble.d.ts +5 -0
- package/dist/ui/Bubble.js +9 -0
- package/dist/ui/CardActions.d.ts +34 -0
- package/dist/ui/CardActions.js +28 -0
- package/dist/ui/Conversation.d.ts +69 -0
- package/dist/ui/Conversation.js +192 -0
- package/dist/ui/Markdown.d.ts +11 -0
- package/dist/ui/Markdown.js +51 -0
- package/dist/ui/NoteFold.d.ts +19 -0
- package/dist/ui/NoteFold.js +12 -0
- package/dist/ui/NoteViewControl.d.ts +19 -0
- package/dist/ui/NoteViewControl.js +20 -0
- package/dist/ui/OrderForm.d.ts +71 -0
- package/dist/ui/OrderForm.js +118 -0
- package/dist/ui/OrdersMenu.d.ts +71 -0
- package/dist/ui/OrdersMenu.js +128 -0
- package/dist/ui/RegimenChoice.d.ts +23 -0
- package/dist/ui/RegimenChoice.js +16 -0
- package/dist/ui/SkipAvatar.d.ts +7 -0
- package/dist/ui/SkipAvatar.js +8 -0
- package/dist/ui/StaffButton.d.ts +18 -0
- package/dist/ui/StaffButton.js +17 -0
- package/dist/ui/Welcome.d.ts +91 -0
- package/dist/ui/Welcome.js +128 -0
- package/dist/ui/index.d.ts +8 -0
- package/dist/ui/index.js +7 -0
- package/dist/ui/parts.d.ts +38 -0
- package/dist/ui/parts.js +6 -0
- package/package.json +115 -4
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { deleteRefusal, reachesPharmacy } from "../helpers/cardNote.js";
|
|
3
|
+
import { isQueued, localDay, newestMessageId, newestUsrxNote, SHOWN_REF_LENGTH, } from "../helpers/medicationOrder.js";
|
|
4
|
+
import { actionCommand, actionReason, cardKey, firstOf, orderLists, shownOrder, STATUS_LIST, tickedRows, } from "../helpers/ordersMenu.js";
|
|
5
|
+
function failureRef(answer) {
|
|
6
|
+
return typeof answer === 'object' && answer !== null && 'ref' in answer && typeof answer.ref === 'string'
|
|
7
|
+
? answer.ref.slice(0, SHOWN_REF_LENGTH)
|
|
8
|
+
: '';
|
|
9
|
+
}
|
|
10
|
+
/** The Orders menu of a conversation: what Treat holds for it, and what staff can do from it. */
|
|
11
|
+
export function useOrdersMenu(source) {
|
|
12
|
+
const { conversationId, messages, role, openForm } = source;
|
|
13
|
+
// The calls below read the newest source when they run, without running again when the host passes new callbacks.
|
|
14
|
+
const latest = useRef(source);
|
|
15
|
+
useEffect(() => {
|
|
16
|
+
latest.current = source;
|
|
17
|
+
});
|
|
18
|
+
const [cards, setCards] = useState([]);
|
|
19
|
+
const [cardsFailed, setCardsFailed] = useState(null);
|
|
20
|
+
const [pastOrders, setPastOrders] = useState(null);
|
|
21
|
+
const [pastFailed, setPastFailed] = useState(false);
|
|
22
|
+
const [reading, setReading] = useState(false);
|
|
23
|
+
// usrx takes one job at a time and answers a second with 429, so one read runs at once and a read asked for meanwhile
|
|
24
|
+
// runs once after it. A conversation change starts a new generation, whose reads alone write.
|
|
25
|
+
const reads = useRef({ generation: 0, running: false, again: false });
|
|
26
|
+
const waiters = useRef([]);
|
|
27
|
+
const read = useCallback(async () => {
|
|
28
|
+
const run = reads.current;
|
|
29
|
+
if (run.running) {
|
|
30
|
+
run.again = true;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
run.running = true;
|
|
34
|
+
setReading(true);
|
|
35
|
+
const generation = run.generation;
|
|
36
|
+
const ours = () => generation === reads.current.generation;
|
|
37
|
+
// Asked for during the awaits below, which a check of run.again after setting it would not see.
|
|
38
|
+
const askedAgain = () => run.again;
|
|
39
|
+
const { conversationId: id } = latest.current;
|
|
40
|
+
do {
|
|
41
|
+
run.again = false;
|
|
42
|
+
try {
|
|
43
|
+
const staged = await latest.current.readStaged(id);
|
|
44
|
+
if (ours()) {
|
|
45
|
+
setCards(staged);
|
|
46
|
+
setCardsFailed(null);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
catch (answer) {
|
|
50
|
+
if (ours())
|
|
51
|
+
setCardsFailed(failureRef(answer));
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const past = await latest.current.readPast(id);
|
|
55
|
+
if (ours()) {
|
|
56
|
+
setPastOrders(past);
|
|
57
|
+
setPastFailed(false);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
if (ours()) {
|
|
62
|
+
setPastOrders(null);
|
|
63
|
+
setPastFailed(true);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} while (ours() && askedAgain());
|
|
67
|
+
if (!ours())
|
|
68
|
+
return;
|
|
69
|
+
run.running = false;
|
|
70
|
+
setReading(false);
|
|
71
|
+
for (const settle of waiters.current.splice(0))
|
|
72
|
+
settle();
|
|
73
|
+
}, []);
|
|
74
|
+
/** Settles once no read is on the way and none is waiting to run. */
|
|
75
|
+
const noReadOnTheWay = useCallback(() => new Promise((resolve) => {
|
|
76
|
+
if (reads.current.running)
|
|
77
|
+
waiters.current.push(resolve);
|
|
78
|
+
else
|
|
79
|
+
resolve();
|
|
80
|
+
}), []);
|
|
81
|
+
const [open, setOpen] = useState(false);
|
|
82
|
+
const [list, setList] = useState(null);
|
|
83
|
+
const [ticked, setTicked] = useState(new Set(STATUS_LIST));
|
|
84
|
+
const [filterOpen, setFilterOpen] = useState(false);
|
|
85
|
+
const [refillChoicesFor, setRefillChoicesFor] = useState(null);
|
|
86
|
+
const [actionsFor, setActionsFor] = useState(null);
|
|
87
|
+
const [staging, setStaging] = useState(null);
|
|
88
|
+
const [stageFailed, setStageFailed] = useState(null);
|
|
89
|
+
const [toDelete, setToDelete] = useState(null);
|
|
90
|
+
const [deleting, setDeleting] = useState(false);
|
|
91
|
+
const [deleteRefused, setDeleteRefused] = useState(null);
|
|
92
|
+
const [deleteFailed, setDeleteFailed] = useState(false);
|
|
93
|
+
const [pending, setPending] = useState(null);
|
|
94
|
+
const [reason, setReason] = useState('');
|
|
95
|
+
const [acting, setActing] = useState(false);
|
|
96
|
+
const [actionFailed, setActionFailed] = useState(false);
|
|
97
|
+
// Two taps can land before a button draws busy, and each would act, so each press holds until its call ends.
|
|
98
|
+
const pressed = useRef({ delete: false, action: false, stage: false });
|
|
99
|
+
// The usrx notes a conversation already holds when it opens are not news.
|
|
100
|
+
const [readAfter, setReadAfter] = useState({ conversationId, id: newestMessageId(messages) });
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
reads.current = { generation: reads.current.generation + 1, running: false, again: false };
|
|
103
|
+
setOpen(false);
|
|
104
|
+
setList(null);
|
|
105
|
+
setCards([]);
|
|
106
|
+
setCardsFailed(null);
|
|
107
|
+
setPastOrders(null);
|
|
108
|
+
setPastFailed(false);
|
|
109
|
+
setReading(false);
|
|
110
|
+
setToDelete(null);
|
|
111
|
+
setPending(null);
|
|
112
|
+
setStageFailed(null);
|
|
113
|
+
setReadAfter({ conversationId, id: newestMessageId(latest.current.messages) });
|
|
114
|
+
void read();
|
|
115
|
+
}, [conversationId, read]);
|
|
116
|
+
const note = readAfter.conversationId === conversationId ? newestUsrxNote(messages, readAfter.id) : null;
|
|
117
|
+
useEffect(() => {
|
|
118
|
+
if (note !== null)
|
|
119
|
+
void read();
|
|
120
|
+
}, [note, read]);
|
|
121
|
+
const { formChanges } = source;
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (formChanges)
|
|
124
|
+
void read();
|
|
125
|
+
}, [formChanges, read]);
|
|
126
|
+
const today = localDay();
|
|
127
|
+
// A card Treat can no longer vouch for after a failed read could be sent or gone, so none show.
|
|
128
|
+
const shownCards = cardsFailed === null ? cards : [];
|
|
129
|
+
const lists = orderLists(pastOrders ?? [], today);
|
|
130
|
+
const close = () => {
|
|
131
|
+
setOpen(false);
|
|
132
|
+
setList(null);
|
|
133
|
+
setRefillChoicesFor(null);
|
|
134
|
+
setActionsFor(null);
|
|
135
|
+
setFilterOpen(false);
|
|
136
|
+
};
|
|
137
|
+
const show = (shown) => {
|
|
138
|
+
setList(shown);
|
|
139
|
+
if (shown === 'past')
|
|
140
|
+
setTicked(new Set(STATUS_LIST));
|
|
141
|
+
setFilterOpen(false);
|
|
142
|
+
setRefillChoicesFor(null);
|
|
143
|
+
setActionsFor(null);
|
|
144
|
+
};
|
|
145
|
+
const form = (card, options) => {
|
|
146
|
+
close();
|
|
147
|
+
openForm?.({ card, readsDone: noReadOnTheWay(), options });
|
|
148
|
+
};
|
|
149
|
+
const stageNewPharmacy = (row) => {
|
|
150
|
+
form(shownOrder(row, today), { reorder: true, samePharmacy: false });
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
count: cards.length,
|
|
154
|
+
reading,
|
|
155
|
+
open,
|
|
156
|
+
list,
|
|
157
|
+
cards: shownCards,
|
|
158
|
+
cardsFailed,
|
|
159
|
+
pastFailed,
|
|
160
|
+
lists,
|
|
161
|
+
shownPast: list === 'past' ? tickedRows(lists.past, ticked, today) : [],
|
|
162
|
+
ticked,
|
|
163
|
+
filterOpen,
|
|
164
|
+
refillChoicesFor,
|
|
165
|
+
actionsFor,
|
|
166
|
+
staging,
|
|
167
|
+
stageFailed,
|
|
168
|
+
mayReachPharmacy: reachesPharmacy(role),
|
|
169
|
+
canOrder: Boolean(openForm),
|
|
170
|
+
today,
|
|
171
|
+
toDelete,
|
|
172
|
+
deleting,
|
|
173
|
+
deleteRefused,
|
|
174
|
+
deleteFailed,
|
|
175
|
+
pending,
|
|
176
|
+
reason,
|
|
177
|
+
acting,
|
|
178
|
+
actionFailed,
|
|
179
|
+
toggle: () => {
|
|
180
|
+
if (open) {
|
|
181
|
+
close();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
setOpen(true);
|
|
185
|
+
// Treat is the source of truth, so every open reads it again.
|
|
186
|
+
void read();
|
|
187
|
+
},
|
|
188
|
+
close,
|
|
189
|
+
show,
|
|
190
|
+
refetch: read,
|
|
191
|
+
toggleFilter: () => {
|
|
192
|
+
setFilterOpen(!filterOpen);
|
|
193
|
+
},
|
|
194
|
+
toggleStatus: (status) => {
|
|
195
|
+
const next = new Set(ticked);
|
|
196
|
+
if (next.has(status))
|
|
197
|
+
next.delete(status);
|
|
198
|
+
else
|
|
199
|
+
next.add(status);
|
|
200
|
+
setTicked(next);
|
|
201
|
+
},
|
|
202
|
+
deselectAll: () => {
|
|
203
|
+
setTicked(new Set());
|
|
204
|
+
},
|
|
205
|
+
newOrder: () => {
|
|
206
|
+
form(null, {});
|
|
207
|
+
},
|
|
208
|
+
editCard: (card) => {
|
|
209
|
+
form(card, { view: false, queued: isQueued(card) });
|
|
210
|
+
},
|
|
211
|
+
sendCard: (card) => {
|
|
212
|
+
form(card, { view: true, queued: isQueued(card) });
|
|
213
|
+
},
|
|
214
|
+
askDelete: (card) => {
|
|
215
|
+
close();
|
|
216
|
+
setToDelete(card);
|
|
217
|
+
setDeleteRefused(null);
|
|
218
|
+
setDeleteFailed(false);
|
|
219
|
+
},
|
|
220
|
+
cancelDelete: () => {
|
|
221
|
+
setToDelete(null);
|
|
222
|
+
},
|
|
223
|
+
confirmDelete: () => {
|
|
224
|
+
const card = toDelete;
|
|
225
|
+
if (!card || pressed.current.delete)
|
|
226
|
+
return;
|
|
227
|
+
pressed.current.delete = true;
|
|
228
|
+
// The conversation the delete was asked in, whatever opens while it waits.
|
|
229
|
+
const id = conversationId;
|
|
230
|
+
const generation = reads.current.generation;
|
|
231
|
+
setDeleting(true);
|
|
232
|
+
setDeleteRefused(null);
|
|
233
|
+
setDeleteFailed(false);
|
|
234
|
+
void (async () => {
|
|
235
|
+
try {
|
|
236
|
+
await noReadOnTheWay();
|
|
237
|
+
const { deleteCard, deleteQueued } = latest.current;
|
|
238
|
+
const answer = isQueued(card) ? await deleteQueued(id, String(card.queuedId)) : await deleteCard(id, card.uid);
|
|
239
|
+
if (generation !== reads.current.generation)
|
|
240
|
+
return;
|
|
241
|
+
const refused = deleteRefusal(answer);
|
|
242
|
+
if (refused !== null) {
|
|
243
|
+
setDeleteRefused(refused);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// usrx's note that the card went makes the menu read Treat again.
|
|
247
|
+
// The newest list, which a read that landed while the delete waited may have changed.
|
|
248
|
+
setCards((left) => left.filter((each) => cardKey(each) !== cardKey(card)));
|
|
249
|
+
setToDelete(null);
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
setDeleteFailed(true);
|
|
253
|
+
}
|
|
254
|
+
finally {
|
|
255
|
+
setDeleting(false);
|
|
256
|
+
pressed.current.delete = false;
|
|
257
|
+
}
|
|
258
|
+
})();
|
|
259
|
+
},
|
|
260
|
+
showRefillChoices: (row) => {
|
|
261
|
+
setRefillChoicesFor(row.key);
|
|
262
|
+
setStageFailed(null);
|
|
263
|
+
},
|
|
264
|
+
stageSamePharmacy: (row) => {
|
|
265
|
+
const { stageRefill } = latest.current;
|
|
266
|
+
if (!stageRefill || pressed.current.stage)
|
|
267
|
+
return;
|
|
268
|
+
pressed.current.stage = true;
|
|
269
|
+
setStaging(row.key);
|
|
270
|
+
setStageFailed(null);
|
|
271
|
+
const generation = reads.current.generation;
|
|
272
|
+
void (async () => {
|
|
273
|
+
try {
|
|
274
|
+
await stageRefill(shownOrder(row, today), noReadOnTheWay());
|
|
275
|
+
if (generation !== reads.current.generation)
|
|
276
|
+
return;
|
|
277
|
+
setRefillChoicesFor(null);
|
|
278
|
+
// Back to the menu's top level, which a menu closed meanwhile stays closed on.
|
|
279
|
+
setList(null);
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
if (generation === reads.current.generation)
|
|
283
|
+
setStageFailed(row.key);
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
pressed.current.stage = false;
|
|
287
|
+
setStaging(null);
|
|
288
|
+
}
|
|
289
|
+
})();
|
|
290
|
+
},
|
|
291
|
+
stageNewPharmacy,
|
|
292
|
+
reorder: (row) => {
|
|
293
|
+
form(firstOf(row), row.set ? { reorder: true, rounds: row.orders } : { reorder: true });
|
|
294
|
+
},
|
|
295
|
+
showActions: (row) => {
|
|
296
|
+
setActionsFor(row.key);
|
|
297
|
+
},
|
|
298
|
+
askAction: (action, row) => {
|
|
299
|
+
close();
|
|
300
|
+
setPending({ action, row });
|
|
301
|
+
setReason(actionReason(action, row));
|
|
302
|
+
setActionFailed(false);
|
|
303
|
+
},
|
|
304
|
+
setReason,
|
|
305
|
+
cancelAction: () => {
|
|
306
|
+
setPending(null);
|
|
307
|
+
},
|
|
308
|
+
runAction: () => {
|
|
309
|
+
if (!pending || pressed.current.action)
|
|
310
|
+
return;
|
|
311
|
+
pressed.current.action = true;
|
|
312
|
+
const { action, row } = pending;
|
|
313
|
+
setActing(true);
|
|
314
|
+
// The conversation the action was asked in, whatever opens while it waits.
|
|
315
|
+
const generation = reads.current.generation;
|
|
316
|
+
void (async () => {
|
|
317
|
+
try {
|
|
318
|
+
await latest.current.sendNote(conversationId, actionCommand(action, row, reason));
|
|
319
|
+
if (generation !== reads.current.generation)
|
|
320
|
+
return;
|
|
321
|
+
setPending(null);
|
|
322
|
+
// A pharmacy change orders the medicine anew, which the provider sends once the cancel shows Cancelled.
|
|
323
|
+
if (action === 'pharmacy')
|
|
324
|
+
stageNewPharmacy(row);
|
|
325
|
+
}
|
|
326
|
+
catch {
|
|
327
|
+
if (generation === reads.current.generation)
|
|
328
|
+
setActionFailed(true);
|
|
329
|
+
}
|
|
330
|
+
finally {
|
|
331
|
+
setActing(false);
|
|
332
|
+
pressed.current.action = false;
|
|
333
|
+
}
|
|
334
|
+
})();
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type RegimenOption } from '../helpers/regimenChoice.ts';
|
|
2
|
+
import type { StaffMessage } from '../helpers/usrxNote.ts';
|
|
3
|
+
export interface RegimenChoiceSource {
|
|
4
|
+
/** The conversation's messages, which say whether a pick was made, answered or replaced. */
|
|
5
|
+
messages: readonly StaffMessage[] | undefined;
|
|
6
|
+
noteId: number;
|
|
7
|
+
contentAttributes: unknown;
|
|
8
|
+
/**
|
|
9
|
+
* Posts a pick as a private note in the conversation. It reports its own failure, and its rejection opens the
|
|
10
|
+
* buttons again.
|
|
11
|
+
*/
|
|
12
|
+
sendNote: (note: string) => Promise<unknown>;
|
|
13
|
+
}
|
|
14
|
+
export interface RegimenChoiceView {
|
|
15
|
+
options: RegimenOption[];
|
|
16
|
+
picked: number | null;
|
|
17
|
+
closed: boolean;
|
|
18
|
+
waiting: boolean;
|
|
19
|
+
replaced: boolean;
|
|
20
|
+
pick: (number: number) => void;
|
|
21
|
+
}
|
|
22
|
+
/** What a regimen choice note shows, and its pick, from the conversation it is in. */
|
|
23
|
+
export declare function useRegimenChoice({ messages, noteId, contentAttributes, sendNote }: RegimenChoiceSource): RegimenChoiceView;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { choiceState, choiceView, pickNote, regimenOptions, } from "../helpers/regimenChoice.js";
|
|
3
|
+
const nowSeconds = () => Date.now() / 1000;
|
|
4
|
+
/** What a regimen choice note shows, and its pick, from the conversation it is in. */
|
|
5
|
+
export function useRegimenChoice({ messages, noteId, contentAttributes, sendNote }) {
|
|
6
|
+
const options = regimenOptions(contentAttributes);
|
|
7
|
+
const [now, setNow] = useState(nowSeconds);
|
|
8
|
+
const [press, setPress] = useState(null);
|
|
9
|
+
const sending = useRef(false);
|
|
10
|
+
const view = choiceView(choiceState(messages, noteId, options, now), press, now);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (view.waitEnds === null)
|
|
13
|
+
return undefined;
|
|
14
|
+
// Rounded up, since a timer that fires a fraction early would leave now short of waitEnds for good.
|
|
15
|
+
const timer = setTimeout(() => {
|
|
16
|
+
setNow(nowSeconds());
|
|
17
|
+
}, Math.ceil((view.waitEnds - nowSeconds()) * 1000));
|
|
18
|
+
return () => {
|
|
19
|
+
clearTimeout(timer);
|
|
20
|
+
};
|
|
21
|
+
}, [view.waitEnds]);
|
|
22
|
+
return {
|
|
23
|
+
options,
|
|
24
|
+
picked: view.picked,
|
|
25
|
+
closed: view.closed,
|
|
26
|
+
waiting: view.waiting,
|
|
27
|
+
replaced: view.replaced,
|
|
28
|
+
pick: (number) => {
|
|
29
|
+
// Two taps can land before the buttons draw closed, and each would post a pick.
|
|
30
|
+
if (sending.current)
|
|
31
|
+
return;
|
|
32
|
+
sending.current = true;
|
|
33
|
+
const at = nowSeconds();
|
|
34
|
+
setNow(at);
|
|
35
|
+
setPress({ number, at });
|
|
36
|
+
void (async () => {
|
|
37
|
+
try {
|
|
38
|
+
await sendNote(pickNote(number));
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
// sendNote has told staff why, so the buttons only open again for another try.
|
|
42
|
+
setPress(null);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
sending.current = false;
|
|
46
|
+
}
|
|
47
|
+
})();
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
const TICK_MS = 250;
|
|
3
|
+
/** Whole seconds left before a new code can be sent, counting down to 0 from when the wait starts. */
|
|
4
|
+
export function useResendCountdown(waitMs) {
|
|
5
|
+
const [secondsLeft, setSecondsLeft] = useState(Math.ceil(waitMs / 1000));
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const end = Date.now() + waitMs;
|
|
8
|
+
const timer = setInterval(() => {
|
|
9
|
+
const left = Math.max(0, Math.ceil((end - Date.now()) / 1000));
|
|
10
|
+
setSecondsLeft(left);
|
|
11
|
+
if (left === 0)
|
|
12
|
+
clearInterval(timer);
|
|
13
|
+
}, TICK_MS);
|
|
14
|
+
return () => {
|
|
15
|
+
clearInterval(timer);
|
|
16
|
+
};
|
|
17
|
+
}, [waitMs]);
|
|
18
|
+
return secondsLeft;
|
|
19
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A welcome step's field: what was typed, whether it was refused, and its change and submit. */
|
|
2
|
+
export interface StepField {
|
|
3
|
+
typed: string;
|
|
4
|
+
refused: boolean;
|
|
5
|
+
onChangeText: (typed: string) => void;
|
|
6
|
+
submit: () => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function useStepField(usable: (typed: string) => string | null, onSubmit: (value: string) => void, initial?: string): StepField;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useState } from 'react';
|
|
2
|
+
// usable turns what was typed into what is handed on, or null when it cannot be used, as givenEmail and givenName do.
|
|
3
|
+
export function useStepField(usable, onSubmit, initial = '') {
|
|
4
|
+
const [typed, setTyped] = useState(initial);
|
|
5
|
+
const [refused, setRefused] = useState(false);
|
|
6
|
+
return {
|
|
7
|
+
typed,
|
|
8
|
+
refused,
|
|
9
|
+
onChangeText: setTyped,
|
|
10
|
+
submit: () => {
|
|
11
|
+
const value = usable(typed);
|
|
12
|
+
setRefused(value === null);
|
|
13
|
+
if (value !== null)
|
|
14
|
+
onSubmit(value);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type TypingTimes } from '../helpers/skipTyping.ts';
|
|
2
|
+
/** The Skips App's last word on Skip typing. */
|
|
3
|
+
export interface SkipsTyping {
|
|
4
|
+
typing: boolean;
|
|
5
|
+
count: number;
|
|
6
|
+
}
|
|
7
|
+
export interface TypingCue {
|
|
8
|
+
dots: boolean;
|
|
9
|
+
held: ReadonlySet<number>;
|
|
10
|
+
}
|
|
11
|
+
export declare function useTypingCue(skipsLineIds: readonly number[], skipsTyping: SkipsTyping, times?: TypingTimes): TypingCue;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { useEffect, useReducer, useState } from 'react';
|
|
2
|
+
import { heldFrom, TYPING_IDLE, TYPING_TIMES, typingReducer } from "../helpers/skipTyping.js";
|
|
3
|
+
const NONE = new Set();
|
|
4
|
+
// The Skips App's word on Skip typing and Skip's lines landing drive the cue, and two timers keep the dots up for
|
|
5
|
+
// their minimum and stop them at the limit. skipsLineIds are Skip's lines in the conversation, in order.
|
|
6
|
+
export function useTypingCue(skipsLineIds, skipsTyping, times = TYPING_TIMES) {
|
|
7
|
+
const [state, dispatch] = useReducer(typingReducer, TYPING_IDLE);
|
|
8
|
+
const [wordsSeen, setWordsSeen] = useState(skipsTyping.count);
|
|
9
|
+
const [linesSeen, setLinesSeen] = useState(skipsLineIds.length);
|
|
10
|
+
if (skipsTyping.count !== wordsSeen) {
|
|
11
|
+
setWordsSeen(skipsTyping.count);
|
|
12
|
+
// The lines before this render: a reply that lands with typing_on, as one sent in a keystroke does, waits for the dots.
|
|
13
|
+
dispatch(skipsTyping.typing ? { type: 'typing', skipsLines: linesSeen } : { type: 'stopped' });
|
|
14
|
+
}
|
|
15
|
+
if (skipsLineIds.length !== linesSeen) {
|
|
16
|
+
setLinesSeen(skipsLineIds.length);
|
|
17
|
+
dispatch({ type: 'stopped' });
|
|
18
|
+
}
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!state.dots || state.shown)
|
|
21
|
+
return undefined;
|
|
22
|
+
const shown = setTimeout(() => {
|
|
23
|
+
dispatch({ type: 'shown' });
|
|
24
|
+
}, times.leastMs);
|
|
25
|
+
return () => {
|
|
26
|
+
clearTimeout(shown);
|
|
27
|
+
};
|
|
28
|
+
}, [state.dots, state.shown, state.turn, times.leastMs]);
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!state.dots)
|
|
31
|
+
return undefined;
|
|
32
|
+
const capped = setTimeout(() => {
|
|
33
|
+
dispatch({ type: 'capped' });
|
|
34
|
+
}, times.limitMs);
|
|
35
|
+
return () => {
|
|
36
|
+
clearTimeout(capped);
|
|
37
|
+
};
|
|
38
|
+
}, [state.dots, state.beats, times.limitMs]);
|
|
39
|
+
const from = heldFrom(state);
|
|
40
|
+
return { dots: state.dots, held: from === null ? NONE : new Set(skipsLineIds.slice(from)) };
|
|
41
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { typesBefore, typingMs, WELCOME_TYPING_TIMES, } from "../helpers/welcome.js";
|
|
3
|
+
// The welcome step whose new line Skip is still typing, kept out of sight with its field or buttons until the
|
|
4
|
+
// dots go, or null.
|
|
5
|
+
export function useWelcomeTyping(step, times = WELCOME_TYPING_TIMES) {
|
|
6
|
+
const [seen, setSeen] = useState(null);
|
|
7
|
+
const [typing, setTyping] = useState(null);
|
|
8
|
+
const { leastMs, mostMs } = times;
|
|
9
|
+
if (step !== seen) {
|
|
10
|
+
setSeen(step);
|
|
11
|
+
// With no time to type in, the line shows as the step is reached.
|
|
12
|
+
setTyping(step !== null && mostMs > 0 && typesBefore(seen, step) ? step : null);
|
|
13
|
+
}
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (typing === null)
|
|
16
|
+
return undefined;
|
|
17
|
+
const typed = setTimeout(() => {
|
|
18
|
+
setTyping(null);
|
|
19
|
+
}, typingMs({ leastMs, mostMs }, Math.random()));
|
|
20
|
+
return () => {
|
|
21
|
+
clearTimeout(typed);
|
|
22
|
+
};
|
|
23
|
+
}, [typing, leastMs, mostMs]);
|
|
24
|
+
return typing;
|
|
25
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type CardNoteSource } from '../hooks/useCardNoteActions.ts';
|
|
2
|
+
import { type CardActionsWords } from '../ui/CardActions.tsx';
|
|
3
|
+
export interface CardNoteActionsProps extends CardNoteSource {
|
|
4
|
+
words: CardActionsWords;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* usrx's card note in a conversation, with its Send, Edit and Delete, as the web dashboard and the mobile app show it.
|
|
8
|
+
* A note whose cards cannot be read draws nothing.
|
|
9
|
+
*/
|
|
10
|
+
export declare function CardNoteActions(props: CardNoteActionsProps): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { cardNote } from "../helpers/cardNote.js";
|
|
3
|
+
import { useCardNoteActions } from "../hooks/useCardNoteActions.js";
|
|
4
|
+
import { CardActions } from "../ui/CardActions.js";
|
|
5
|
+
function Drawn({ note, words, ...source }) {
|
|
6
|
+
const view = useCardNoteActions(note, source);
|
|
7
|
+
return _jsx(CardActions, { ...view, words: words });
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* usrx's card note in a conversation, with its Send, Edit and Delete, as the web dashboard and the mobile app show it.
|
|
11
|
+
* A note whose cards cannot be read draws nothing.
|
|
12
|
+
*/
|
|
13
|
+
export function CardNoteActions(props) {
|
|
14
|
+
const note = cardNote(props.contentAttributes);
|
|
15
|
+
return note ? _jsx(Drawn, { ...props, note: note }) : null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type OrderFormSource } from '../hooks/useOrderForm.ts';
|
|
2
|
+
import { type OrderFormWords } from '../ui/OrderForm.tsx';
|
|
3
|
+
export interface OrderFormNoteProps extends OrderFormSource {
|
|
4
|
+
words: OrderFormWords;
|
|
5
|
+
openLink: (url: string) => void;
|
|
6
|
+
}
|
|
7
|
+
/** The Order form, which the web dashboard and the mobile app each show in their own dialog when the Orders menu asks. */
|
|
8
|
+
export declare function OrderFormNote({ words, openLink, ...source }: OrderFormNoteProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useOrderForm } from "../hooks/useOrderForm.js";
|
|
3
|
+
import { OrderForm } from "../ui/OrderForm.js";
|
|
4
|
+
/** The Order form, which the web dashboard and the mobile app each show in their own dialog when the Orders menu asks. */
|
|
5
|
+
export function OrderFormNote({ words, openLink, ...source }) {
|
|
6
|
+
const view = useOrderForm(source);
|
|
7
|
+
return _jsx(OrderForm, { ...view, words: words, openLink: openLink });
|
|
8
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type OrdersMenuSource } from '../hooks/useOrdersMenu.ts';
|
|
2
|
+
import { type OrdersMenuWords } from '../ui/OrdersMenu.tsx';
|
|
3
|
+
export interface OrdersMenuNoteProps extends OrdersMenuSource {
|
|
4
|
+
words: OrdersMenuWords;
|
|
5
|
+
openLink: (url: string) => void;
|
|
6
|
+
}
|
|
7
|
+
/** A conversation's Orders menu, as the web dashboard and the mobile app both show it beside the private note box. */
|
|
8
|
+
export declare function OrdersMenuNote({ words, openLink, ...source }: OrdersMenuNoteProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useOrdersMenu } from "../hooks/useOrdersMenu.js";
|
|
3
|
+
import { OrdersMenu } from "../ui/OrdersMenu.js";
|
|
4
|
+
/** A conversation's Orders menu, as the web dashboard and the mobile app both show it beside the private note box. */
|
|
5
|
+
export function OrdersMenuNote({ words, openLink, ...source }) {
|
|
6
|
+
const view = useOrdersMenu(source);
|
|
7
|
+
return _jsx(OrdersMenu, { ...view, words: words, openLink: openLink });
|
|
8
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type RegimenChoiceSource } from '../hooks/useRegimenChoice.ts';
|
|
2
|
+
import { type RegimenChoiceWords } from '../ui/RegimenChoice.tsx';
|
|
3
|
+
export interface RegimenChoiceNoteProps extends RegimenChoiceSource {
|
|
4
|
+
words: RegimenChoiceWords;
|
|
5
|
+
}
|
|
6
|
+
/** usrx's regimen choice note in a conversation, as the web dashboard and the mobile app both show it. */
|
|
7
|
+
export declare function RegimenChoiceNote({ words, ...source }: RegimenChoiceNoteProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRegimenChoice } from "../hooks/useRegimenChoice.js";
|
|
3
|
+
import { RegimenChoice } from "../ui/RegimenChoice.js";
|
|
4
|
+
/** usrx's regimen choice note in a conversation, as the web dashboard and the mobile app both show it. */
|
|
5
|
+
export function RegimenChoiceNote({ words, ...source }) {
|
|
6
|
+
const { pick, ...view } = useRegimenChoice(source);
|
|
7
|
+
return _jsx(RegimenChoice, { ...view, words: words, onPick: pick });
|
|
8
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type SkipsFeatureName = 'regimen-choice' | 'card-note-actions' | 'note-fold' | 'note-view' | 'orders-menu' | 'order-form';
|
|
2
|
+
export interface SkipsFeature {
|
|
3
|
+
name: SkipsFeatureName;
|
|
4
|
+
/** The custom element the web dashboard mounts it as. */
|
|
5
|
+
element: string;
|
|
6
|
+
/** The component under @joinskips/common/staff the mobile app draws it with. */
|
|
7
|
+
component: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const SKIPS_FEATURES: readonly SkipsFeature[];
|