g-comments 0.0.0-stage → 0.2.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/CHANGELOG.md +48 -0
- package/LICENSE +21 -0
- package/README.md +389 -2
- package/dist/chunk-IBNNENCZ.cjs +3089 -0
- package/dist/chunk-IBNNENCZ.cjs.map +1 -0
- package/dist/chunk-WACVXJHD.js +3068 -0
- package/dist/chunk-WACVXJHD.js.map +1 -0
- package/dist/element.d.ts +823 -0
- package/dist/element.js +1230 -0
- package/dist/element.js.map +1 -0
- package/dist/index.cjs +68 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +37 -0
- package/dist/index.d.ts +37 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/messages-C9bMljl3.d.cts +699 -0
- package/dist/messages-C9bMljl3.d.ts +699 -0
- package/dist/react.cjs +2754 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +335 -0
- package/dist/react.d.ts +335 -0
- package/dist/react.js +2745 -0
- package/dist/react.js.map +1 -0
- package/package.json +90 -4
- package/styles/styles.css +1145 -0
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,2754 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
var chunkIBNNENCZ_cjs = require('./chunk-IBNNENCZ.cjs');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
|
|
8
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
9
|
+
function toCommentsError(error) {
|
|
10
|
+
if (chunkIBNNENCZ_cjs.isCommentsError(error)) {
|
|
11
|
+
return error;
|
|
12
|
+
}
|
|
13
|
+
return new chunkIBNNENCZ_cjs.CommentsError("unknown", error instanceof Error ? error.message : String(error), {
|
|
14
|
+
cause: error
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function noop() {
|
|
18
|
+
}
|
|
19
|
+
function rootNodeOf(element) {
|
|
20
|
+
const root = element.getRootNode();
|
|
21
|
+
return root instanceof Document || root instanceof ShadowRoot ? root : element.ownerDocument;
|
|
22
|
+
}
|
|
23
|
+
function isLocalPage() {
|
|
24
|
+
if (typeof window === "undefined") {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const host = window.location.hostname;
|
|
28
|
+
return host === "localhost" || host === "127.0.0.1" || host === "[::1]" || host.endsWith(".localhost") || host.endsWith(".test");
|
|
29
|
+
}
|
|
30
|
+
function readDraft(key) {
|
|
31
|
+
try {
|
|
32
|
+
return globalThis.localStorage?.getItem(key) ?? "";
|
|
33
|
+
} catch {
|
|
34
|
+
return "";
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function writeDraft(key, value) {
|
|
38
|
+
try {
|
|
39
|
+
if (value.trim() === "") {
|
|
40
|
+
globalThis.localStorage?.removeItem(key);
|
|
41
|
+
} else {
|
|
42
|
+
globalThis.localStorage?.setItem(key, value);
|
|
43
|
+
}
|
|
44
|
+
} catch {
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
var tickListeners = /* @__PURE__ */ new Set();
|
|
48
|
+
var tickTimer;
|
|
49
|
+
function onMinuteTick(listener) {
|
|
50
|
+
tickListeners.add(listener);
|
|
51
|
+
tickTimer ??= setInterval(() => {
|
|
52
|
+
for (const tick of [...tickListeners]) {
|
|
53
|
+
tick();
|
|
54
|
+
}
|
|
55
|
+
}, 6e4);
|
|
56
|
+
return () => {
|
|
57
|
+
tickListeners.delete(listener);
|
|
58
|
+
if (tickListeners.size === 0) {
|
|
59
|
+
clearInterval(tickTimer);
|
|
60
|
+
tickTimer = void 0;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
var UNITS = [
|
|
65
|
+
["year", 31536e3],
|
|
66
|
+
["month", 2592e3],
|
|
67
|
+
["week", 604800],
|
|
68
|
+
["day", 86400],
|
|
69
|
+
["hour", 3600],
|
|
70
|
+
["minute", 60]
|
|
71
|
+
];
|
|
72
|
+
function formatRelative(iso, now, locale) {
|
|
73
|
+
const seconds = Math.round((Date.parse(iso) - now) / 1e3);
|
|
74
|
+
const format = new Intl.RelativeTimeFormat(locale, { numeric: "auto" });
|
|
75
|
+
for (const [unit, size] of UNITS) {
|
|
76
|
+
if (Math.abs(seconds) >= size) {
|
|
77
|
+
return format.format(Math.round(seconds / size), unit);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return format.format(0, "second");
|
|
81
|
+
}
|
|
82
|
+
function formatAbsolute(iso, locale) {
|
|
83
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium", timeStyle: "short" }).format(
|
|
84
|
+
new Date(iso)
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// src/react/context.ts
|
|
89
|
+
var CommentsContext = react.createContext(null);
|
|
90
|
+
var defaultTranslator = chunkIBNNENCZ_cjs.createTranslator();
|
|
91
|
+
function useCommentsContext(forum) {
|
|
92
|
+
const value = useOptionalCommentsContext(forum);
|
|
93
|
+
if (!value) {
|
|
94
|
+
throw new Error(
|
|
95
|
+
`${chunkIBNNENCZ_cjs.PACKAGE_NAME}: render this inside <DiscourseComments forum="\u2026"> or pass a \`forum\` prop`
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
function useOptionalCommentsContext(forum) {
|
|
101
|
+
const context = react.useContext(CommentsContext);
|
|
102
|
+
const standalone = react.useMemo(() => {
|
|
103
|
+
if (forum === void 0 || context && chunkIBNNENCZ_cjs.normalizeForum(forum).base === context.client.forum) {
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
client: chunkIBNNENCZ_cjs.clientRegistry.acquire({ forum }),
|
|
108
|
+
t: context?.t ?? defaultTranslator,
|
|
109
|
+
locale: context?.locale ?? chunkIBNNENCZ_cjs.resolveLocale(void 0),
|
|
110
|
+
messages: context?.messages,
|
|
111
|
+
theme: context?.theme ?? "auto",
|
|
112
|
+
appearance: context?.appearance,
|
|
113
|
+
debug: context?.debug ?? false,
|
|
114
|
+
emit: context?.emit ?? noop
|
|
115
|
+
};
|
|
116
|
+
}, [forum, context]);
|
|
117
|
+
return standalone ?? context;
|
|
118
|
+
}
|
|
119
|
+
function useClient() {
|
|
120
|
+
const context = react.useContext(CommentsContext);
|
|
121
|
+
if (!context) {
|
|
122
|
+
throw new Error(`${chunkIBNNENCZ_cjs.PACKAGE_NAME}: useClient() needs a <DiscourseComments> provider`);
|
|
123
|
+
}
|
|
124
|
+
return context.client;
|
|
125
|
+
}
|
|
126
|
+
var QUIET_AUTH_ERRORS = /* @__PURE__ */ new Set(["cancelled", "invalid_key"]);
|
|
127
|
+
function DiscourseComments(props) {
|
|
128
|
+
const { children, appearance, locale, messages, theme = "auto", debug = false, onEvent } = props;
|
|
129
|
+
const explicitClient = props.client;
|
|
130
|
+
const forum = props.forum;
|
|
131
|
+
const creation = props.forum === void 0 ? null : {
|
|
132
|
+
forum: props.forum,
|
|
133
|
+
fetch: props.fetch,
|
|
134
|
+
headers: props.headers,
|
|
135
|
+
storage: props.storage,
|
|
136
|
+
clientId: props.clientId,
|
|
137
|
+
locale,
|
|
138
|
+
debug
|
|
139
|
+
};
|
|
140
|
+
const client = react.useMemo(
|
|
141
|
+
() => explicitClient ?? chunkIBNNENCZ_cjs.clientRegistry.acquire(creation),
|
|
142
|
+
[explicitClient, forum]
|
|
143
|
+
);
|
|
144
|
+
const messagesKey = messages ? JSON.stringify(messages) : "";
|
|
145
|
+
const t = react.useMemo(() => chunkIBNNENCZ_cjs.createTranslator(locale, messages), [locale, messagesKey]);
|
|
146
|
+
const onEventRef = react.useRef(onEvent);
|
|
147
|
+
react.useEffect(() => {
|
|
148
|
+
onEventRef.current = onEvent;
|
|
149
|
+
});
|
|
150
|
+
const value = react.useMemo(
|
|
151
|
+
() => ({
|
|
152
|
+
client,
|
|
153
|
+
t,
|
|
154
|
+
locale: chunkIBNNENCZ_cjs.resolveLocale(locale),
|
|
155
|
+
messages,
|
|
156
|
+
theme,
|
|
157
|
+
appearance,
|
|
158
|
+
debug,
|
|
159
|
+
emit: (event) => onEventRef.current?.(event)
|
|
160
|
+
}),
|
|
161
|
+
[client, t, locale, messagesKey, theme, appearance, debug]
|
|
162
|
+
);
|
|
163
|
+
react.useEffect(() => {
|
|
164
|
+
let previous = client.auth.state();
|
|
165
|
+
return client.auth.onChange((state) => {
|
|
166
|
+
const wasSignedIn = previous.status === "signed-in";
|
|
167
|
+
if (state.status === "signed-in" && state.user && !wasSignedIn) {
|
|
168
|
+
onEventRef.current?.({ type: "auth.signedIn", user: state.user });
|
|
169
|
+
} else if (state.status === "anonymous" && wasSignedIn) {
|
|
170
|
+
onEventRef.current?.({
|
|
171
|
+
type: "auth.signedOut",
|
|
172
|
+
expired: state.error?.type === "invalid_key"
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
const failure = state.error;
|
|
176
|
+
if (failure && failure !== previous.error && !QUIET_AUTH_ERRORS.has(failure.type)) {
|
|
177
|
+
onEventRef.current?.({ type: "error", action: "signIn", error: failure });
|
|
178
|
+
}
|
|
179
|
+
previous = state;
|
|
180
|
+
});
|
|
181
|
+
}, [client]);
|
|
182
|
+
return /* @__PURE__ */ jsxRuntime.jsx(CommentsContext.Provider, { value, children });
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// src/react/appearance.ts
|
|
186
|
+
function cx(...classes) {
|
|
187
|
+
return classes.filter(Boolean).join(" ");
|
|
188
|
+
}
|
|
189
|
+
function mergeAppearance(...layers) {
|
|
190
|
+
const variables = {};
|
|
191
|
+
const elements = {};
|
|
192
|
+
for (const layer of layers) {
|
|
193
|
+
if (!layer) {
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
Object.assign(variables, layer.variables);
|
|
197
|
+
for (const [name, value] of Object.entries(layer.elements ?? {})) {
|
|
198
|
+
elements[name] = cx(elements[name], value) || void 0;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return { variables, elements };
|
|
202
|
+
}
|
|
203
|
+
function variablesStyle(variables) {
|
|
204
|
+
const style = {};
|
|
205
|
+
for (const [name, value] of Object.entries(variables ?? {})) {
|
|
206
|
+
if (value !== void 0 && value !== "") {
|
|
207
|
+
style[`--gc-${name.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`] = value;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return style;
|
|
211
|
+
}
|
|
212
|
+
function Icon({ children }) {
|
|
213
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
214
|
+
"svg",
|
|
215
|
+
{
|
|
216
|
+
className: "gc-icon",
|
|
217
|
+
width: "16",
|
|
218
|
+
height: "16",
|
|
219
|
+
viewBox: "0 0 24 24",
|
|
220
|
+
fill: "none",
|
|
221
|
+
stroke: "currentColor",
|
|
222
|
+
strokeWidth: "2",
|
|
223
|
+
strokeLinecap: "round",
|
|
224
|
+
strokeLinejoin: "round",
|
|
225
|
+
"aria-hidden": "true",
|
|
226
|
+
focusable: "false",
|
|
227
|
+
children
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
function HeartIcon({ filled = false }) {
|
|
232
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
233
|
+
"path",
|
|
234
|
+
{
|
|
235
|
+
fill: filled ? "currentColor" : "none",
|
|
236
|
+
d: "M20.8 4.6a5.5 5.5 0 0 0-7.8 0L12 5.7l-1-1.1a5.5 5.5 0 0 0-7.8 7.8l1 1.1L12 21l7.8-7.5 1-1.1a5.5 5.5 0 0 0 0-7.8z"
|
|
237
|
+
}
|
|
238
|
+
) });
|
|
239
|
+
}
|
|
240
|
+
function ReplyIcon() {
|
|
241
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Icon, { children: [
|
|
242
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M9 17 4 12l5-5" }),
|
|
243
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M20 18v-2a4 4 0 0 0-4-4H4" })
|
|
244
|
+
] });
|
|
245
|
+
}
|
|
246
|
+
function MoreIcon() {
|
|
247
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Icon, { children: [
|
|
248
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "5", cy: "12", r: "1" }),
|
|
249
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "12", cy: "12", r: "1" }),
|
|
250
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "19", cy: "12", r: "1" })
|
|
251
|
+
] });
|
|
252
|
+
}
|
|
253
|
+
function PinIcon() {
|
|
254
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Icon, { children: [
|
|
255
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M12 17v5" }),
|
|
256
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M9 10.8V4h6v6.8l2 3.2H7z" })
|
|
257
|
+
] });
|
|
258
|
+
}
|
|
259
|
+
function BoldIcon() {
|
|
260
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M7 5h6a3.5 3.5 0 0 1 0 7H7zM7 12h7a3.5 3.5 0 0 1 0 7H7z" }) });
|
|
261
|
+
}
|
|
262
|
+
function ItalicIcon() {
|
|
263
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M19 4h-9M14 20H5M15 4 9 20" }) });
|
|
264
|
+
}
|
|
265
|
+
function LinkIcon() {
|
|
266
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Icon, { children: [
|
|
267
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M10 13a5 5 0 0 0 7.5.5l3-3a5 5 0 0 0-7-7l-1.7 1.7" }),
|
|
268
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "M14 11a5 5 0 0 0-7.5-.5l-3 3a5 5 0 0 0 7 7l1.7-1.7" })
|
|
269
|
+
] });
|
|
270
|
+
}
|
|
271
|
+
function CodeIcon() {
|
|
272
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "m16 18 6-6-6-6M8 6l-6 6 6 6" }) });
|
|
273
|
+
}
|
|
274
|
+
function QuoteIcon() {
|
|
275
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "M3 21c3 0 7-1 7-8V5H3v7h4c0 3-1 5-4 6zM14 21c3 0 7-1 7-8V5h-7v7h4c0 3-1 5-4 6z" }) });
|
|
276
|
+
}
|
|
277
|
+
function ImageIcon() {
|
|
278
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Icon, { children: [
|
|
279
|
+
/* @__PURE__ */ jsxRuntime.jsx("rect", { x: "3", y: "3", width: "18", height: "18", rx: "2" }),
|
|
280
|
+
/* @__PURE__ */ jsxRuntime.jsx("circle", { cx: "9", cy: "9", r: "2" }),
|
|
281
|
+
/* @__PURE__ */ jsxRuntime.jsx("path", { d: "m21 15-3.1-3.1a2 2 0 0 0-2.8 0L6 21" })
|
|
282
|
+
] });
|
|
283
|
+
}
|
|
284
|
+
function BackIcon() {
|
|
285
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Icon, { children: /* @__PURE__ */ jsxRuntime.jsx("path", { d: "m15 18-6-6 6-6" }) });
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/react/forum-theme.ts
|
|
289
|
+
var FORUM_ROOT = '.gc-root[data-gc-theme="forum"]';
|
|
290
|
+
var FORUM_STYLED_ROOT = '.gc-root--styled[data-gc-theme="forum"]';
|
|
291
|
+
var TOKEN_SOURCES = [
|
|
292
|
+
["fg", "primary"],
|
|
293
|
+
["fg-muted", "primary-medium"],
|
|
294
|
+
["bg", "secondary"],
|
|
295
|
+
["bg-subtle", "primary-very-low"],
|
|
296
|
+
["border", "primary-low"],
|
|
297
|
+
["accent", "tertiary"],
|
|
298
|
+
["accent-fg", "secondary"],
|
|
299
|
+
["danger", "danger"]
|
|
300
|
+
];
|
|
301
|
+
var SAFE_NAME = /^[\w-]+$/;
|
|
302
|
+
var SAFE_VALUE = /^[^;{}<>]+$/;
|
|
303
|
+
var SAFE_FAMILY = /^[^"'\;{}<>]+$/;
|
|
304
|
+
var FONT_SOURCE = /^url\("https?:\/\/[^"\s<>]+"\)(?:\s+format\("[\w-]+"\))?$|^local\("[^"\\<>]+"\)$/;
|
|
305
|
+
var FONT_WEIGHT = /^(?:[1-9]\d{0,2}|normal|bold)$/;
|
|
306
|
+
var SCHEME_TYPES = /* @__PURE__ */ new Set(["light", "dark"]);
|
|
307
|
+
var STYLE_ATTRIBUTE = "data-gc-forum-theme";
|
|
308
|
+
var FONTS_ATTRIBUTE = "data-gc-forum-fonts";
|
|
309
|
+
function colorDeclarations(scheme) {
|
|
310
|
+
let css = "";
|
|
311
|
+
for (const [name, value] of Object.entries(scheme.colors)) {
|
|
312
|
+
if (SAFE_NAME.test(name) && SAFE_VALUE.test(value)) {
|
|
313
|
+
css += `--gc-forum-${name}:${value};`;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (SCHEME_TYPES.has(scheme.type)) {
|
|
317
|
+
css += `color-scheme:${scheme.type};`;
|
|
318
|
+
}
|
|
319
|
+
return css;
|
|
320
|
+
}
|
|
321
|
+
function fontDeclarations(fonts) {
|
|
322
|
+
let css = "";
|
|
323
|
+
for (const [role, font] of Object.entries(fonts)) {
|
|
324
|
+
if (font && SAFE_NAME.test(role) && SAFE_VALUE.test(font.stack)) {
|
|
325
|
+
css += `--gc-forum-font-${role}:${font.stack};`;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return css;
|
|
329
|
+
}
|
|
330
|
+
function tokenDeclarations(theme) {
|
|
331
|
+
let css = "";
|
|
332
|
+
for (const [token, color] of TOKEN_SOURCES) {
|
|
333
|
+
if (color in theme.schemes.light.colors) {
|
|
334
|
+
css += `--_gc-${token}:var(--gc-${token},var(--gc-forum-${color}));`;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
if (theme.fonts.body) {
|
|
338
|
+
css += "--_gc-font:var(--gc-font,var(--gc-forum-font-body));";
|
|
339
|
+
}
|
|
340
|
+
return css;
|
|
341
|
+
}
|
|
342
|
+
function forumThemeCss(theme) {
|
|
343
|
+
const { light, dark } = theme.schemes;
|
|
344
|
+
let css = `${FORUM_ROOT}{${colorDeclarations(light)}${fontDeclarations(theme.fonts)}}
|
|
345
|
+
`;
|
|
346
|
+
css += `${FORUM_STYLED_ROOT}{${tokenDeclarations(theme)}}
|
|
347
|
+
`;
|
|
348
|
+
if (dark) {
|
|
349
|
+
css += `@media (prefers-color-scheme: dark){${FORUM_ROOT}{${colorDeclarations(dark)}}}
|
|
350
|
+
`;
|
|
351
|
+
}
|
|
352
|
+
return css;
|
|
353
|
+
}
|
|
354
|
+
function forumFontFacesCss(theme) {
|
|
355
|
+
let css = "";
|
|
356
|
+
for (const font of Object.values(theme.fonts)) {
|
|
357
|
+
for (const face of font?.faces ?? []) {
|
|
358
|
+
const weight = String(face.weight);
|
|
359
|
+
if (SAFE_FAMILY.test(face.family) && FONT_SOURCE.test(face.src) && FONT_WEIGHT.test(weight)) {
|
|
360
|
+
css += `@font-face{font-family:"${face.family}";src:${face.src};font-weight:${weight};font-display:swap;}
|
|
361
|
+
`;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return css;
|
|
366
|
+
}
|
|
367
|
+
function forumStylesheetUrls(theme, forum) {
|
|
368
|
+
const secure = forum.startsWith("https:");
|
|
369
|
+
const urls = [];
|
|
370
|
+
for (const href of theme.stylesheets) {
|
|
371
|
+
let url;
|
|
372
|
+
try {
|
|
373
|
+
url = new URL(href);
|
|
374
|
+
} catch {
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
if (url.protocol === "https:" || !secure && url.protocol === "http:") {
|
|
378
|
+
urls.push(url.href);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return urls;
|
|
382
|
+
}
|
|
383
|
+
function documentOf(root) {
|
|
384
|
+
return root.nodeType === 9 ? root : root.ownerDocument;
|
|
385
|
+
}
|
|
386
|
+
function replaceNodes(container, attribute, forum, key, create) {
|
|
387
|
+
let current = false;
|
|
388
|
+
for (const node of container.querySelectorAll(`[${attribute}]`)) {
|
|
389
|
+
const value = node.getAttribute(attribute);
|
|
390
|
+
if (value === key) {
|
|
391
|
+
current = true;
|
|
392
|
+
} else if (value?.startsWith(`${forum}#`)) {
|
|
393
|
+
node.remove();
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
if (!current) {
|
|
397
|
+
container.append(...create());
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
function applyForumTheme(root, theme, forum) {
|
|
401
|
+
const doc = documentOf(root);
|
|
402
|
+
const container = root.nodeType === 9 ? doc.head : root;
|
|
403
|
+
const key = `${forum}#${theme.version}`;
|
|
404
|
+
replaceNodes(container, STYLE_ATTRIBUTE, forum, key, () => {
|
|
405
|
+
const style = doc.createElement("style");
|
|
406
|
+
style.setAttribute(STYLE_ATTRIBUTE, key);
|
|
407
|
+
style.textContent = forumThemeCss(theme);
|
|
408
|
+
const links = forumStylesheetUrls(theme, forum).map((href) => {
|
|
409
|
+
const link = doc.createElement("link");
|
|
410
|
+
link.rel = "stylesheet";
|
|
411
|
+
link.href = href;
|
|
412
|
+
link.setAttribute(STYLE_ATTRIBUTE, key);
|
|
413
|
+
return link;
|
|
414
|
+
});
|
|
415
|
+
return [style, ...links];
|
|
416
|
+
});
|
|
417
|
+
const faces = forumFontFacesCss(theme);
|
|
418
|
+
if (faces) {
|
|
419
|
+
replaceNodes(doc.head, FONTS_ATTRIBUTE, forum, key, () => {
|
|
420
|
+
const style = doc.createElement("style");
|
|
421
|
+
style.setAttribute(FONTS_ATTRIBUTE, key);
|
|
422
|
+
style.textContent = faces;
|
|
423
|
+
return [style];
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
var loaded = /* @__PURE__ */ new WeakMap();
|
|
428
|
+
function initial(client) {
|
|
429
|
+
return { client, config: loaded.get(client) ?? null, error: null };
|
|
430
|
+
}
|
|
431
|
+
function useConfig(client) {
|
|
432
|
+
const [stored, setState] = react.useState(() => initial(client));
|
|
433
|
+
let state = stored;
|
|
434
|
+
if (stored.client !== client) {
|
|
435
|
+
state = initial(client);
|
|
436
|
+
setState(state);
|
|
437
|
+
}
|
|
438
|
+
const [attempt, setAttempt] = react.useState(0);
|
|
439
|
+
react.useEffect(() => {
|
|
440
|
+
let active = true;
|
|
441
|
+
const update = (next) => setState((current) => active && current.client === client ? next(current) : current);
|
|
442
|
+
const load = () => client.config().then(
|
|
443
|
+
(config) => {
|
|
444
|
+
loaded.set(client, config);
|
|
445
|
+
update(
|
|
446
|
+
(current) => current.config === config && !current.error ? current : { client, config, error: null }
|
|
447
|
+
);
|
|
448
|
+
},
|
|
449
|
+
(error) => {
|
|
450
|
+
update((current) => ({ ...current, error: toCommentsError(error) }));
|
|
451
|
+
}
|
|
452
|
+
);
|
|
453
|
+
void load();
|
|
454
|
+
const stop = client.auth.onChange(() => void load());
|
|
455
|
+
return () => {
|
|
456
|
+
active = false;
|
|
457
|
+
stop();
|
|
458
|
+
};
|
|
459
|
+
}, [client, attempt]);
|
|
460
|
+
const reload = react.useCallback(() => setAttempt((count) => count + 1), []);
|
|
461
|
+
return { config: state.config, error: state.error, reload };
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// src/react/hooks/use-commenter.ts
|
|
465
|
+
function useCommenter(options = {}) {
|
|
466
|
+
const { client } = useCommentsContext(options.forum);
|
|
467
|
+
const { config } = useConfig(client);
|
|
468
|
+
return useCommenterFor(client, config);
|
|
469
|
+
}
|
|
470
|
+
function useCommenterFor(client, config) {
|
|
471
|
+
const { auth } = client;
|
|
472
|
+
const subscribe = react.useCallback((onChange) => auth.onChange(onChange), [auth]);
|
|
473
|
+
const getState = react.useCallback(() => auth.state(), [auth]);
|
|
474
|
+
const state = react.useSyncExternalStore(subscribe, getState, getState);
|
|
475
|
+
const actions = react.useMemo(
|
|
476
|
+
() => ({
|
|
477
|
+
signIn: (signInOptions) => auth.signIn(signInOptions),
|
|
478
|
+
cancelSignIn: () => auth.cancelSignIn(),
|
|
479
|
+
signOut: () => auth.signOut()
|
|
480
|
+
}),
|
|
481
|
+
[auth]
|
|
482
|
+
);
|
|
483
|
+
const providers = config?.auth_providers ?? auth.providers;
|
|
484
|
+
return react.useMemo(
|
|
485
|
+
() => ({
|
|
486
|
+
user: state.user,
|
|
487
|
+
status: state.status,
|
|
488
|
+
providers,
|
|
489
|
+
degraded: state.degraded,
|
|
490
|
+
error: state.error,
|
|
491
|
+
...actions
|
|
492
|
+
}),
|
|
493
|
+
[state, providers, actions]
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
var loaded2 = /* @__PURE__ */ new WeakMap();
|
|
497
|
+
function initial2(client) {
|
|
498
|
+
return { client, theme: loaded2.get(client) ?? null, error: null };
|
|
499
|
+
}
|
|
500
|
+
function useForumThemeFor(client, enabled) {
|
|
501
|
+
const [stored, setState] = react.useState(() => initial2(client));
|
|
502
|
+
let state = stored;
|
|
503
|
+
if (stored.client !== client) {
|
|
504
|
+
state = initial2(client);
|
|
505
|
+
setState(state);
|
|
506
|
+
}
|
|
507
|
+
react.useEffect(() => {
|
|
508
|
+
if (!enabled) {
|
|
509
|
+
return;
|
|
510
|
+
}
|
|
511
|
+
let active = true;
|
|
512
|
+
client.theme().then(
|
|
513
|
+
(theme) => {
|
|
514
|
+
loaded2.set(client, theme);
|
|
515
|
+
if (active) {
|
|
516
|
+
setState(
|
|
517
|
+
(current) => current.client === client && current.theme !== theme ? { client, theme, error: null } : current
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
},
|
|
521
|
+
(error) => {
|
|
522
|
+
if (active) {
|
|
523
|
+
setState(
|
|
524
|
+
(current) => current.client === client ? { ...current, error: toCommentsError(error) } : current
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
);
|
|
529
|
+
return () => {
|
|
530
|
+
active = false;
|
|
531
|
+
};
|
|
532
|
+
}, [client, enabled]);
|
|
533
|
+
return { theme: state.theme, error: state.error };
|
|
534
|
+
}
|
|
535
|
+
function useForumTheme() {
|
|
536
|
+
const { client } = useCommentsContext();
|
|
537
|
+
return useForumThemeFor(client, true);
|
|
538
|
+
}
|
|
539
|
+
function useThread(options = {}) {
|
|
540
|
+
const { client } = useCommentsContext(options.forum);
|
|
541
|
+
return useThreadFor(client, options);
|
|
542
|
+
}
|
|
543
|
+
function useThreadFor(client, { identifier, url, title, sort }, onOpenError) {
|
|
544
|
+
const [attempt, setAttempt] = react.useState(0);
|
|
545
|
+
const [opened, setOpened] = react.useState(
|
|
546
|
+
{ thread: null, error: null }
|
|
547
|
+
);
|
|
548
|
+
const pageKey = url === void 0 ? chunkIBNNENCZ_cjs.currentUrlWithoutHash() ?? "" : "";
|
|
549
|
+
const latest = react.useRef({ title, sort, onOpenError });
|
|
550
|
+
latest.current = { title, sort, onOpenError };
|
|
551
|
+
const requestedSort = react.useRef(sort);
|
|
552
|
+
react.useEffect(() => {
|
|
553
|
+
let active = true;
|
|
554
|
+
let handle = null;
|
|
555
|
+
setOpened((current) => current.thread || current.error ? { thread: null, error: null } : current);
|
|
556
|
+
queueMicrotask(() => {
|
|
557
|
+
if (!active) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
const { title: openTitle, sort: openSort } = latest.current;
|
|
561
|
+
requestedSort.current = openSort;
|
|
562
|
+
client.threads.open({ identifier, url, title: openTitle, sort: openSort }).then(
|
|
563
|
+
(thread2) => {
|
|
564
|
+
if (!active) {
|
|
565
|
+
thread2.dispose();
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
handle = thread2;
|
|
569
|
+
setOpened({ thread: thread2, error: null });
|
|
570
|
+
},
|
|
571
|
+
(error) => {
|
|
572
|
+
if (active) {
|
|
573
|
+
const failure = toCommentsError(error);
|
|
574
|
+
setOpened({ thread: null, error: failure });
|
|
575
|
+
latest.current.onOpenError?.(failure);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
);
|
|
579
|
+
});
|
|
580
|
+
return () => {
|
|
581
|
+
active = false;
|
|
582
|
+
handle?.dispose();
|
|
583
|
+
};
|
|
584
|
+
}, [client, identifier, url, pageKey, attempt]);
|
|
585
|
+
const { thread } = opened;
|
|
586
|
+
react.useEffect(() => {
|
|
587
|
+
if (thread && sort !== void 0 && sort !== requestedSort.current) {
|
|
588
|
+
requestedSort.current = sort;
|
|
589
|
+
void thread.setSort(sort).catch(() => {
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
}, [thread, sort]);
|
|
593
|
+
const subscribe = react.useCallback(
|
|
594
|
+
(onChange) => thread ? thread.subscribe(onChange) : () => {
|
|
595
|
+
},
|
|
596
|
+
[thread]
|
|
597
|
+
);
|
|
598
|
+
const getSnapshot = react.useCallback(() => thread?.snapshot() ?? null, [thread]);
|
|
599
|
+
const snapshot = react.useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
600
|
+
const retry = react.useCallback(() => {
|
|
601
|
+
if (thread) {
|
|
602
|
+
void thread.load().catch(() => {
|
|
603
|
+
});
|
|
604
|
+
} else {
|
|
605
|
+
setAttempt((count) => count + 1);
|
|
606
|
+
}
|
|
607
|
+
}, [thread]);
|
|
608
|
+
return { thread, snapshot, error: opened.error, retry };
|
|
609
|
+
}
|
|
610
|
+
var RootContext = react.createContext(null);
|
|
611
|
+
function useRootContext(component = "This component") {
|
|
612
|
+
const value = react.useContext(RootContext);
|
|
613
|
+
if (!value) {
|
|
614
|
+
throw new Error(`${chunkIBNNENCZ_cjs.PACKAGE_NAME}: ${component} must be rendered inside <Comments.Root>`);
|
|
615
|
+
}
|
|
616
|
+
return value;
|
|
617
|
+
}
|
|
618
|
+
function Root(props) {
|
|
619
|
+
const context = useOptionalCommentsContext(props.forum);
|
|
620
|
+
if (!context) {
|
|
621
|
+
return /* @__PURE__ */ jsxRuntime.jsx("section", { className: cx("gc-root", "gc-root--unconfigured", props.className), "data-gc-unconfigured": "", children: /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-notice", children: `${chunkIBNNENCZ_cjs.PACKAGE_NAME}: render <Comments> inside <DiscourseComments forum="\u2026"> or pass a forum prop.` }) });
|
|
622
|
+
}
|
|
623
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ConfiguredRoot, { ...props });
|
|
624
|
+
}
|
|
625
|
+
function ConfiguredRoot(props) {
|
|
626
|
+
const {
|
|
627
|
+
forum,
|
|
628
|
+
identifier,
|
|
629
|
+
url,
|
|
630
|
+
title,
|
|
631
|
+
sort,
|
|
632
|
+
maxDepth = 2,
|
|
633
|
+
signIn: signInMode = "inline",
|
|
634
|
+
onSignInRequired,
|
|
635
|
+
readOnly = false,
|
|
636
|
+
highlight,
|
|
637
|
+
guidelinesUrl,
|
|
638
|
+
onPosted,
|
|
639
|
+
appearance,
|
|
640
|
+
unstyled = false,
|
|
641
|
+
theme: themeProp,
|
|
642
|
+
locale: localeProp,
|
|
643
|
+
className,
|
|
644
|
+
children
|
|
645
|
+
} = props;
|
|
646
|
+
const context = useCommentsContext(forum);
|
|
647
|
+
const { client, emit } = context;
|
|
648
|
+
const rootRef = react.useRef(null);
|
|
649
|
+
const t = react.useMemo(
|
|
650
|
+
() => localeProp ? chunkIBNNENCZ_cjs.createTranslator(localeProp, context.messages) : context.t,
|
|
651
|
+
[localeProp, context.messages, context.t]
|
|
652
|
+
);
|
|
653
|
+
const locale = localeProp ? chunkIBNNENCZ_cjs.resolveLocale(localeProp) : context.locale;
|
|
654
|
+
const theme = themeProp ?? context.theme;
|
|
655
|
+
const { variables, elements } = react.useMemo(
|
|
656
|
+
() => mergeAppearance(context.appearance, appearance),
|
|
657
|
+
[context.appearance, appearance]
|
|
658
|
+
);
|
|
659
|
+
const threadIdRef = react.useRef(void 0);
|
|
660
|
+
const reportError = react.useCallback(
|
|
661
|
+
(action, error) => emit({ type: "error", action, error: toCommentsError(error), threadId: threadIdRef.current }),
|
|
662
|
+
[emit]
|
|
663
|
+
);
|
|
664
|
+
const { thread, snapshot, error: openError, retry: retryThread } = useThreadFor(
|
|
665
|
+
client,
|
|
666
|
+
{ identifier, url, title, sort },
|
|
667
|
+
(error) => reportError("open", error)
|
|
668
|
+
);
|
|
669
|
+
threadIdRef.current = thread?.id;
|
|
670
|
+
const { config, error: configError, reload: reloadConfig } = useConfig(client);
|
|
671
|
+
const commenter = useCommenterFor(client, config);
|
|
672
|
+
const forumTheme = useForumThemeFor(client, theme === "forum");
|
|
673
|
+
react.useEffect(() => {
|
|
674
|
+
const element = rootRef.current;
|
|
675
|
+
if (theme === "forum" && forumTheme.theme && element) {
|
|
676
|
+
applyForumTheme(rootNodeOf(element), forumTheme.theme, client.forum);
|
|
677
|
+
}
|
|
678
|
+
}, [theme, forumTheme.theme, client.forum]);
|
|
679
|
+
const themeError = theme === "forum" ? forumTheme.error : null;
|
|
680
|
+
react.useEffect(() => {
|
|
681
|
+
if (themeError) {
|
|
682
|
+
reportError("theme", themeError);
|
|
683
|
+
}
|
|
684
|
+
}, [themeError, reportError]);
|
|
685
|
+
const retry = react.useCallback(() => {
|
|
686
|
+
retryThread();
|
|
687
|
+
if (configError) {
|
|
688
|
+
reloadConfig();
|
|
689
|
+
}
|
|
690
|
+
}, [retryThread, configError, reloadConfig]);
|
|
691
|
+
const loadError = snapshot?.status === "error" ? snapshot.error : null;
|
|
692
|
+
react.useEffect(() => {
|
|
693
|
+
if (loadError) {
|
|
694
|
+
reportError("load", loadError);
|
|
695
|
+
}
|
|
696
|
+
}, [loadError, reportError]);
|
|
697
|
+
const userId = commenter.user?.id ?? null;
|
|
698
|
+
const [authEpoch, setAuthEpoch] = react.useState(
|
|
699
|
+
{ userId, thread: null }
|
|
700
|
+
);
|
|
701
|
+
if (authEpoch.userId !== userId) {
|
|
702
|
+
setAuthEpoch({ userId, thread: snapshot?.thread ?? null });
|
|
703
|
+
}
|
|
704
|
+
const permissionsFresh = !!snapshot && snapshot.thread !== authEpoch.thread;
|
|
705
|
+
const signedIn = commenter.status === "signed-in";
|
|
706
|
+
const closed = snapshot?.thread.closed ?? false;
|
|
707
|
+
const writable = !readOnly && !commenter.degraded;
|
|
708
|
+
const open = !!snapshot && snapshot.status !== "error" && writable && !closed;
|
|
709
|
+
const commentingDenied = open && signedIn && permissionsFresh && !snapshot.thread.can_comment;
|
|
710
|
+
const canCompose = open && !commentingDenied && (signedIn || signInMode === "on-submit");
|
|
711
|
+
const canReply = open && !commentingDenied;
|
|
712
|
+
const [focusView, setFocusView] = react.useState(null);
|
|
713
|
+
const viewRequest = react.useRef(0);
|
|
714
|
+
const [highlightId, setHighlightId] = react.useState(null);
|
|
715
|
+
const focusRequest = react.useRef(null);
|
|
716
|
+
const [, setFocusTick] = react.useState(0);
|
|
717
|
+
const focusComment = react.useCallback((commentId, options) => {
|
|
718
|
+
focusRequest.current = { id: commentId, scroll: options?.scroll ?? false };
|
|
719
|
+
setFocusTick((tick) => tick + 1);
|
|
720
|
+
}, []);
|
|
721
|
+
react.useEffect(() => {
|
|
722
|
+
const request = focusRequest.current;
|
|
723
|
+
const element = request && rootRef.current?.querySelector(`#gc-${request.id}`);
|
|
724
|
+
if (!request || !element) {
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
focusRequest.current = null;
|
|
728
|
+
if (request.scroll) {
|
|
729
|
+
element.scrollIntoView?.({ block: "center" });
|
|
730
|
+
}
|
|
731
|
+
element.focus({ preventScroll: request.scroll });
|
|
732
|
+
});
|
|
733
|
+
react.useEffect(() => {
|
|
734
|
+
viewRequest.current++;
|
|
735
|
+
setFocusView(null);
|
|
736
|
+
}, [thread]);
|
|
737
|
+
const continueThread = react.useCallback(
|
|
738
|
+
async (commentId) => {
|
|
739
|
+
if (!thread) {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
const request = ++viewRequest.current;
|
|
743
|
+
try {
|
|
744
|
+
const context2 = await thread.context(commentId);
|
|
745
|
+
if (request !== viewRequest.current) {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
setFocusView({
|
|
749
|
+
id: commentId,
|
|
750
|
+
ancestors: context2.ancestors,
|
|
751
|
+
truncated: context2.ancestorsTruncated
|
|
752
|
+
});
|
|
753
|
+
focusComment(commentId);
|
|
754
|
+
} catch (error) {
|
|
755
|
+
if (request === viewRequest.current) {
|
|
756
|
+
reportError("context", error);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
},
|
|
760
|
+
[thread, focusComment, reportError]
|
|
761
|
+
);
|
|
762
|
+
const closeFocusView = react.useCallback(() => {
|
|
763
|
+
viewRequest.current++;
|
|
764
|
+
if (focusView) {
|
|
765
|
+
focusComment(focusView.id);
|
|
766
|
+
}
|
|
767
|
+
setFocusView(null);
|
|
768
|
+
}, [focusView, focusComment]);
|
|
769
|
+
const hashId = useHashCommentId();
|
|
770
|
+
const requestedHighlight = highlight ?? hashId;
|
|
771
|
+
const handledHighlight = react.useRef(null);
|
|
772
|
+
const ready = snapshot?.status === "ready";
|
|
773
|
+
react.useEffect(() => {
|
|
774
|
+
const handled = handledHighlight.current;
|
|
775
|
+
if (!thread || !ready || requestedHighlight === null || handled?.thread === thread && handled.id === requestedHighlight) {
|
|
776
|
+
return;
|
|
777
|
+
}
|
|
778
|
+
handledHighlight.current = { thread, id: requestedHighlight };
|
|
779
|
+
const id = requestedHighlight;
|
|
780
|
+
const request = ++viewRequest.current;
|
|
781
|
+
void (async () => {
|
|
782
|
+
let location = chunkIBNNENCZ_cjs.findComment(thread.snapshot().comments, id);
|
|
783
|
+
if (!location) {
|
|
784
|
+
try {
|
|
785
|
+
await thread.context(id);
|
|
786
|
+
} catch (error) {
|
|
787
|
+
if (request === viewRequest.current) {
|
|
788
|
+
reportError("context", error);
|
|
789
|
+
}
|
|
790
|
+
return;
|
|
791
|
+
}
|
|
792
|
+
location = chunkIBNNENCZ_cjs.findComment(thread.snapshot().comments, id);
|
|
793
|
+
}
|
|
794
|
+
if (!location || request !== viewRequest.current) {
|
|
795
|
+
return;
|
|
796
|
+
}
|
|
797
|
+
if (location.depth > maxDepth) {
|
|
798
|
+
const cut = location.depth - maxDepth;
|
|
799
|
+
const tree = thread.snapshot().comments;
|
|
800
|
+
const ancestors = location.path.slice(0, cut).map((ancestorId) => chunkIBNNENCZ_cjs.findComment(tree, ancestorId)?.node).filter((node) => node !== void 0);
|
|
801
|
+
setFocusView({ id: location.path[cut], ancestors, truncated: false });
|
|
802
|
+
} else {
|
|
803
|
+
setFocusView(null);
|
|
804
|
+
}
|
|
805
|
+
setHighlightId(id);
|
|
806
|
+
focusComment(id, { scroll: true });
|
|
807
|
+
})();
|
|
808
|
+
}, [thread, ready, requestedHighlight, maxDepth, focusComment, reportError]);
|
|
809
|
+
const onSignInRequiredRef = react.useRef(onSignInRequired);
|
|
810
|
+
onSignInRequiredRef.current = onSignInRequired;
|
|
811
|
+
const canRequestSignIn = signInMode !== "manual" || onSignInRequired !== void 0;
|
|
812
|
+
const ensureSignedIn = react.useCallback(() => {
|
|
813
|
+
if (client.auth.status === "signed-in") {
|
|
814
|
+
return Promise.resolve(true);
|
|
815
|
+
}
|
|
816
|
+
if (signInMode === "manual") {
|
|
817
|
+
onSignInRequiredRef.current?.();
|
|
818
|
+
return Promise.resolve(false);
|
|
819
|
+
}
|
|
820
|
+
return client.auth.signIn().then(
|
|
821
|
+
(user) => user !== null,
|
|
822
|
+
() => false
|
|
823
|
+
);
|
|
824
|
+
}, [client, signInMode]);
|
|
825
|
+
const threadKey = snapshot?.thread.key;
|
|
826
|
+
const draftKey = react.useCallback(
|
|
827
|
+
(scope) => threadKey === void 0 ? null : `${chunkIBNNENCZ_cjs.STORAGE_PREFIX}:${client.forum}:draft:${threadKey}:${scope}`,
|
|
828
|
+
[client.forum, threadKey]
|
|
829
|
+
);
|
|
830
|
+
const value = {
|
|
831
|
+
client,
|
|
832
|
+
t,
|
|
833
|
+
locale,
|
|
834
|
+
thread,
|
|
835
|
+
snapshot,
|
|
836
|
+
openError,
|
|
837
|
+
retry,
|
|
838
|
+
config,
|
|
839
|
+
configError,
|
|
840
|
+
commenter,
|
|
841
|
+
maxDepth,
|
|
842
|
+
signInMode,
|
|
843
|
+
readOnly,
|
|
844
|
+
guidelinesUrl: guidelinesUrl ?? config?.guidelines_url ?? null,
|
|
845
|
+
debug: context.debug,
|
|
846
|
+
elements,
|
|
847
|
+
writable,
|
|
848
|
+
canCompose,
|
|
849
|
+
commentingDenied,
|
|
850
|
+
canReply,
|
|
851
|
+
canRequestSignIn,
|
|
852
|
+
focusView,
|
|
853
|
+
continueThread,
|
|
854
|
+
closeFocusView,
|
|
855
|
+
highlightId,
|
|
856
|
+
focusComment,
|
|
857
|
+
ensureSignedIn,
|
|
858
|
+
reportError,
|
|
859
|
+
emit,
|
|
860
|
+
onPosted,
|
|
861
|
+
draftKey
|
|
862
|
+
};
|
|
863
|
+
return /* @__PURE__ */ jsxRuntime.jsx(RootContext.Provider, { value, children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
864
|
+
"section",
|
|
865
|
+
{
|
|
866
|
+
ref: rootRef,
|
|
867
|
+
className: cx("gc-root", !unstyled && "gc-root--styled", elements.root, className),
|
|
868
|
+
"data-gc-theme": theme === "inherit" ? void 0 : theme,
|
|
869
|
+
style: variablesStyle(variables),
|
|
870
|
+
"aria-label": t("comments"),
|
|
871
|
+
"aria-busy": !snapshot && !openError ? true : void 0,
|
|
872
|
+
children
|
|
873
|
+
}
|
|
874
|
+
) });
|
|
875
|
+
}
|
|
876
|
+
function useHashCommentId() {
|
|
877
|
+
const [id, setId] = react.useState(null);
|
|
878
|
+
react.useEffect(() => {
|
|
879
|
+
const read = () => {
|
|
880
|
+
const match = /^#gc-(\d+)$/.exec(window.location.hash);
|
|
881
|
+
setId(match ? Number(match[1]) : null);
|
|
882
|
+
};
|
|
883
|
+
read();
|
|
884
|
+
window.addEventListener("hashchange", read);
|
|
885
|
+
return () => window.removeEventListener("hashchange", read);
|
|
886
|
+
}, []);
|
|
887
|
+
return id;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// src/react/composer/markdown.ts
|
|
891
|
+
function applyFormat(value, start, end, format) {
|
|
892
|
+
const selected = value.slice(start, end);
|
|
893
|
+
switch (format) {
|
|
894
|
+
case "bold":
|
|
895
|
+
return wrap(value, start, end, "**", "**");
|
|
896
|
+
case "italic":
|
|
897
|
+
return wrap(value, start, end, "*", "*");
|
|
898
|
+
case "code":
|
|
899
|
+
return selected.includes("\n") ? fence(value, start, end) : wrap(value, start, end, "`", "`");
|
|
900
|
+
case "link": {
|
|
901
|
+
const url = "https://";
|
|
902
|
+
const urlStart = start + selected.length + 3;
|
|
903
|
+
return {
|
|
904
|
+
value: `${value.slice(0, start)}[${selected}](${url})${value.slice(end)}`,
|
|
905
|
+
selectionStart: urlStart,
|
|
906
|
+
selectionEnd: urlStart + url.length
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
case "quote":
|
|
910
|
+
return quote(value, start, end);
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function insertText(value, start, end, text) {
|
|
914
|
+
const caret = start + text.length;
|
|
915
|
+
return {
|
|
916
|
+
value: value.slice(0, start) + text + value.slice(end),
|
|
917
|
+
selectionStart: caret,
|
|
918
|
+
selectionEnd: caret
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function imageMarkdown(upload) {
|
|
922
|
+
const name = upload.original_filename.replace(/\.[^.]+$/, "").replace(/[[\]|]/g, "");
|
|
923
|
+
const size = upload.width && upload.height ? `|${upload.width}x${upload.height}` : "";
|
|
924
|
+
return ``;
|
|
925
|
+
}
|
|
926
|
+
function mentionQuery(value, caret) {
|
|
927
|
+
const match = /(?:^|[\s([{])@([\w.-]{1,60})$/.exec(value.slice(0, caret));
|
|
928
|
+
if (!match) {
|
|
929
|
+
return null;
|
|
930
|
+
}
|
|
931
|
+
const query = match[1];
|
|
932
|
+
return { start: caret - query.length - 1, query };
|
|
933
|
+
}
|
|
934
|
+
function wrap(value, start, end, before, after) {
|
|
935
|
+
return {
|
|
936
|
+
value: value.slice(0, start) + before + value.slice(start, end) + after + value.slice(end),
|
|
937
|
+
selectionStart: start + before.length,
|
|
938
|
+
selectionEnd: end + before.length
|
|
939
|
+
};
|
|
940
|
+
}
|
|
941
|
+
function fence(value, start, end) {
|
|
942
|
+
const before = start > 0 && value[start - 1] !== "\n" ? "\n" : "";
|
|
943
|
+
const open = `${before}\`\`\`
|
|
944
|
+
`;
|
|
945
|
+
const close = "\n```\n";
|
|
946
|
+
return {
|
|
947
|
+
value: value.slice(0, start) + open + value.slice(start, end) + close + value.slice(end),
|
|
948
|
+
selectionStart: start + open.length,
|
|
949
|
+
selectionEnd: end + open.length
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
function quote(value, start, end) {
|
|
953
|
+
const lineStart = value.lastIndexOf("\n", start - 1) + 1;
|
|
954
|
+
const lines = value.slice(lineStart, end).split("\n");
|
|
955
|
+
const quoted = lines.map((line) => `> ${line}`).join("\n");
|
|
956
|
+
return {
|
|
957
|
+
value: value.slice(0, lineStart) + quoted + value.slice(end),
|
|
958
|
+
selectionStart: start + 2,
|
|
959
|
+
selectionEnd: end + 2 * lines.length
|
|
960
|
+
};
|
|
961
|
+
}
|
|
962
|
+
var SEARCH_DELAY_MS = 150;
|
|
963
|
+
function useMentions(options) {
|
|
964
|
+
const { client, threadId, enabled, value, caret, onPick } = options;
|
|
965
|
+
const query = enabled ? mentionQuery(value, caret) : null;
|
|
966
|
+
const [results, setResults] = react.useState({
|
|
967
|
+
query: "",
|
|
968
|
+
users: []
|
|
969
|
+
});
|
|
970
|
+
const [active, setActive] = react.useState(0);
|
|
971
|
+
const [dismissed, setDismissed] = react.useState(null);
|
|
972
|
+
const queryKey = query ? `${query.start}:${query.query}` : null;
|
|
973
|
+
react.useEffect(() => {
|
|
974
|
+
if (!query) {
|
|
975
|
+
return;
|
|
976
|
+
}
|
|
977
|
+
let current = true;
|
|
978
|
+
const timer = setTimeout(() => {
|
|
979
|
+
client.mentions(query.query, threadId).then(
|
|
980
|
+
(users) => {
|
|
981
|
+
if (current) {
|
|
982
|
+
setResults({ query: query.query, users });
|
|
983
|
+
setActive(0);
|
|
984
|
+
}
|
|
985
|
+
},
|
|
986
|
+
() => {
|
|
987
|
+
}
|
|
988
|
+
);
|
|
989
|
+
}, SEARCH_DELAY_MS);
|
|
990
|
+
return () => {
|
|
991
|
+
current = false;
|
|
992
|
+
clearTimeout(timer);
|
|
993
|
+
};
|
|
994
|
+
}, [client, threadId, query?.query]);
|
|
995
|
+
const open = query !== null && results.query === query.query && results.users.length > 0 && dismissed !== queryKey;
|
|
996
|
+
const pick = react.useCallback(
|
|
997
|
+
(index) => {
|
|
998
|
+
const user = results.users[index];
|
|
999
|
+
if (query && user) {
|
|
1000
|
+
onPick(query, user.username);
|
|
1001
|
+
}
|
|
1002
|
+
},
|
|
1003
|
+
[results.users, query, onPick]
|
|
1004
|
+
);
|
|
1005
|
+
const onKeyDown = (event) => {
|
|
1006
|
+
if (!open) {
|
|
1007
|
+
return false;
|
|
1008
|
+
}
|
|
1009
|
+
const count = results.users.length;
|
|
1010
|
+
switch (event.key) {
|
|
1011
|
+
case "ArrowDown":
|
|
1012
|
+
setActive((index) => (index + 1) % count);
|
|
1013
|
+
break;
|
|
1014
|
+
case "ArrowUp":
|
|
1015
|
+
setActive((index) => (index - 1 + count) % count);
|
|
1016
|
+
break;
|
|
1017
|
+
case "Enter":
|
|
1018
|
+
case "Tab":
|
|
1019
|
+
if (event.metaKey || event.ctrlKey) {
|
|
1020
|
+
return false;
|
|
1021
|
+
}
|
|
1022
|
+
pick(active);
|
|
1023
|
+
break;
|
|
1024
|
+
case "Escape":
|
|
1025
|
+
setDismissed(queryKey);
|
|
1026
|
+
break;
|
|
1027
|
+
default:
|
|
1028
|
+
return false;
|
|
1029
|
+
}
|
|
1030
|
+
event.preventDefault();
|
|
1031
|
+
event.stopPropagation();
|
|
1032
|
+
return true;
|
|
1033
|
+
};
|
|
1034
|
+
return { open, users: results.users, active, setActive, onKeyDown, pick };
|
|
1035
|
+
}
|
|
1036
|
+
function Composer(props) {
|
|
1037
|
+
const root = useRootContext("Comments.Composer");
|
|
1038
|
+
if (!root.thread || !root.snapshot || !composerAllowed(root, props.editing)) {
|
|
1039
|
+
return null;
|
|
1040
|
+
}
|
|
1041
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ComposerForm, { ...props, thread: root.thread });
|
|
1042
|
+
}
|
|
1043
|
+
function composerAllowed(root, editing) {
|
|
1044
|
+
if (editing) {
|
|
1045
|
+
return root.writable && root.commenter.status === "signed-in" && editing.can.edit;
|
|
1046
|
+
}
|
|
1047
|
+
return root.canCompose;
|
|
1048
|
+
}
|
|
1049
|
+
var FORMATS = [
|
|
1050
|
+
{ format: "bold", label: "bold", icon: /* @__PURE__ */ jsxRuntime.jsx(BoldIcon, {}) },
|
|
1051
|
+
{ format: "italic", label: "italic", icon: /* @__PURE__ */ jsxRuntime.jsx(ItalicIcon, {}) },
|
|
1052
|
+
{ format: "link", label: "insertLink", icon: /* @__PURE__ */ jsxRuntime.jsx(LinkIcon, {}) },
|
|
1053
|
+
{ format: "code", label: "code", icon: /* @__PURE__ */ jsxRuntime.jsx(CodeIcon, {}) },
|
|
1054
|
+
{ format: "quote", label: "quote", icon: /* @__PURE__ */ jsxRuntime.jsx(QuoteIcon, {}) }
|
|
1055
|
+
];
|
|
1056
|
+
function ComposerForm({
|
|
1057
|
+
replyTo,
|
|
1058
|
+
editing,
|
|
1059
|
+
placeholder,
|
|
1060
|
+
autoFocus,
|
|
1061
|
+
onDone,
|
|
1062
|
+
className,
|
|
1063
|
+
thread
|
|
1064
|
+
}) {
|
|
1065
|
+
const root = useRootContext();
|
|
1066
|
+
const { t, client, config, commenter, elements } = root;
|
|
1067
|
+
const id = react.useId();
|
|
1068
|
+
const textareaRef = react.useRef(null);
|
|
1069
|
+
const fileRef = react.useRef(null);
|
|
1070
|
+
const pendingSelection = react.useRef(null);
|
|
1071
|
+
const [timer] = react.useState(chunkIBNNENCZ_cjs.createComposerTimer);
|
|
1072
|
+
const draftKey = editing ? null : root.draftKey(replyTo ? `reply:${replyTo.id}` : "root");
|
|
1073
|
+
const [value, setValueState] = react.useState(() => draftKey ? readDraft(draftKey) : "");
|
|
1074
|
+
const valueRef = react.useRef(value);
|
|
1075
|
+
valueRef.current = value;
|
|
1076
|
+
const [caret, setCaret] = react.useState(0);
|
|
1077
|
+
const [raw, setRaw] = react.useState(editing ? "loading" : "loaded");
|
|
1078
|
+
const [rawAttempt, setRawAttempt] = react.useState(0);
|
|
1079
|
+
const [tab, setTab] = react.useState("write");
|
|
1080
|
+
const [preview, setPreview] = react.useState(
|
|
1081
|
+
{ html: null, loading: false, failed: false }
|
|
1082
|
+
);
|
|
1083
|
+
const previewRequest = react.useRef(0);
|
|
1084
|
+
const [submitting, setSubmitting] = react.useState(false);
|
|
1085
|
+
const [error, setError] = react.useState(null);
|
|
1086
|
+
const [uploading, setUploading] = react.useState(0);
|
|
1087
|
+
const [waitSeconds, startCountdown] = useCountdown();
|
|
1088
|
+
const submission = react.useRef(0);
|
|
1089
|
+
const allowedRef = react.useRef(true);
|
|
1090
|
+
allowedRef.current = composerAllowed(root, editing);
|
|
1091
|
+
const setValue = react.useCallback(
|
|
1092
|
+
(next, selection) => {
|
|
1093
|
+
valueRef.current = next;
|
|
1094
|
+
setValueState(next);
|
|
1095
|
+
if (draftKey) {
|
|
1096
|
+
writeDraft(draftKey, next);
|
|
1097
|
+
}
|
|
1098
|
+
if (selection) {
|
|
1099
|
+
pendingSelection.current = selection;
|
|
1100
|
+
}
|
|
1101
|
+
},
|
|
1102
|
+
[draftKey]
|
|
1103
|
+
);
|
|
1104
|
+
const applyEdit = react.useCallback(
|
|
1105
|
+
(edit) => setValue(edit.value, [edit.selectionStart, edit.selectionEnd]),
|
|
1106
|
+
[setValue]
|
|
1107
|
+
);
|
|
1108
|
+
useIsomorphicLayoutEffect(() => {
|
|
1109
|
+
const selection = pendingSelection.current;
|
|
1110
|
+
const textarea = textareaRef.current;
|
|
1111
|
+
if (selection && textarea) {
|
|
1112
|
+
pendingSelection.current = null;
|
|
1113
|
+
textarea.focus();
|
|
1114
|
+
textarea.setSelectionRange(selection[0], selection[1]);
|
|
1115
|
+
setCaret(selection[1]);
|
|
1116
|
+
}
|
|
1117
|
+
});
|
|
1118
|
+
react.useEffect(() => {
|
|
1119
|
+
const textarea = textareaRef.current;
|
|
1120
|
+
if (autoFocus && !editing && textarea) {
|
|
1121
|
+
const end = valueRef.current.length;
|
|
1122
|
+
textarea.focus();
|
|
1123
|
+
textarea.setSelectionRange(end, end);
|
|
1124
|
+
setCaret(end);
|
|
1125
|
+
}
|
|
1126
|
+
}, [autoFocus, editing]);
|
|
1127
|
+
react.useEffect(
|
|
1128
|
+
() => () => {
|
|
1129
|
+
submission.current++;
|
|
1130
|
+
},
|
|
1131
|
+
[thread]
|
|
1132
|
+
);
|
|
1133
|
+
const editingId = editing?.id;
|
|
1134
|
+
react.useEffect(() => {
|
|
1135
|
+
if (editingId === void 0) {
|
|
1136
|
+
return;
|
|
1137
|
+
}
|
|
1138
|
+
let active = true;
|
|
1139
|
+
setRaw("loading");
|
|
1140
|
+
setError(null);
|
|
1141
|
+
thread.raw(editingId).then(
|
|
1142
|
+
(source) => {
|
|
1143
|
+
if (active) {
|
|
1144
|
+
setValue(source, [source.length, source.length]);
|
|
1145
|
+
setRaw("loaded");
|
|
1146
|
+
}
|
|
1147
|
+
},
|
|
1148
|
+
(failure) => {
|
|
1149
|
+
if (active) {
|
|
1150
|
+
setError(toCommentsError(failure));
|
|
1151
|
+
setRaw("failed");
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
);
|
|
1155
|
+
return () => {
|
|
1156
|
+
active = false;
|
|
1157
|
+
};
|
|
1158
|
+
}, [thread, editingId, setValue, rawAttempt]);
|
|
1159
|
+
react.useEffect(() => {
|
|
1160
|
+
if (waitSeconds === 0) {
|
|
1161
|
+
setError((current) => current?.type === "rate_limit" ? null : current);
|
|
1162
|
+
}
|
|
1163
|
+
}, [waitSeconds]);
|
|
1164
|
+
const signedIn = commenter.status === "signed-in";
|
|
1165
|
+
const signingIn = commenter.status === "signing-in";
|
|
1166
|
+
const uploadsEnabled = signedIn && config?.features.uploads === true;
|
|
1167
|
+
const min = Math.max(config?.limits.min_post_length ?? 1, 1);
|
|
1168
|
+
const max = config?.limits.max_post_length ?? Number.POSITIVE_INFINITY;
|
|
1169
|
+
const length = value.trim().length;
|
|
1170
|
+
const tooShort = length > 0 && length < min;
|
|
1171
|
+
const tooLong = length > max;
|
|
1172
|
+
const loadingRaw = raw === "loading";
|
|
1173
|
+
const editable = raw === "loaded" && !submitting;
|
|
1174
|
+
const busy = submitting || raw !== "loaded" || uploading > 0 || waitSeconds > 0 || signingIn;
|
|
1175
|
+
const canSubmit = !busy && length >= min && !tooLong;
|
|
1176
|
+
const onPickMention = react.useCallback(
|
|
1177
|
+
(query, username) => applyEdit(insertText(valueRef.current, query.start, caret, `@${username} `)),
|
|
1178
|
+
[applyEdit, caret]
|
|
1179
|
+
);
|
|
1180
|
+
const mentions = useMentions({
|
|
1181
|
+
client,
|
|
1182
|
+
threadId: thread.id,
|
|
1183
|
+
enabled: signedIn && config?.features.mentions !== false && tab === "write" && editable,
|
|
1184
|
+
value,
|
|
1185
|
+
caret,
|
|
1186
|
+
onPick: onPickMention
|
|
1187
|
+
});
|
|
1188
|
+
async function send() {
|
|
1189
|
+
if (!allowedRef.current) {
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
const body = valueRef.current;
|
|
1193
|
+
setSubmitting(true);
|
|
1194
|
+
setError(null);
|
|
1195
|
+
try {
|
|
1196
|
+
if (editing) {
|
|
1197
|
+
await thread.edit(editing.id, body);
|
|
1198
|
+
onDone?.();
|
|
1199
|
+
return;
|
|
1200
|
+
}
|
|
1201
|
+
const result = await thread.post({ body, replyTo: replyTo?.id, composer: timer });
|
|
1202
|
+
if (valueRef.current === body) {
|
|
1203
|
+
setValue("");
|
|
1204
|
+
}
|
|
1205
|
+
setTab("write");
|
|
1206
|
+
if (result.type === "created") {
|
|
1207
|
+
root.emit({ type: "comment.posted", threadId: thread.id, comment: result.comment });
|
|
1208
|
+
if (replyTo) {
|
|
1209
|
+
root.focusComment(result.comment.id);
|
|
1210
|
+
}
|
|
1211
|
+
} else {
|
|
1212
|
+
root.emit({ type: "comment.pending", threadId: thread.id, pending: result.pending });
|
|
1213
|
+
}
|
|
1214
|
+
root.onPosted?.(result);
|
|
1215
|
+
onDone?.();
|
|
1216
|
+
} catch (failure) {
|
|
1217
|
+
const commentsError = toCommentsError(failure);
|
|
1218
|
+
if (commentsError.type === "rate_limit" && commentsError.waitSeconds) {
|
|
1219
|
+
startCountdown(commentsError.waitSeconds);
|
|
1220
|
+
}
|
|
1221
|
+
setError(commentsError);
|
|
1222
|
+
root.reportError(editing ? "edit" : "post", commentsError);
|
|
1223
|
+
} finally {
|
|
1224
|
+
setSubmitting(false);
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
function submit() {
|
|
1228
|
+
if (!canSubmit) {
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
if (signedIn) {
|
|
1232
|
+
void send();
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
if (root.signInMode === "on-submit") {
|
|
1236
|
+
const token = ++submission.current;
|
|
1237
|
+
setError(null);
|
|
1238
|
+
client.auth.signIn().then(
|
|
1239
|
+
(user) => {
|
|
1240
|
+
if (user && token === submission.current) {
|
|
1241
|
+
void send();
|
|
1242
|
+
}
|
|
1243
|
+
},
|
|
1244
|
+
(failure) => {
|
|
1245
|
+
const signInError = toCommentsError(failure);
|
|
1246
|
+
if (token === submission.current && signInError.type !== "cancelled") {
|
|
1247
|
+
setError(signInError);
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
);
|
|
1251
|
+
}
|
|
1252
|
+
}
|
|
1253
|
+
function cancel() {
|
|
1254
|
+
submission.current++;
|
|
1255
|
+
onDone?.();
|
|
1256
|
+
}
|
|
1257
|
+
function format(kind) {
|
|
1258
|
+
const textarea = textareaRef.current;
|
|
1259
|
+
const start = textarea?.selectionStart ?? valueRef.current.length;
|
|
1260
|
+
const end = textarea?.selectionEnd ?? start;
|
|
1261
|
+
applyEdit(applyFormat(valueRef.current, start, end, kind));
|
|
1262
|
+
}
|
|
1263
|
+
async function upload(files, pasted = false) {
|
|
1264
|
+
for (const file of files.filter((candidate) => candidate.type.startsWith("image/"))) {
|
|
1265
|
+
setUploading((count) => count + 1);
|
|
1266
|
+
try {
|
|
1267
|
+
const uploaded = await client.attachments.upload(thread.id, file, { pasted });
|
|
1268
|
+
const textarea = textareaRef.current;
|
|
1269
|
+
const current = valueRef.current;
|
|
1270
|
+
const start = textarea?.selectionStart ?? current.length;
|
|
1271
|
+
const end = textarea?.selectionEnd ?? start;
|
|
1272
|
+
const before = start > 0 && current[start - 1] !== "\n" ? "\n" : "";
|
|
1273
|
+
applyEdit(insertText(current, start, end, `${before}${imageMarkdown(uploaded)}
|
|
1274
|
+
`));
|
|
1275
|
+
} catch (failure) {
|
|
1276
|
+
setError(toCommentsError(failure));
|
|
1277
|
+
root.reportError("upload", failure);
|
|
1278
|
+
} finally {
|
|
1279
|
+
setUploading((count) => count - 1);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
function showPreview() {
|
|
1284
|
+
setTab("preview");
|
|
1285
|
+
const body = valueRef.current;
|
|
1286
|
+
const request = ++previewRequest.current;
|
|
1287
|
+
if (!body.trim()) {
|
|
1288
|
+
setPreview({ html: null, loading: false, failed: false });
|
|
1289
|
+
return;
|
|
1290
|
+
}
|
|
1291
|
+
setPreview({ html: null, loading: true, failed: false });
|
|
1292
|
+
client.preview(body).then(
|
|
1293
|
+
(html) => {
|
|
1294
|
+
if (request === previewRequest.current) {
|
|
1295
|
+
setPreview({ html, loading: false, failed: false });
|
|
1296
|
+
}
|
|
1297
|
+
},
|
|
1298
|
+
(failure) => {
|
|
1299
|
+
if (request === previewRequest.current) {
|
|
1300
|
+
setPreview({ html: null, loading: false, failed: true });
|
|
1301
|
+
root.reportError("preview", failure);
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
);
|
|
1305
|
+
}
|
|
1306
|
+
function onChange(event) {
|
|
1307
|
+
timer.typing();
|
|
1308
|
+
setValue(event.target.value);
|
|
1309
|
+
setCaret(event.target.selectionStart);
|
|
1310
|
+
}
|
|
1311
|
+
function onKeyDown(event) {
|
|
1312
|
+
if (mentions.onKeyDown(event)) {
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (event.key === "Enter" && (event.metaKey || event.ctrlKey)) {
|
|
1316
|
+
event.preventDefault();
|
|
1317
|
+
submit();
|
|
1318
|
+
} else if (event.key === "Escape" && (replyTo || editing)) {
|
|
1319
|
+
event.preventDefault();
|
|
1320
|
+
cancel();
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
function onPaste(event) {
|
|
1324
|
+
const files = [...event.clipboardData.files];
|
|
1325
|
+
if (uploadsEnabled && editable && files.some((file) => file.type.startsWith("image/"))) {
|
|
1326
|
+
event.preventDefault();
|
|
1327
|
+
void upload(files, true);
|
|
1328
|
+
}
|
|
1329
|
+
}
|
|
1330
|
+
function onDragOver(event) {
|
|
1331
|
+
if (uploadsEnabled && editable && event.dataTransfer.types.includes("Files")) {
|
|
1332
|
+
event.preventDefault();
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
function onDrop(event) {
|
|
1336
|
+
const files = [...event.dataTransfer.files];
|
|
1337
|
+
if (uploadsEnabled && editable && files.length > 0) {
|
|
1338
|
+
event.preventDefault();
|
|
1339
|
+
void upload(files);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
function onTabKeyDown(event) {
|
|
1343
|
+
if (event.key === "ArrowLeft" || event.key === "ArrowRight") {
|
|
1344
|
+
event.preventDefault();
|
|
1345
|
+
if (tab === "write") {
|
|
1346
|
+
showPreview();
|
|
1347
|
+
} else {
|
|
1348
|
+
setTab("write");
|
|
1349
|
+
}
|
|
1350
|
+
rootNodeOf(event.currentTarget).getElementById(`${id}-${tab === "write" ? "preview" : "write"}-tab`)?.focus();
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
const label = editing ? t("editing") : replyTo ? t("replyingTo", { username: replyTo.author?.username ?? "" }) : t("composerPlaceholder");
|
|
1354
|
+
const hintId = `${id}-hint`;
|
|
1355
|
+
const errorId = `${id}-error`;
|
|
1356
|
+
const listId = `${id}-mentions`;
|
|
1357
|
+
const messages = error ? errorMessages(error, waitSeconds, t) : [];
|
|
1358
|
+
const submitLabel = editing ? t("save") : replyTo ? t("reply") : t("post");
|
|
1359
|
+
const defaultPlaceholder = editing ? void 0 : replyTo ? t("replyPlaceholder") : t("composerPlaceholder");
|
|
1360
|
+
const hint = tooLong ? t("tooLong", { max }) : tooShort ? t("tooShort", { min }) : null;
|
|
1361
|
+
const canPreview = signedIn;
|
|
1362
|
+
const isMain = !replyTo && !editing;
|
|
1363
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
1364
|
+
"div",
|
|
1365
|
+
{
|
|
1366
|
+
className: cx(
|
|
1367
|
+
"gc-composer",
|
|
1368
|
+
replyTo && "gc-composer--replying",
|
|
1369
|
+
editing && "gc-composer--editing",
|
|
1370
|
+
elements.composer,
|
|
1371
|
+
className
|
|
1372
|
+
),
|
|
1373
|
+
children: [
|
|
1374
|
+
isMain && root.guidelinesUrl && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1375
|
+
"a",
|
|
1376
|
+
{
|
|
1377
|
+
className: "gc-composer__guidelines",
|
|
1378
|
+
href: root.guidelinesUrl,
|
|
1379
|
+
target: "_blank",
|
|
1380
|
+
rel: "noopener noreferrer",
|
|
1381
|
+
children: t("guidelines")
|
|
1382
|
+
}
|
|
1383
|
+
),
|
|
1384
|
+
!isMain && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__context", children: label }),
|
|
1385
|
+
canPreview && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-composer__tabs", role: "tablist", children: [
|
|
1386
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1387
|
+
"button",
|
|
1388
|
+
{
|
|
1389
|
+
type: "button",
|
|
1390
|
+
role: "tab",
|
|
1391
|
+
id: `${id}-write-tab`,
|
|
1392
|
+
className: "gc-composer__tab",
|
|
1393
|
+
"aria-selected": tab === "write",
|
|
1394
|
+
"aria-controls": `${id}-write`,
|
|
1395
|
+
tabIndex: tab === "write" ? 0 : -1,
|
|
1396
|
+
onClick: () => setTab("write"),
|
|
1397
|
+
onKeyDown: onTabKeyDown,
|
|
1398
|
+
children: t("write")
|
|
1399
|
+
}
|
|
1400
|
+
),
|
|
1401
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1402
|
+
"button",
|
|
1403
|
+
{
|
|
1404
|
+
type: "button",
|
|
1405
|
+
role: "tab",
|
|
1406
|
+
id: `${id}-preview-tab`,
|
|
1407
|
+
className: "gc-composer__tab",
|
|
1408
|
+
"aria-selected": tab === "preview",
|
|
1409
|
+
"aria-controls": `${id}-preview`,
|
|
1410
|
+
tabIndex: tab === "preview" ? 0 : -1,
|
|
1411
|
+
onClick: showPreview,
|
|
1412
|
+
onKeyDown: onTabKeyDown,
|
|
1413
|
+
children: t("preview")
|
|
1414
|
+
}
|
|
1415
|
+
)
|
|
1416
|
+
] }),
|
|
1417
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
1418
|
+
"div",
|
|
1419
|
+
{
|
|
1420
|
+
className: "gc-composer__panel",
|
|
1421
|
+
id: `${id}-write`,
|
|
1422
|
+
role: canPreview ? "tabpanel" : void 0,
|
|
1423
|
+
"aria-labelledby": canPreview ? `${id}-write-tab` : void 0,
|
|
1424
|
+
hidden: tab !== "write",
|
|
1425
|
+
children: [
|
|
1426
|
+
signedIn && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx("gc-toolbar", elements.toolbar), role: "group", "aria-label": t("formatting"), children: [
|
|
1427
|
+
FORMATS.map(({ format: kind, label: key, icon }) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
1428
|
+
"button",
|
|
1429
|
+
{
|
|
1430
|
+
type: "button",
|
|
1431
|
+
className: "gc-toolbar__button",
|
|
1432
|
+
"aria-label": t(key),
|
|
1433
|
+
title: t(key),
|
|
1434
|
+
disabled: !editable,
|
|
1435
|
+
onClick: () => format(kind),
|
|
1436
|
+
children: icon
|
|
1437
|
+
},
|
|
1438
|
+
kind
|
|
1439
|
+
)),
|
|
1440
|
+
uploadsEnabled && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
1441
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1442
|
+
"button",
|
|
1443
|
+
{
|
|
1444
|
+
type: "button",
|
|
1445
|
+
className: "gc-toolbar__button",
|
|
1446
|
+
"aria-label": t("upload"),
|
|
1447
|
+
title: t("upload"),
|
|
1448
|
+
disabled: !editable,
|
|
1449
|
+
onClick: () => fileRef.current?.click(),
|
|
1450
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(ImageIcon, {})
|
|
1451
|
+
}
|
|
1452
|
+
),
|
|
1453
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1454
|
+
"input",
|
|
1455
|
+
{
|
|
1456
|
+
ref: fileRef,
|
|
1457
|
+
type: "file",
|
|
1458
|
+
accept: "image/*",
|
|
1459
|
+
multiple: true,
|
|
1460
|
+
hidden: true,
|
|
1461
|
+
onChange: (event) => {
|
|
1462
|
+
const files = [...event.target.files ?? []];
|
|
1463
|
+
event.target.value = "";
|
|
1464
|
+
void upload(files);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
)
|
|
1468
|
+
] })
|
|
1469
|
+
] }),
|
|
1470
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-composer__field", children: [
|
|
1471
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1472
|
+
"textarea",
|
|
1473
|
+
{
|
|
1474
|
+
ref: textareaRef,
|
|
1475
|
+
className: cx("gc-composer__input", elements.textarea),
|
|
1476
|
+
value,
|
|
1477
|
+
placeholder: placeholder ?? defaultPlaceholder,
|
|
1478
|
+
"aria-label": label,
|
|
1479
|
+
"aria-describedby": cx(hint && hintId, messages.length > 0 && errorId) || void 0,
|
|
1480
|
+
"aria-invalid": tooLong || void 0,
|
|
1481
|
+
"aria-autocomplete": mentions.open ? "list" : void 0,
|
|
1482
|
+
"aria-controls": mentions.open ? listId : void 0,
|
|
1483
|
+
"aria-activedescendant": mentions.open ? `${listId}-${mentions.active}` : void 0,
|
|
1484
|
+
rows: isMain ? 3 : 2,
|
|
1485
|
+
disabled: raw !== "loaded",
|
|
1486
|
+
readOnly: submitting,
|
|
1487
|
+
onChange,
|
|
1488
|
+
onSelect: (event) => setCaret(event.currentTarget.selectionStart),
|
|
1489
|
+
onFocus: () => timer.start(),
|
|
1490
|
+
onKeyDown,
|
|
1491
|
+
onPaste,
|
|
1492
|
+
onDragOver,
|
|
1493
|
+
onDrop
|
|
1494
|
+
}
|
|
1495
|
+
),
|
|
1496
|
+
mentions.open && /* @__PURE__ */ jsxRuntime.jsx("ul", { className: "gc-mentions", role: "listbox", id: listId, "aria-label": t("mentionSuggestions"), children: mentions.users.map((user, index) => /* @__PURE__ */ jsxRuntime.jsxs(
|
|
1497
|
+
"li",
|
|
1498
|
+
{
|
|
1499
|
+
id: `${listId}-${index}`,
|
|
1500
|
+
role: "option",
|
|
1501
|
+
"aria-selected": index === mentions.active,
|
|
1502
|
+
className: cx(
|
|
1503
|
+
"gc-mentions__option",
|
|
1504
|
+
index === mentions.active && "gc-mentions__option--active"
|
|
1505
|
+
),
|
|
1506
|
+
onMouseDown: (event) => {
|
|
1507
|
+
event.preventDefault();
|
|
1508
|
+
mentions.pick(index);
|
|
1509
|
+
},
|
|
1510
|
+
onMouseEnter: () => mentions.setActive(index),
|
|
1511
|
+
children: [
|
|
1512
|
+
/* @__PURE__ */ jsxRuntime.jsx("img", { className: "gc-avatar gc-avatar--small", src: user.avatar_url, alt: "" }),
|
|
1513
|
+
/* @__PURE__ */ jsxRuntime.jsxs("span", { className: "gc-mentions__username", children: [
|
|
1514
|
+
"@",
|
|
1515
|
+
user.username
|
|
1516
|
+
] }),
|
|
1517
|
+
user.name && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-mentions__name", children: user.name })
|
|
1518
|
+
]
|
|
1519
|
+
},
|
|
1520
|
+
user.id
|
|
1521
|
+
)) })
|
|
1522
|
+
] })
|
|
1523
|
+
]
|
|
1524
|
+
}
|
|
1525
|
+
),
|
|
1526
|
+
canPreview && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1527
|
+
"div",
|
|
1528
|
+
{
|
|
1529
|
+
className: "gc-composer__panel",
|
|
1530
|
+
id: `${id}-preview`,
|
|
1531
|
+
role: "tabpanel",
|
|
1532
|
+
"aria-labelledby": `${id}-preview-tab`,
|
|
1533
|
+
"aria-busy": preview.loading || void 0,
|
|
1534
|
+
hidden: tab !== "preview",
|
|
1535
|
+
children: preview.loading ? /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("loading") }) : preview.failed ? /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("genericError") }) : preview.html ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
1536
|
+
"div",
|
|
1537
|
+
{
|
|
1538
|
+
className: "gc-composer__preview gc-prose",
|
|
1539
|
+
dangerouslySetInnerHTML: { __html: preview.html }
|
|
1540
|
+
}
|
|
1541
|
+
) : /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("previewEmpty") })
|
|
1542
|
+
}
|
|
1543
|
+
),
|
|
1544
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-composer__footer", children: [
|
|
1545
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-composer__messages", "aria-live": "polite", children: [
|
|
1546
|
+
loadingRaw && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("loading") }),
|
|
1547
|
+
uploading > 0 && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("uploading") }),
|
|
1548
|
+
signingIn && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-composer__status", children: t("signingIn") }),
|
|
1549
|
+
hint && /* @__PURE__ */ jsxRuntime.jsx("p", { id: hintId, className: "gc-composer__hint", children: hint }),
|
|
1550
|
+
messages.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("div", { id: errorId, className: "gc-composer__error", role: "alert", children: messages.map((message) => /* @__PURE__ */ jsxRuntime.jsx("p", { children: message }, message)) })
|
|
1551
|
+
] }),
|
|
1552
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-composer__buttons", children: [
|
|
1553
|
+
signingIn && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1554
|
+
"button",
|
|
1555
|
+
{
|
|
1556
|
+
type: "button",
|
|
1557
|
+
className: cx("gc-button", elements.button),
|
|
1558
|
+
onClick: () => client.auth.cancelSignIn(),
|
|
1559
|
+
children: t("signInCancel")
|
|
1560
|
+
}
|
|
1561
|
+
),
|
|
1562
|
+
raw === "failed" && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1563
|
+
"button",
|
|
1564
|
+
{
|
|
1565
|
+
type: "button",
|
|
1566
|
+
className: cx("gc-button", elements.button),
|
|
1567
|
+
onClick: () => setRawAttempt((count) => count + 1),
|
|
1568
|
+
children: t("retry")
|
|
1569
|
+
}
|
|
1570
|
+
),
|
|
1571
|
+
!isMain && !signingIn && /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: cx("gc-button", elements.button), onClick: cancel, children: t("cancel") }),
|
|
1572
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1573
|
+
"button",
|
|
1574
|
+
{
|
|
1575
|
+
type: "button",
|
|
1576
|
+
className: cx("gc-button", "gc-button--primary", elements.button, elements.primaryButton),
|
|
1577
|
+
disabled: !canSubmit,
|
|
1578
|
+
"aria-busy": submitting || void 0,
|
|
1579
|
+
onClick: submit,
|
|
1580
|
+
children: submitting && !editing ? t("posting") : submitLabel
|
|
1581
|
+
}
|
|
1582
|
+
)
|
|
1583
|
+
] })
|
|
1584
|
+
] })
|
|
1585
|
+
]
|
|
1586
|
+
}
|
|
1587
|
+
);
|
|
1588
|
+
}
|
|
1589
|
+
function errorMessages(error, waitSeconds, t) {
|
|
1590
|
+
switch (error.type) {
|
|
1591
|
+
case "rate_limit":
|
|
1592
|
+
return waitSeconds > 0 ? [t("rateLimited", { count: waitSeconds })] : error.errors.slice(0, 1);
|
|
1593
|
+
case "invalid_key":
|
|
1594
|
+
case "not_logged_in":
|
|
1595
|
+
return [t("sessionExpired")];
|
|
1596
|
+
case "unreachable":
|
|
1597
|
+
case "unknown":
|
|
1598
|
+
case "cancelled":
|
|
1599
|
+
return [t("genericError")];
|
|
1600
|
+
default:
|
|
1601
|
+
return error.errors.length > 0 ? error.errors : [t("genericError")];
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
function useCountdown() {
|
|
1605
|
+
const [until, setUntil] = react.useState(0);
|
|
1606
|
+
const [now, setNow] = react.useState(() => Date.now());
|
|
1607
|
+
react.useEffect(() => {
|
|
1608
|
+
if (until <= Date.now()) {
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
const timer = setInterval(() => {
|
|
1612
|
+
const at = Date.now();
|
|
1613
|
+
setNow(at);
|
|
1614
|
+
if (at >= until) {
|
|
1615
|
+
clearInterval(timer);
|
|
1616
|
+
}
|
|
1617
|
+
}, 250);
|
|
1618
|
+
return () => clearInterval(timer);
|
|
1619
|
+
}, [until]);
|
|
1620
|
+
const start = react.useCallback((seconds) => {
|
|
1621
|
+
const at = Date.now();
|
|
1622
|
+
setNow(at);
|
|
1623
|
+
setUntil(at + seconds * 1e3);
|
|
1624
|
+
}, []);
|
|
1625
|
+
return [Math.max(0, Math.ceil((until - now) / 1e3)), start];
|
|
1626
|
+
}
|
|
1627
|
+
var ItemContext = react.createContext(null);
|
|
1628
|
+
function useItemContext(component) {
|
|
1629
|
+
const value = react.useContext(ItemContext);
|
|
1630
|
+
if (!value) {
|
|
1631
|
+
throw new Error(`${chunkIBNNENCZ_cjs.PACKAGE_NAME}: ${component} must be rendered inside <Comments.Item>`);
|
|
1632
|
+
}
|
|
1633
|
+
return value;
|
|
1634
|
+
}
|
|
1635
|
+
function Avatar({ className }) {
|
|
1636
|
+
const { comment } = useItemContext("Comments.Avatar");
|
|
1637
|
+
const { elements } = useRootContext("Comments.Avatar");
|
|
1638
|
+
return /* @__PURE__ */ jsxRuntime.jsx(UserAvatar, { user: comment.author, className: cx(elements.avatar, className) });
|
|
1639
|
+
}
|
|
1640
|
+
function UserAvatar({
|
|
1641
|
+
user,
|
|
1642
|
+
className
|
|
1643
|
+
}) {
|
|
1644
|
+
if (user?.avatar_url) {
|
|
1645
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1646
|
+
"img",
|
|
1647
|
+
{
|
|
1648
|
+
className: cx("gc-avatar", className),
|
|
1649
|
+
src: user.avatar_url,
|
|
1650
|
+
alt: "",
|
|
1651
|
+
loading: "lazy",
|
|
1652
|
+
decoding: "async"
|
|
1653
|
+
}
|
|
1654
|
+
);
|
|
1655
|
+
}
|
|
1656
|
+
const initial3 = (user?.name || user?.username || "").charAt(0).toUpperCase();
|
|
1657
|
+
return /* @__PURE__ */ jsxRuntime.jsx("span", { className: cx("gc-avatar", "gc-avatar--placeholder", className), "aria-hidden": "true", children: initial3 });
|
|
1658
|
+
}
|
|
1659
|
+
function Meta({ className }) {
|
|
1660
|
+
const { comment } = useItemContext("Comments.Meta");
|
|
1661
|
+
const { t, locale, client, elements } = useRootContext("Comments.Meta");
|
|
1662
|
+
const author = comment.author;
|
|
1663
|
+
const name = author ? author.name || author.username : null;
|
|
1664
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx("gc-comment__meta", elements.meta, className), children: [
|
|
1665
|
+
name && (author?.username ? /* @__PURE__ */ jsxRuntime.jsx(
|
|
1666
|
+
"a",
|
|
1667
|
+
{
|
|
1668
|
+
className: "gc-comment__author",
|
|
1669
|
+
href: `${client.forum}/u/${encodeURIComponent(author.username)}`,
|
|
1670
|
+
target: "_blank",
|
|
1671
|
+
rel: "noopener noreferrer",
|
|
1672
|
+
children: name
|
|
1673
|
+
}
|
|
1674
|
+
) : /* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-comment__author", children: name })),
|
|
1675
|
+
author?.staff && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-badge gc-badge--staff", children: t("staff") }),
|
|
1676
|
+
comment.pinned && /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "gc-badge gc-badge--featured", children: [
|
|
1677
|
+
/* @__PURE__ */ jsxRuntime.jsx(PinIcon, {}),
|
|
1678
|
+
t("pinned")
|
|
1679
|
+
] }),
|
|
1680
|
+
/* @__PURE__ */ jsxRuntime.jsx("a", { className: "gc-comment__time", href: `#gc-${comment.id}`, children: /* @__PURE__ */ jsxRuntime.jsx(RelativeTime, { iso: comment.created_at, locale }) }),
|
|
1681
|
+
comment.edited && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-comment__edited", title: formatAbsolute(comment.updated_at, locale), children: t("edited") })
|
|
1682
|
+
] });
|
|
1683
|
+
}
|
|
1684
|
+
function RelativeTime({ iso, locale }) {
|
|
1685
|
+
const [now, setNow] = react.useState(Date.now);
|
|
1686
|
+
react.useEffect(() => onMinuteTick(() => setNow(Date.now())), []);
|
|
1687
|
+
return /* @__PURE__ */ jsxRuntime.jsx("time", { dateTime: iso, title: formatAbsolute(iso, locale), children: formatRelative(iso, now, locale) });
|
|
1688
|
+
}
|
|
1689
|
+
function Body({ className }) {
|
|
1690
|
+
const { comment, mode, setMode } = useItemContext("Comments.Body");
|
|
1691
|
+
const { t, elements } = useRootContext("Comments.Body");
|
|
1692
|
+
const classes = cx("gc-comment__body", elements.body, className);
|
|
1693
|
+
if (mode === "edit") {
|
|
1694
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Composer, { editing: comment, onDone: () => setMode("view") });
|
|
1695
|
+
}
|
|
1696
|
+
if (comment.deleted) {
|
|
1697
|
+
return /* @__PURE__ */ jsxRuntime.jsx("p", { className: cx(classes, "gc-comment__body--removed"), children: t("deleted") });
|
|
1698
|
+
}
|
|
1699
|
+
if (comment.hidden && !comment.html) {
|
|
1700
|
+
return /* @__PURE__ */ jsxRuntime.jsx("p", { className: cx(classes, "gc-comment__body--removed"), children: t("hidden") });
|
|
1701
|
+
}
|
|
1702
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
1703
|
+
comment.hidden && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-comment__hidden-note", children: t("hidden") }),
|
|
1704
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: cx(classes, "gc-prose"), dangerouslySetInnerHTML: { __html: comment.html } })
|
|
1705
|
+
] });
|
|
1706
|
+
}
|
|
1707
|
+
function Actions({ className }) {
|
|
1708
|
+
const root = useRootContext("Comments.Actions");
|
|
1709
|
+
const { comment, depth, setMode, setNotice } = useItemContext("Comments.Actions");
|
|
1710
|
+
const { t, thread, commenter, elements, writable } = root;
|
|
1711
|
+
if (!thread || comment.deleted) {
|
|
1712
|
+
return null;
|
|
1713
|
+
}
|
|
1714
|
+
const signedIn = commenter.status === "signed-in";
|
|
1715
|
+
const { likes, can } = comment;
|
|
1716
|
+
const likeDisabled = !writable || (signedIn ? !likes.can_like && !likes.liked : !root.canRequestSignIn);
|
|
1717
|
+
const buttonClass = cx("gc-button", "gc-button--ghost", "gc-button--small", elements.button);
|
|
1718
|
+
const fail = (action, error) => {
|
|
1719
|
+
setNotice({ text: actionError(error, t), error: true });
|
|
1720
|
+
root.reportError(action, error);
|
|
1721
|
+
};
|
|
1722
|
+
const toggleLike = () => {
|
|
1723
|
+
setNotice(null);
|
|
1724
|
+
const liked = !likes.liked;
|
|
1725
|
+
const ready = signedIn ? Promise.resolve(true) : root.ensureSignedIn();
|
|
1726
|
+
void ready.then(async (ok) => {
|
|
1727
|
+
if (!ok) {
|
|
1728
|
+
return;
|
|
1729
|
+
}
|
|
1730
|
+
try {
|
|
1731
|
+
const updated = liked ? await thread.like(comment.id) : await thread.unlike(comment.id);
|
|
1732
|
+
root.emit({ type: "comment.liked", threadId: thread.id, comment: updated, liked });
|
|
1733
|
+
} catch (error) {
|
|
1734
|
+
fail("like", error);
|
|
1735
|
+
}
|
|
1736
|
+
});
|
|
1737
|
+
};
|
|
1738
|
+
const reply = (event) => {
|
|
1739
|
+
const trigger = event.currentTarget;
|
|
1740
|
+
setNotice(null);
|
|
1741
|
+
if (signedIn || root.signInMode === "on-submit") {
|
|
1742
|
+
setMode("reply", trigger);
|
|
1743
|
+
return;
|
|
1744
|
+
}
|
|
1745
|
+
void root.ensureSignedIn().then((ok) => ok && setMode("reply", trigger));
|
|
1746
|
+
};
|
|
1747
|
+
const items = [
|
|
1748
|
+
{
|
|
1749
|
+
key: "copy",
|
|
1750
|
+
label: t("copyLink"),
|
|
1751
|
+
onSelect: () => {
|
|
1752
|
+
const href = `${chunkIBNNENCZ_cjs.currentUrlWithoutHash() ?? ""}#gc-${comment.id}`;
|
|
1753
|
+
const clipboard = typeof navigator === "undefined" ? void 0 : navigator.clipboard;
|
|
1754
|
+
if (!clipboard) {
|
|
1755
|
+
window.location.hash = `gc-${comment.id}`;
|
|
1756
|
+
return;
|
|
1757
|
+
}
|
|
1758
|
+
clipboard.writeText(href).then(
|
|
1759
|
+
() => setNotice({ text: t("linkCopied"), error: false }),
|
|
1760
|
+
() => {
|
|
1761
|
+
window.location.hash = `gc-${comment.id}`;
|
|
1762
|
+
}
|
|
1763
|
+
);
|
|
1764
|
+
}
|
|
1765
|
+
}
|
|
1766
|
+
];
|
|
1767
|
+
if (writable && can.edit) {
|
|
1768
|
+
items.push({ key: "edit", label: t("edit"), onSelect: (trigger) => setMode("edit", trigger) });
|
|
1769
|
+
}
|
|
1770
|
+
if (writable && can.pin && depth === 0) {
|
|
1771
|
+
items.push({
|
|
1772
|
+
key: "pin",
|
|
1773
|
+
label: comment.pinned ? t("unpin") : t("pin"),
|
|
1774
|
+
onSelect: () => {
|
|
1775
|
+
const action = comment.pinned ? thread.unpin(comment.id) : thread.pin(comment.id);
|
|
1776
|
+
action.catch((error) => fail("pin", error));
|
|
1777
|
+
}
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
if (writable && can.flag) {
|
|
1781
|
+
items.push({ key: "report", label: t("report"), onSelect: (trigger) => setMode("report", trigger) });
|
|
1782
|
+
}
|
|
1783
|
+
if (writable && can.delete) {
|
|
1784
|
+
items.push({
|
|
1785
|
+
key: "delete",
|
|
1786
|
+
label: t("delete"),
|
|
1787
|
+
danger: true,
|
|
1788
|
+
onSelect: (trigger) => setMode("delete", trigger)
|
|
1789
|
+
});
|
|
1790
|
+
}
|
|
1791
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx("gc-actions", elements.actions, className), children: [
|
|
1792
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
1793
|
+
"button",
|
|
1794
|
+
{
|
|
1795
|
+
type: "button",
|
|
1796
|
+
className: cx(buttonClass, "gc-actions__like", likes.liked && "gc-actions__like--active"),
|
|
1797
|
+
"aria-pressed": likes.liked,
|
|
1798
|
+
"aria-label": `${t(likes.liked ? "unlike" : "like")}, ${t("likeCount", { count: likes.count })}`,
|
|
1799
|
+
disabled: likeDisabled,
|
|
1800
|
+
onClick: toggleLike,
|
|
1801
|
+
children: [
|
|
1802
|
+
/* @__PURE__ */ jsxRuntime.jsx(HeartIcon, { filled: likes.liked }),
|
|
1803
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-actions__count", children: likes.count })
|
|
1804
|
+
]
|
|
1805
|
+
}
|
|
1806
|
+
),
|
|
1807
|
+
root.canReply && (signedIn || root.canRequestSignIn) && /* @__PURE__ */ jsxRuntime.jsxs("button", { type: "button", className: buttonClass, onClick: reply, children: [
|
|
1808
|
+
/* @__PURE__ */ jsxRuntime.jsx(ReplyIcon, {}),
|
|
1809
|
+
t("reply")
|
|
1810
|
+
] }),
|
|
1811
|
+
/* @__PURE__ */ jsxRuntime.jsx(ActionMenu, { items, label: t("moreActions"), buttonClass })
|
|
1812
|
+
] });
|
|
1813
|
+
}
|
|
1814
|
+
function ActionMenu({
|
|
1815
|
+
items,
|
|
1816
|
+
label,
|
|
1817
|
+
buttonClass
|
|
1818
|
+
}) {
|
|
1819
|
+
const { elements } = useRootContext();
|
|
1820
|
+
const [open, setOpen] = react.useState(false);
|
|
1821
|
+
const id = react.useId();
|
|
1822
|
+
const buttonRef = react.useRef(null);
|
|
1823
|
+
const menuRef = react.useRef(null);
|
|
1824
|
+
react.useEffect(() => {
|
|
1825
|
+
if (!open) {
|
|
1826
|
+
return;
|
|
1827
|
+
}
|
|
1828
|
+
menuRef.current?.querySelector('[role="menuitem"]')?.focus();
|
|
1829
|
+
const onPointerDown = (event) => {
|
|
1830
|
+
const path = event.composedPath();
|
|
1831
|
+
const inside = (node) => node !== null && path.includes(node);
|
|
1832
|
+
if (!inside(menuRef.current) && !inside(buttonRef.current)) {
|
|
1833
|
+
setOpen(false);
|
|
1834
|
+
}
|
|
1835
|
+
};
|
|
1836
|
+
document.addEventListener("pointerdown", onPointerDown);
|
|
1837
|
+
return () => document.removeEventListener("pointerdown", onPointerDown);
|
|
1838
|
+
}, [open]);
|
|
1839
|
+
const close = (refocus) => {
|
|
1840
|
+
setOpen(false);
|
|
1841
|
+
{
|
|
1842
|
+
buttonRef.current?.focus();
|
|
1843
|
+
}
|
|
1844
|
+
};
|
|
1845
|
+
const onMenuKeyDown = (event) => {
|
|
1846
|
+
const options = [...menuRef.current?.querySelectorAll('[role="menuitem"]') ?? []];
|
|
1847
|
+
const index = options.indexOf(rootNodeOf(event.currentTarget).activeElement);
|
|
1848
|
+
const move = (next) => options[(next + options.length) % options.length]?.focus();
|
|
1849
|
+
switch (event.key) {
|
|
1850
|
+
case "ArrowDown":
|
|
1851
|
+
move(index + 1);
|
|
1852
|
+
break;
|
|
1853
|
+
case "ArrowUp":
|
|
1854
|
+
move(index - 1);
|
|
1855
|
+
break;
|
|
1856
|
+
case "Home":
|
|
1857
|
+
move(0);
|
|
1858
|
+
break;
|
|
1859
|
+
case "End":
|
|
1860
|
+
move(options.length - 1);
|
|
1861
|
+
break;
|
|
1862
|
+
case "Escape":
|
|
1863
|
+
close();
|
|
1864
|
+
break;
|
|
1865
|
+
case "Tab":
|
|
1866
|
+
setOpen(false);
|
|
1867
|
+
return;
|
|
1868
|
+
default:
|
|
1869
|
+
return;
|
|
1870
|
+
}
|
|
1871
|
+
event.preventDefault();
|
|
1872
|
+
event.stopPropagation();
|
|
1873
|
+
};
|
|
1874
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-menu", children: [
|
|
1875
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1876
|
+
"button",
|
|
1877
|
+
{
|
|
1878
|
+
ref: buttonRef,
|
|
1879
|
+
type: "button",
|
|
1880
|
+
className: cx(buttonClass, "gc-button--icon"),
|
|
1881
|
+
"aria-label": label,
|
|
1882
|
+
"aria-haspopup": "menu",
|
|
1883
|
+
"aria-expanded": open,
|
|
1884
|
+
"aria-controls": open ? id : void 0,
|
|
1885
|
+
onClick: () => setOpen((current) => !current),
|
|
1886
|
+
children: /* @__PURE__ */ jsxRuntime.jsx(MoreIcon, {})
|
|
1887
|
+
}
|
|
1888
|
+
),
|
|
1889
|
+
open && /* @__PURE__ */ jsxRuntime.jsx(
|
|
1890
|
+
"ul",
|
|
1891
|
+
{
|
|
1892
|
+
ref: menuRef,
|
|
1893
|
+
id,
|
|
1894
|
+
role: "menu",
|
|
1895
|
+
"aria-label": label,
|
|
1896
|
+
className: cx("gc-menu__list", elements.menu),
|
|
1897
|
+
onKeyDown: onMenuKeyDown,
|
|
1898
|
+
children: items.map((item) => /* @__PURE__ */ jsxRuntime.jsx("li", { role: "none", children: /* @__PURE__ */ jsxRuntime.jsx(
|
|
1899
|
+
"button",
|
|
1900
|
+
{
|
|
1901
|
+
type: "button",
|
|
1902
|
+
role: "menuitem",
|
|
1903
|
+
tabIndex: -1,
|
|
1904
|
+
className: cx("gc-menu__item", item.danger && "gc-menu__item--danger"),
|
|
1905
|
+
onClick: () => {
|
|
1906
|
+
close();
|
|
1907
|
+
item.onSelect(buttonRef.current);
|
|
1908
|
+
},
|
|
1909
|
+
children: item.label
|
|
1910
|
+
}
|
|
1911
|
+
) }, item.key))
|
|
1912
|
+
}
|
|
1913
|
+
)
|
|
1914
|
+
] });
|
|
1915
|
+
}
|
|
1916
|
+
var FLAG_TYPES = [
|
|
1917
|
+
"inappropriate",
|
|
1918
|
+
"spam",
|
|
1919
|
+
"illegal",
|
|
1920
|
+
"off_topic",
|
|
1921
|
+
"notify_moderators"
|
|
1922
|
+
];
|
|
1923
|
+
var MESSAGE_REQUIRED = /* @__PURE__ */ new Set(["illegal", "notify_moderators"]);
|
|
1924
|
+
function ReportForm({ onClose }) {
|
|
1925
|
+
const root = useRootContext("Comments.Item");
|
|
1926
|
+
const { comment, setNotice } = useItemContext("Comments.Item");
|
|
1927
|
+
const { t, thread, elements } = root;
|
|
1928
|
+
const [type, setType] = react.useState("inappropriate");
|
|
1929
|
+
const [message, setMessage] = react.useState("");
|
|
1930
|
+
const [submitting, setSubmitting] = react.useState(false);
|
|
1931
|
+
const [error, setError] = react.useState(null);
|
|
1932
|
+
const id = react.useId();
|
|
1933
|
+
const formRef = react.useRef(null);
|
|
1934
|
+
const needsMessage = MESSAGE_REQUIRED.has(type);
|
|
1935
|
+
const messageLabel = needsMessage ? t("reportMessageRequired") : t("reportMessagePlaceholder");
|
|
1936
|
+
react.useEffect(() => {
|
|
1937
|
+
formRef.current?.querySelector("input:checked")?.focus();
|
|
1938
|
+
}, []);
|
|
1939
|
+
const submit = async (event) => {
|
|
1940
|
+
event.preventDefault();
|
|
1941
|
+
if (!thread || !root.writable || !comment.can.flag || needsMessage && !message.trim()) {
|
|
1942
|
+
return;
|
|
1943
|
+
}
|
|
1944
|
+
setSubmitting(true);
|
|
1945
|
+
setError(null);
|
|
1946
|
+
try {
|
|
1947
|
+
await thread.flag(comment.id, { type, message: message.trim() || void 0 });
|
|
1948
|
+
setNotice({ text: t("reportSent"), error: false });
|
|
1949
|
+
onClose();
|
|
1950
|
+
} catch (failure) {
|
|
1951
|
+
setError(actionError(failure, t));
|
|
1952
|
+
root.reportError("flag", failure);
|
|
1953
|
+
setSubmitting(false);
|
|
1954
|
+
}
|
|
1955
|
+
};
|
|
1956
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
1957
|
+
"div",
|
|
1958
|
+
{
|
|
1959
|
+
role: "dialog",
|
|
1960
|
+
"aria-modal": "false",
|
|
1961
|
+
"aria-labelledby": `${id}-title`,
|
|
1962
|
+
className: cx("gc-report", elements.dialog),
|
|
1963
|
+
onKeyDown: (event) => {
|
|
1964
|
+
if (event.key === "Escape") {
|
|
1965
|
+
event.stopPropagation();
|
|
1966
|
+
onClose();
|
|
1967
|
+
}
|
|
1968
|
+
},
|
|
1969
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs("form", { ref: formRef, onSubmit: submit, children: [
|
|
1970
|
+
/* @__PURE__ */ jsxRuntime.jsxs("fieldset", { className: "gc-report__options", children: [
|
|
1971
|
+
/* @__PURE__ */ jsxRuntime.jsx("legend", { id: `${id}-title`, className: "gc-report__title", children: t("reportTitle") }),
|
|
1972
|
+
FLAG_TYPES.map((flag) => /* @__PURE__ */ jsxRuntime.jsxs("label", { className: "gc-report__option", children: [
|
|
1973
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1974
|
+
"input",
|
|
1975
|
+
{
|
|
1976
|
+
type: "radio",
|
|
1977
|
+
name: `${id}-type`,
|
|
1978
|
+
value: flag,
|
|
1979
|
+
checked: type === flag,
|
|
1980
|
+
onChange: () => setType(flag)
|
|
1981
|
+
}
|
|
1982
|
+
),
|
|
1983
|
+
t(`report.${flag}`)
|
|
1984
|
+
] }, flag))
|
|
1985
|
+
] }),
|
|
1986
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
1987
|
+
"textarea",
|
|
1988
|
+
{
|
|
1989
|
+
className: "gc-report__message",
|
|
1990
|
+
value: message,
|
|
1991
|
+
placeholder: messageLabel,
|
|
1992
|
+
"aria-label": messageLabel,
|
|
1993
|
+
required: needsMessage,
|
|
1994
|
+
rows: 2,
|
|
1995
|
+
onChange: (event) => setMessage(event.target.value)
|
|
1996
|
+
}
|
|
1997
|
+
),
|
|
1998
|
+
error && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-report__error", role: "alert", children: error }),
|
|
1999
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-report__buttons", children: [
|
|
2000
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: cx("gc-button", elements.button), onClick: onClose, children: t("cancel") }),
|
|
2001
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2002
|
+
"button",
|
|
2003
|
+
{
|
|
2004
|
+
type: "submit",
|
|
2005
|
+
className: cx("gc-button", "gc-button--primary", elements.button, elements.primaryButton),
|
|
2006
|
+
disabled: submitting || needsMessage && !message.trim(),
|
|
2007
|
+
children: t("report")
|
|
2008
|
+
}
|
|
2009
|
+
)
|
|
2010
|
+
] })
|
|
2011
|
+
] })
|
|
2012
|
+
}
|
|
2013
|
+
);
|
|
2014
|
+
}
|
|
2015
|
+
function DeleteConfirm({ onClose }) {
|
|
2016
|
+
const root = useRootContext("Comments.Item");
|
|
2017
|
+
const { comment, setNotice } = useItemContext("Comments.Item");
|
|
2018
|
+
const { t, thread, elements } = root;
|
|
2019
|
+
const [deleting, setDeleting] = react.useState(false);
|
|
2020
|
+
const id = react.useId();
|
|
2021
|
+
const cancelRef = react.useRef(null);
|
|
2022
|
+
react.useEffect(() => cancelRef.current?.focus(), []);
|
|
2023
|
+
const confirm = async () => {
|
|
2024
|
+
if (!thread || !root.writable || !comment.can.delete) {
|
|
2025
|
+
return;
|
|
2026
|
+
}
|
|
2027
|
+
setDeleting(true);
|
|
2028
|
+
try {
|
|
2029
|
+
await thread.remove(comment.id);
|
|
2030
|
+
} catch (failure) {
|
|
2031
|
+
setNotice({ text: actionError(failure, t), error: true });
|
|
2032
|
+
root.reportError("delete", failure);
|
|
2033
|
+
}
|
|
2034
|
+
onClose();
|
|
2035
|
+
};
|
|
2036
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2037
|
+
"div",
|
|
2038
|
+
{
|
|
2039
|
+
role: "alertdialog",
|
|
2040
|
+
"aria-modal": "false",
|
|
2041
|
+
"aria-labelledby": id,
|
|
2042
|
+
className: cx("gc-confirm", elements.dialog),
|
|
2043
|
+
onKeyDown: (event) => {
|
|
2044
|
+
if (event.key === "Escape") {
|
|
2045
|
+
event.stopPropagation();
|
|
2046
|
+
onClose();
|
|
2047
|
+
}
|
|
2048
|
+
},
|
|
2049
|
+
children: [
|
|
2050
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { id, className: "gc-confirm__text", children: t("deleteConfirm") }),
|
|
2051
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-confirm__buttons", children: [
|
|
2052
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { ref: cancelRef, type: "button", className: cx("gc-button", elements.button), onClick: onClose, children: t("cancel") }),
|
|
2053
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2054
|
+
"button",
|
|
2055
|
+
{
|
|
2056
|
+
type: "button",
|
|
2057
|
+
className: cx("gc-button", "gc-button--danger", elements.button),
|
|
2058
|
+
disabled: deleting,
|
|
2059
|
+
onClick: () => void confirm(),
|
|
2060
|
+
children: t("delete")
|
|
2061
|
+
}
|
|
2062
|
+
)
|
|
2063
|
+
] })
|
|
2064
|
+
]
|
|
2065
|
+
}
|
|
2066
|
+
);
|
|
2067
|
+
}
|
|
2068
|
+
function actionError(error, t) {
|
|
2069
|
+
const failure = toCommentsError(error);
|
|
2070
|
+
if (failure.type === "rate_limit" && failure.waitSeconds) {
|
|
2071
|
+
return t("rateLimited", { count: failure.waitSeconds });
|
|
2072
|
+
}
|
|
2073
|
+
if (failure.type === "invalid_key" || failure.type === "not_logged_in") {
|
|
2074
|
+
return t("sessionExpired");
|
|
2075
|
+
}
|
|
2076
|
+
if (failure.type === "unreachable" || failure.type === "unknown" || failure.errors.length === 0) {
|
|
2077
|
+
return t("genericError");
|
|
2078
|
+
}
|
|
2079
|
+
return failure.errors[0];
|
|
2080
|
+
}
|
|
2081
|
+
function DevPanel({ className }) {
|
|
2082
|
+
const { client, openError, snapshot, configError, debug, commenter } = useRootContext("Comments.DevPanel");
|
|
2083
|
+
if (!debug && !isLocalPage()) {
|
|
2084
|
+
return null;
|
|
2085
|
+
}
|
|
2086
|
+
const error = openError ?? (snapshot?.status === "error" ? snapshot.error : null) ?? configError ?? null;
|
|
2087
|
+
const origin = typeof window === "undefined" ? "" : window.location.origin;
|
|
2088
|
+
if (!error) {
|
|
2089
|
+
if (!debug || !snapshot) {
|
|
2090
|
+
return null;
|
|
2091
|
+
}
|
|
2092
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("details", { className: cx("gc-devpanel", "gc-devpanel--ok", className), children: [
|
|
2093
|
+
/* @__PURE__ */ jsxRuntime.jsxs("summary", { className: "gc-devpanel__title", children: [
|
|
2094
|
+
chunkIBNNENCZ_cjs.PACKAGE_NAME,
|
|
2095
|
+
" diagnostics"
|
|
2096
|
+
] }),
|
|
2097
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dl", { className: "gc-devpanel__details", children: [
|
|
2098
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Forum" }),
|
|
2099
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: client.forum }),
|
|
2100
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Thread" }),
|
|
2101
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dd", { children: [
|
|
2102
|
+
snapshot.thread.id,
|
|
2103
|
+
" ",
|
|
2104
|
+
snapshot.virtual ? "(not created yet)" : ""
|
|
2105
|
+
] }),
|
|
2106
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Key" }),
|
|
2107
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: snapshot.thread.key }),
|
|
2108
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Updates" }),
|
|
2109
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: snapshot.connection }),
|
|
2110
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Reader" }),
|
|
2111
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dd", { children: [
|
|
2112
|
+
commenter.status,
|
|
2113
|
+
commenter.degraded ? " (degraded)" : ""
|
|
2114
|
+
] })
|
|
2115
|
+
] })
|
|
2116
|
+
] });
|
|
2117
|
+
}
|
|
2118
|
+
const { summary, causes } = diagnose(error, client.forum, origin);
|
|
2119
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2120
|
+
"aside",
|
|
2121
|
+
{
|
|
2122
|
+
className: cx("gc-devpanel", className),
|
|
2123
|
+
"aria-label": `${chunkIBNNENCZ_cjs.PACKAGE_NAME} setup diagnostics`,
|
|
2124
|
+
children: [
|
|
2125
|
+
/* @__PURE__ */ jsxRuntime.jsxs("p", { className: "gc-devpanel__title", children: [
|
|
2126
|
+
chunkIBNNENCZ_cjs.PACKAGE_NAME,
|
|
2127
|
+
" could not load comments"
|
|
2128
|
+
] }),
|
|
2129
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-devpanel__summary", children: summary }),
|
|
2130
|
+
causes.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("ol", { className: "gc-devpanel__causes", children: causes.map((cause) => /* @__PURE__ */ jsxRuntime.jsxs("li", { children: [
|
|
2131
|
+
cause.text,
|
|
2132
|
+
cause.link && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2133
|
+
" ",
|
|
2134
|
+
/* @__PURE__ */ jsxRuntime.jsx("a", { href: cause.link.href, target: "_blank", rel: "noopener noreferrer", children: cause.link.label })
|
|
2135
|
+
] }),
|
|
2136
|
+
cause.code && /* @__PURE__ */ jsxRuntime.jsx("pre", { className: "gc-devpanel__code", children: /* @__PURE__ */ jsxRuntime.jsx("code", { children: cause.code }) })
|
|
2137
|
+
] }, cause.text)) }),
|
|
2138
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dl", { className: "gc-devpanel__details", children: [
|
|
2139
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Forum" }),
|
|
2140
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: client.forum }),
|
|
2141
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Page origin" }),
|
|
2142
|
+
/* @__PURE__ */ jsxRuntime.jsx("dd", { children: origin }),
|
|
2143
|
+
/* @__PURE__ */ jsxRuntime.jsx("dt", { children: "Error" }),
|
|
2144
|
+
/* @__PURE__ */ jsxRuntime.jsxs("dd", { children: [
|
|
2145
|
+
/* @__PURE__ */ jsxRuntime.jsx("code", { children: error.type }),
|
|
2146
|
+
" (status ",
|
|
2147
|
+
error.status,
|
|
2148
|
+
"): ",
|
|
2149
|
+
error.message
|
|
2150
|
+
] })
|
|
2151
|
+
] }),
|
|
2152
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-devpanel__note", children: "Shown on localhost or with debug. Readers see a short \u201CComments are unavailable\u201D notice." })
|
|
2153
|
+
]
|
|
2154
|
+
}
|
|
2155
|
+
);
|
|
2156
|
+
}
|
|
2157
|
+
function diagnose(error, forum, origin) {
|
|
2158
|
+
const host = origin ? new URL(origin).host : "this site's host";
|
|
2159
|
+
const embedding = {
|
|
2160
|
+
href: `${forum}/admin/customize/embedding`,
|
|
2161
|
+
label: "Open Admin \u2192 Customize \u2192 Embedding"
|
|
2162
|
+
};
|
|
2163
|
+
const setting = (name) => ({
|
|
2164
|
+
href: `${forum}/admin/site_settings/category/all_results?filter=${name}`,
|
|
2165
|
+
label: `Open ${name}`
|
|
2166
|
+
});
|
|
2167
|
+
const originNotAllowed = {
|
|
2168
|
+
text: `This page's origin (${origin || "unknown"}) is not allowed: add ${host} as an embedding host, or add the origin to discourse_comments_allowed_origins.`,
|
|
2169
|
+
link: embedding
|
|
2170
|
+
};
|
|
2171
|
+
const pluginDisabled = {
|
|
2172
|
+
text: "The discourse-comments plugin is not installed or is disabled (discourse_comments_enabled).",
|
|
2173
|
+
link: setting("discourse_comments_enabled")
|
|
2174
|
+
};
|
|
2175
|
+
switch (error.type) {
|
|
2176
|
+
case "unreachable":
|
|
2177
|
+
return {
|
|
2178
|
+
summary: `The browser got no usable response from ${forum}; CORS hides the reason, so check these in order.`,
|
|
2179
|
+
causes: [
|
|
2180
|
+
originNotAllowed,
|
|
2181
|
+
{
|
|
2182
|
+
text: "The forum is behind HTTP basic auth, which rejects CORS preflight (OPTIONS) requests. Let OPTIONS through without credentials and pass the credentials with the provider's headers prop, e.g. in nginx:",
|
|
2183
|
+
code: [
|
|
2184
|
+
"location / {",
|
|
2185
|
+
" limit_except OPTIONS {",
|
|
2186
|
+
' auth_basic "Restricted";',
|
|
2187
|
+
" auth_basic_user_file /etc/nginx/.htpasswd;",
|
|
2188
|
+
" }",
|
|
2189
|
+
"}"
|
|
2190
|
+
].join("\n")
|
|
2191
|
+
},
|
|
2192
|
+
pluginDisabled,
|
|
2193
|
+
{
|
|
2194
|
+
text: "The forum URL is wrong or the forum is offline.",
|
|
2195
|
+
link: { href: forum, label: "Open the forum" }
|
|
2196
|
+
}
|
|
2197
|
+
]
|
|
2198
|
+
};
|
|
2199
|
+
case "not_found":
|
|
2200
|
+
return { summary: "The comments API answered 404.", causes: [pluginDisabled] };
|
|
2201
|
+
case "page_not_allowed":
|
|
2202
|
+
return {
|
|
2203
|
+
summary: `${forum} does not accept comments for this page.`,
|
|
2204
|
+
causes: [
|
|
2205
|
+
{
|
|
2206
|
+
text: `Add ${host} as an embedding host (check its allowed paths), or add this origin to discourse_comments_allowed_origins.`,
|
|
2207
|
+
link: embedding
|
|
2208
|
+
}
|
|
2209
|
+
]
|
|
2210
|
+
};
|
|
2211
|
+
case "no_category":
|
|
2212
|
+
return {
|
|
2213
|
+
summary: "The forum has no category for new comment threads.",
|
|
2214
|
+
causes: [
|
|
2215
|
+
{ text: `Pick a category for ${host} under Embedding.`, link: embedding },
|
|
2216
|
+
{
|
|
2217
|
+
text: "Or set discourse_comments_default_category (and discourse_comments_default_author).",
|
|
2218
|
+
link: setting("discourse_comments_default_category")
|
|
2219
|
+
}
|
|
2220
|
+
]
|
|
2221
|
+
};
|
|
2222
|
+
case "invalid_url":
|
|
2223
|
+
return {
|
|
2224
|
+
summary: "The forum rejected the page URL.",
|
|
2225
|
+
causes: [
|
|
2226
|
+
{
|
|
2227
|
+
text: 'Pass an absolute http(s) url prop, or add a <link rel="canonical"> to the page.'
|
|
2228
|
+
}
|
|
2229
|
+
]
|
|
2230
|
+
};
|
|
2231
|
+
case "thread_conflict":
|
|
2232
|
+
return {
|
|
2233
|
+
summary: "The identifier and the URL already belong to different threads.",
|
|
2234
|
+
causes: [
|
|
2235
|
+
{ text: "Pass the identifier this URL was first used with, or only one of the two." }
|
|
2236
|
+
]
|
|
2237
|
+
};
|
|
2238
|
+
case "invalid_access":
|
|
2239
|
+
return {
|
|
2240
|
+
summary: "The forum refused access to this thread.",
|
|
2241
|
+
causes: [
|
|
2242
|
+
{
|
|
2243
|
+
text: "The thread's category may not be visible to this reader; check the category's security settings."
|
|
2244
|
+
}
|
|
2245
|
+
]
|
|
2246
|
+
};
|
|
2247
|
+
default:
|
|
2248
|
+
return { summary: error.message, causes: [] };
|
|
2249
|
+
}
|
|
2250
|
+
}
|
|
2251
|
+
function Header({ children, className }) {
|
|
2252
|
+
const { elements } = useRootContext("Comments.Header");
|
|
2253
|
+
return /* @__PURE__ */ jsxRuntime.jsx("header", { className: cx("gc-header", elements.header, className), children: children ?? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2254
|
+
/* @__PURE__ */ jsxRuntime.jsx(Count, {}),
|
|
2255
|
+
/* @__PURE__ */ jsxRuntime.jsx(Sort, {})
|
|
2256
|
+
] }) });
|
|
2257
|
+
}
|
|
2258
|
+
function Count({ as: Tag = "h2", render, className }) {
|
|
2259
|
+
const { t, snapshot, elements } = useRootContext("Comments.Count");
|
|
2260
|
+
const count = snapshot?.thread.count;
|
|
2261
|
+
return /* @__PURE__ */ jsxRuntime.jsx(Tag, { className: cx("gc-header__count", elements.count, className), children: count === void 0 ? t("comments") : render ? render(count) : t("commentCount", { count }) });
|
|
2262
|
+
}
|
|
2263
|
+
function Sort({ className }) {
|
|
2264
|
+
const { t, thread, snapshot, elements, reportError } = useRootContext("Comments.Sort");
|
|
2265
|
+
const id = react.useId();
|
|
2266
|
+
if (!thread || !snapshot || snapshot.thread.sorts.length < 2) {
|
|
2267
|
+
return null;
|
|
2268
|
+
}
|
|
2269
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx("gc-sort", elements.sort, className), children: [
|
|
2270
|
+
/* @__PURE__ */ jsxRuntime.jsx("label", { className: "gc-sort__label", htmlFor: id, children: t("sortBy") }),
|
|
2271
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2272
|
+
"select",
|
|
2273
|
+
{
|
|
2274
|
+
id,
|
|
2275
|
+
className: "gc-sort__select",
|
|
2276
|
+
value: snapshot.sort,
|
|
2277
|
+
disabled: snapshot.status === "loading",
|
|
2278
|
+
onChange: (event) => void thread.setSort(event.target.value).catch(
|
|
2279
|
+
(error) => reportError("load", error)
|
|
2280
|
+
),
|
|
2281
|
+
children: snapshot.thread.sorts.map((sort) => /* @__PURE__ */ jsxRuntime.jsx("option", { value: sort, children: t(`sort.${sort}`) }, sort))
|
|
2282
|
+
}
|
|
2283
|
+
)
|
|
2284
|
+
] });
|
|
2285
|
+
}
|
|
2286
|
+
var defaultRender = (comment, depth) => /* @__PURE__ */ jsxRuntime.jsx(Item, { comment, depth });
|
|
2287
|
+
var ListContext = react.createContext(defaultRender);
|
|
2288
|
+
function List({ children, className }) {
|
|
2289
|
+
const root = useRootContext("Comments.List");
|
|
2290
|
+
const render = children ?? defaultRender;
|
|
2291
|
+
const { snapshot, openError, t, elements, focusView } = root;
|
|
2292
|
+
if (openError || snapshot?.status === "error") {
|
|
2293
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx("gc-notice", "gc-notice--error", elements.notice), role: "alert", children: [
|
|
2294
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-notice__text", children: t("unavailable") }),
|
|
2295
|
+
/* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: cx("gc-button", elements.button), onClick: root.retry, children: t("retry") })
|
|
2296
|
+
] });
|
|
2297
|
+
}
|
|
2298
|
+
if (!snapshot || snapshot.status === "loading" && snapshot.comments.length === 0) {
|
|
2299
|
+
return /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-list__status", role: "status", children: t("loading") });
|
|
2300
|
+
}
|
|
2301
|
+
const listClass = cx("gc-list", elements.list, className);
|
|
2302
|
+
if (focusView) {
|
|
2303
|
+
const tree = snapshot.comments;
|
|
2304
|
+
const target = chunkIBNNENCZ_cjs.findComment(tree, focusView.id)?.node;
|
|
2305
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ListContext.Provider, { value: render, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-focus", children: [
|
|
2306
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
2307
|
+
"button",
|
|
2308
|
+
{
|
|
2309
|
+
type: "button",
|
|
2310
|
+
className: cx("gc-button", "gc-button--link", "gc-focus__back", elements.button),
|
|
2311
|
+
onClick: root.closeFocusView,
|
|
2312
|
+
children: [
|
|
2313
|
+
/* @__PURE__ */ jsxRuntime.jsx(BackIcon, {}),
|
|
2314
|
+
t("backToAll")
|
|
2315
|
+
]
|
|
2316
|
+
}
|
|
2317
|
+
),
|
|
2318
|
+
focusView.truncated && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-focus__truncated", children: t("ancestorsTruncated") }),
|
|
2319
|
+
focusView.ancestors.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("ol", { className: "gc-context", children: focusView.ancestors.map((ancestor) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2320
|
+
Item,
|
|
2321
|
+
{
|
|
2322
|
+
comment: chunkIBNNENCZ_cjs.findComment(tree, ancestor.id)?.node ?? ancestor,
|
|
2323
|
+
showReplies: false
|
|
2324
|
+
},
|
|
2325
|
+
ancestor.id
|
|
2326
|
+
)) }),
|
|
2327
|
+
target && /* @__PURE__ */ jsxRuntime.jsx("ol", { className: listClass, children: render(target, 0) })
|
|
2328
|
+
] }) });
|
|
2329
|
+
}
|
|
2330
|
+
if (snapshot.comments.length === 0) {
|
|
2331
|
+
return null;
|
|
2332
|
+
}
|
|
2333
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ListContext.Provider, { value: render, children: /* @__PURE__ */ jsxRuntime.jsx("ol", { className: listClass, children: snapshot.comments.map((comment) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: render(comment, 0) }, comment.id)) }) });
|
|
2334
|
+
}
|
|
2335
|
+
function Item({ comment, depth = 0, children, showReplies = true, className }) {
|
|
2336
|
+
const root = useRootContext("Comments.Item");
|
|
2337
|
+
const { t, elements } = root;
|
|
2338
|
+
const [mode, setModeState] = react.useState("view");
|
|
2339
|
+
const [notice, setNotice] = react.useState(null);
|
|
2340
|
+
const articleRef = react.useRef(null);
|
|
2341
|
+
const triggerRef = react.useRef(null);
|
|
2342
|
+
const restoreFocus = react.useRef(false);
|
|
2343
|
+
const setMode = react.useCallback((next, trigger) => {
|
|
2344
|
+
if (next === "view") {
|
|
2345
|
+
restoreFocus.current = true;
|
|
2346
|
+
} else {
|
|
2347
|
+
triggerRef.current = trigger ?? null;
|
|
2348
|
+
}
|
|
2349
|
+
setModeState(next);
|
|
2350
|
+
}, []);
|
|
2351
|
+
react.useEffect(() => {
|
|
2352
|
+
if (mode !== "view" || !restoreFocus.current) {
|
|
2353
|
+
return;
|
|
2354
|
+
}
|
|
2355
|
+
restoreFocus.current = false;
|
|
2356
|
+
const trigger = triggerRef.current;
|
|
2357
|
+
(trigger?.isConnected ? trigger : articleRef.current)?.focus();
|
|
2358
|
+
}, [mode]);
|
|
2359
|
+
const signedIn = root.commenter.status === "signed-in";
|
|
2360
|
+
const writable = root.writable && signedIn;
|
|
2361
|
+
const allowed = {
|
|
2362
|
+
view: true,
|
|
2363
|
+
reply: root.canCompose,
|
|
2364
|
+
edit: writable && comment.can.edit,
|
|
2365
|
+
report: writable && comment.can.flag,
|
|
2366
|
+
delete: writable && comment.can.delete
|
|
2367
|
+
};
|
|
2368
|
+
const modeAllowed = allowed[mode];
|
|
2369
|
+
const activeMode = modeAllowed ? mode : "view";
|
|
2370
|
+
react.useEffect(() => {
|
|
2371
|
+
if (!modeAllowed) {
|
|
2372
|
+
setMode("view");
|
|
2373
|
+
}
|
|
2374
|
+
}, [modeAllowed, setMode]);
|
|
2375
|
+
react.useEffect(() => {
|
|
2376
|
+
if (!notice || notice.error) {
|
|
2377
|
+
return;
|
|
2378
|
+
}
|
|
2379
|
+
const timer = setTimeout(() => setNotice(null), 3e3);
|
|
2380
|
+
return () => clearTimeout(timer);
|
|
2381
|
+
}, [notice]);
|
|
2382
|
+
const value = react.useMemo(
|
|
2383
|
+
() => ({ comment, depth, mode: activeMode, setMode, notice, setNotice }),
|
|
2384
|
+
[comment, depth, activeMode, setMode, notice]
|
|
2385
|
+
);
|
|
2386
|
+
const name = comment.author ? comment.author.name || comment.author.username : null;
|
|
2387
|
+
const label = comment.deleted || !name ? t("deletedComment") : t("commentBy", { name });
|
|
2388
|
+
const close = () => setMode("view");
|
|
2389
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ItemContext.Provider, { value, children: /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2390
|
+
"li",
|
|
2391
|
+
{
|
|
2392
|
+
className: cx(
|
|
2393
|
+
"gc-comment",
|
|
2394
|
+
depth > 0 && "gc-comment--reply",
|
|
2395
|
+
comment.pinned && "gc-comment--pinned",
|
|
2396
|
+
comment.deleted && "gc-comment--deleted",
|
|
2397
|
+
root.highlightId === comment.id && "gc-comment--highlighted",
|
|
2398
|
+
elements.comment,
|
|
2399
|
+
className
|
|
2400
|
+
),
|
|
2401
|
+
"data-depth": depth,
|
|
2402
|
+
children: [
|
|
2403
|
+
/* @__PURE__ */ jsxRuntime.jsxs(
|
|
2404
|
+
"article",
|
|
2405
|
+
{
|
|
2406
|
+
ref: articleRef,
|
|
2407
|
+
id: `gc-${comment.id}`,
|
|
2408
|
+
className: "gc-comment__card",
|
|
2409
|
+
tabIndex: -1,
|
|
2410
|
+
"aria-label": label,
|
|
2411
|
+
children: [
|
|
2412
|
+
children ?? /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2413
|
+
/* @__PURE__ */ jsxRuntime.jsx(Avatar, {}),
|
|
2414
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { className: "gc-comment__main", children: [
|
|
2415
|
+
/* @__PURE__ */ jsxRuntime.jsx(Meta, {}),
|
|
2416
|
+
/* @__PURE__ */ jsxRuntime.jsx(Body, {}),
|
|
2417
|
+
/* @__PURE__ */ jsxRuntime.jsx(Actions, {})
|
|
2418
|
+
] })
|
|
2419
|
+
] }),
|
|
2420
|
+
activeMode === "report" && /* @__PURE__ */ jsxRuntime.jsx(ReportForm, { onClose: close }),
|
|
2421
|
+
activeMode === "delete" && /* @__PURE__ */ jsxRuntime.jsx(DeleteConfirm, { onClose: close }),
|
|
2422
|
+
notice && /* @__PURE__ */ jsxRuntime.jsx(
|
|
2423
|
+
"p",
|
|
2424
|
+
{
|
|
2425
|
+
className: cx("gc-comment__notice", notice.error && "gc-comment__notice--error"),
|
|
2426
|
+
role: notice.error ? "alert" : "status",
|
|
2427
|
+
children: notice.text
|
|
2428
|
+
}
|
|
2429
|
+
)
|
|
2430
|
+
]
|
|
2431
|
+
}
|
|
2432
|
+
),
|
|
2433
|
+
activeMode === "reply" && /* @__PURE__ */ jsxRuntime.jsx(Composer, { replyTo: comment, autoFocus: true, onDone: close }),
|
|
2434
|
+
showReplies && /* @__PURE__ */ jsxRuntime.jsx(Replies, {})
|
|
2435
|
+
]
|
|
2436
|
+
}
|
|
2437
|
+
) });
|
|
2438
|
+
}
|
|
2439
|
+
function Replies() {
|
|
2440
|
+
const root = useRootContext();
|
|
2441
|
+
const { comment, depth } = useItemContext("Comments.Item");
|
|
2442
|
+
const render = react.useContext(ListContext);
|
|
2443
|
+
const { t, thread, snapshot, maxDepth, elements } = root;
|
|
2444
|
+
if (!thread || !snapshot) {
|
|
2445
|
+
return null;
|
|
2446
|
+
}
|
|
2447
|
+
const children = comment.children;
|
|
2448
|
+
const linkButton = cx("gc-button", "gc-button--link", elements.button);
|
|
2449
|
+
if (depth >= maxDepth) {
|
|
2450
|
+
if (comment.direct_reply_count === 0 && children.length === 0) {
|
|
2451
|
+
return null;
|
|
2452
|
+
}
|
|
2453
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2454
|
+
"button",
|
|
2455
|
+
{
|
|
2456
|
+
type: "button",
|
|
2457
|
+
className: cx(linkButton, "gc-comment__continue"),
|
|
2458
|
+
onClick: () => void root.continueThread(comment.id),
|
|
2459
|
+
children: t("continueThread")
|
|
2460
|
+
}
|
|
2461
|
+
);
|
|
2462
|
+
}
|
|
2463
|
+
const branch = snapshot.branches[comment.id];
|
|
2464
|
+
const remaining = comment.direct_reply_count - children.length;
|
|
2465
|
+
const hasMore = (branch?.hasMore ?? true) && remaining > 0;
|
|
2466
|
+
const name = comment.author?.name || comment.author?.username || "";
|
|
2467
|
+
const loadReplies = async () => {
|
|
2468
|
+
const before = new Set(children.map((child) => child.id));
|
|
2469
|
+
try {
|
|
2470
|
+
await thread.loadReplies(comment.id);
|
|
2471
|
+
} catch (error) {
|
|
2472
|
+
root.reportError("loadReplies", error);
|
|
2473
|
+
return;
|
|
2474
|
+
}
|
|
2475
|
+
const loaded3 = chunkIBNNENCZ_cjs.findComment(thread.snapshot().comments, comment.id)?.node.children ?? [];
|
|
2476
|
+
const first = loaded3.find((child) => !before.has(child.id));
|
|
2477
|
+
if (first) {
|
|
2478
|
+
root.focusComment(first.id);
|
|
2479
|
+
}
|
|
2480
|
+
};
|
|
2481
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2482
|
+
children.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("ol", { className: "gc-replies", "aria-label": t("repliesTo", { name }), children: children.map((child) => /* @__PURE__ */ jsxRuntime.jsx(react.Fragment, { children: render(child, depth + 1) }, child.id)) }),
|
|
2483
|
+
hasMore && /* @__PURE__ */ jsxRuntime.jsx(
|
|
2484
|
+
"button",
|
|
2485
|
+
{
|
|
2486
|
+
type: "button",
|
|
2487
|
+
className: cx(linkButton, "gc-comment__more"),
|
|
2488
|
+
disabled: branch?.loading,
|
|
2489
|
+
"aria-busy": branch?.loading || void 0,
|
|
2490
|
+
onClick: () => void loadReplies(),
|
|
2491
|
+
children: children.length === 0 ? t("loadReplies", { count: remaining }) : t("loadMoreReplies", { count: remaining })
|
|
2492
|
+
}
|
|
2493
|
+
)
|
|
2494
|
+
] });
|
|
2495
|
+
}
|
|
2496
|
+
function SignIn({ children, className }) {
|
|
2497
|
+
const root = useRootContext("Comments.SignIn");
|
|
2498
|
+
const { t, commenter, elements, client, config, snapshot, signInMode } = root;
|
|
2499
|
+
const titleId = react.useId();
|
|
2500
|
+
if (children) {
|
|
2501
|
+
return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: children(commenter) });
|
|
2502
|
+
}
|
|
2503
|
+
if (signInMode === "manual") {
|
|
2504
|
+
return null;
|
|
2505
|
+
}
|
|
2506
|
+
const button = cx("gc-button", elements.button);
|
|
2507
|
+
const classes = cx("gc-signin", elements.signIn, className);
|
|
2508
|
+
if (commenter.status === "signed-in" && commenter.user) {
|
|
2509
|
+
const { user } = commenter;
|
|
2510
|
+
return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: cx(classes, "gc-signin--signed-in"), children: [
|
|
2511
|
+
/* @__PURE__ */ jsxRuntime.jsx(UserAvatar, { user, className: "gc-avatar--small" }),
|
|
2512
|
+
/* @__PURE__ */ jsxRuntime.jsx("span", { className: "gc-signin__user", children: t("signedInAs", { username: `@${user.username}` }) }),
|
|
2513
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2514
|
+
"button",
|
|
2515
|
+
{
|
|
2516
|
+
type: "button",
|
|
2517
|
+
className: cx(button, "gc-button--link"),
|
|
2518
|
+
onClick: () => void commenter.signOut(),
|
|
2519
|
+
children: t("signOut")
|
|
2520
|
+
}
|
|
2521
|
+
)
|
|
2522
|
+
] });
|
|
2523
|
+
}
|
|
2524
|
+
const closed = snapshot?.thread.closed ?? false;
|
|
2525
|
+
if (signInMode !== "inline" || !root.writable || closed || !snapshot) {
|
|
2526
|
+
return null;
|
|
2527
|
+
}
|
|
2528
|
+
const forumName = config?.forum.name ?? new URL(client.forum).host;
|
|
2529
|
+
const signingIn = commenter.status === "signing-in";
|
|
2530
|
+
const failure = commenter.error && commenter.error.type !== "cancelled" ? commenter.error.type === "invalid_key" ? t("sessionExpired") : commenter.error.errors[0] ?? t("genericError") : null;
|
|
2531
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
2532
|
+
"div",
|
|
2533
|
+
{
|
|
2534
|
+
className: cx(classes, signingIn && "gc-signin--signing-in"),
|
|
2535
|
+
role: "group",
|
|
2536
|
+
"aria-labelledby": titleId,
|
|
2537
|
+
children: [
|
|
2538
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { id: titleId, className: "gc-signin__title", children: t("joinConversation") }),
|
|
2539
|
+
/* @__PURE__ */ jsxRuntime.jsxs("div", { "aria-live": "polite", children: [
|
|
2540
|
+
signingIn ? /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-signin__body", children: t("signingIn") }) : /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-signin__body", children: t("joinConversationBody", { forum: forumName }) }),
|
|
2541
|
+
failure && !signingIn && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "gc-signin__error", role: "alert", children: failure })
|
|
2542
|
+
] }),
|
|
2543
|
+
/* @__PURE__ */ jsxRuntime.jsx("div", { className: "gc-signin__actions", children: signingIn ? /* @__PURE__ */ jsxRuntime.jsx("button", { type: "button", className: button, onClick: () => commenter.cancelSignIn(), children: t("signInCancel") }) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
|
|
2544
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
2545
|
+
"button",
|
|
2546
|
+
{
|
|
2547
|
+
type: "button",
|
|
2548
|
+
className: cx(button, "gc-button--primary", elements.primaryButton),
|
|
2549
|
+
onClick: () => void commenter.signIn().catch(() => {
|
|
2550
|
+
}),
|
|
2551
|
+
children: t("signIn")
|
|
2552
|
+
}
|
|
2553
|
+
),
|
|
2554
|
+
commenter.providers.map((provider) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2555
|
+
"button",
|
|
2556
|
+
{
|
|
2557
|
+
type: "button",
|
|
2558
|
+
className: button,
|
|
2559
|
+
onClick: () => void commenter.signIn({ provider: provider.name }).catch(() => {
|
|
2560
|
+
}),
|
|
2561
|
+
children: t("signInWith", { provider: provider.title })
|
|
2562
|
+
},
|
|
2563
|
+
provider.name
|
|
2564
|
+
))
|
|
2565
|
+
] }) })
|
|
2566
|
+
]
|
|
2567
|
+
}
|
|
2568
|
+
);
|
|
2569
|
+
}
|
|
2570
|
+
function Notice({ className }) {
|
|
2571
|
+
const { t, snapshot, readOnly, commenter, commentingDenied, elements } = useRootContext("Comments.Notice");
|
|
2572
|
+
if (!snapshot || snapshot.status === "error") {
|
|
2573
|
+
return null;
|
|
2574
|
+
}
|
|
2575
|
+
let text = null;
|
|
2576
|
+
let modifier = "";
|
|
2577
|
+
if (readOnly || commenter.degraded) {
|
|
2578
|
+
text = t("readOnly");
|
|
2579
|
+
modifier = "gc-notice--read-only";
|
|
2580
|
+
} else if (snapshot.thread.closed) {
|
|
2581
|
+
text = t("closed");
|
|
2582
|
+
modifier = "gc-notice--closed";
|
|
2583
|
+
} else if (commentingDenied) {
|
|
2584
|
+
text = t("cannotComment");
|
|
2585
|
+
modifier = "gc-notice--denied";
|
|
2586
|
+
}
|
|
2587
|
+
if (!text) {
|
|
2588
|
+
return null;
|
|
2589
|
+
}
|
|
2590
|
+
return /* @__PURE__ */ jsxRuntime.jsx("p", { className: cx("gc-notice", modifier, elements.notice, className), role: "status", children: text });
|
|
2591
|
+
}
|
|
2592
|
+
function NewComments({ className }) {
|
|
2593
|
+
const root = useRootContext("Comments.NewComments");
|
|
2594
|
+
const { t, thread, snapshot, elements, focusView } = root;
|
|
2595
|
+
const count = focusView ? 0 : snapshot?.pendingNew ?? 0;
|
|
2596
|
+
const show = () => {
|
|
2597
|
+
if (!thread) {
|
|
2598
|
+
return;
|
|
2599
|
+
}
|
|
2600
|
+
const before = new Set(thread.snapshot().comments.map((comment) => comment.id));
|
|
2601
|
+
thread.showPending();
|
|
2602
|
+
const first = thread.snapshot().comments.find((comment) => !before.has(comment.id));
|
|
2603
|
+
if (first) {
|
|
2604
|
+
root.focusComment(first.id);
|
|
2605
|
+
}
|
|
2606
|
+
};
|
|
2607
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "gc-pill-region", "aria-live": "polite", children: count > 0 && /* @__PURE__ */ jsxRuntime.jsx(
|
|
2608
|
+
"button",
|
|
2609
|
+
{
|
|
2610
|
+
type: "button",
|
|
2611
|
+
className: cx("gc-button", "gc-pill", elements.button, elements.pill, className),
|
|
2612
|
+
onClick: show,
|
|
2613
|
+
children: t("newComments", { count })
|
|
2614
|
+
}
|
|
2615
|
+
) });
|
|
2616
|
+
}
|
|
2617
|
+
function AwaitingApproval({ className }) {
|
|
2618
|
+
const { t, snapshot, elements } = useRootContext("Comments.AwaitingApproval");
|
|
2619
|
+
const pending = snapshot?.awaitingApproval ?? [];
|
|
2620
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", { className: "gc-pending-region", "aria-live": "polite", children: pending.map((entry, index) => /* @__PURE__ */ jsxRuntime.jsx(
|
|
2621
|
+
"p",
|
|
2622
|
+
{
|
|
2623
|
+
className: cx("gc-notice", "gc-notice--pending", elements.notice, className),
|
|
2624
|
+
children: entry.message || t("pending")
|
|
2625
|
+
},
|
|
2626
|
+
entry.reviewable_id ?? `pending-${index}`
|
|
2627
|
+
)) });
|
|
2628
|
+
}
|
|
2629
|
+
function LoadMore({ className }) {
|
|
2630
|
+
const root = useRootContext("Comments.LoadMore");
|
|
2631
|
+
const { t, thread, snapshot, elements, focusView } = root;
|
|
2632
|
+
if (!thread || !snapshot?.hasMore || focusView || snapshot.status !== "ready") {
|
|
2633
|
+
return null;
|
|
2634
|
+
}
|
|
2635
|
+
const loadMore = async () => {
|
|
2636
|
+
const before = new Set(thread.snapshot().comments.map((comment) => comment.id));
|
|
2637
|
+
try {
|
|
2638
|
+
await thread.loadMore();
|
|
2639
|
+
} catch (error) {
|
|
2640
|
+
root.reportError("loadMore", error);
|
|
2641
|
+
return;
|
|
2642
|
+
}
|
|
2643
|
+
const first = thread.snapshot().comments.find((comment) => !before.has(comment.id));
|
|
2644
|
+
if (first && !thread.snapshot().hasMore) {
|
|
2645
|
+
root.focusComment(first.id);
|
|
2646
|
+
}
|
|
2647
|
+
};
|
|
2648
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
2649
|
+
"button",
|
|
2650
|
+
{
|
|
2651
|
+
type: "button",
|
|
2652
|
+
className: cx("gc-button", "gc-load-more", elements.button, elements.loadMore, className),
|
|
2653
|
+
disabled: snapshot.loadingMore,
|
|
2654
|
+
"aria-busy": snapshot.loadingMore || void 0,
|
|
2655
|
+
onClick: () => void loadMore(),
|
|
2656
|
+
children: snapshot.loadingMore ? t("loading") : t("loadMore")
|
|
2657
|
+
}
|
|
2658
|
+
);
|
|
2659
|
+
}
|
|
2660
|
+
function CommentsSection(props) {
|
|
2661
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(Root, { ...props, children: [
|
|
2662
|
+
/* @__PURE__ */ jsxRuntime.jsx(Header, {}),
|
|
2663
|
+
/* @__PURE__ */ jsxRuntime.jsx(DevPanel, {}),
|
|
2664
|
+
/* @__PURE__ */ jsxRuntime.jsx(Notice, {}),
|
|
2665
|
+
/* @__PURE__ */ jsxRuntime.jsx(SignIn, {}),
|
|
2666
|
+
/* @__PURE__ */ jsxRuntime.jsx(Composer, {}),
|
|
2667
|
+
/* @__PURE__ */ jsxRuntime.jsx(AwaitingApproval, {}),
|
|
2668
|
+
/* @__PURE__ */ jsxRuntime.jsx(NewComments, {}),
|
|
2669
|
+
/* @__PURE__ */ jsxRuntime.jsx(List, {}),
|
|
2670
|
+
/* @__PURE__ */ jsxRuntime.jsx(LoadMore, {})
|
|
2671
|
+
] });
|
|
2672
|
+
}
|
|
2673
|
+
var Comments = Object.assign(CommentsSection, {
|
|
2674
|
+
Root,
|
|
2675
|
+
Header,
|
|
2676
|
+
Count,
|
|
2677
|
+
Sort,
|
|
2678
|
+
SignIn,
|
|
2679
|
+
Composer,
|
|
2680
|
+
List,
|
|
2681
|
+
Item,
|
|
2682
|
+
Avatar,
|
|
2683
|
+
Meta,
|
|
2684
|
+
Body,
|
|
2685
|
+
Actions,
|
|
2686
|
+
LoadMore,
|
|
2687
|
+
NewComments,
|
|
2688
|
+
AwaitingApproval,
|
|
2689
|
+
Notice,
|
|
2690
|
+
DevPanel
|
|
2691
|
+
});
|
|
2692
|
+
function useCommentCount(ref, options = {}) {
|
|
2693
|
+
const client = useOptionalCommentsContext(options.forum)?.client;
|
|
2694
|
+
const key = ref ? safeEncode(ref) : null;
|
|
2695
|
+
const latestRef = react.useRef(ref);
|
|
2696
|
+
latestRef.current = ref;
|
|
2697
|
+
const [state, setState] = react.useState({
|
|
2698
|
+
key: null,
|
|
2699
|
+
count: null,
|
|
2700
|
+
error: null
|
|
2701
|
+
});
|
|
2702
|
+
react.useEffect(() => {
|
|
2703
|
+
const current = latestRef.current;
|
|
2704
|
+
if (key === null || !current || !client) {
|
|
2705
|
+
return;
|
|
2706
|
+
}
|
|
2707
|
+
let active = true;
|
|
2708
|
+
client.count(current).then(
|
|
2709
|
+
(count) => active && setState({ key, count, error: null }),
|
|
2710
|
+
(error) => active && setState({ key, count: null, error: toCommentsError(error) })
|
|
2711
|
+
);
|
|
2712
|
+
return () => {
|
|
2713
|
+
active = false;
|
|
2714
|
+
};
|
|
2715
|
+
}, [client, key]);
|
|
2716
|
+
return state.key === key ? { count: state.count, error: state.error } : { count: null, error: null };
|
|
2717
|
+
}
|
|
2718
|
+
function safeEncode(ref) {
|
|
2719
|
+
try {
|
|
2720
|
+
return chunkIBNNENCZ_cjs.encodeRef(ref);
|
|
2721
|
+
} catch {
|
|
2722
|
+
return null;
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
function CommentCount({
|
|
2726
|
+
identifier,
|
|
2727
|
+
host,
|
|
2728
|
+
url,
|
|
2729
|
+
topicId,
|
|
2730
|
+
forum,
|
|
2731
|
+
render,
|
|
2732
|
+
className
|
|
2733
|
+
}) {
|
|
2734
|
+
const context = useOptionalCommentsContext(forum);
|
|
2735
|
+
const pageUrl = url ?? chunkIBNNENCZ_cjs.defaultPageUrl();
|
|
2736
|
+
const ref = topicId !== void 0 ? { topicId } : identifier !== void 0 ? { identifier, host } : pageUrl ? { url: pageUrl } : null;
|
|
2737
|
+
const { count } = useCommentCount(ref, { forum });
|
|
2738
|
+
if (!context) {
|
|
2739
|
+
return null;
|
|
2740
|
+
}
|
|
2741
|
+
const { t } = context;
|
|
2742
|
+
return /* @__PURE__ */ jsxRuntime.jsx("span", { className: cx("gc-count", className), "aria-busy": count === null || void 0, children: count === null ? null : render ? render(count) : t("commentCount", { count }) });
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
exports.CommentCount = CommentCount;
|
|
2746
|
+
exports.Comments = Comments;
|
|
2747
|
+
exports.DiscourseComments = DiscourseComments;
|
|
2748
|
+
exports.useClient = useClient;
|
|
2749
|
+
exports.useCommentCount = useCommentCount;
|
|
2750
|
+
exports.useCommenter = useCommenter;
|
|
2751
|
+
exports.useForumTheme = useForumTheme;
|
|
2752
|
+
exports.useThread = useThread;
|
|
2753
|
+
//# sourceMappingURL=react.cjs.map
|
|
2754
|
+
//# sourceMappingURL=react.cjs.map
|