@12-apps/chat 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +153 -2
- package/package.json +95 -4
- package/prisma/chat.prisma +59 -0
- package/prisma/migrations/20261002120000_add_chat/migration.sql +41 -0
- package/scripts/sync-chat-schema.mjs +64 -0
- package/src/client/api.ts +146 -0
- package/src/client/assert.ts +7 -0
- package/src/client/copy.ts +43 -0
- package/src/client/en-US.ts +17 -0
- package/src/client/index.ts +17 -0
- package/src/client/locales.ts +12 -0
- package/src/client/pt-BR.ts +17 -0
- package/src/client/use-chat-thread.ts +232 -0
- package/src/core/contact-views.ts +231 -0
- package/src/core/contact.ts +333 -0
- package/src/core/errors.ts +9 -0
- package/src/core/types.ts +82 -0
- package/src/index.ts +26 -0
- package/src/manifest/index.ts +20 -0
- package/src/manifest/native.ts +13 -0
- package/src/manifest/server.ts +14 -0
- package/src/manifest/web.ts +14 -0
- package/src/native/composer.tsx +52 -0
- package/src/native/index.ts +16 -0
- package/src/native/layout.tsx +60 -0
- package/src/react/composer.tsx +48 -0
- package/src/react/index.ts +15 -0
- package/src/react/layout.tsx +36 -0
- package/src/server/context.ts +228 -0
- package/src/server/copy.ts +73 -0
- package/src/server/db.ts +87 -0
- package/src/server/en-US.ts +14 -0
- package/src/server/handler.ts +110 -0
- package/src/server/index.ts +34 -0
- package/src/server/locales.ts +12 -0
- package/src/server/pt-BR.ts +14 -0
- package/src/server/rate.ts +34 -0
- package/src/server/reads.ts +52 -0
- package/src/server/routes.ts +119 -0
- package/src/server/send.ts +203 -0
- package/src/ui/surface.tsx +83 -0
- package/src/ui/thread-view.tsx +198 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The thread, drawn once for both renderers.
|
|
3
|
+
*
|
|
4
|
+
* Built only from `@12-apps/ui` primitives and only their cross-platform
|
|
5
|
+
* (`*.base`) props, so the same tree renders on the web (MUI) and in a React
|
|
6
|
+
* Native app (the package's `react-native` export condition resolves each
|
|
7
|
+
* primitive to its native twin). The pieces that differ per platform arrive
|
|
8
|
+
* as a {@link ChatPlatform}: the frame (keyboard avoidance on native), the
|
|
9
|
+
* scrolling message list, and the composer, whose change events are not the
|
|
10
|
+
* same shape.
|
|
11
|
+
*
|
|
12
|
+
* Every word on screen is host copy or server data: role labels come from the
|
|
13
|
+
* server (the host's role config), the rest from {@link ChatUiCopy}.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { Alert } from "@12-apps/ui/data-display/Alert";
|
|
17
|
+
import { Chip } from "@12-apps/ui/data-display/Chip";
|
|
18
|
+
import { EmptyState } from "@12-apps/ui/data-display/EmptyState";
|
|
19
|
+
import { ErrorState } from "@12-apps/ui/data-display/ErrorState";
|
|
20
|
+
import { LoadingState } from "@12-apps/ui/data-display/LoadingState";
|
|
21
|
+
import { Box } from "@12-apps/ui/layout/Box";
|
|
22
|
+
import { Stack } from "@12-apps/ui/layout/Stack";
|
|
23
|
+
import { Text } from "@12-apps/ui/typography/Text";
|
|
24
|
+
import { useState, type ComponentType, type JSX, type ReactNode } from "react";
|
|
25
|
+
|
|
26
|
+
import type { ChatThreadControls } from "../client/use-chat-thread";
|
|
27
|
+
import type { ChatUiCopy } from "../client/copy";
|
|
28
|
+
import type { ChatQuickReply, ChatWireMessage } from "../core/types";
|
|
29
|
+
|
|
30
|
+
/** What a platform's composer receives: a controlled field plus its send action. */
|
|
31
|
+
export interface ChatComposerProps {
|
|
32
|
+
readonly value: string;
|
|
33
|
+
readonly onChange: (text: string) => void;
|
|
34
|
+
readonly onSubmit: () => void;
|
|
35
|
+
/** A message is in flight: the send action is gated, the FIELD stays usable. */
|
|
36
|
+
readonly sending: boolean;
|
|
37
|
+
readonly maxLength: number;
|
|
38
|
+
readonly copy: ChatUiCopy;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** What a platform's message list receives: the bubbles, and when to follow the newest. */
|
|
42
|
+
export interface ChatMessageListProps {
|
|
43
|
+
readonly children: ReactNode;
|
|
44
|
+
/** The list's accessible name. */
|
|
45
|
+
readonly label: string;
|
|
46
|
+
/** Changes whenever a message is added — the list scrolls to its end on each change. */
|
|
47
|
+
readonly scrollKey: string;
|
|
48
|
+
readonly testID: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** What a platform's frame receives: the list above, the writing area pinned below. */
|
|
52
|
+
export interface ChatFrameProps {
|
|
53
|
+
readonly children: ReactNode;
|
|
54
|
+
readonly testID: string;
|
|
55
|
+
/** Native only: the distance from the window top to the thread (a navigator header), in dp. */
|
|
56
|
+
readonly keyboardOffset?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** The per-platform pieces of the thread. */
|
|
60
|
+
export interface ChatPlatform {
|
|
61
|
+
readonly Frame: ComponentType<ChatFrameProps>;
|
|
62
|
+
readonly MessageList: ComponentType<ChatMessageListProps>;
|
|
63
|
+
readonly Composer: ComponentType<ChatComposerProps>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface ChatThreadViewProps {
|
|
67
|
+
readonly controls: ChatThreadControls;
|
|
68
|
+
readonly copy: ChatUiCopy;
|
|
69
|
+
readonly formatTime: (iso: string) => string;
|
|
70
|
+
readonly platform: ChatPlatform;
|
|
71
|
+
readonly keyboardOffset?: number;
|
|
72
|
+
readonly testID?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function Bubble({ message, formatTime }: { message: ChatWireMessage; formatTime: (iso: string) => string }): JSX.Element {
|
|
76
|
+
return (
|
|
77
|
+
<Box direction="row" justify={message.mine ? "end" : "start"} testID={`chat-message-${message.id}`}>
|
|
78
|
+
<Box bg={message.mine ? "default" : "paper"} bordered radius="md" px={2} py={1} gap={0.5}>
|
|
79
|
+
<Stack direction="row" gap={1} align="baseline">
|
|
80
|
+
<Text variant="caption" weight="semibold" color={message.mine ? "primary" : "secondary"}>
|
|
81
|
+
{message.label}
|
|
82
|
+
</Text>
|
|
83
|
+
<Text variant="caption" color="secondary">
|
|
84
|
+
{formatTime(message.createdAt)}
|
|
85
|
+
</Text>
|
|
86
|
+
</Stack>
|
|
87
|
+
<Text>{message.body}</Text>
|
|
88
|
+
</Box>
|
|
89
|
+
</Box>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function QuickReplies(props: {
|
|
94
|
+
replies: readonly ChatQuickReply[];
|
|
95
|
+
disabled: boolean;
|
|
96
|
+
copy: ChatUiCopy;
|
|
97
|
+
onPick: (key: string) => void;
|
|
98
|
+
}): JSX.Element | null {
|
|
99
|
+
if (props.replies.length === 0) return null;
|
|
100
|
+
return (
|
|
101
|
+
<Stack gap={1} testID="chat-quick-replies">
|
|
102
|
+
<Text variant="caption" color="secondary">
|
|
103
|
+
{props.copy.quickReplies}
|
|
104
|
+
</Text>
|
|
105
|
+
<Box direction="row" wrap gap={1}>
|
|
106
|
+
{props.replies.map((reply) => (
|
|
107
|
+
<Chip
|
|
108
|
+
key={reply.key}
|
|
109
|
+
label={reply.text}
|
|
110
|
+
variant="outlined"
|
|
111
|
+
disabled={props.disabled}
|
|
112
|
+
onClick={() => props.onPick(reply.key)}
|
|
113
|
+
testID={`chat-quick-${reply.key}`}
|
|
114
|
+
/>
|
|
115
|
+
))}
|
|
116
|
+
</Box>
|
|
117
|
+
</Stack>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function Writing(props: ChatThreadViewProps): JSX.Element {
|
|
122
|
+
const { controls, copy, platform } = props;
|
|
123
|
+
const { Composer } = platform;
|
|
124
|
+
const [draft, setDraft] = useState("");
|
|
125
|
+
const thread = controls.payload?.thread;
|
|
126
|
+
if (!thread) return <></>;
|
|
127
|
+
if (!thread.canWrite) {
|
|
128
|
+
return <Alert variant="info" description={copy.closed} testID="chat-closed" />;
|
|
129
|
+
}
|
|
130
|
+
const submit = async (): Promise<void> => {
|
|
131
|
+
if (draft.trim() === "" || controls.sending) return;
|
|
132
|
+
const sent = draft;
|
|
133
|
+
// Only what was sent is cleared: the field stays live during the send.
|
|
134
|
+
if (await controls.send(sent)) setDraft((current) => (current === sent ? "" : current));
|
|
135
|
+
};
|
|
136
|
+
return (
|
|
137
|
+
<Stack gap={2}>
|
|
138
|
+
{controls.sendFailure ? (
|
|
139
|
+
<Alert variant="danger" description={controls.sendFailure.message ?? copy.sendFailed} testID="chat-send-failed" />
|
|
140
|
+
) : null}
|
|
141
|
+
<QuickReplies
|
|
142
|
+
replies={thread.quickReplies}
|
|
143
|
+
disabled={controls.sending}
|
|
144
|
+
copy={copy}
|
|
145
|
+
onPick={(key) => void controls.sendQuickReply(key)}
|
|
146
|
+
/>
|
|
147
|
+
{thread.freeText ? (
|
|
148
|
+
<Composer
|
|
149
|
+
value={draft}
|
|
150
|
+
onChange={setDraft}
|
|
151
|
+
onSubmit={() => void submit()}
|
|
152
|
+
sending={controls.sending}
|
|
153
|
+
maxLength={thread.maxLength}
|
|
154
|
+
copy={copy}
|
|
155
|
+
/>
|
|
156
|
+
) : null}
|
|
157
|
+
</Stack>
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function Messages(props: ChatThreadViewProps & { messages: readonly ChatWireMessage[] }): JSX.Element {
|
|
162
|
+
const { copy, formatTime, messages } = props;
|
|
163
|
+
const { MessageList } = props.platform;
|
|
164
|
+
const newest = messages.at(-1);
|
|
165
|
+
return (
|
|
166
|
+
<MessageList label={copy.messagesLabel} scrollKey={`${messages.length}:${newest?.id ?? ""}`} testID="chat-messages">
|
|
167
|
+
{messages.length === 0 ? (
|
|
168
|
+
<EmptyState variant="minimal" title={copy.emptyTitle} description={copy.emptyDescription} testID="chat-empty" />
|
|
169
|
+
) : (
|
|
170
|
+
messages.map((message) => <Bubble key={message.id} message={message} formatTime={formatTime} />)
|
|
171
|
+
)}
|
|
172
|
+
</MessageList>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function ChatThreadView(props: ChatThreadViewProps): JSX.Element {
|
|
177
|
+
const { controls, copy } = props;
|
|
178
|
+
const { Frame } = props.platform;
|
|
179
|
+
if (controls.phase === "loading") {
|
|
180
|
+
return <LoadingState message={copy.loading} testID="chat-loading" />;
|
|
181
|
+
}
|
|
182
|
+
if (controls.phase === "error" || !controls.payload) {
|
|
183
|
+
return (
|
|
184
|
+
<ErrorState
|
|
185
|
+
message={controls.loadFailure ?? copy.loadError}
|
|
186
|
+
onRetry={controls.reload}
|
|
187
|
+
retryLabel={copy.retry}
|
|
188
|
+
testID="chat-error"
|
|
189
|
+
/>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
return (
|
|
193
|
+
<Frame testID={props.testID ?? "chat-thread"} keyboardOffset={props.keyboardOffset}>
|
|
194
|
+
<Messages {...props} messages={controls.payload.messages} />
|
|
195
|
+
<Writing {...props} />
|
|
196
|
+
</Frame>
|
|
197
|
+
);
|
|
198
|
+
}
|