@wtfalch/chat 0.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/LICENSE +21 -0
- package/README.md +293 -0
- package/dist/chat/admin/admin.css +78 -0
- package/dist/chat/admin/index.d.ts +92 -0
- package/dist/chat/admin/index.js +124 -0
- package/dist/chat/admin/react.d.ts +40 -0
- package/dist/chat/admin/react.js +169 -0
- package/dist/chat/chat.css +1003 -0
- package/dist/chat/client.d.ts +30 -0
- package/dist/chat/client.js +1300 -0
- package/dist/chat/exchange.d.ts +9 -0
- package/dist/chat/exchange.js +31 -0
- package/dist/chat/fake/index.d.ts +27 -0
- package/dist/chat/fake/index.js +48 -0
- package/dist/chat/fake/intercept.d.ts +21 -0
- package/dist/chat/fake/intercept.js +57 -0
- package/dist/chat/fake/sample.d.ts +4 -0
- package/dist/chat/fake/sample.js +6 -0
- package/dist/chat/fake/server.d.ts +168 -0
- package/dist/chat/fake/server.js +811 -0
- package/dist/chat/fake/xhr.d.ts +3 -0
- package/dist/chat/fake/xhr.js +104 -0
- package/dist/chat/index.d.ts +10 -0
- package/dist/chat/index.js +9 -0
- package/dist/chat/markdown.d.ts +29 -0
- package/dist/chat/markdown.js +161 -0
- package/dist/chat/react/Attachment.d.ts +39 -0
- package/dist/chat/react/Attachment.js +113 -0
- package/dist/chat/react/Chat.d.ts +48 -0
- package/dist/chat/react/Chat.js +139 -0
- package/dist/chat/react/Composer.d.ts +25 -0
- package/dist/chat/react/Composer.js +157 -0
- package/dist/chat/react/MessageRow.d.ts +21 -0
- package/dist/chat/react/MessageRow.js +114 -0
- package/dist/chat/react/NewDirectMessage.d.ts +23 -0
- package/dist/chat/react/NewDirectMessage.js +47 -0
- package/dist/chat/react/ThreadPanel.d.ts +25 -0
- package/dist/chat/react/ThreadPanel.js +17 -0
- package/dist/chat/react/Timeline.d.ts +27 -0
- package/dist/chat/react/Timeline.js +120 -0
- package/dist/chat/react/index.d.ts +7 -0
- package/dist/chat/react/index.js +6 -0
- package/dist/chat/react/mentions.d.ts +34 -0
- package/dist/chat/react/mentions.js +70 -0
- package/dist/chat/react/scroll.d.ts +26 -0
- package/dist/chat/react/scroll.js +27 -0
- package/dist/chat/react/timelineLayout.d.ts +26 -0
- package/dist/chat/react/timelineLayout.js +47 -0
- package/dist/chat/sanitize.d.ts +49 -0
- package/dist/chat/sanitize.js +264 -0
- package/dist/chat/store.d.ts +16 -0
- package/dist/chat/store.js +25 -0
- package/dist/chat/types.d.ts +340 -0
- package/dist/chat/types.js +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
import { Button, Field, Input, Select, Textarea } from '@wtfalch/design';
|
|
4
|
+
import { useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
|
5
|
+
/** The SDK owns the controls. Hosts pass authenticated actions, not tokens;
|
|
6
|
+
* every action must authorize its own caller before reaching the service. */
|
|
7
|
+
export function ChatAdminPanel({ actions }) {
|
|
8
|
+
const [view, setView] = useState(null);
|
|
9
|
+
const [selected, setSelected] = useState(null);
|
|
10
|
+
const [message, setMessage] = useState('');
|
|
11
|
+
const [failed, setFailed] = useState(false);
|
|
12
|
+
const [busy, setBusy] = useState(false);
|
|
13
|
+
const [cursor, setCursor] = useState();
|
|
14
|
+
const started = useRef(false);
|
|
15
|
+
const heading = useRef(null);
|
|
16
|
+
const [focusVersion, setFocusVersion] = useState(0);
|
|
17
|
+
useLayoutEffect(() => {
|
|
18
|
+
if (focusVersion)
|
|
19
|
+
heading.current?.focus();
|
|
20
|
+
}, [focusVersion]);
|
|
21
|
+
const locked = useRef(false);
|
|
22
|
+
const dirty = useRef(false);
|
|
23
|
+
function mayDiscard() {
|
|
24
|
+
return !dirty.current || window.confirm('Discard your unsaved channel changes?');
|
|
25
|
+
}
|
|
26
|
+
async function load(next) {
|
|
27
|
+
if (locked.current || !mayDiscard())
|
|
28
|
+
return;
|
|
29
|
+
locked.current = true;
|
|
30
|
+
setBusy(true);
|
|
31
|
+
try {
|
|
32
|
+
const result = await actions.load(next);
|
|
33
|
+
setView(result);
|
|
34
|
+
setCursor(next);
|
|
35
|
+
setSelected(null);
|
|
36
|
+
setFailed(false);
|
|
37
|
+
setMessage('');
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
setFailed(true);
|
|
41
|
+
setMessage('Chat settings could not be loaded. Check your access, then try again.');
|
|
42
|
+
}
|
|
43
|
+
finally {
|
|
44
|
+
locked.current = false;
|
|
45
|
+
setBusy(false);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
useEffect(() => {
|
|
49
|
+
if (started.current)
|
|
50
|
+
return;
|
|
51
|
+
started.current = true;
|
|
52
|
+
void load();
|
|
53
|
+
});
|
|
54
|
+
async function run(command) {
|
|
55
|
+
if (locked.current)
|
|
56
|
+
return false;
|
|
57
|
+
locked.current = true;
|
|
58
|
+
setBusy(true);
|
|
59
|
+
try {
|
|
60
|
+
const result = await actions.run(command);
|
|
61
|
+
setMessage(result.message);
|
|
62
|
+
setFailed(!result.ok);
|
|
63
|
+
if (!result.ok)
|
|
64
|
+
return false;
|
|
65
|
+
// A write may succeed even if this read fails. Never suggest retrying
|
|
66
|
+
// the mutation in that case (creating a channel is not idempotent).
|
|
67
|
+
try {
|
|
68
|
+
setView(await actions.load(cursor));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
setMessage(`${result.message} Refresh to see the latest settings.`);
|
|
72
|
+
}
|
|
73
|
+
if (command.operation === 'delete')
|
|
74
|
+
setSelected(null);
|
|
75
|
+
if (['delete', 'remove', 'deactivate'].includes(command.operation)) {
|
|
76
|
+
setFocusVersion((value) => value + 1);
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
setFailed(true);
|
|
82
|
+
setMessage('The result could not be confirmed. Refresh before trying the change again.');
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
locked.current = false;
|
|
87
|
+
setBusy(false);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const channel = view?.channels.find((item) => item.room_id === selected);
|
|
91
|
+
return (_jsxs("section", { className: "chat-admin", "aria-label": "Chat settings", "aria-busy": busy, children: [message && _jsx("p", { role: failed ? 'alert' : 'status', children: message }), _jsxs("div", { className: "chat-admin-toolbar", children: [_jsx("h2", { ref: heading, tabIndex: -1, children: "Channels" }), _jsx(Button, { disabled: busy, onPress: () => void load(cursor), children: "Refresh" })] }), !view ? (_jsx("p", { children: busy ? 'Loading chat settings…' : 'Settings are unavailable.' })) : (_jsx(_Fragment, { children: _jsxs("fieldset", { disabled: busy, className: "chat-admin-fields", children: [_jsxs("details", { children: [_jsx("summary", { children: "Create a channel" }), _jsx(ChannelForm, { onSubmit: (input) => run({ operation: 'create', input }) })] }), view.channels.length === 0 ? (_jsx("p", { children: "No managed channels on this page. The default general channel stays in Agora." })) : (_jsx(Field, { label: "Channel", children: _jsxs(Select, { value: selected ?? '', onChange: (event) => {
|
|
92
|
+
if (mayDiscard())
|
|
93
|
+
setSelected(event.target.value);
|
|
94
|
+
}, children: [_jsx("option", { value: "", children: "Choose a channel" }), view.channels.map((item) => (_jsx("option", { value: item.room_id, children: `${item.name}${item.archived ? ' · Archived' : ''}${item.visibility === 'private' ? ' · Private' : ''}` }, item.room_id)))] }) })), _jsxs("div", { className: "chat-admin-toolbar", children: [cursor && _jsx(Button, { onPress: () => void load(), children: "First page" }), view.next_cursor && (_jsx(Button, { onPress: () => void load(view.next_cursor ?? undefined), children: "Next channels" }))] }), channel && (_jsx(ChannelSettings, { channel: channel, people: view.people, run: run, onDirty: (value) => {
|
|
95
|
+
dirty.current = value;
|
|
96
|
+
} }, `${channel.room_id}:${channel.name}:${channel.topic}`)), _jsxs("details", { children: [_jsx("summary", { children: "Remove a person from Chat" }), _jsx("p", { children: "This ends their Chat sessions. Their existing messages remain." }), _jsx(RemovePerson, { people: view.people, run: run })] })] }) }))] }));
|
|
97
|
+
}
|
|
98
|
+
function ChannelForm({ channel, onSubmit, onDirty, }) {
|
|
99
|
+
const [name, setName] = useState(channel?.name ?? '');
|
|
100
|
+
const [topic, setTopic] = useState(channel?.topic ?? '');
|
|
101
|
+
const [visibility, setVisibility] = useState(channel?.visibility ?? 'public');
|
|
102
|
+
useEffect(() => {
|
|
103
|
+
onDirty?.(name !== (channel?.name ?? '') || topic !== (channel?.topic ?? ''));
|
|
104
|
+
return () => onDirty?.(false);
|
|
105
|
+
}, [name, topic, channel, onDirty]);
|
|
106
|
+
async function submit(event) {
|
|
107
|
+
event.preventDefault();
|
|
108
|
+
if ((await onSubmit({ name: name.trim(), topic, visibility })) && !channel) {
|
|
109
|
+
setName('');
|
|
110
|
+
setTopic('');
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return (_jsxs("form", { className: "chat-admin-form", onSubmit: (event) => void submit(event), children: [_jsx(Field, { label: "Name", required: true, children: _jsx(Input, { value: name, onChange: (event) => setName(event.target.value), maxLength: 255, required: true }) }), _jsx(Field, { label: "Topic", children: _jsx(Textarea, { value: topic, onChange: (event) => setTopic(event.target.value), maxLength: 4096 }) }), !channel && (_jsx(Field, { label: "Who can join", children: _jsxs(Select, { value: visibility, onChange: (event) => setVisibility(event.target.value), children: [_jsx("option", { value: "public", children: "Everyone in the company" }), _jsx("option", { value: "private", children: "Invited people only" })] }) })), _jsx(Button, { kind: "primary", type: "submit", disabled: !name.trim(), children: channel ? 'Save channel' : 'Create channel' })] }));
|
|
114
|
+
}
|
|
115
|
+
function ChannelSettings({ channel, people, run, onDirty, }) {
|
|
116
|
+
const [invite, setInvite] = useState('');
|
|
117
|
+
const available = people.filter((person) => !channel.people.some((member) => member.subject === person.subject));
|
|
118
|
+
return (_jsxs("section", { className: "chat-admin-section", "aria-label": channel.name, children: [_jsx("h3", { children: channel.name }), _jsx("p", { children: channel.archived
|
|
119
|
+
? 'Archived — people can read the channel but cannot post.'
|
|
120
|
+
: channel.visibility === 'private'
|
|
121
|
+
? 'Private — only invited people can join.'
|
|
122
|
+
: 'Open to everyone in the company.' }), _jsx(ChannelForm, { channel: channel, onDirty: onDirty, onSubmit: (input) => run({
|
|
123
|
+
operation: 'update',
|
|
124
|
+
roomId: channel.room_id,
|
|
125
|
+
name: input.name,
|
|
126
|
+
topic: input.topic ?? '',
|
|
127
|
+
}) }), channel.visibility === 'private' && (_jsxs(_Fragment, { children: [_jsx("h3", { children: "People in this channel" }), channel.people.length === 0 && _jsx("p", { children: "Nobody has been invited yet." }), _jsx("ul", { className: "chat-admin-people", children: channel.people.map((person) => {
|
|
128
|
+
const name = people.find((p) => p.subject === person.subject)?.name ??
|
|
129
|
+
person.display_name ??
|
|
130
|
+
person.subject;
|
|
131
|
+
return (_jsxs("li", { children: [_jsxs("span", { children: [name, person.membership === 'invite' ? ' · Invited' : ''] }), _jsx(Confirm, { label: `Remove ${name}`, consequence: `Remove ${name} from ${channel.name}?`, onConfirm: () => run({ operation: 'remove', roomId: channel.room_id, subject: person.subject }) })] }, person.subject));
|
|
132
|
+
}) }), available.length > 0 && (_jsxs("form", { className: "chat-admin-form", onSubmit: (event) => {
|
|
133
|
+
event.preventDefault();
|
|
134
|
+
void run({ operation: 'invite', roomId: channel.room_id, subject: invite }).then((ok) => {
|
|
135
|
+
if (ok)
|
|
136
|
+
setInvite('');
|
|
137
|
+
});
|
|
138
|
+
}, children: [_jsx(Field, { label: "Invite a person", children: _jsxs(Select, { value: invite, onChange: (event) => setInvite(event.target.value), children: [_jsx("option", { value: "", children: "Choose a person" }), available.map((person) => (_jsx("option", { value: person.subject, children: person.name }, person.subject)))] }) }), _jsx(Button, { type: "submit", disabled: !invite, children: "Send invitation" })] }))] })), _jsxs("div", { className: "chat-admin-toolbar", children: [!channel.archived && (_jsx(Confirm, { label: "Archive channel", consequence: "Archive this channel? People keep its history and can no longer post.", onConfirm: () => run({ operation: 'archive', roomId: channel.room_id }) })), _jsx(Confirm, { label: "Delete channel", consequence: "Delete this channel and its history? This cannot be undone.", onConfirm: () => run({ operation: 'delete', roomId: channel.room_id }) })] })] }));
|
|
139
|
+
}
|
|
140
|
+
function RemovePerson({ people, run, }) {
|
|
141
|
+
const [subject, setSubject] = useState('');
|
|
142
|
+
const name = people.find((person) => person.subject === subject)?.name;
|
|
143
|
+
return (_jsxs("div", { className: "chat-admin-form", children: [_jsx(Field, { label: "Person", children: _jsxs(Select, { value: subject, onChange: (event) => setSubject(event.target.value), children: [_jsx("option", { value: "", children: "Choose a person" }), people.map((person) => (_jsx("option", { value: person.subject, children: person.name }, person.subject)))] }) }), subject && (_jsx(Confirm, { label: "Remove from Chat", consequence: `End ${name}'s Chat sessions and remove their access?`, onConfirm: () => run({ operation: 'deactivate', subject }) }, subject))] }));
|
|
144
|
+
}
|
|
145
|
+
function Confirm({ label, consequence, onConfirm, }) {
|
|
146
|
+
const [confirming, setConfirming] = useState(false);
|
|
147
|
+
const trigger = useRef(null);
|
|
148
|
+
const cancelControl = useRef(null);
|
|
149
|
+
const descriptionId = useId();
|
|
150
|
+
const opened = useRef(false);
|
|
151
|
+
useLayoutEffect(() => {
|
|
152
|
+
if (confirming) {
|
|
153
|
+
opened.current = true;
|
|
154
|
+
cancelControl.current?.querySelector('button')?.focus();
|
|
155
|
+
}
|
|
156
|
+
else if (opened.current) {
|
|
157
|
+
trigger.current?.focus();
|
|
158
|
+
}
|
|
159
|
+
}, [confirming]);
|
|
160
|
+
function cancel() {
|
|
161
|
+
setConfirming(false);
|
|
162
|
+
}
|
|
163
|
+
return (_jsx("fieldset", { className: "chat-admin-confirm", "aria-label": label, "aria-describedby": confirming ? descriptionId : undefined, children: !confirming ? (_jsx("span", { ref: (node) => {
|
|
164
|
+
trigger.current = node?.querySelector('button') ?? null;
|
|
165
|
+
}, children: _jsx(Button, { kind: "danger", onPress: () => setConfirming(true), children: label }) })) : (_jsxs(_Fragment, { children: [_jsx("p", { id: descriptionId, children: consequence }), _jsxs(Button, { kind: "danger", onPress: () => void onConfirm().then((ok) => {
|
|
166
|
+
if (ok)
|
|
167
|
+
cancel();
|
|
168
|
+
}), children: ["Confirm ", label.toLowerCase()] }), _jsx("span", { ref: cancelControl, children: _jsx(Button, { onPress: cancel, children: "Cancel" }) })] })) }));
|
|
169
|
+
}
|