@sweberdev/witness-react 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +18 -2
- package/dist/index.cjs +346 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +96 -0
- package/dist/index.d.ts +96 -0
- package/dist/index.js +321 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -3
- package/src/AiContent.tsx +65 -0
- package/src/AiLabel.tsx +138 -0
- package/src/AiNotice.tsx +104 -0
- package/src/context.tsx +40 -0
- package/src/index.ts +5 -0
- package/src/useAiDisclosure.ts +67 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/AiContent.tsx
|
|
4
|
+
import {
|
|
5
|
+
createMarking,
|
|
6
|
+
markingAttributes
|
|
7
|
+
} from "@sweberdev/witness";
|
|
8
|
+
import { useMemo as useMemo3 } from "react";
|
|
9
|
+
|
|
10
|
+
// src/AiLabel.tsx
|
|
11
|
+
import { format } from "@sweberdev/witness";
|
|
12
|
+
import { useEffect as useEffect2, useId as useId2, useRef as useRef2, useState as useState2 } from "react";
|
|
13
|
+
|
|
14
|
+
// src/AiNotice.tsx
|
|
15
|
+
import { useEffect, useId, useState } from "react";
|
|
16
|
+
|
|
17
|
+
// src/context.tsx
|
|
18
|
+
import {
|
|
19
|
+
getMessages,
|
|
20
|
+
resolveLocale
|
|
21
|
+
} from "@sweberdev/witness";
|
|
22
|
+
import { createContext, useContext, useMemo } from "react";
|
|
23
|
+
import { jsx } from "react/jsx-runtime";
|
|
24
|
+
var WitnessContext = createContext(null);
|
|
25
|
+
function WitnessProvider({ locale, messages, children }) {
|
|
26
|
+
const value = useMemo(
|
|
27
|
+
() => ({ locale: resolveLocale(locale), messages: getMessages(locale, messages) }),
|
|
28
|
+
[locale, messages]
|
|
29
|
+
);
|
|
30
|
+
return /* @__PURE__ */ jsx(WitnessContext.Provider, { value, children });
|
|
31
|
+
}
|
|
32
|
+
function useWitnessMessages(locale) {
|
|
33
|
+
const context = useContext(WitnessContext);
|
|
34
|
+
return useMemo(() => {
|
|
35
|
+
if (locale) return { locale: resolveLocale(locale), messages: getMessages(locale) };
|
|
36
|
+
return context ?? { locale: "en", messages: getMessages("en") };
|
|
37
|
+
}, [context, locale]);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/useAiDisclosure.ts
|
|
41
|
+
import { createDisclosure } from "@sweberdev/witness";
|
|
42
|
+
import { useCallback, useMemo as useMemo2, useRef, useSyncExternalStore } from "react";
|
|
43
|
+
function useAiDisclosure(options = {}) {
|
|
44
|
+
const onEvent = useRef(options.onEvent);
|
|
45
|
+
onEvent.current = options.onEvent;
|
|
46
|
+
const { id, kind, version, locale, storage } = options;
|
|
47
|
+
const disclosure = useMemo2(
|
|
48
|
+
() => createDisclosure({
|
|
49
|
+
...id ? { id } : {},
|
|
50
|
+
...kind ? { kind } : {},
|
|
51
|
+
...version ? { version } : {},
|
|
52
|
+
...locale ? { locale } : {},
|
|
53
|
+
...storage ? { storage } : {},
|
|
54
|
+
onEvent: (event) => onEvent.current?.(event)
|
|
55
|
+
}),
|
|
56
|
+
[id, kind, version, locale, storage]
|
|
57
|
+
);
|
|
58
|
+
const subscribe = useCallback(
|
|
59
|
+
(listener) => {
|
|
60
|
+
const unsubscribe = disclosure.subscribe(listener);
|
|
61
|
+
const onStorage = (event) => {
|
|
62
|
+
if (event.key === null || event.key.startsWith("witness:ack:")) listener();
|
|
63
|
+
};
|
|
64
|
+
if (typeof window !== "undefined") window.addEventListener("storage", onStorage);
|
|
65
|
+
return () => {
|
|
66
|
+
unsubscribe();
|
|
67
|
+
if (typeof window !== "undefined") window.removeEventListener("storage", onStorage);
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
[disclosure]
|
|
71
|
+
);
|
|
72
|
+
const acknowledgedAt = useSyncExternalStore(
|
|
73
|
+
subscribe,
|
|
74
|
+
() => disclosure.acknowledgedAt(),
|
|
75
|
+
() => null
|
|
76
|
+
);
|
|
77
|
+
return {
|
|
78
|
+
needsAcknowledgement: acknowledgedAt === null,
|
|
79
|
+
acknowledgedAt,
|
|
80
|
+
acknowledge: disclosure.acknowledge,
|
|
81
|
+
markShown: disclosure.markShown,
|
|
82
|
+
reset: disclosure.reset,
|
|
83
|
+
disclosure
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/AiNotice.tsx
|
|
88
|
+
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
89
|
+
function AiNotice({
|
|
90
|
+
kind = "chatbot",
|
|
91
|
+
id,
|
|
92
|
+
version,
|
|
93
|
+
locale,
|
|
94
|
+
href,
|
|
95
|
+
storage,
|
|
96
|
+
onEvent,
|
|
97
|
+
children,
|
|
98
|
+
className
|
|
99
|
+
}) {
|
|
100
|
+
const { locale: code, messages } = useWitnessMessages(locale);
|
|
101
|
+
const disclosure = useAiDisclosure({
|
|
102
|
+
kind,
|
|
103
|
+
locale: code,
|
|
104
|
+
...id ? { id } : {},
|
|
105
|
+
...version ? { version } : {},
|
|
106
|
+
...storage ? { storage } : {},
|
|
107
|
+
...onEvent ? { onEvent } : {}
|
|
108
|
+
});
|
|
109
|
+
const [reopened, setReopened] = useState(false);
|
|
110
|
+
const titleId = useId();
|
|
111
|
+
const text = messages.kinds[kind];
|
|
112
|
+
const full = reopened || disclosure.needsAcknowledgement;
|
|
113
|
+
const { markShown } = disclosure;
|
|
114
|
+
useEffect(() => {
|
|
115
|
+
if (full) markShown();
|
|
116
|
+
}, [full, markShown]);
|
|
117
|
+
if (!full) {
|
|
118
|
+
return /* @__PURE__ */ jsxs(
|
|
119
|
+
"button",
|
|
120
|
+
{
|
|
121
|
+
type: "button",
|
|
122
|
+
className: cx("witness-notice--compact", className),
|
|
123
|
+
"aria-label": `${text.label}: ${messages.ui.details}`,
|
|
124
|
+
onClick: () => setReopened(true),
|
|
125
|
+
children: [
|
|
126
|
+
/* @__PURE__ */ jsx2("span", { className: "witness-label__icon", "aria-hidden": "true", children: "AI" }),
|
|
127
|
+
text.label
|
|
128
|
+
]
|
|
129
|
+
}
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return /* @__PURE__ */ jsxs("section", { className: cx("witness-notice", className), role: "note", "aria-labelledby": titleId, children: [
|
|
133
|
+
/* @__PURE__ */ jsx2("span", { className: "witness-label__icon", "aria-hidden": "true", children: "AI" }),
|
|
134
|
+
/* @__PURE__ */ jsxs("div", { children: [
|
|
135
|
+
/* @__PURE__ */ jsx2("h2", { id: titleId, className: "witness-notice__title", children: text.title }),
|
|
136
|
+
/* @__PURE__ */ jsx2("p", { className: "witness-notice__body", children: children ?? text.body }),
|
|
137
|
+
/* @__PURE__ */ jsxs("div", { className: "witness-notice__actions", children: [
|
|
138
|
+
/* @__PURE__ */ jsx2(
|
|
139
|
+
"button",
|
|
140
|
+
{
|
|
141
|
+
type: "button",
|
|
142
|
+
className: "witness-notice__ack",
|
|
143
|
+
onClick: () => {
|
|
144
|
+
setReopened(false);
|
|
145
|
+
disclosure.acknowledge();
|
|
146
|
+
},
|
|
147
|
+
children: messages.ui.acknowledge
|
|
148
|
+
}
|
|
149
|
+
),
|
|
150
|
+
href ? /* @__PURE__ */ jsx2("a", { href, children: messages.ui.moreInfo }) : null
|
|
151
|
+
] })
|
|
152
|
+
] })
|
|
153
|
+
] });
|
|
154
|
+
}
|
|
155
|
+
function cx(...names) {
|
|
156
|
+
return names.filter(Boolean).join(" ");
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/AiLabel.tsx
|
|
160
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
161
|
+
function AiLabel({
|
|
162
|
+
kind = "generated",
|
|
163
|
+
generator,
|
|
164
|
+
generatorVersion,
|
|
165
|
+
createdAt,
|
|
166
|
+
reviewed,
|
|
167
|
+
href,
|
|
168
|
+
locale,
|
|
169
|
+
variant = "inline",
|
|
170
|
+
className
|
|
171
|
+
}) {
|
|
172
|
+
const { locale: code, messages } = useWitnessMessages(locale);
|
|
173
|
+
const [open, setOpen] = useState2(false);
|
|
174
|
+
const panelId = useId2();
|
|
175
|
+
const root = useRef2(null);
|
|
176
|
+
const button = useRef2(null);
|
|
177
|
+
const text = messages.kinds[kind];
|
|
178
|
+
const name = [generator, generatorVersion].filter(Boolean).join(" ");
|
|
179
|
+
useEffect2(() => {
|
|
180
|
+
if (!open) return;
|
|
181
|
+
const onKey = (event) => {
|
|
182
|
+
if (event.key === "Escape") {
|
|
183
|
+
setOpen(false);
|
|
184
|
+
button.current?.focus();
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
const onClick = (event) => {
|
|
188
|
+
if (root.current && !root.current.contains(event.target)) setOpen(false);
|
|
189
|
+
};
|
|
190
|
+
document.addEventListener("keydown", onKey);
|
|
191
|
+
document.addEventListener("click", onClick);
|
|
192
|
+
return () => {
|
|
193
|
+
document.removeEventListener("keydown", onKey);
|
|
194
|
+
document.removeEventListener("click", onClick);
|
|
195
|
+
};
|
|
196
|
+
}, [open]);
|
|
197
|
+
const facts = [];
|
|
198
|
+
if (name) facts.push(format(messages.ui.generatedWith, { generator: name }));
|
|
199
|
+
if (createdAt) facts.push(format(messages.ui.createdOn, { date: formatDate(createdAt, code) }));
|
|
200
|
+
if (reviewed) facts.push(messages.ui.humanReviewed);
|
|
201
|
+
return /* @__PURE__ */ jsxs2(
|
|
202
|
+
"span",
|
|
203
|
+
{
|
|
204
|
+
ref: root,
|
|
205
|
+
className: cx("witness-badge", `witness-badge--${variant}`, className),
|
|
206
|
+
style: variant === "overlay" ? overlayStyle : inlineStyle,
|
|
207
|
+
children: [
|
|
208
|
+
/* @__PURE__ */ jsxs2(
|
|
209
|
+
"button",
|
|
210
|
+
{
|
|
211
|
+
ref: button,
|
|
212
|
+
type: "button",
|
|
213
|
+
className: "witness-label",
|
|
214
|
+
style: { margin: 0, font: "inherit", cursor: "pointer" },
|
|
215
|
+
"aria-expanded": open,
|
|
216
|
+
"aria-controls": panelId,
|
|
217
|
+
title: messages.ui.details,
|
|
218
|
+
onClick: () => setOpen((value) => !value),
|
|
219
|
+
children: [
|
|
220
|
+
/* @__PURE__ */ jsx3("span", { className: "witness-label__icon", "aria-hidden": "true", children: "AI" }),
|
|
221
|
+
/* @__PURE__ */ jsx3("span", { className: "witness-label__text", children: text.label })
|
|
222
|
+
]
|
|
223
|
+
}
|
|
224
|
+
),
|
|
225
|
+
/* @__PURE__ */ jsxs2(
|
|
226
|
+
"span",
|
|
227
|
+
{
|
|
228
|
+
id: panelId,
|
|
229
|
+
role: "dialog",
|
|
230
|
+
"aria-label": text.title,
|
|
231
|
+
className: "witness-badge__panel",
|
|
232
|
+
hidden: !open,
|
|
233
|
+
style: variant === "overlay" ? { ...panelStyle, right: 0 } : { ...panelStyle, left: 0 },
|
|
234
|
+
children: [
|
|
235
|
+
/* @__PURE__ */ jsx3("strong", { style: { display: "block", marginBottom: "0.25em" }, children: text.title }),
|
|
236
|
+
/* @__PURE__ */ jsx3("span", { style: { display: "block" }, children: text.body }),
|
|
237
|
+
facts.length > 0 ? /* @__PURE__ */ jsx3("ul", { style: { margin: "0.4em 0", paddingInlineStart: "1.1em" }, children: facts.map((fact) => /* @__PURE__ */ jsx3("li", { children: fact }, fact)) }) : null,
|
|
238
|
+
href ? /* @__PURE__ */ jsx3("a", { href, children: messages.ui.moreInfo }) : null
|
|
239
|
+
]
|
|
240
|
+
}
|
|
241
|
+
)
|
|
242
|
+
]
|
|
243
|
+
}
|
|
244
|
+
);
|
|
245
|
+
}
|
|
246
|
+
var inlineStyle = { position: "relative", display: "inline-block" };
|
|
247
|
+
var overlayStyle = { position: "absolute", top: "0.5em", right: "0.5em", zIndex: 1 };
|
|
248
|
+
var panelStyle = {
|
|
249
|
+
position: "absolute",
|
|
250
|
+
top: "calc(100% + 0.4em)",
|
|
251
|
+
zIndex: 10,
|
|
252
|
+
width: "max-content",
|
|
253
|
+
maxWidth: "min(22em, 80vw)",
|
|
254
|
+
padding: "0.75em 0.875em",
|
|
255
|
+
border: "1px solid var(--witness-border, #d7dbe3)",
|
|
256
|
+
borderRadius: "var(--witness-radius, 8px)",
|
|
257
|
+
background: "var(--witness-bg, #fff)",
|
|
258
|
+
color: "var(--witness-fg, #1a1a1a)",
|
|
259
|
+
boxShadow: "0 6px 24px rgb(0 0 0 / 0.15)",
|
|
260
|
+
lineHeight: 1.45,
|
|
261
|
+
fontSize: "0.875rem",
|
|
262
|
+
textAlign: "start"
|
|
263
|
+
};
|
|
264
|
+
function formatDate(value, locale) {
|
|
265
|
+
const date = new Date(value);
|
|
266
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
267
|
+
try {
|
|
268
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(date);
|
|
269
|
+
} catch {
|
|
270
|
+
return value;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/AiContent.tsx
|
|
275
|
+
import { jsx as jsx4, jsxs as jsxs3 } from "react/jsx-runtime";
|
|
276
|
+
function AiContent({
|
|
277
|
+
marking,
|
|
278
|
+
as: Tag = "div",
|
|
279
|
+
labelPosition = "top",
|
|
280
|
+
locale,
|
|
281
|
+
className,
|
|
282
|
+
children
|
|
283
|
+
}) {
|
|
284
|
+
const m = useMemo3(() => createMarking(marking), [marking]);
|
|
285
|
+
const label = labelPosition === "none" ? null : /* @__PURE__ */ jsx4(
|
|
286
|
+
AiLabel,
|
|
287
|
+
{
|
|
288
|
+
kind: m.kind,
|
|
289
|
+
variant: labelPosition === "overlay" ? "overlay" : "inline",
|
|
290
|
+
createdAt: m.createdAt,
|
|
291
|
+
reviewed: m.humanReviewed,
|
|
292
|
+
...m.generator ? { generator: m.generator } : {},
|
|
293
|
+
...m.generatorVersion ? { generatorVersion: m.generatorVersion } : {},
|
|
294
|
+
...m.disclosureUrl ? { href: m.disclosureUrl } : {},
|
|
295
|
+
...locale ? { locale } : {}
|
|
296
|
+
}
|
|
297
|
+
);
|
|
298
|
+
const labelBlock = label && labelPosition !== "overlay" ? /* @__PURE__ */ jsx4("div", { style: { marginBlock: "0.5em" }, children: label }) : label;
|
|
299
|
+
return /* @__PURE__ */ jsxs3(
|
|
300
|
+
Tag,
|
|
301
|
+
{
|
|
302
|
+
className: cx("witness-content", className),
|
|
303
|
+
style: labelPosition === "overlay" ? { position: "relative" } : void 0,
|
|
304
|
+
...markingAttributes(m),
|
|
305
|
+
children: [
|
|
306
|
+
labelPosition === "bottom" ? null : labelBlock,
|
|
307
|
+
children,
|
|
308
|
+
labelPosition === "bottom" ? labelBlock : null
|
|
309
|
+
]
|
|
310
|
+
}
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
export {
|
|
314
|
+
AiContent,
|
|
315
|
+
AiLabel,
|
|
316
|
+
AiNotice,
|
|
317
|
+
WitnessProvider,
|
|
318
|
+
useAiDisclosure,
|
|
319
|
+
useWitnessMessages
|
|
320
|
+
};
|
|
321
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/AiContent.tsx","../src/AiLabel.tsx","../src/AiNotice.tsx","../src/context.tsx","../src/useAiDisclosure.ts"],"sourcesContent":["import {\n createMarking,\n type Marking,\n type MarkingInput,\n markingAttributes,\n} from \"@sweberdev/witness\";\nimport { type ElementType, type ReactNode, useMemo } from \"react\";\nimport { AiLabel } from \"./AiLabel.js\";\nimport { cx } from \"./AiNotice.js\";\n\nexport interface AiContentProps {\n marking: Marking | MarkingInput;\n /** Element that wraps label and content. Default `div`. */\n as?: ElementType;\n /** Text should be labelled at the top (default). Use `overlay` for images and video. */\n labelPosition?: \"top\" | \"bottom\" | \"overlay\" | \"none\";\n locale?: string | readonly string[];\n className?: string;\n children: ReactNode;\n}\n\n/**\n * Wraps AI-generated content: adds the `data-ai-*` marking attributes and a visible label.\n * With `labelPosition=\"overlay\"` the wrapper becomes `position: relative`.\n */\nexport function AiContent({\n marking,\n as: Tag = \"div\",\n labelPosition = \"top\",\n locale,\n className,\n children,\n}: AiContentProps) {\n const m = useMemo(() => createMarking(marking), [marking]);\n const label =\n labelPosition === \"none\" ? null : (\n <AiLabel\n kind={m.kind}\n variant={labelPosition === \"overlay\" ? \"overlay\" : \"inline\"}\n createdAt={m.createdAt}\n reviewed={m.humanReviewed}\n {...(m.generator ? { generator: m.generator } : {})}\n {...(m.generatorVersion ? { generatorVersion: m.generatorVersion } : {})}\n {...(m.disclosureUrl ? { href: m.disclosureUrl } : {})}\n {...(locale ? { locale } : {})}\n />\n );\n const labelBlock =\n label && labelPosition !== \"overlay\" ? (\n <div style={{ marginBlock: \"0.5em\" }}>{label}</div>\n ) : (\n label\n );\n return (\n <Tag\n className={cx(\"witness-content\", className)}\n style={labelPosition === \"overlay\" ? { position: \"relative\" } : undefined}\n {...markingAttributes(m)}\n >\n {labelPosition === \"bottom\" ? null : labelBlock}\n {children}\n {labelPosition === \"bottom\" ? labelBlock : null}\n </Tag>\n );\n}\n","import { type ContentKind, type DisclosureKind, format } from \"@sweberdev/witness\";\nimport { useEffect, useId, useRef, useState } from \"react\";\nimport { cx } from \"./AiNotice.js\";\nimport { useWitnessMessages } from \"./context.js\";\n\nexport interface AiLabelProps {\n /** Default `generated`. */\n kind?: ContentKind | DisclosureKind;\n generator?: string;\n generatorVersion?: string;\n /** ISO date or timestamp. */\n createdAt?: string;\n /** A person reviewed the content and holds editorial responsibility. */\n reviewed?: boolean;\n /** Link to a page that explains how you use AI. */\n href?: string;\n locale?: string | readonly string[];\n /** `overlay` places the label in the top corner of a positioned parent, e.g. over an image. */\n variant?: \"inline\" | \"overlay\";\n className?: string;\n}\n\n/** A badge for AI-generated or AI-edited content. Clicking it shows what the label means. */\nexport function AiLabel({\n kind = \"generated\",\n generator,\n generatorVersion,\n createdAt,\n reviewed,\n href,\n locale,\n variant = \"inline\",\n className,\n}: AiLabelProps) {\n const { locale: code, messages } = useWitnessMessages(locale);\n const [open, setOpen] = useState(false);\n const panelId = useId();\n const root = useRef<HTMLSpanElement>(null);\n const button = useRef<HTMLButtonElement>(null);\n const text = messages.kinds[kind];\n const name = [generator, generatorVersion].filter(Boolean).join(\" \");\n\n useEffect(() => {\n if (!open) return;\n const onKey = (event: KeyboardEvent) => {\n if (event.key === \"Escape\") {\n setOpen(false);\n button.current?.focus();\n }\n };\n const onClick = (event: MouseEvent) => {\n if (root.current && !root.current.contains(event.target as Node)) setOpen(false);\n };\n document.addEventListener(\"keydown\", onKey);\n document.addEventListener(\"click\", onClick);\n return () => {\n document.removeEventListener(\"keydown\", onKey);\n document.removeEventListener(\"click\", onClick);\n };\n }, [open]);\n\n const facts: string[] = [];\n if (name) facts.push(format(messages.ui.generatedWith, { generator: name }));\n if (createdAt) facts.push(format(messages.ui.createdOn, { date: formatDate(createdAt, code) }));\n if (reviewed) facts.push(messages.ui.humanReviewed);\n\n return (\n <span\n ref={root}\n className={cx(\"witness-badge\", `witness-badge--${variant}`, className)}\n style={variant === \"overlay\" ? overlayStyle : inlineStyle}\n >\n <button\n ref={button}\n type=\"button\"\n className=\"witness-label\"\n style={{ margin: 0, font: \"inherit\", cursor: \"pointer\" }}\n aria-expanded={open}\n aria-controls={panelId}\n title={messages.ui.details}\n onClick={() => setOpen((value) => !value)}\n >\n <span className=\"witness-label__icon\" aria-hidden=\"true\">\n AI\n </span>\n <span className=\"witness-label__text\">{text.label}</span>\n </button>\n <span\n id={panelId}\n role=\"dialog\"\n aria-label={text.title}\n className=\"witness-badge__panel\"\n hidden={!open}\n style={variant === \"overlay\" ? { ...panelStyle, right: 0 } : { ...panelStyle, left: 0 }}\n >\n <strong style={{ display: \"block\", marginBottom: \"0.25em\" }}>{text.title}</strong>\n <span style={{ display: \"block\" }}>{text.body}</span>\n {facts.length > 0 ? (\n <ul style={{ margin: \"0.4em 0\", paddingInlineStart: \"1.1em\" }}>\n {facts.map((fact) => (\n <li key={fact}>{fact}</li>\n ))}\n </ul>\n ) : null}\n {href ? <a href={href}>{messages.ui.moreInfo}</a> : null}\n </span>\n </span>\n );\n}\n\nconst inlineStyle = { position: \"relative\", display: \"inline-block\" } as const;\nconst overlayStyle = { position: \"absolute\", top: \"0.5em\", right: \"0.5em\", zIndex: 1 } as const;\nconst panelStyle = {\n position: \"absolute\",\n top: \"calc(100% + 0.4em)\",\n zIndex: 10,\n width: \"max-content\",\n maxWidth: \"min(22em, 80vw)\",\n padding: \"0.75em 0.875em\",\n border: \"1px solid var(--witness-border, #d7dbe3)\",\n borderRadius: \"var(--witness-radius, 8px)\",\n background: \"var(--witness-bg, #fff)\",\n color: \"var(--witness-fg, #1a1a1a)\",\n boxShadow: \"0 6px 24px rgb(0 0 0 / 0.15)\",\n lineHeight: 1.45,\n fontSize: \"0.875rem\",\n textAlign: \"start\",\n} as const;\n\nfunction formatDate(value: string, locale: string): string {\n const date = new Date(value);\n if (Number.isNaN(date.getTime())) return value;\n try {\n return new Intl.DateTimeFormat(locale, { dateStyle: \"medium\" }).format(date);\n } catch {\n return value;\n }\n}\n","import type { DisclosureEvent, DisclosureKind } from \"@sweberdev/witness\";\nimport { type ReactNode, useEffect, useId, useState } from \"react\";\nimport { useWitnessMessages } from \"./context.js\";\nimport { useAiDisclosure } from \"./useAiDisclosure.js\";\n\nexport interface AiNoticeProps {\n /** Default `chatbot`. Also fits `emotion-recognition` and `biometric-categorisation`. */\n kind?: DisclosureKind;\n /** Your id for this disclosure. Default: the kind. */\n id?: string;\n /** Bump when the wording changes, so people see it again. Default \"1\". */\n version?: string;\n locale?: string | readonly string[];\n /** Link to a page that explains how you use AI. */\n href?: string;\n storage?: \"local\" | \"session\" | \"memory\";\n /** Evidence hook: called when the notice is shown and acknowledged. */\n onEvent?: (event: DisclosureEvent) => void;\n /** Replaces the default body text. */\n children?: ReactNode;\n className?: string;\n}\n\n/**\n * The AI notice for a chat (Art. 50(1)): expanded until acknowledged, then a compact label\n * that stays visible and can be reopened. Style it with `@sweberdev/witness/styles.css`.\n */\nexport function AiNotice({\n kind = \"chatbot\",\n id,\n version,\n locale,\n href,\n storage,\n onEvent,\n children,\n className,\n}: AiNoticeProps) {\n const { locale: code, messages } = useWitnessMessages(locale);\n const disclosure = useAiDisclosure({\n kind,\n locale: code,\n ...(id ? { id } : {}),\n ...(version ? { version } : {}),\n ...(storage ? { storage } : {}),\n ...(onEvent ? { onEvent } : {}),\n });\n const [reopened, setReopened] = useState(false);\n const titleId = useId();\n const text = messages.kinds[kind];\n const full = reopened || disclosure.needsAcknowledgement;\n const { markShown } = disclosure;\n\n useEffect(() => {\n if (full) markShown();\n }, [full, markShown]);\n\n if (!full) {\n return (\n <button\n type=\"button\"\n className={cx(\"witness-notice--compact\", className)}\n aria-label={`${text.label}: ${messages.ui.details}`}\n onClick={() => setReopened(true)}\n >\n <span className=\"witness-label__icon\" aria-hidden=\"true\">\n AI\n </span>\n {text.label}\n </button>\n );\n }\n\n return (\n <section className={cx(\"witness-notice\", className)} role=\"note\" aria-labelledby={titleId}>\n <span className=\"witness-label__icon\" aria-hidden=\"true\">\n AI\n </span>\n <div>\n <h2 id={titleId} className=\"witness-notice__title\">\n {text.title}\n </h2>\n <p className=\"witness-notice__body\">{children ?? text.body}</p>\n <div className=\"witness-notice__actions\">\n <button\n type=\"button\"\n className=\"witness-notice__ack\"\n onClick={() => {\n setReopened(false);\n disclosure.acknowledge();\n }}\n >\n {messages.ui.acknowledge}\n </button>\n {href ? <a href={href}>{messages.ui.moreInfo}</a> : null}\n </div>\n </div>\n </section>\n );\n}\n\nexport function cx(...names: Array<string | undefined | false>): string {\n return names.filter(Boolean).join(\" \");\n}\n","import {\n getMessages,\n type LocaleMessages,\n type LocaleOverride,\n resolveLocale,\n} from \"@sweberdev/witness\";\nimport { createContext, type ReactNode, useContext, useMemo } from \"react\";\n\ninterface WitnessContextValue {\n locale: string;\n messages: LocaleMessages;\n}\n\nconst WitnessContext = createContext<WitnessContextValue | null>(null);\n\nexport interface WitnessProviderProps {\n /** A locale tag such as \"de-CH\", or a list such as `navigator.languages`. Default \"en\". */\n locale?: string | readonly string[];\n /** Overrides for the wording, e.g. the informal \"du\" in German. */\n messages?: LocaleOverride;\n children: ReactNode;\n}\n\n/** Sets the language and wording for all Witness components below it. */\nexport function WitnessProvider({ locale, messages, children }: WitnessProviderProps) {\n const value = useMemo(\n () => ({ locale: resolveLocale(locale), messages: getMessages(locale, messages) }),\n [locale, messages],\n );\n return <WitnessContext.Provider value={value}>{children}</WitnessContext.Provider>;\n}\n\n/** The active locale and messages. A `locale` prop on a component wins over the provider. */\nexport function useWitnessMessages(locale?: string | readonly string[]): WitnessContextValue {\n const context = useContext(WitnessContext);\n return useMemo(() => {\n if (locale) return { locale: resolveLocale(locale), messages: getMessages(locale) };\n return context ?? { locale: \"en\", messages: getMessages(\"en\") };\n }, [context, locale]);\n}\n","import { createDisclosure, type Disclosure, type DisclosureOptions } from \"@sweberdev/witness\";\nimport { useCallback, useMemo, useRef, useSyncExternalStore } from \"react\";\n\nexport interface UseAiDisclosureResult {\n /** True until the current version was acknowledged. Always true during server rendering. */\n needsAcknowledgement: boolean;\n acknowledgedAt: string | null;\n acknowledge: () => void;\n /** Records that the notice was rendered (emits a `shown` event once). */\n markShown: () => void;\n reset: () => void;\n disclosure: Disclosure;\n}\n\n/**\n * State for a custom AI disclosure UI. The server snapshot always needs acknowledgement, so\n * the full notice is in the server HTML and collapses after hydration for people who already\n * acknowledged it.\n */\nexport function useAiDisclosure(options: DisclosureOptions = {}): UseAiDisclosureResult {\n const onEvent = useRef(options.onEvent);\n onEvent.current = options.onEvent;\n const { id, kind, version, locale, storage } = options;\n\n const disclosure = useMemo(\n () =>\n createDisclosure({\n ...(id ? { id } : {}),\n ...(kind ? { kind } : {}),\n ...(version ? { version } : {}),\n ...(locale ? { locale } : {}),\n ...(storage ? { storage } : {}),\n onEvent: (event) => onEvent.current?.(event),\n }),\n [id, kind, version, locale, storage],\n );\n\n const subscribe = useCallback(\n (listener: () => void) => {\n const unsubscribe = disclosure.subscribe(listener);\n // Another tab acknowledged or reset: the browser tells us through a storage event.\n const onStorage = (event: StorageEvent) => {\n if (event.key === null || event.key.startsWith(\"witness:ack:\")) listener();\n };\n if (typeof window !== \"undefined\") window.addEventListener(\"storage\", onStorage);\n return () => {\n unsubscribe();\n if (typeof window !== \"undefined\") window.removeEventListener(\"storage\", onStorage);\n };\n },\n [disclosure],\n );\n const acknowledgedAt = useSyncExternalStore(\n subscribe,\n () => disclosure.acknowledgedAt(),\n () => null,\n );\n\n return {\n needsAcknowledgement: acknowledgedAt === null,\n acknowledgedAt,\n acknowledge: disclosure.acknowledge,\n markShown: disclosure.markShown,\n reset: disclosure.reset,\n disclosure,\n };\n}\n"],"mappings":";;;AAAA;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAA2C,WAAAA,gBAAe;;;ACN1D,SAAgD,cAAc;AAC9D,SAAS,aAAAC,YAAW,SAAAC,QAAO,UAAAC,SAAQ,YAAAC,iBAAgB;;;ACAnD,SAAyB,WAAW,OAAO,gBAAgB;;;ACD3D;AAAA,EACE;AAAA,EAGA;AAAA,OACK;AACP,SAAS,eAA+B,YAAY,eAAe;AAuB1D;AAhBT,IAAM,iBAAiB,cAA0C,IAAI;AAW9D,SAAS,gBAAgB,EAAE,QAAQ,UAAU,SAAS,GAAyB;AACpF,QAAM,QAAQ;AAAA,IACZ,OAAO,EAAE,QAAQ,cAAc,MAAM,GAAG,UAAU,YAAY,QAAQ,QAAQ,EAAE;AAAA,IAChF,CAAC,QAAQ,QAAQ;AAAA,EACnB;AACA,SAAO,oBAAC,eAAe,UAAf,EAAwB,OAAe,UAAS;AAC1D;AAGO,SAAS,mBAAmB,QAA0D;AAC3F,QAAM,UAAU,WAAW,cAAc;AACzC,SAAO,QAAQ,MAAM;AACnB,QAAI,OAAQ,QAAO,EAAE,QAAQ,cAAc,MAAM,GAAG,UAAU,YAAY,MAAM,EAAE;AAClF,WAAO,WAAW,EAAE,QAAQ,MAAM,UAAU,YA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","useId","useRef","useState","useMemo","jsx","jsx","jsxs","useState","useId","useRef","useEffect","jsx","jsxs","useMemo"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,75 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sweberdev/witness-react",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React components for Witness: chatbot AI notice, AI labels and marked content blocks for Article 50 of the EU AI Act.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Seya Weber (https://github.com/sxwxbxr)",
|
|
7
|
+
"homepage": "https://packages.sweber.dev/witness/docs",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/Weber-Development/witness.git",
|
|
11
|
+
"directory": "packages/react"
|
|
12
|
+
},
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/Weber-Development/witness/issues"
|
|
15
|
+
},
|
|
16
|
+
"type": "module",
|
|
17
|
+
"sideEffects": false,
|
|
18
|
+
"main": "./dist/index.cjs",
|
|
19
|
+
"module": "./dist/index.js",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"require": {
|
|
28
|
+
"types": "./dist/index.d.cts",
|
|
29
|
+
"default": "./dist/index.cjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist",
|
|
36
|
+
"src",
|
|
37
|
+
"README.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"keywords": [
|
|
41
|
+
"ai-act",
|
|
42
|
+
"article-50",
|
|
43
|
+
"ai-label",
|
|
44
|
+
"ai-disclosure",
|
|
45
|
+
"react",
|
|
46
|
+
"chatbot"
|
|
47
|
+
],
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public",
|
|
50
|
+
"provenance": true
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@sweberdev/witness": "^0.1.0"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@testing-library/react": "^16.3.0",
|
|
60
|
+
"@types/node": "^22.20.5",
|
|
61
|
+
"@types/react": "^19.1.0",
|
|
62
|
+
"@types/react-dom": "^19.3.0",
|
|
63
|
+
"jsdom": "^27.0.0",
|
|
64
|
+
"react": "^19.1.0",
|
|
65
|
+
"react-dom": "^19.1.0",
|
|
66
|
+
"tsup": "^8.5.1",
|
|
67
|
+
"typescript": "^5.9.0",
|
|
68
|
+
"vitest": "^5.0.3"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsup",
|
|
72
|
+
"test": "vitest run",
|
|
73
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
74
|
+
}
|
|
6
75
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createMarking,
|
|
3
|
+
type Marking,
|
|
4
|
+
type MarkingInput,
|
|
5
|
+
markingAttributes,
|
|
6
|
+
} from "@sweberdev/witness";
|
|
7
|
+
import { type ElementType, type ReactNode, useMemo } from "react";
|
|
8
|
+
import { AiLabel } from "./AiLabel.js";
|
|
9
|
+
import { cx } from "./AiNotice.js";
|
|
10
|
+
|
|
11
|
+
export interface AiContentProps {
|
|
12
|
+
marking: Marking | MarkingInput;
|
|
13
|
+
/** Element that wraps label and content. Default `div`. */
|
|
14
|
+
as?: ElementType;
|
|
15
|
+
/** Text should be labelled at the top (default). Use `overlay` for images and video. */
|
|
16
|
+
labelPosition?: "top" | "bottom" | "overlay" | "none";
|
|
17
|
+
locale?: string | readonly string[];
|
|
18
|
+
className?: string;
|
|
19
|
+
children: ReactNode;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Wraps AI-generated content: adds the `data-ai-*` marking attributes and a visible label.
|
|
24
|
+
* With `labelPosition="overlay"` the wrapper becomes `position: relative`.
|
|
25
|
+
*/
|
|
26
|
+
export function AiContent({
|
|
27
|
+
marking,
|
|
28
|
+
as: Tag = "div",
|
|
29
|
+
labelPosition = "top",
|
|
30
|
+
locale,
|
|
31
|
+
className,
|
|
32
|
+
children,
|
|
33
|
+
}: AiContentProps) {
|
|
34
|
+
const m = useMemo(() => createMarking(marking), [marking]);
|
|
35
|
+
const label =
|
|
36
|
+
labelPosition === "none" ? null : (
|
|
37
|
+
<AiLabel
|
|
38
|
+
kind={m.kind}
|
|
39
|
+
variant={labelPosition === "overlay" ? "overlay" : "inline"}
|
|
40
|
+
createdAt={m.createdAt}
|
|
41
|
+
reviewed={m.humanReviewed}
|
|
42
|
+
{...(m.generator ? { generator: m.generator } : {})}
|
|
43
|
+
{...(m.generatorVersion ? { generatorVersion: m.generatorVersion } : {})}
|
|
44
|
+
{...(m.disclosureUrl ? { href: m.disclosureUrl } : {})}
|
|
45
|
+
{...(locale ? { locale } : {})}
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
const labelBlock =
|
|
49
|
+
label && labelPosition !== "overlay" ? (
|
|
50
|
+
<div style={{ marginBlock: "0.5em" }}>{label}</div>
|
|
51
|
+
) : (
|
|
52
|
+
label
|
|
53
|
+
);
|
|
54
|
+
return (
|
|
55
|
+
<Tag
|
|
56
|
+
className={cx("witness-content", className)}
|
|
57
|
+
style={labelPosition === "overlay" ? { position: "relative" } : undefined}
|
|
58
|
+
{...markingAttributes(m)}
|
|
59
|
+
>
|
|
60
|
+
{labelPosition === "bottom" ? null : labelBlock}
|
|
61
|
+
{children}
|
|
62
|
+
{labelPosition === "bottom" ? labelBlock : null}
|
|
63
|
+
</Tag>
|
|
64
|
+
);
|
|
65
|
+
}
|
package/src/AiLabel.tsx
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { type ContentKind, type DisclosureKind, format } from "@sweberdev/witness";
|
|
2
|
+
import { useEffect, useId, useRef, useState } from "react";
|
|
3
|
+
import { cx } from "./AiNotice.js";
|
|
4
|
+
import { useWitnessMessages } from "./context.js";
|
|
5
|
+
|
|
6
|
+
export interface AiLabelProps {
|
|
7
|
+
/** Default `generated`. */
|
|
8
|
+
kind?: ContentKind | DisclosureKind;
|
|
9
|
+
generator?: string;
|
|
10
|
+
generatorVersion?: string;
|
|
11
|
+
/** ISO date or timestamp. */
|
|
12
|
+
createdAt?: string;
|
|
13
|
+
/** A person reviewed the content and holds editorial responsibility. */
|
|
14
|
+
reviewed?: boolean;
|
|
15
|
+
/** Link to a page that explains how you use AI. */
|
|
16
|
+
href?: string;
|
|
17
|
+
locale?: string | readonly string[];
|
|
18
|
+
/** `overlay` places the label in the top corner of a positioned parent, e.g. over an image. */
|
|
19
|
+
variant?: "inline" | "overlay";
|
|
20
|
+
className?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** A badge for AI-generated or AI-edited content. Clicking it shows what the label means. */
|
|
24
|
+
export function AiLabel({
|
|
25
|
+
kind = "generated",
|
|
26
|
+
generator,
|
|
27
|
+
generatorVersion,
|
|
28
|
+
createdAt,
|
|
29
|
+
reviewed,
|
|
30
|
+
href,
|
|
31
|
+
locale,
|
|
32
|
+
variant = "inline",
|
|
33
|
+
className,
|
|
34
|
+
}: AiLabelProps) {
|
|
35
|
+
const { locale: code, messages } = useWitnessMessages(locale);
|
|
36
|
+
const [open, setOpen] = useState(false);
|
|
37
|
+
const panelId = useId();
|
|
38
|
+
const root = useRef<HTMLSpanElement>(null);
|
|
39
|
+
const button = useRef<HTMLButtonElement>(null);
|
|
40
|
+
const text = messages.kinds[kind];
|
|
41
|
+
const name = [generator, generatorVersion].filter(Boolean).join(" ");
|
|
42
|
+
|
|
43
|
+
useEffect(() => {
|
|
44
|
+
if (!open) return;
|
|
45
|
+
const onKey = (event: KeyboardEvent) => {
|
|
46
|
+
if (event.key === "Escape") {
|
|
47
|
+
setOpen(false);
|
|
48
|
+
button.current?.focus();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
const onClick = (event: MouseEvent) => {
|
|
52
|
+
if (root.current && !root.current.contains(event.target as Node)) setOpen(false);
|
|
53
|
+
};
|
|
54
|
+
document.addEventListener("keydown", onKey);
|
|
55
|
+
document.addEventListener("click", onClick);
|
|
56
|
+
return () => {
|
|
57
|
+
document.removeEventListener("keydown", onKey);
|
|
58
|
+
document.removeEventListener("click", onClick);
|
|
59
|
+
};
|
|
60
|
+
}, [open]);
|
|
61
|
+
|
|
62
|
+
const facts: string[] = [];
|
|
63
|
+
if (name) facts.push(format(messages.ui.generatedWith, { generator: name }));
|
|
64
|
+
if (createdAt) facts.push(format(messages.ui.createdOn, { date: formatDate(createdAt, code) }));
|
|
65
|
+
if (reviewed) facts.push(messages.ui.humanReviewed);
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<span
|
|
69
|
+
ref={root}
|
|
70
|
+
className={cx("witness-badge", `witness-badge--${variant}`, className)}
|
|
71
|
+
style={variant === "overlay" ? overlayStyle : inlineStyle}
|
|
72
|
+
>
|
|
73
|
+
<button
|
|
74
|
+
ref={button}
|
|
75
|
+
type="button"
|
|
76
|
+
className="witness-label"
|
|
77
|
+
style={{ margin: 0, font: "inherit", cursor: "pointer" }}
|
|
78
|
+
aria-expanded={open}
|
|
79
|
+
aria-controls={panelId}
|
|
80
|
+
title={messages.ui.details}
|
|
81
|
+
onClick={() => setOpen((value) => !value)}
|
|
82
|
+
>
|
|
83
|
+
<span className="witness-label__icon" aria-hidden="true">
|
|
84
|
+
AI
|
|
85
|
+
</span>
|
|
86
|
+
<span className="witness-label__text">{text.label}</span>
|
|
87
|
+
</button>
|
|
88
|
+
<span
|
|
89
|
+
id={panelId}
|
|
90
|
+
role="dialog"
|
|
91
|
+
aria-label={text.title}
|
|
92
|
+
className="witness-badge__panel"
|
|
93
|
+
hidden={!open}
|
|
94
|
+
style={variant === "overlay" ? { ...panelStyle, right: 0 } : { ...panelStyle, left: 0 }}
|
|
95
|
+
>
|
|
96
|
+
<strong style={{ display: "block", marginBottom: "0.25em" }}>{text.title}</strong>
|
|
97
|
+
<span style={{ display: "block" }}>{text.body}</span>
|
|
98
|
+
{facts.length > 0 ? (
|
|
99
|
+
<ul style={{ margin: "0.4em 0", paddingInlineStart: "1.1em" }}>
|
|
100
|
+
{facts.map((fact) => (
|
|
101
|
+
<li key={fact}>{fact}</li>
|
|
102
|
+
))}
|
|
103
|
+
</ul>
|
|
104
|
+
) : null}
|
|
105
|
+
{href ? <a href={href}>{messages.ui.moreInfo}</a> : null}
|
|
106
|
+
</span>
|
|
107
|
+
</span>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const inlineStyle = { position: "relative", display: "inline-block" } as const;
|
|
112
|
+
const overlayStyle = { position: "absolute", top: "0.5em", right: "0.5em", zIndex: 1 } as const;
|
|
113
|
+
const panelStyle = {
|
|
114
|
+
position: "absolute",
|
|
115
|
+
top: "calc(100% + 0.4em)",
|
|
116
|
+
zIndex: 10,
|
|
117
|
+
width: "max-content",
|
|
118
|
+
maxWidth: "min(22em, 80vw)",
|
|
119
|
+
padding: "0.75em 0.875em",
|
|
120
|
+
border: "1px solid var(--witness-border, #d7dbe3)",
|
|
121
|
+
borderRadius: "var(--witness-radius, 8px)",
|
|
122
|
+
background: "var(--witness-bg, #fff)",
|
|
123
|
+
color: "var(--witness-fg, #1a1a1a)",
|
|
124
|
+
boxShadow: "0 6px 24px rgb(0 0 0 / 0.15)",
|
|
125
|
+
lineHeight: 1.45,
|
|
126
|
+
fontSize: "0.875rem",
|
|
127
|
+
textAlign: "start",
|
|
128
|
+
} as const;
|
|
129
|
+
|
|
130
|
+
function formatDate(value: string, locale: string): string {
|
|
131
|
+
const date = new Date(value);
|
|
132
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
133
|
+
try {
|
|
134
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: "medium" }).format(date);
|
|
135
|
+
} catch {
|
|
136
|
+
return value;
|
|
137
|
+
}
|
|
138
|
+
}
|