@joinskips/common 0.0.0-stage → 0.1.0-develop.138

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 (131) hide show
  1. package/README.md +102 -2
  2. package/dist/chat/SkipsChat.d.ts +37 -0
  3. package/dist/chat/SkipsChat.js +103 -0
  4. package/dist/chat/client.d.ts +53 -0
  5. package/dist/chat/client.js +178 -0
  6. package/dist/chat/connection.d.ts +4 -0
  7. package/dist/chat/connection.js +6 -0
  8. package/dist/chat/generated/skips-app/index.d.ts +1 -0
  9. package/dist/chat/generated/skips-app/index.js +2 -0
  10. package/dist/chat/generated/skips-app/types.gen.d.ts +733 -0
  11. package/dist/chat/generated/skips-app/types.gen.js +2 -0
  12. package/dist/chat/index.d.ts +3 -0
  13. package/dist/chat/index.js +2 -0
  14. package/dist/chat/platform.d.ts +4 -0
  15. package/dist/chat/platform.js +46 -0
  16. package/dist/chat/session.d.ts +19 -0
  17. package/dist/chat/session.js +95 -0
  18. package/dist/chat/useSkipsChat.d.ts +79 -0
  19. package/dist/chat/useSkipsChat.js +507 -0
  20. package/dist/chat/verify.d.ts +35 -0
  21. package/dist/chat/verify.js +84 -0
  22. package/dist/elements/skips-elements.js +18521 -0
  23. package/dist/helpers/attachments.d.ts +12 -0
  24. package/dist/helpers/attachments.js +34 -0
  25. package/dist/helpers/cable.d.ts +34 -0
  26. package/dist/helpers/cable.js +88 -0
  27. package/dist/helpers/cableFrames.d.ts +40 -0
  28. package/dist/helpers/cableFrames.js +70 -0
  29. package/dist/helpers/cardNote.d.ts +79 -0
  30. package/dist/helpers/cardNote.js +152 -0
  31. package/dist/helpers/commandNote.d.ts +4 -0
  32. package/dist/helpers/commandNote.js +23 -0
  33. package/dist/helpers/index.d.ts +19 -0
  34. package/dist/helpers/index.js +19 -0
  35. package/dist/helpers/markdown.d.ts +47 -0
  36. package/dist/helpers/markdown.js +129 -0
  37. package/dist/helpers/medicationOrder.d.ts +333 -0
  38. package/dist/helpers/medicationOrder.js +383 -0
  39. package/dist/helpers/message.d.ts +46 -0
  40. package/dist/helpers/message.js +12 -0
  41. package/dist/helpers/noteView.d.ts +35 -0
  42. package/dist/helpers/noteView.js +65 -0
  43. package/dist/helpers/orderForm.d.ts +126 -0
  44. package/dist/helpers/orderForm.js +271 -0
  45. package/dist/helpers/ordersMenu.d.ts +56 -0
  46. package/dist/helpers/ordersMenu.js +129 -0
  47. package/dist/helpers/regimenChoice.d.ts +42 -0
  48. package/dist/helpers/regimenChoice.js +73 -0
  49. package/dist/helpers/scrollBox.d.ts +5 -0
  50. package/dist/helpers/scrollBox.js +15 -0
  51. package/dist/helpers/skipTyping.d.ts +26 -0
  52. package/dist/helpers/skipTyping.js +29 -0
  53. package/dist/helpers/skipsMessage.d.ts +55 -0
  54. package/dist/helpers/skipsMessage.js +130 -0
  55. package/dist/helpers/stageRefill.d.ts +33 -0
  56. package/dist/helpers/stageRefill.js +41 -0
  57. package/dist/helpers/thread.d.ts +54 -0
  58. package/dist/helpers/thread.js +122 -0
  59. package/dist/helpers/typing.d.ts +13 -0
  60. package/dist/helpers/typing.js +68 -0
  61. package/dist/helpers/usrxNote.d.ts +22 -0
  62. package/dist/helpers/usrxNote.js +26 -0
  63. package/dist/helpers/visibleArea.d.ts +15 -0
  64. package/dist/helpers/visibleArea.js +11 -0
  65. package/dist/helpers/welcome.d.ts +25 -0
  66. package/dist/helpers/welcome.js +51 -0
  67. package/dist/hooks/index.d.ts +8 -0
  68. package/dist/hooks/index.js +8 -0
  69. package/dist/hooks/useCardNoteActions.d.ts +82 -0
  70. package/dist/hooks/useCardNoteActions.js +210 -0
  71. package/dist/hooks/useCodeEntry.d.ts +6 -0
  72. package/dist/hooks/useCodeEntry.js +14 -0
  73. package/dist/hooks/useComposer.d.ts +20 -0
  74. package/dist/hooks/useComposer.js +40 -0
  75. package/dist/hooks/useOrderForm.d.ts +80 -0
  76. package/dist/hooks/useOrderForm.js +403 -0
  77. package/dist/hooks/useOrdersMenu.d.ts +114 -0
  78. package/dist/hooks/useOrdersMenu.js +337 -0
  79. package/dist/hooks/useRegimenChoice.d.ts +23 -0
  80. package/dist/hooks/useRegimenChoice.js +50 -0
  81. package/dist/hooks/useResendCountdown.d.ts +2 -0
  82. package/dist/hooks/useResendCountdown.js +19 -0
  83. package/dist/hooks/useStepField.d.ts +8 -0
  84. package/dist/hooks/useStepField.js +17 -0
  85. package/dist/hooks/useTypingCue.d.ts +11 -0
  86. package/dist/hooks/useTypingCue.js +41 -0
  87. package/dist/hooks/useWelcomeTyping.d.ts +2 -0
  88. package/dist/hooks/useWelcomeTyping.js +25 -0
  89. package/dist/index.d.ts +5 -0
  90. package/dist/index.js +5 -0
  91. package/dist/staff/CardNoteActions.d.ts +10 -0
  92. package/dist/staff/CardNoteActions.js +16 -0
  93. package/dist/staff/OrderFormNote.d.ts +8 -0
  94. package/dist/staff/OrderFormNote.js +8 -0
  95. package/dist/staff/OrdersMenuNote.d.ts +8 -0
  96. package/dist/staff/OrdersMenuNote.js +8 -0
  97. package/dist/staff/RegimenChoiceNote.d.ts +7 -0
  98. package/dist/staff/RegimenChoiceNote.js +8 -0
  99. package/dist/staff/features.d.ts +9 -0
  100. package/dist/staff/features.js +8 -0
  101. package/dist/staff/index.d.ts +11 -0
  102. package/dist/staff/index.js +7 -0
  103. package/dist/ui/Bubble.d.ts +5 -0
  104. package/dist/ui/Bubble.js +9 -0
  105. package/dist/ui/CardActions.d.ts +34 -0
  106. package/dist/ui/CardActions.js +28 -0
  107. package/dist/ui/Conversation.d.ts +69 -0
  108. package/dist/ui/Conversation.js +192 -0
  109. package/dist/ui/Markdown.d.ts +11 -0
  110. package/dist/ui/Markdown.js +51 -0
  111. package/dist/ui/NoteFold.d.ts +19 -0
  112. package/dist/ui/NoteFold.js +12 -0
  113. package/dist/ui/NoteViewControl.d.ts +19 -0
  114. package/dist/ui/NoteViewControl.js +20 -0
  115. package/dist/ui/OrderForm.d.ts +71 -0
  116. package/dist/ui/OrderForm.js +118 -0
  117. package/dist/ui/OrdersMenu.d.ts +71 -0
  118. package/dist/ui/OrdersMenu.js +128 -0
  119. package/dist/ui/RegimenChoice.d.ts +23 -0
  120. package/dist/ui/RegimenChoice.js +16 -0
  121. package/dist/ui/SkipAvatar.d.ts +7 -0
  122. package/dist/ui/SkipAvatar.js +8 -0
  123. package/dist/ui/StaffButton.d.ts +18 -0
  124. package/dist/ui/StaffButton.js +17 -0
  125. package/dist/ui/Welcome.d.ts +91 -0
  126. package/dist/ui/Welcome.js +128 -0
  127. package/dist/ui/index.d.ts +8 -0
  128. package/dist/ui/index.js +7 -0
  129. package/dist/ui/parts.d.ts +38 -0
  130. package/dist/ui/parts.js +6 -0
  131. package/package.json +115 -4
@@ -0,0 +1,51 @@
1
+ // What a new visitor gives before the conversation starts, as the website's welcome asks for it: a choice, their
2
+ // email, the code sent to it, and their name if the Skips App does not know them.
3
+ import isEmailAddress from 'validator/lib/isEmail.js';
4
+ export const NAME_MAX_LENGTH = 100;
5
+ // The longest address a mailbox can have.
6
+ export const EMAIL_MAX_LENGTH = 254;
7
+ export function isEmail(email) {
8
+ return isEmailAddress(email);
9
+ }
10
+ export function isName(name) {
11
+ return name.length > 0 && name.length <= NAME_MAX_LENGTH;
12
+ }
13
+ // What was typed, without the spaces around it that a phone keyboard can add, or null when it is no address.
14
+ export function givenEmail(typed) {
15
+ const given = typed.trim();
16
+ return isEmail(given) ? given : null;
17
+ }
18
+ export function givenName(typed) {
19
+ const given = typed.trim();
20
+ return isName(given) ? given : null;
21
+ }
22
+ // Skip types before a line the welcome says, as before a reply from the Skips App, for a random time between the
23
+ // two so it reads as typed.
24
+ export const WELCOME_TYPING_TIMES = { leastMs: 1000, mostMs: 2000 };
25
+ // The steps that each bring a new line from Skip, in the order the welcome goes through them.
26
+ const NEW_LINE_STEPS = ['choosing', 'email', 'code', 'name'];
27
+ /**
28
+ * Whether reaching this step has Skip say a new line. Going back a step, as Change email does, or staying on one,
29
+ * as a new code does, says nothing new, and the conversation after the welcome is the Skips App's to type.
30
+ */
31
+ export function typesBefore(from, to) {
32
+ const next = NEW_LINE_STEPS.indexOf(to);
33
+ return next !== -1 && (from === null || next > NEW_LINE_STEPS.indexOf(from));
34
+ }
35
+ /** What the code field holds: its digits only, never more than the code has. A pasted "246 810" keeps its digits. */
36
+ export function codeDigits(typed, length) {
37
+ return typed.replace(/\D/g, '').slice(0, length);
38
+ }
39
+ /** Whether a change to the code field has just made it a whole code, so it goes on with no button pressed. */
40
+ export function completesCode(digits, before, length) {
41
+ return digits.length === length && digits !== before;
42
+ }
43
+ // How long after a code goes out before a new one can be asked for. The code service holds the same wait.
44
+ export const RESEND_AFTER_MS = 30_000;
45
+ /** The wait before a new code, as a clock: 0:30, then 0:09. */
46
+ export function resendClock(seconds) {
47
+ return `${String(Math.floor(seconds / 60))}:${String(seconds % 60).padStart(2, '0')}`;
48
+ }
49
+ export function typingMs(times, random) {
50
+ return times.leastMs + random * (times.mostMs - times.leastMs);
51
+ }
@@ -0,0 +1,8 @@
1
+ export { useCardNoteActions, type CardNoteActionsView, type CardNoteSource } from './useCardNoteActions.ts';
2
+ export { useCodeEntry, type CodeEntry } from './useCodeEntry.ts';
3
+ export { useComposer, type Composer, type ComposerOptions } from './useComposer.ts';
4
+ export { useRegimenChoice, type RegimenChoiceSource, type RegimenChoiceView } from './useRegimenChoice.ts';
5
+ export { useResendCountdown } from './useResendCountdown.ts';
6
+ export { useStepField, type StepField } from './useStepField.ts';
7
+ export { useTypingCue, type SkipsTyping, type TypingCue } from './useTypingCue.ts';
8
+ export { useWelcomeTyping } from './useWelcomeTyping.ts';
@@ -0,0 +1,8 @@
1
+ export { useCardNoteActions } from "./useCardNoteActions.js";
2
+ export { useCodeEntry } from "./useCodeEntry.js";
3
+ export { useComposer } from "./useComposer.js";
4
+ export { useRegimenChoice } from "./useRegimenChoice.js";
5
+ export { useResendCountdown } from "./useResendCountdown.js";
6
+ export { useStepField } from "./useStepField.js";
7
+ export { useTypingCue } from "./useTypingCue.js";
8
+ export { useWelcomeTyping } from "./useWelcomeTyping.js";
@@ -0,0 +1,82 @@
1
+ import { type CardNote, type CardPeople, type NoteCard } from '../helpers/cardNote.ts';
2
+ import type { StagedCard } from '../helpers/medicationOrder.ts';
3
+ import type { StaffMessage } from '../helpers/usrxNote.ts';
4
+ export interface CardNoteSource {
5
+ /** The conversation's messages, which say what became of the note's cards. */
6
+ messages: readonly StaffMessage[] | undefined;
7
+ noteId: number;
8
+ /** The note's attributes, which make it a card note. */
9
+ contentAttributes: unknown;
10
+ /** The signed-in staff member's skips_role. Only a role that reaches a pharmacy gets Send. */
11
+ role: string | null;
12
+ /** Posts a private note in the conversation. It reports its own failure, and its rejection opens Send again. */
13
+ sendNote: (note: string) => Promise<unknown>;
14
+ /** The conversation's staged cards from Treat, through treat_lookups. */
15
+ readCards: () => Promise<StagedCard[]>;
16
+ /**
17
+ * Asks usrx to delete one card through treat_lookups, and resolves its answer unchanged: null for a delete, usrx's
18
+ * refusal otherwise. Any other value reads as a delete that did not happen.
19
+ */
20
+ deleteCard: (uid: string) => Promise<unknown>;
21
+ /** Opens a staged card in the app's order form, at the field usrx asked for if any. Without it the note has no Edit. */
22
+ editCard?: ((card: StagedCard, focus?: string) => void) | undefined;
23
+ /**
24
+ * Sends the ID.me app's code with the cards usrx left unsigned to treat_lookups' send_with_code, never into a note.
25
+ * Rejects when the code did not reach usrx.
26
+ */
27
+ sendWithCode: (cardUids: string[], code: string) => Promise<unknown>;
28
+ }
29
+ /** The field for the ID.me app's code, which signs the cards usrx left unsigned. */
30
+ export interface CodeSigningView {
31
+ code: string;
32
+ setCode: (code: string) => void;
33
+ /** Six digits are typed. */
34
+ ready: boolean;
35
+ /** The code is on its way, or waits on usrx's next note. */
36
+ sending: boolean;
37
+ failed: boolean;
38
+ sign: () => void;
39
+ }
40
+ /** Edit, which reads the note's cards from Treat and opens one in the app's order form. */
41
+ export interface CardEdit {
42
+ start: () => void;
43
+ /** Opens one of the cards Edit listed. */
44
+ open: (card: StagedCard) => void;
45
+ }
46
+ export interface CardNoteActionsView {
47
+ cards: NoteCard[];
48
+ submitted: boolean;
49
+ replaced: boolean;
50
+ allDeleted: boolean;
51
+ closed: boolean;
52
+ people: CardPeople | null;
53
+ canSend: boolean;
54
+ /** A Send is on its way or waits on usrx's answer. */
55
+ sending: boolean;
56
+ reading: boolean;
57
+ /** The uid of the card being deleted, or null. */
58
+ deleting: string | null;
59
+ busy: boolean;
60
+ readFailed: boolean;
61
+ noCards: boolean;
62
+ /** The staged cards to pick from when Edit found several. */
63
+ editing: StagedCard[];
64
+ /** The note's cards are listed to pick one to delete. */
65
+ picking: boolean;
66
+ /** The card a delete waits on confirmation for. */
67
+ toDelete: NoteCard | null;
68
+ /** usrx's reason for refusing the delete, or null. */
69
+ deleteRefused: string | null;
70
+ deleteFailed: boolean;
71
+ /** Null unless usrx left some of the cards unsigned and the staff member reaches a pharmacy. */
72
+ signing: CodeSigningView | null;
73
+ send: () => void;
74
+ /** Null when the app has no order form to edit a card in. */
75
+ edit: CardEdit | null;
76
+ togglePicking: () => void;
77
+ askDelete: (card: NoteCard) => void;
78
+ cancelDelete: () => void;
79
+ confirmDelete: (card: NoteCard) => void;
80
+ }
81
+ /** What a card note shows, and its Send, Edit and Delete, from the conversation it is in. */
82
+ export declare function useCardNoteActions(note: CardNote, source: CardNoteSource): CardNoteActionsView;
@@ -0,0 +1,210 @@
1
+ import { useEffect, useRef, useState } from 'react';
2
+ import { cardNoteView, deleteRefusal, isIdMeCode, noteState, reachesPharmacy, submitCommand, } from "../helpers/cardNote.js";
3
+ /** What a card note shows, and its Send, Edit and Delete, from the conversation it is in. */
4
+ export function useCardNoteActions(note, source) {
5
+ const { messages, noteId, role, sendNote, readCards, deleteCard, editCard, sendWithCode } = source;
6
+ const state = noteState(messages, noteId, note.cards.map((card) => card.uid));
7
+ const [deletedHere, setDeletedHere] = useState([]);
8
+ const view = cardNoteView(note, state, deletedHere);
9
+ // Two taps can land before a button draws busy, and each would act, so each press holds until its call ends.
10
+ const pressed = useRef({ send: false, edit: false, delete: false, code: false });
11
+ // A note on its way has no id yet for noteState to count, so the press holds Send until one does.
12
+ const [posting, setPosting] = useState(false);
13
+ const sentHere = useRef(false);
14
+ useEffect(() => {
15
+ setPosting(false);
16
+ }, [state.asks]);
17
+ const [reading, setReading] = useState(false);
18
+ const [readFailed, setReadFailed] = useState(false);
19
+ const [noCards, setNoCards] = useState(false);
20
+ const [editing, setEditing] = useState([]);
21
+ const [picking, setPicking] = useState(false);
22
+ const [toDelete, setToDelete] = useState(null);
23
+ const [deleting, setDeleting] = useState(null);
24
+ const [deleteRefused, setDeleteRefused] = useState(null);
25
+ const [deleteFailed, setDeleteFailed] = useState(false);
26
+ // The refusal below reads these when it runs, without running again when the host passes new callbacks.
27
+ const latest = useRef({ needs: state.needs, readCards, editCard });
28
+ useEffect(() => {
29
+ latest.current = { needs: state.needs, readCards, editCard };
30
+ });
31
+ // When usrx refuses a Send for what a card lacks, only the screen that pressed it opens the form there
32
+ // (joinskips/skips-app#427). usrx's answer of any kind ends the wait. Keyed on the refusal note and the count of
33
+ // sends, since a send can land with its answer at once.
34
+ const refusalId = state.needs?.noteId;
35
+ useEffect(() => {
36
+ if (state.asked || !sentHere.current)
37
+ return;
38
+ sentHere.current = false;
39
+ const { needs, readCards: read, editCard: open } = latest.current;
40
+ if (!needs || !open)
41
+ return;
42
+ setReadFailed(false);
43
+ setNoCards(false);
44
+ void (async () => {
45
+ try {
46
+ const card = (await read()).find((each) => each.uid === needs.uid);
47
+ setNoCards(!card);
48
+ if (card)
49
+ open(card, needs.field);
50
+ }
51
+ catch {
52
+ setReadFailed(true);
53
+ }
54
+ })();
55
+ }, [refusalId, state.asked, state.asks]);
56
+ const [code, setCode] = useState('');
57
+ const [codeSending, setCodeSending] = useState(false);
58
+ const [codeFailed, setCodeFailed] = useState(false);
59
+ // usrx's next note about the cards, signed or not, ends the wait (joinskips/usrx#335).
60
+ useEffect(() => {
61
+ setCodeSending(false);
62
+ }, [state.lastUsrxNoteId]);
63
+ const codeReady = isIdMeCode(code);
64
+ const { signing } = state;
65
+ const sending = posting || state.asked;
66
+ return {
67
+ cards: view.left,
68
+ submitted: state.submitted,
69
+ replaced: state.replaced,
70
+ allDeleted: view.allDeleted,
71
+ closed: view.closed,
72
+ people: view.people,
73
+ canSend: reachesPharmacy(role),
74
+ sending,
75
+ reading,
76
+ deleting,
77
+ busy: sending || reading || deleting !== null,
78
+ readFailed,
79
+ noCards,
80
+ editing,
81
+ picking,
82
+ toDelete,
83
+ deleteRefused,
84
+ deleteFailed,
85
+ signing: signing && reachesPharmacy(role)
86
+ ? {
87
+ code,
88
+ setCode: (typed) => {
89
+ setCode(typed.trim());
90
+ },
91
+ ready: codeReady,
92
+ sending: codeSending,
93
+ failed: codeFailed,
94
+ sign: () => {
95
+ if (pressed.current.code || !codeReady)
96
+ return;
97
+ pressed.current.code = true;
98
+ setCodeSending(true);
99
+ setCodeFailed(false);
100
+ void (async () => {
101
+ try {
102
+ await sendWithCode(signing, code);
103
+ setCode('');
104
+ }
105
+ catch {
106
+ setCodeSending(false);
107
+ setCodeFailed(true);
108
+ }
109
+ finally {
110
+ pressed.current.code = false;
111
+ }
112
+ })();
113
+ },
114
+ }
115
+ : null,
116
+ send: () => {
117
+ if (pressed.current.send)
118
+ return;
119
+ pressed.current.send = true;
120
+ sentHere.current = true;
121
+ setPosting(true);
122
+ void (async () => {
123
+ try {
124
+ await sendNote(submitCommand(view.left.map((card) => card.uid)));
125
+ }
126
+ catch {
127
+ setPosting(false);
128
+ sentHere.current = false;
129
+ }
130
+ finally {
131
+ pressed.current.send = false;
132
+ }
133
+ })();
134
+ },
135
+ edit: editCard
136
+ ? {
137
+ start: () => {
138
+ if (pressed.current.edit)
139
+ return;
140
+ pressed.current.edit = true;
141
+ setReading(true);
142
+ setReadFailed(false);
143
+ setNoCards(false);
144
+ setEditing([]);
145
+ void (async () => {
146
+ // Treat is the source of truth for a card, so Edit reads it each time instead of trusting the note.
147
+ try {
148
+ const ours = (await readCards()).filter((card) => view.left.some((each) => each.uid === card.uid));
149
+ setNoCards(ours.length === 0);
150
+ const [only, ...more] = ours;
151
+ if (only && more.length === 0)
152
+ editCard(only);
153
+ else
154
+ setEditing(ours);
155
+ }
156
+ catch {
157
+ setReadFailed(true);
158
+ }
159
+ finally {
160
+ setReading(false);
161
+ pressed.current.edit = false;
162
+ }
163
+ })();
164
+ },
165
+ open: (card) => {
166
+ setEditing([]);
167
+ editCard(card);
168
+ },
169
+ }
170
+ : null,
171
+ togglePicking: () => {
172
+ setPicking(!picking);
173
+ },
174
+ askDelete: (card) => {
175
+ setToDelete(card);
176
+ setDeleteRefused(null);
177
+ setDeleteFailed(false);
178
+ },
179
+ cancelDelete: () => {
180
+ setToDelete(null);
181
+ },
182
+ confirmDelete: (card) => {
183
+ if (pressed.current.delete)
184
+ return;
185
+ pressed.current.delete = true;
186
+ setDeleting(card.uid);
187
+ setDeleteRefused(null);
188
+ setDeleteFailed(false);
189
+ void (async () => {
190
+ try {
191
+ const refused = deleteRefusal(await deleteCard(card.uid));
192
+ if (refused === null) {
193
+ setDeletedHere((done) => [...done, card.uid]);
194
+ setPicking(false);
195
+ setToDelete(null);
196
+ }
197
+ else
198
+ setDeleteRefused(refused);
199
+ }
200
+ catch {
201
+ setDeleteFailed(true);
202
+ }
203
+ finally {
204
+ setDeleting(null);
205
+ pressed.current.delete = false;
206
+ }
207
+ })();
208
+ },
209
+ };
210
+ }
@@ -0,0 +1,6 @@
1
+ /** The emailed code as it is typed: digits only, handed on by itself once it is whole. */
2
+ export interface CodeEntry {
3
+ code: string;
4
+ onChangeText: (typed: string) => void;
5
+ }
6
+ export declare function useCodeEntry(length: number, onSubmit: (code: string) => void): CodeEntry;
@@ -0,0 +1,14 @@
1
+ import { useState } from 'react';
2
+ import { codeDigits, completesCode } from "../helpers/welcome.js";
3
+ export function useCodeEntry(length, onSubmit) {
4
+ const [code, setCode] = useState('');
5
+ return {
6
+ code,
7
+ onChangeText: (typed) => {
8
+ const digits = codeDigits(typed, length);
9
+ setCode(digits);
10
+ if (completesCode(digits, code, length))
11
+ onSubmit(digits);
12
+ },
13
+ };
14
+ }
@@ -0,0 +1,20 @@
1
+ import { type AttachmentProblem } from '../helpers/attachments.ts';
2
+ import type { PickedFile } from '../helpers/message.ts';
3
+ export interface ComposerOptions {
4
+ onSend: (text: string, file: PickedFile | null) => void;
5
+ onTyped: (text: string) => void;
6
+ pickFile?: () => Promise<PickedFile | null>;
7
+ initialText?: string;
8
+ }
9
+ /** The message box's state and what each control does, which Conversation draws. */
10
+ export interface Composer {
11
+ text: string;
12
+ picked: PickedFile | null;
13
+ refused: AttachmentProblem | null;
14
+ canSend: boolean;
15
+ onChangeText: (text: string) => void;
16
+ attach: (() => void) | null;
17
+ remove: () => void;
18
+ submit: () => void;
19
+ }
20
+ export declare function useComposer({ onSend, onTyped, pickFile, initialText }: ComposerOptions): Composer;
@@ -0,0 +1,40 @@
1
+ import { useState } from 'react';
2
+ import { attachmentProblem } from "../helpers/attachments.js";
3
+ export function useComposer({ onSend, onTyped, pickFile, initialText = '' }) {
4
+ const [text, setText] = useState(initialText);
5
+ const [picked, setPicked] = useState(null);
6
+ const [refused, setRefused] = useState(null);
7
+ const attach = async (pick) => {
8
+ const file = await pick();
9
+ if (!file)
10
+ return;
11
+ const problem = attachmentProblem(file);
12
+ setRefused(problem);
13
+ if (!problem)
14
+ setPicked(file);
15
+ };
16
+ return {
17
+ text,
18
+ picked,
19
+ refused,
20
+ canSend: Boolean(text.trim() || picked),
21
+ onChangeText: (value) => {
22
+ setText(value);
23
+ onTyped(value);
24
+ },
25
+ attach: pickFile
26
+ ? () => {
27
+ void attach(pickFile);
28
+ }
29
+ : null,
30
+ remove: () => {
31
+ setPicked(null);
32
+ },
33
+ submit: () => {
34
+ onSend(text, picked);
35
+ setText('');
36
+ setPicked(null);
37
+ setRefused(null);
38
+ },
39
+ };
40
+ }
@@ -0,0 +1,80 @@
1
+ import { type Drug, type FieldValue, type LookupFailure, type Pharmacy, type StagedCard, type UsrxOrder, type UsrxOrderItem, type CardFields } from '../helpers/medicationOrder.ts';
2
+ import { type ChartsToPick, type DrugChoices, type FocusField, type Medicine, type PharmacyChange, type Picker } from '../helpers/orderForm.ts';
3
+ import type { OrderFormRequest } from './useOrdersMenu.ts';
4
+ /** Where a pharmacy search looks: a zip, or mail order. */
5
+ export interface PharmacyPlace {
6
+ zip: string;
7
+ mailOrder: boolean;
8
+ }
9
+ export interface OrderFormSource {
10
+ conversationId: number;
11
+ /** The signed-in staff member's skips_role. Only a role that reaches a pharmacy gets Send. */
12
+ role: string | null;
13
+ /** What the Orders menu asked the form to open on, or null for a blank new order. */
14
+ request: OrderFormRequest | null;
15
+ findDrugs: (conversationId: number, term: string) => Promise<Drug[]>;
16
+ readChoices: (conversationId: number, ddid: string) => Promise<DrugChoices>;
17
+ findPharmacies: (conversationId: number, place: PharmacyPlace) => Promise<Pharmacy[]>;
18
+ readCharts: (conversationId: number) => Promise<unknown>;
19
+ /** Changes a staged card, resolving usrx's answer: a refusal carries `refused` and `why`. */
20
+ updateCard: (conversationId: number, uid: string, fields: CardFields, pharmacy: PharmacyChange | null) => Promise<unknown>;
21
+ /** Saves a queued medicine whole, resolving usrx's answer as updateCard does. */
22
+ updateQueued: (conversationId: number, queuedId: string, item: UsrxOrderItem) => Promise<unknown>;
23
+ /** Posts the order note, a private note carrying content_attributes.usrx_order. */
24
+ postOrder: (conversationId: number, note: string, order: UsrxOrder) => Promise<unknown>;
25
+ /** Posts a private note, which Send uses for /treat-submit. */
26
+ sendNote: (conversationId: number, note: string) => Promise<unknown>;
27
+ /** Treat's cards changed, so the Orders menu reads them again. */
28
+ changed: () => void;
29
+ /** The form is done: the app closes it. */
30
+ close: () => void;
31
+ }
32
+ /** A field of a round the provider types or picks. */
33
+ export type ItemField = 'doseUnitId' | 'quantity' | 'frequencyId' | 'maxPerDay' | 'days' | 'dispenseUnitId' | 'dispenseAmount' | 'refills' | 'pharmacyNotes' | 'earliestFillDate' | 'indication';
34
+ export interface FocusTarget {
35
+ itemKey: string;
36
+ medicineKey: string;
37
+ field: FocusField;
38
+ }
39
+ export interface OrderFormView {
40
+ medicines: Medicine[];
41
+ /** The staged card the form changes, or null for a new order. */
42
+ editing: StagedCard | null;
43
+ /** Read only, with Edit and Send. */
44
+ viewing: boolean;
45
+ mayReachPharmacy: boolean;
46
+ /** Create, Update or Send may run. */
47
+ ready: boolean;
48
+ busy: boolean;
49
+ lookupError: LookupFailure | null;
50
+ /** usrx's words for a refused change, or ''. */
51
+ refusal: string;
52
+ /** The order note or the Send did not post. */
53
+ postFailed: boolean;
54
+ /** Treat's tied charts to pick one from, or null on the form. */
55
+ charts: ChartsToPick | null;
56
+ pickedChart: string;
57
+ /** Where the form opened at, once the choices loaded. */
58
+ focus: FocusTarget | null;
59
+ /** Pharmacy searches on their way, by medicine key. */
60
+ searchingPharmacies: ReadonlySet<string>;
61
+ typeTerm: (itemKey: string, term: string) => void;
62
+ pickDrug: (itemKey: string, drug: Drug) => void;
63
+ setField: (itemKey: string, field: ItemField, value: FieldValue) => void;
64
+ setSubstituteOk: (itemKey: string, substituteOk: boolean) => void;
65
+ setDiscontinueDuplicate: (itemKey: string, discontinueDuplicate: boolean) => void;
66
+ /** A new zip or mail order for a medicine's search, from the picker the form shows now. */
67
+ setPlace: (medicineKey: string, was: Picker, place: Partial<PharmacyPlace>) => void;
68
+ setWords: (medicineKey: string, words: string) => void;
69
+ pickPharmacy: (medicineKey: string, pharmacy: Pharmacy) => void;
70
+ addMedication: () => void;
71
+ removeMedication: (medicineKey: string) => void;
72
+ addRound: (medicineKey: string) => void;
73
+ removeRound: (medicineKey: string, itemKey: string) => void;
74
+ edit: () => void;
75
+ confirm: () => void;
76
+ pickChart: (uid: string) => void;
77
+ backToOrder: () => void;
78
+ }
79
+ /** The Order form: a new order, a staged card's change or Send, a queued medicine's save, or a sent order again. */
80
+ export declare function useOrderForm(source: OrderFormSource): OrderFormView;