@12-apps/chat 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +194 -2
- package/package.json +95 -4
- package/prisma/chat.prisma +61 -0
- package/prisma/migrations/20261002120000_add_chat/migration.sql +41 -0
- package/prisma/migrations/20261003120000_add_chat_thread_activity_index/migration.sql +2 -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 +18 -0
- package/src/client/locales.ts +12 -0
- package/src/client/pt-BR.ts +17 -0
- package/src/client/use-chat-thread.ts +302 -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 +21 -0
- package/src/native/layout.tsx +96 -0
- package/src/react/composer.tsx +58 -0
- package/src/react/index.ts +20 -0
- package/src/react/layout.tsx +103 -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/bubble-tint.ts +19 -0
- package/src/ui/surface.tsx +87 -0
- package/src/ui/thread-view.tsx +225 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The surface factory both renderers share: config in, `{ ChatThread }` out.
|
|
3
|
+
* Each platform entry passes its own frame, message list and composer;
|
|
4
|
+
* everything else is common.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { useMemo, type JSX } from "react";
|
|
8
|
+
|
|
9
|
+
import { createChatClient, type ChatFetch } from "../client/api";
|
|
10
|
+
import { missingUiCopy } from "../client/assert";
|
|
11
|
+
import type { ChatUiCopy } from "../client/copy";
|
|
12
|
+
import { useChatThread, type ChatAutoMarkRead } from "../client/use-chat-thread";
|
|
13
|
+
import { ChatConfigError } from "../core/errors";
|
|
14
|
+
import { ChatThreadView, type ChatPlatform } from "./thread-view";
|
|
15
|
+
|
|
16
|
+
/** What a host supplies once. All REQUIRED. */
|
|
17
|
+
export interface ChatSurfaceConfig {
|
|
18
|
+
/** The host's credentialed fetch (cookie session, Bearer link, …). */
|
|
19
|
+
readonly fetch: ChatFetch;
|
|
20
|
+
/** Every rendered string. */
|
|
21
|
+
readonly copy: ChatUiCopy;
|
|
22
|
+
/** How a message's time reads — the host's locale and clock conventions. */
|
|
23
|
+
readonly formatTime: (iso: string) => string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ChatThreadProps {
|
|
27
|
+
/** Where this thread's three routes are mounted. */
|
|
28
|
+
readonly endpoint: string;
|
|
29
|
+
/** Change it to reload — the host's live channel says the thread moved. */
|
|
30
|
+
readonly refreshSignal?: unknown;
|
|
31
|
+
/** The unread count each load found, then what is left (0) once marked read. */
|
|
32
|
+
readonly onUnreadChange?: (unread: number) => void;
|
|
33
|
+
/**
|
|
34
|
+
* Mark the thread read once a load shows unread messages. Default true.
|
|
35
|
+
* A host that mounts the thread hidden only to drive a badge passes false,
|
|
36
|
+
* so nothing is marked read that nobody saw. `"visible"` waits until the
|
|
37
|
+
* message list is on screen: for a thread mounted below the fold (a drawer's
|
|
38
|
+
* lower half), so mounting it is not reading it. On the web that is an
|
|
39
|
+
* IntersectionObserver on the list; on native the list is on screen once
|
|
40
|
+
* it is mounted.
|
|
41
|
+
*/
|
|
42
|
+
readonly autoMarkRead?: ChatAutoMarkRead;
|
|
43
|
+
/**
|
|
44
|
+
* Native only: the distance from the window top to the thread (a navigator
|
|
45
|
+
* header), in dp, so the composer clears the keyboard exactly. Default 0.
|
|
46
|
+
*/
|
|
47
|
+
readonly keyboardOffset?: number;
|
|
48
|
+
readonly testID?: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface ChatSurface {
|
|
52
|
+
ChatThread: (props: ChatThreadProps) => JSX.Element;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function buildChatSurface(config: ChatSurfaceConfig, platform: ChatPlatform): ChatSurface {
|
|
56
|
+
if (typeof config?.fetch !== "function") throw new ChatConfigError("fetch is required — the host's credentialed client.");
|
|
57
|
+
if (typeof config.formatTime !== "function") throw new ChatConfigError("formatTime is required — time formatting is the host's.");
|
|
58
|
+
const missing = missingUiCopy(config.copy);
|
|
59
|
+
if (missing.length > 0) {
|
|
60
|
+
throw new ChatConfigError(`copy is required, with every key non-blank — missing: ${missing.join(", ")}.`);
|
|
61
|
+
}
|
|
62
|
+
const { fetch, copy, formatTime } = config;
|
|
63
|
+
|
|
64
|
+
function ChatThread(props: ChatThreadProps): JSX.Element {
|
|
65
|
+
const client = useMemo(() => createChatClient({ fetch, endpoint: props.endpoint }), [props.endpoint]);
|
|
66
|
+
const controls = useChatThread({
|
|
67
|
+
client,
|
|
68
|
+
refreshSignal: props.refreshSignal,
|
|
69
|
+
onUnreadChange: props.onUnreadChange,
|
|
70
|
+
autoMarkRead: props.autoMarkRead,
|
|
71
|
+
});
|
|
72
|
+
// Keyed by endpoint: another thread starts with an empty draft, not this one's.
|
|
73
|
+
return (
|
|
74
|
+
<ChatThreadView
|
|
75
|
+
key={props.endpoint}
|
|
76
|
+
controls={controls}
|
|
77
|
+
copy={copy}
|
|
78
|
+
formatTime={formatTime}
|
|
79
|
+
platform={platform}
|
|
80
|
+
keyboardOffset={props.keyboardOffset}
|
|
81
|
+
testID={props.testID}
|
|
82
|
+
/>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return { ChatThread };
|
|
87
|
+
}
|
|
@@ -0,0 +1,225 @@
|
|
|
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, a message's bubble (its surface is styled beyond the
|
|
10
|
+
* shared props), and the composer, whose change events are not the 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
|
+
* Set only when the thread marks itself read once SEEN (`autoMarkRead="visible"`):
|
|
51
|
+
* the list reports whether it is on screen now, and `false` when it unmounts.
|
|
52
|
+
*/
|
|
53
|
+
readonly onVisibleChange?: (visible: boolean) => void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What a platform's bubble receives: whose message it is, and its label, time and text. */
|
|
57
|
+
export interface ChatBubbleProps {
|
|
58
|
+
/** The reader's own message: drawn on a tint of the theme's primary colour, others on paper. */
|
|
59
|
+
readonly mine: boolean;
|
|
60
|
+
readonly children: ReactNode;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What a platform's frame receives: the list above, the writing area pinned below. */
|
|
64
|
+
export interface ChatFrameProps {
|
|
65
|
+
readonly children: ReactNode;
|
|
66
|
+
readonly testID: string;
|
|
67
|
+
/** Native only: the distance from the window top to the thread (a navigator header), in dp. */
|
|
68
|
+
readonly keyboardOffset?: number;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The per-platform pieces of the thread. */
|
|
72
|
+
export interface ChatPlatform {
|
|
73
|
+
readonly Frame: ComponentType<ChatFrameProps>;
|
|
74
|
+
readonly MessageList: ComponentType<ChatMessageListProps>;
|
|
75
|
+
readonly Bubble: ComponentType<ChatBubbleProps>;
|
|
76
|
+
readonly Composer: ComponentType<ChatComposerProps>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
interface ChatThreadViewProps {
|
|
80
|
+
readonly controls: ChatThreadControls;
|
|
81
|
+
readonly copy: ChatUiCopy;
|
|
82
|
+
readonly formatTime: (iso: string) => string;
|
|
83
|
+
readonly platform: ChatPlatform;
|
|
84
|
+
readonly keyboardOffset?: number;
|
|
85
|
+
readonly testID?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function Message(props: {
|
|
89
|
+
message: ChatWireMessage;
|
|
90
|
+
formatTime: (iso: string) => string;
|
|
91
|
+
Bubble: ChatPlatform["Bubble"];
|
|
92
|
+
}): JSX.Element {
|
|
93
|
+
const { message, formatTime, Bubble } = props;
|
|
94
|
+
return (
|
|
95
|
+
<Box direction="row" justify={message.mine ? "end" : "start"} testID={`chat-message-${message.id}`}>
|
|
96
|
+
<Bubble mine={message.mine}>
|
|
97
|
+
<Stack direction="row" gap={1} align="baseline">
|
|
98
|
+
{/* Body-text colour on the own tint: the primary colour on its own tint falls under 4.5:1. */}
|
|
99
|
+
<Text variant="caption" weight="semibold" color={message.mine ? "neutral" : "secondary"}>
|
|
100
|
+
{message.label}
|
|
101
|
+
</Text>
|
|
102
|
+
<Text variant="caption" color="secondary">
|
|
103
|
+
{formatTime(message.createdAt)}
|
|
104
|
+
</Text>
|
|
105
|
+
</Stack>
|
|
106
|
+
<Text>{message.body}</Text>
|
|
107
|
+
</Bubble>
|
|
108
|
+
</Box>
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function QuickReplies(props: {
|
|
113
|
+
replies: readonly ChatQuickReply[];
|
|
114
|
+
disabled: boolean;
|
|
115
|
+
copy: ChatUiCopy;
|
|
116
|
+
onPick: (key: string) => void;
|
|
117
|
+
}): JSX.Element | null {
|
|
118
|
+
if (props.replies.length === 0) return null;
|
|
119
|
+
return (
|
|
120
|
+
<Stack gap={1} testID="chat-quick-replies">
|
|
121
|
+
<Text variant="caption" color="secondary">
|
|
122
|
+
{props.copy.quickReplies}
|
|
123
|
+
</Text>
|
|
124
|
+
<Box direction="row" wrap gap={1}>
|
|
125
|
+
{props.replies.map((reply) => (
|
|
126
|
+
<Chip
|
|
127
|
+
key={reply.key}
|
|
128
|
+
label={reply.text}
|
|
129
|
+
variant="outlined"
|
|
130
|
+
disabled={props.disabled}
|
|
131
|
+
onClick={() => props.onPick(reply.key)}
|
|
132
|
+
testID={`chat-quick-${reply.key}`}
|
|
133
|
+
/>
|
|
134
|
+
))}
|
|
135
|
+
</Box>
|
|
136
|
+
</Stack>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function Writing(props: ChatThreadViewProps): JSX.Element {
|
|
141
|
+
const { controls, copy, platform } = props;
|
|
142
|
+
const { Composer } = platform;
|
|
143
|
+
const [draft, setDraft] = useState("");
|
|
144
|
+
const thread = controls.payload?.thread;
|
|
145
|
+
if (!thread) return <></>;
|
|
146
|
+
if (!thread.canWrite) {
|
|
147
|
+
return <Alert variant="info" description={copy.closed} testID="chat-closed" />;
|
|
148
|
+
}
|
|
149
|
+
const submit = async (): Promise<void> => {
|
|
150
|
+
if (draft.trim() === "" || controls.sending) return;
|
|
151
|
+
const sent = draft;
|
|
152
|
+
// Only what was sent is cleared: the field stays live during the send.
|
|
153
|
+
if (await controls.send(sent)) setDraft((current) => (current === sent ? "" : current));
|
|
154
|
+
};
|
|
155
|
+
return (
|
|
156
|
+
<Stack gap={2}>
|
|
157
|
+
{controls.sendFailure ? (
|
|
158
|
+
<Alert variant="danger" description={controls.sendFailure.message ?? copy.sendFailed} testID="chat-send-failed" />
|
|
159
|
+
) : null}
|
|
160
|
+
<QuickReplies
|
|
161
|
+
replies={thread.quickReplies}
|
|
162
|
+
disabled={controls.sending}
|
|
163
|
+
copy={copy}
|
|
164
|
+
onPick={(key) => void controls.sendQuickReply(key)}
|
|
165
|
+
/>
|
|
166
|
+
{thread.freeText ? (
|
|
167
|
+
<Composer
|
|
168
|
+
value={draft}
|
|
169
|
+
onChange={setDraft}
|
|
170
|
+
onSubmit={() => void submit()}
|
|
171
|
+
sending={controls.sending}
|
|
172
|
+
maxLength={thread.maxLength}
|
|
173
|
+
copy={copy}
|
|
174
|
+
/>
|
|
175
|
+
) : null}
|
|
176
|
+
</Stack>
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function Messages(props: ChatThreadViewProps & { messages: readonly ChatWireMessage[] }): JSX.Element {
|
|
181
|
+
const { copy, formatTime, messages } = props;
|
|
182
|
+
const { MessageList, Bubble } = props.platform;
|
|
183
|
+
const newest = messages.at(-1);
|
|
184
|
+
return (
|
|
185
|
+
<MessageList
|
|
186
|
+
label={copy.messagesLabel}
|
|
187
|
+
scrollKey={`${messages.length}:${newest?.id ?? ""}`}
|
|
188
|
+
testID="chat-messages"
|
|
189
|
+
onVisibleChange={props.controls.onVisibleChange}
|
|
190
|
+
>
|
|
191
|
+
{messages.length === 0 ? (
|
|
192
|
+
<EmptyState variant="minimal" title={copy.emptyTitle} description={copy.emptyDescription} testID="chat-empty" />
|
|
193
|
+
) : (
|
|
194
|
+
messages.map((message) => <Message key={message.id} message={message} formatTime={formatTime} Bubble={Bubble} />)
|
|
195
|
+
)}
|
|
196
|
+
</MessageList>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function ChatThreadView(props: ChatThreadViewProps): JSX.Element {
|
|
201
|
+
const { controls, copy } = props;
|
|
202
|
+
const { Frame } = props.platform;
|
|
203
|
+
if (controls.phase === "loading") {
|
|
204
|
+
return <LoadingState message={copy.loading} testID="chat-loading" />;
|
|
205
|
+
}
|
|
206
|
+
if (controls.phase === "error" || !controls.payload) {
|
|
207
|
+
return (
|
|
208
|
+
<ErrorState
|
|
209
|
+
message={controls.loadFailure ?? copy.loadError}
|
|
210
|
+
onRetry={controls.reload}
|
|
211
|
+
retryLabel={copy.retry}
|
|
212
|
+
testID="chat-error"
|
|
213
|
+
/>
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
const { thread, messages } = controls.payload;
|
|
217
|
+
// A closed thread nobody wrote in has nothing to show and nothing to invite: only the notice.
|
|
218
|
+
const nothingToShow = !thread.canWrite && messages.length === 0;
|
|
219
|
+
return (
|
|
220
|
+
<Frame testID={props.testID ?? "chat-thread"} keyboardOffset={props.keyboardOffset}>
|
|
221
|
+
{nothingToShow ? null : <Messages {...props} messages={messages} />}
|
|
222
|
+
<Writing {...props} />
|
|
223
|
+
</Frame>
|
|
224
|
+
);
|
|
225
|
+
}
|