@sixb/app 0.1.12 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -8
- package/dist/agent/activity-label.d.ts.map +1 -1
- package/dist/agent/agent-chat.d.ts +9 -8
- package/dist/agent/agent-chat.d.ts.map +1 -1
- package/dist/agent/agent-panel.d.ts +5 -8
- package/dist/agent/agent-panel.d.ts.map +1 -1
- package/dist/agent/agents-page.d.ts +4 -0
- package/dist/agent/agents-page.d.ts.map +1 -0
- package/dist/agent/artifact-card.d.ts +9 -0
- package/dist/agent/artifact-card.d.ts.map +1 -0
- package/dist/agent/artifact-panel-state.d.ts +16 -0
- package/dist/agent/artifact-panel-state.d.ts.map +1 -0
- package/dist/agent/artifact-panel.d.ts +38 -0
- package/dist/agent/artifact-panel.d.ts.map +1 -0
- package/dist/agent/components/file-attachment-card.d.ts +3 -3
- package/dist/agent/components/file-attachment-card.d.ts.map +1 -1
- package/dist/agent/components/message-parts.d.ts +2 -1
- package/dist/agent/components/message-parts.d.ts.map +1 -1
- package/dist/agent/components/message-view.d.ts +3 -3
- package/dist/agent/components/message-view.d.ts.map +1 -1
- package/dist/agent/components/thread-artifacts-menu.d.ts +8 -0
- package/dist/agent/components/thread-artifacts-menu.d.ts.map +1 -0
- package/dist/agent/hooks/use-agent-conversation.d.ts +3 -4
- package/dist/agent/hooks/use-agent-conversation.d.ts.map +1 -1
- package/dist/agent/i18n/en.d.ts +10 -49
- package/dist/agent/i18n/en.d.ts.map +1 -1
- package/dist/agent/i18n/fr.d.ts.map +1 -1
- package/dist/agent/i18n/index.d.ts +16 -98
- package/dist/agent/i18n/index.d.ts.map +1 -1
- package/dist/agent/parts.d.ts +4 -4
- package/dist/agent/parts.d.ts.map +1 -1
- package/dist/agent/publish-artifact/interpret.d.ts +32 -0
- package/dist/agent/publish-artifact/interpret.d.ts.map +1 -0
- package/dist/agent/publish-artifact/publish-artifact-tool-view.d.ts +9 -0
- package/dist/agent/publish-artifact/publish-artifact-tool-view.d.ts.map +1 -0
- package/dist/agent/publish-artifact/use-live-publishes.d.ts +10 -0
- package/dist/agent/publish-artifact/use-live-publishes.d.ts.map +1 -0
- package/dist/agent/react-router.d.ts +1 -2
- package/dist/agent/react-router.d.ts.map +1 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts +1 -1
- package/dist/agent/view-file/view-file-tool-view.d.ts.map +1 -1
- package/dist/artifacts/artifact-page.d.ts +7 -0
- package/dist/artifacts/artifact-page.d.ts.map +1 -0
- package/dist/artifacts/card-details.d.ts +15 -0
- package/dist/artifacts/card-details.d.ts.map +1 -0
- package/dist/artifacts/card-preview.d.ts +59 -0
- package/dist/artifacts/card-preview.d.ts.map +1 -0
- package/dist/artifacts/copy-feedback.d.ts +6 -0
- package/dist/artifacts/copy-feedback.d.ts.map +1 -0
- package/dist/artifacts/file-size.d.ts +11 -0
- package/dist/artifacts/file-size.d.ts.map +1 -0
- package/dist/artifacts/file-source.d.ts +45 -0
- package/dist/artifacts/file-source.d.ts.map +1 -0
- package/dist/artifacts/file-view.d.ts +9 -0
- package/dist/artifacts/file-view.d.ts.map +1 -0
- package/dist/artifacts/frame.d.ts +45 -0
- package/dist/artifacts/frame.d.ts.map +1 -0
- package/dist/artifacts/gallery.d.ts +6 -0
- package/dist/artifacts/gallery.d.ts.map +1 -0
- package/dist/artifacts/kind-icon.d.ts +8 -0
- package/dist/artifacts/kind-icon.d.ts.map +1 -0
- package/dist/artifacts/markdown-tree.d.ts +22 -0
- package/dist/artifacts/markdown-tree.d.ts.map +1 -0
- package/dist/artifacts/messages.d.ts +301 -0
- package/dist/artifacts/messages.d.ts.map +1 -0
- package/dist/artifacts/queries.d.ts +21 -0
- package/dist/artifacts/queries.d.ts.map +1 -0
- package/dist/artifacts/references/embed-tree.d.ts +17 -0
- package/dist/artifacts/references/embed-tree.d.ts.map +1 -0
- package/dist/artifacts/references/references.d.ts +50 -0
- package/dist/artifacts/references/references.d.ts.map +1 -0
- package/dist/artifacts/renderers/document-tree.d.ts +11 -0
- package/dist/artifacts/renderers/document-tree.d.ts.map +1 -0
- package/dist/artifacts/renderers/document.d.ts +17 -0
- package/dist/artifacts/renderers/document.d.ts.map +1 -0
- package/dist/artifacts/renderers/file.d.ts +10 -0
- package/dist/artifacts/renderers/file.d.ts.map +1 -0
- package/dist/artifacts/renderers/notice.d.ts +12 -0
- package/dist/artifacts/renderers/notice.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/chart-data.d.ts +33 -0
- package/dist/artifacts/renderers/sheet/chart-data.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/chart.d.ts +15 -0
- package/dist/artifacts/renderers/sheet/chart.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/format.d.ts +35 -0
- package/dist/artifacts/renderers/sheet/format.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/grid.d.ts +25 -0
- package/dist/artifacts/renderers/sheet/grid.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/mini-chart.d.ts +7 -0
- package/dist/artifacts/renderers/sheet/mini-chart.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/model.d.ts +29 -0
- package/dist/artifacts/renderers/sheet/model.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/selection.d.ts +42 -0
- package/dist/artifacts/renderers/sheet/selection.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/sheet.d.ts +17 -0
- package/dist/artifacts/renderers/sheet/sheet.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/virtual.d.ts +13 -0
- package/dist/artifacts/renderers/sheet/virtual.d.ts.map +1 -0
- package/dist/artifacts/renderers/sheet/workbook.d.ts +24 -0
- package/dist/artifacts/renderers/sheet/workbook.d.ts.map +1 -0
- package/dist/artifacts/renderers/slides/navigation.d.ts +17 -0
- package/dist/artifacts/renderers/slides/navigation.d.ts.map +1 -0
- package/dist/artifacts/renderers/slides/slides.d.ts +14 -0
- package/dist/artifacts/renderers/slides/slides.d.ts.map +1 -0
- package/dist/artifacts/target.d.ts +29 -0
- package/dist/artifacts/target.d.ts.map +1 -0
- package/dist/artifacts/theme.d.ts +18 -0
- package/dist/artifacts/theme.d.ts.map +1 -0
- package/dist/artifacts/viewer.d.ts +19 -0
- package/dist/artifacts/viewer.d.ts.map +1 -0
- package/dist/{chunk-shmw3f8t.js → chunk-3schcf10.js} +1 -1
- package/dist/chunk-c7gy61rx.js +276 -0
- package/dist/chunk-c7gy61rx.js.map +10 -0
- package/dist/{chunk-48pkffj6.js → chunk-g3f4cm8x.js} +1 -1
- package/dist/{chunk-zcxrt24m.js → chunk-gn3k23q5.js} +3 -2
- package/dist/{chunk-zcxrt24m.js.map → chunk-gn3k23q5.js.map} +3 -3
- package/dist/{chunk-3a99tmwg.js → chunk-ng0hdnrq.js} +10 -3
- package/dist/chunk-ng0hdnrq.js.map +10 -0
- package/dist/{chunk-j97e06rg.js → chunk-xxpdn63v.js} +7224 -2874
- package/dist/chunk-xxpdn63v.js.map +105 -0
- package/dist/codegen.d.ts +2 -0
- package/dist/codegen.d.ts.map +1 -1
- package/dist/createCustomApp.d.ts.map +1 -1
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +34 -31
- package/dist/index.js.map +7 -7
- package/dist/locale-messages.d.ts +2 -0
- package/dist/locale-messages.d.ts.map +1 -1
- package/dist/locale.d.ts +2 -0
- package/dist/locale.d.ts.map +1 -1
- package/dist/locale.js +1 -1
- package/dist/pages/observability/activity/calendar.d.ts +9 -0
- package/dist/pages/observability/activity/calendar.d.ts.map +1 -0
- package/dist/pages/observability/activity/connections.d.ts +7 -0
- package/dist/pages/observability/activity/connections.d.ts.map +1 -0
- package/dist/pages/observability/activity/format.d.ts +11 -0
- package/dist/pages/observability/activity/format.d.ts.map +1 -0
- package/dist/pages/observability/activity/messages.d.ts +231 -0
- package/dist/pages/observability/activity/messages.d.ts.map +1 -0
- package/dist/pages/observability/activity/meta-items.d.ts +10 -0
- package/dist/pages/observability/activity/meta-items.d.ts.map +1 -0
- package/dist/pages/observability/activity/page.d.ts +6 -0
- package/dist/pages/observability/activity/page.d.ts.map +1 -0
- package/dist/pages/observability/activity/period.d.ts +31 -0
- package/dist/pages/observability/activity/period.d.ts.map +1 -0
- package/dist/pages/observability/activity/steps.d.ts +44 -0
- package/dist/pages/observability/activity/steps.d.ts.map +1 -0
- package/dist/pages/observability/activity/timeline.d.ts +36 -0
- package/dist/pages/observability/activity/timeline.d.ts.map +1 -0
- package/dist/pages/observability/activity/users.d.ts +18 -0
- package/dist/pages/observability/activity/users.d.ts.map +1 -0
- package/dist/pages/routes.d.ts +7 -0
- package/dist/pages/routes.d.ts.map +1 -1
- package/dist/pages/section-ui.d.ts +2 -1
- package/dist/pages/section-ui.d.ts.map +1 -1
- package/dist/pages.d.ts +4 -1
- package/dist/pages.d.ts.map +1 -1
- package/dist/pages.js +5852 -3754
- package/dist/pages.js.map +20 -7
- package/dist/sections/catalog.d.ts.map +1 -1
- package/dist/sections/sections.d.ts.map +1 -1
- package/dist/server.js +63 -10
- package/dist/server.js.map +5 -5
- package/dist/shell/search-command.d.ts.map +1 -1
- package/dist/shell/user-menu.d.ts +3 -3
- package/dist/shell/user-menu.d.ts.map +1 -1
- package/package.json +4 -6
- package/src/agent/activity-label.ts +7 -0
- package/src/agent/agent-chat.tsx +29 -18
- package/src/agent/agent-execution-trace.tsx +1 -1
- package/src/agent/agent-panel.tsx +8 -13
- package/src/agent/agent-surface.tsx +4 -4
- package/src/agent/agents-page.ts +7 -0
- package/src/agent/artifact-card.tsx +190 -0
- package/src/agent/artifact-panel-state.ts +38 -0
- package/src/agent/artifact-panel.tsx +320 -0
- package/src/agent/components/file-attachment-card.tsx +13 -14
- package/src/agent/components/message-parts.tsx +30 -5
- package/src/agent/components/message-view.tsx +13 -19
- package/src/agent/components/thread-artifacts-menu.tsx +77 -0
- package/src/agent/i18n/en.ts +16 -58
- package/src/agent/i18n/fr.ts +15 -59
- package/src/agent/parts.ts +6 -6
- package/src/agent/publish-artifact/interpret.ts +110 -0
- package/src/agent/publish-artifact/publish-artifact-tool-view.tsx +62 -0
- package/src/agent/publish-artifact/use-live-publishes.ts +45 -0
- package/src/agent/react-router.tsx +26 -7
- package/src/agent/view-file/view-file-tool-view.tsx +21 -18
- package/src/artifacts/artifact-page.tsx +61 -0
- package/src/artifacts/artifacts.css +172 -0
- package/src/artifacts/card-details.ts +92 -0
- package/src/artifacts/card-preview.tsx +612 -0
- package/src/artifacts/copy-feedback.ts +25 -0
- package/src/artifacts/file-size.ts +24 -0
- package/src/artifacts/file-source.ts +109 -0
- package/src/artifacts/file-view.ts +57 -0
- package/src/artifacts/frame.tsx +288 -0
- package/src/artifacts/gallery.tsx +405 -0
- package/src/artifacts/kind-icon.tsx +23 -0
- package/src/artifacts/markdown-tree.ts +32 -0
- package/src/artifacts/messages.ts +317 -0
- package/src/artifacts/queries.ts +85 -0
- package/src/artifacts/references/embed-tree.ts +86 -0
- package/src/artifacts/references/references.tsx +739 -0
- package/src/artifacts/renderers/document-tree.ts +157 -0
- package/src/artifacts/renderers/document.tsx +334 -0
- package/src/artifacts/renderers/file.tsx +160 -0
- package/src/artifacts/renderers/notice.tsx +38 -0
- package/src/artifacts/renderers/sheet/chart-data.ts +73 -0
- package/src/artifacts/renderers/sheet/chart.tsx +288 -0
- package/src/artifacts/renderers/sheet/format.ts +142 -0
- package/src/artifacts/renderers/sheet/grid.tsx +709 -0
- package/src/artifacts/renderers/sheet/mini-chart.tsx +241 -0
- package/src/artifacts/renderers/sheet/model.ts +136 -0
- package/src/artifacts/renderers/sheet/selection.ts +152 -0
- package/src/artifacts/renderers/sheet/sheet.tsx +471 -0
- package/src/artifacts/renderers/sheet/virtual.ts +49 -0
- package/src/artifacts/renderers/sheet/workbook.ts +57 -0
- package/src/artifacts/renderers/slides/navigation.ts +38 -0
- package/src/artifacts/renderers/slides/slides.tsx +441 -0
- package/src/artifacts/target.ts +76 -0
- package/src/artifacts/theme.ts +29 -0
- package/src/artifacts/viewer.tsx +476 -0
- package/src/build.ts +13 -2
- package/src/codegen.ts +38 -2
- package/src/createCustomApp.ts +21 -1
- package/src/index.ts +0 -5
- package/src/locale-messages.ts +9 -2
- package/src/pages/observability/activity/calendar.tsx +239 -0
- package/src/pages/observability/activity/connections.tsx +144 -0
- package/src/pages/observability/activity/format.ts +49 -0
- package/src/pages/observability/activity/messages.ts +255 -0
- package/src/pages/observability/activity/meta-items.tsx +23 -0
- package/src/pages/observability/activity/page.tsx +605 -0
- package/src/pages/observability/activity/period.ts +63 -0
- package/src/pages/observability/activity/steps.ts +102 -0
- package/src/pages/observability/activity/timeline.tsx +411 -0
- package/src/pages/observability/activity/users.tsx +133 -0
- package/src/pages/observability/messages.ts +2 -2
- package/src/pages/routes.ts +15 -0
- package/src/pages/section-ui.tsx +3 -1
- package/src/pages.ts +4 -1
- package/src/sections/catalog.ts +1 -0
- package/src/sections/sections.tsx +2 -0
- package/src/shell/search-command.tsx +8 -6
- package/src/shell/user-menu.tsx +22 -22
- package/dist/agent/document-preview/classify.d.ts +0 -4
- package/dist/agent/document-preview/classify.d.ts.map +0 -1
- package/dist/agent/document-preview/content-policy.d.ts +0 -10
- package/dist/agent/document-preview/content-policy.d.ts.map +0 -1
- package/dist/agent/document-preview/content.d.ts +0 -26
- package/dist/agent/document-preview/content.d.ts.map +0 -1
- package/dist/agent/document-preview/delimited.d.ts +0 -19
- package/dist/agent/document-preview/delimited.d.ts.map +0 -1
- package/dist/agent/document-preview/document-preview-root.d.ts +0 -24
- package/dist/agent/document-preview/document-preview-root.d.ts.map +0 -1
- package/dist/agent/document-preview/file-size.d.ts +0 -3
- package/dist/agent/document-preview/file-size.d.ts.map +0 -1
- package/dist/agent/document-preview/html.d.ts +0 -9
- package/dist/agent/document-preview/html.d.ts.map +0 -1
- package/dist/agent/document-preview/persistence.d.ts +0 -6
- package/dist/agent/document-preview/persistence.d.ts.map +0 -1
- package/dist/agent/document-preview/presentation.d.ts +0 -3
- package/dist/agent/document-preview/presentation.d.ts.map +0 -1
- package/dist/agent/document-preview/rendering.d.ts +0 -14
- package/dist/agent/document-preview/rendering.d.ts.map +0 -1
- package/dist/agent/document-preview/source.d.ts +0 -13
- package/dist/agent/document-preview/source.d.ts.map +0 -1
- package/dist/agent/document-preview/state.d.ts +0 -27
- package/dist/agent/document-preview/state.d.ts.map +0 -1
- package/dist/agent/document-preview/types.d.ts +0 -33
- package/dist/agent/document-preview/types.d.ts.map +0 -1
- package/dist/agent/workspace.d.ts +0 -11
- package/dist/agent/workspace.d.ts.map +0 -1
- package/dist/chunk-3a99tmwg.js.map +0 -10
- package/dist/chunk-j97e06rg.js.map +0 -80
- package/src/agent/document-preview/classify.ts +0 -55
- package/src/agent/document-preview/content-policy.ts +0 -37
- package/src/agent/document-preview/content.ts +0 -82
- package/src/agent/document-preview/delimited.ts +0 -118
- package/src/agent/document-preview/document-preview-root.tsx +0 -1017
- package/src/agent/document-preview/file-size.ts +0 -26
- package/src/agent/document-preview/html.ts +0 -67
- package/src/agent/document-preview/persistence.ts +0 -115
- package/src/agent/document-preview/presentation.ts +0 -13
- package/src/agent/document-preview/rendering.ts +0 -48
- package/src/agent/document-preview/source.ts +0 -48
- package/src/agent/document-preview/state.ts +0 -77
- package/src/agent/document-preview/types.ts +0 -36
- package/src/agent/workspace.ts +0 -33
- /package/dist/{chunk-shmw3f8t.js.map → chunk-3schcf10.js.map} +0 -0
- /package/dist/{chunk-48pkffj6.js.map → chunk-g3f4cm8x.js.map} +0 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { createHeadingSlugger } from "@sixb/core/artifacts"
|
|
2
|
+
import {
|
|
3
|
+
isElement,
|
|
4
|
+
meaningfulChildren,
|
|
5
|
+
type TreeElement,
|
|
6
|
+
type TreeNode,
|
|
7
|
+
type TreeRoot,
|
|
8
|
+
type TreeText,
|
|
9
|
+
} from "../markdown-tree"
|
|
10
|
+
|
|
11
|
+
export const CALLOUT_KINDS = ["note", "tip", "important", "warning", "caution"] as const
|
|
12
|
+
export type CalloutKind = (typeof CALLOUT_KINDS)[number]
|
|
13
|
+
|
|
14
|
+
const CALLOUT_MARKER = /^\s*\[!(note|tip|important|warning|caution)\][ \t]*\n?/i
|
|
15
|
+
// A figure like 1,234.5 or -$5.56M or (1.3%) or 3.2x: digits with an optional sign, currency, and
|
|
16
|
+
// unit. Dates and identifiers with letters in the middle stay text.
|
|
17
|
+
const NUMERIC_CELL = /^[-+−(]?\s?[$€£¥]?\s?\d[\d,.\s]*(?:%|[kKmMbB]|bn|x)?\)?$/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Shapes a document's Markdown into the document page: the first heading becomes the title and the
|
|
21
|
+
* paragraph after it the lede, headings get the slugs publishing gave them, `> [!NOTE]` quotes
|
|
22
|
+
* become callouts, a titled image alone in its paragraph becomes a figure with a caption, and table
|
|
23
|
+
* columns of figures align right.
|
|
24
|
+
*/
|
|
25
|
+
export function rehypeArtifactDocument() {
|
|
26
|
+
return (tree: TreeRoot) => {
|
|
27
|
+
markTitleAndLede(tree)
|
|
28
|
+
slugHeadings(tree)
|
|
29
|
+
visitElements(tree, (element, parent, index) => {
|
|
30
|
+
if (element.tagName === "blockquote") shapeCallout(element)
|
|
31
|
+
if (element.tagName === "p" && parent) shapeFigure(element, parent, index)
|
|
32
|
+
if (element.tagName === "table") markNumericColumns(element)
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function markTitleAndLede(tree: TreeRoot): void {
|
|
38
|
+
const elements = tree.children.filter(isElement)
|
|
39
|
+
const [first, second] = elements
|
|
40
|
+
if (first?.tagName !== "h1") return
|
|
41
|
+
addClass(first, "sixb-document-title")
|
|
42
|
+
if (second?.tagName === "p") addClass(second, "sixb-document-lede")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function slugHeadings(tree: TreeRoot): void {
|
|
46
|
+
const slug = createHeadingSlugger()
|
|
47
|
+
visitElements(tree, (element) => {
|
|
48
|
+
// Headings that already have an id, such as the footnotes label, keep it.
|
|
49
|
+
if (!/^h[1-6]$/.test(element.tagName) || element.properties.id !== undefined) return
|
|
50
|
+
element.properties.id = slug(textContent(element).replace(/\s+/g, " ").trim())
|
|
51
|
+
})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function shapeCallout(blockquote: TreeElement): void {
|
|
55
|
+
const paragraph = blockquote.children.find(isElement)
|
|
56
|
+
const marker = paragraph?.tagName === "p" ? paragraph.children[0] : undefined
|
|
57
|
+
if (!paragraph || marker?.type !== "text") return
|
|
58
|
+
const text = marker as TreeText
|
|
59
|
+
const match = CALLOUT_MARKER.exec(text.value)
|
|
60
|
+
if (!match) return
|
|
61
|
+
|
|
62
|
+
text.value = text.value.slice(match[0].length)
|
|
63
|
+
if (text.value === "") paragraph.children.shift()
|
|
64
|
+
if (
|
|
65
|
+
paragraph.children.every((child) => child.type === "text" && !(child as TreeText).value.trim())
|
|
66
|
+
) {
|
|
67
|
+
blockquote.children = blockquote.children.filter((child) => child !== paragraph)
|
|
68
|
+
}
|
|
69
|
+
blockquote.tagName = "aside"
|
|
70
|
+
blockquote.properties = { ...blockquote.properties, dataCallout: match[1]?.toLowerCase() }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function shapeFigure(paragraph: TreeElement, parent: TreeElement | TreeRoot, index: number): void {
|
|
74
|
+
const meaningful = meaningfulChildren(paragraph)
|
|
75
|
+
const image = meaningful.length === 1 && isElement(meaningful[0]) ? meaningful[0] : undefined
|
|
76
|
+
const caption = image?.tagName === "img" ? image.properties.title : undefined
|
|
77
|
+
if (!image || typeof caption !== "string" || !caption.trim()) return
|
|
78
|
+
|
|
79
|
+
const { title: _title, ...properties } = image.properties
|
|
80
|
+
parent.children[index] = {
|
|
81
|
+
type: "element",
|
|
82
|
+
tagName: "figure",
|
|
83
|
+
properties: {},
|
|
84
|
+
children: [
|
|
85
|
+
{ ...image, properties },
|
|
86
|
+
{
|
|
87
|
+
type: "element",
|
|
88
|
+
tagName: "figcaption",
|
|
89
|
+
properties: {},
|
|
90
|
+
children: [{ type: "text", value: caption.trim() }],
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function markNumericColumns(table: TreeElement): void {
|
|
97
|
+
const rows = collect(table, "tr")
|
|
98
|
+
const header = rows.find((row) => row.children.some((cell) => isCell(cell, "th")))
|
|
99
|
+
const body = rows.filter((row) => row !== header)
|
|
100
|
+
// A loop, not a spread: a long table's rows are more arguments than a call can take.
|
|
101
|
+
let width = 0
|
|
102
|
+
for (const row of rows) width = Math.max(width, row.children.filter(isCellElement).length)
|
|
103
|
+
|
|
104
|
+
for (let column = 0; column < width; column += 1) {
|
|
105
|
+
const values = body
|
|
106
|
+
.map((row) => row.children.filter(isCellElement)[column])
|
|
107
|
+
.map((cell) => (cell ? textContent(cell).trim() : ""))
|
|
108
|
+
.filter((value) => value !== "")
|
|
109
|
+
if (values.length === 0 || !values.every((value) => NUMERIC_CELL.test(value))) continue
|
|
110
|
+
for (const row of rows) {
|
|
111
|
+
const cell = row.children.filter(isCellElement)[column]
|
|
112
|
+
if (cell) addClass(cell, "sixb-numeric")
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function visitElements(
|
|
118
|
+
node: TreeRoot | TreeElement,
|
|
119
|
+
visit: (element: TreeElement, parent: TreeElement | TreeRoot | null, index: number) => void
|
|
120
|
+
): void {
|
|
121
|
+
const children = node.children
|
|
122
|
+
for (let index = 0; index < children.length; index += 1) {
|
|
123
|
+
const child = children[index]
|
|
124
|
+
if (!isElement(child)) continue
|
|
125
|
+
visit(child, node, index)
|
|
126
|
+
// A visit may replace the child (a paragraph that became a figure): walk what is there now.
|
|
127
|
+
const current = children[index]
|
|
128
|
+
if (isElement(current)) visitElements(current, visit)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function collect(node: TreeElement, tagName: string): TreeElement[] {
|
|
133
|
+
const found: TreeElement[] = []
|
|
134
|
+
visitElements(node, (element) => {
|
|
135
|
+
if (element.tagName === tagName) found.push(element)
|
|
136
|
+
})
|
|
137
|
+
return found
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function textContent(node: TreeNode): string {
|
|
141
|
+
if (node.type === "text") return (node as TreeText).value
|
|
142
|
+
return "children" in node && node.children ? node.children.map(textContent).join("") : ""
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function addClass(element: TreeElement, className: string): void {
|
|
146
|
+
const current = element.properties.className
|
|
147
|
+
const classes = Array.isArray(current) ? current : typeof current === "string" ? [current] : []
|
|
148
|
+
element.properties.className = [...classes, className]
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function isCellElement(node: TreeNode): node is TreeElement {
|
|
152
|
+
return isCell(node, "th") || isCell(node, "td")
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function isCell(node: TreeNode, tagName: "th" | "td"): boolean {
|
|
156
|
+
return isElement(node) && node.tagName === tagName
|
|
157
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { Markdown, type MarkdownProps } from "@sixb/ui/components"
|
|
2
|
+
import { cn } from "@sixb/ui/lib/utils"
|
|
3
|
+
import {
|
|
4
|
+
AlertTriangle,
|
|
5
|
+
Info,
|
|
6
|
+
Lightbulb,
|
|
7
|
+
Link2,
|
|
8
|
+
MessageSquareWarning,
|
|
9
|
+
OctagonAlert,
|
|
10
|
+
} from "lucide-react"
|
|
11
|
+
import {
|
|
12
|
+
type ComponentProps,
|
|
13
|
+
type ReactNode,
|
|
14
|
+
useCallback,
|
|
15
|
+
useEffect,
|
|
16
|
+
useMemo,
|
|
17
|
+
useRef,
|
|
18
|
+
useState,
|
|
19
|
+
} from "react"
|
|
20
|
+
import { useCopyFeedback } from "../copy-feedback"
|
|
21
|
+
import { type ArtifactMessages, useArtifactMessages } from "../messages"
|
|
22
|
+
import {
|
|
23
|
+
type ArtifactVersion,
|
|
24
|
+
useFileText,
|
|
25
|
+
versionFileSource,
|
|
26
|
+
versionImageSource,
|
|
27
|
+
} from "../queries"
|
|
28
|
+
import { artifactUrlTransform, rehypeArtifactEmbeds } from "../references/embed-tree"
|
|
29
|
+
import { withArtifactReferences } from "../references/references"
|
|
30
|
+
import { artifactLink } from "../target"
|
|
31
|
+
import { useArtifactThemeAttributes } from "../theme"
|
|
32
|
+
import { CALLOUT_KINDS, type CalloutKind, rehypeArtifactDocument } from "./document-tree"
|
|
33
|
+
import { ViewerLoading, ViewerNotice } from "./notice"
|
|
34
|
+
|
|
35
|
+
// Embeds first, so an artifact shown alone in its paragraph is an embed, not a captioned figure.
|
|
36
|
+
const REHYPE_PLUGINS: MarkdownProps["rehypePlugins"] = [
|
|
37
|
+
rehypeArtifactEmbeds,
|
|
38
|
+
rehypeArtifactDocument,
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A document as a page to read: its title and lede, a measured column, figures with captions,
|
|
43
|
+
* callouts, and an outline that follows the reader. It draws only on theme tokens, so the app's
|
|
44
|
+
* theme, dark mode, and the artifact's own `theme.json` all apply.
|
|
45
|
+
*/
|
|
46
|
+
export function DocumentRenderer({
|
|
47
|
+
version,
|
|
48
|
+
latestVersion,
|
|
49
|
+
part,
|
|
50
|
+
}: {
|
|
51
|
+
readonly version: ArtifactVersion
|
|
52
|
+
/** Links to a section of the latest version follow the artifact; others pin this version. */
|
|
53
|
+
readonly latestVersion: number
|
|
54
|
+
/** The section to show first, by its slug. */
|
|
55
|
+
readonly part?: string
|
|
56
|
+
}) {
|
|
57
|
+
const messages = useArtifactMessages()
|
|
58
|
+
const source = versionFileSource(version, version.entry)
|
|
59
|
+
const file = useFileText(source)
|
|
60
|
+
const theme = useArtifactThemeAttributes(version)
|
|
61
|
+
const scrollRef = useRef<HTMLDivElement | null>(null)
|
|
62
|
+
const headings = useMemo(
|
|
63
|
+
() =>
|
|
64
|
+
version.outline.kind === "document"
|
|
65
|
+
? version.outline.headings.filter((heading) => heading.depth === 2 || heading.depth === 3)
|
|
66
|
+
: [],
|
|
67
|
+
[version.outline]
|
|
68
|
+
)
|
|
69
|
+
const activeSlug = useActiveHeading(scrollRef, headings, file.text)
|
|
70
|
+
|
|
71
|
+
const scrollToSection = useCallback((slug: string) => {
|
|
72
|
+
const heading = scrollRef.current?.querySelector(`[id="${CSS.escape(slug)}"]`)
|
|
73
|
+
heading?.scrollIntoView({ block: "start", behavior: "smooth" })
|
|
74
|
+
}, [])
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!part || file.text === undefined) return
|
|
78
|
+
scrollRef.current
|
|
79
|
+
?.querySelector(`[id="${CSS.escape(part)}"]`)
|
|
80
|
+
?.scrollIntoView({ block: "start" })
|
|
81
|
+
}, [part, file.text])
|
|
82
|
+
|
|
83
|
+
const components = useMemo(
|
|
84
|
+
() =>
|
|
85
|
+
withArtifactReferences(
|
|
86
|
+
documentComponents(version, latestVersion, messages.document, scrollToSection)
|
|
87
|
+
),
|
|
88
|
+
[version, latestVersion, messages.document, scrollToSection]
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
if (!source) return <ViewerNotice title={messages.viewer.loadFailed} />
|
|
92
|
+
if (file.loading) return <ViewerLoading label={messages.viewer.loading} />
|
|
93
|
+
if (file.tooLarge || file.error || file.text === undefined) {
|
|
94
|
+
return <ViewerNotice title={file.error ? messages.files[file.error] : messages.files.noText} />
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return (
|
|
98
|
+
<div
|
|
99
|
+
ref={scrollRef}
|
|
100
|
+
data-sixb-artifact=""
|
|
101
|
+
className={cn(
|
|
102
|
+
"@container min-h-0 flex-1 overflow-y-auto bg-background text-foreground",
|
|
103
|
+
theme.className
|
|
104
|
+
)}
|
|
105
|
+
style={theme.style}
|
|
106
|
+
>
|
|
107
|
+
<div className="mx-auto flex w-full max-w-[66rem] gap-14 px-6 py-10 @lg:px-12 @lg:py-14">
|
|
108
|
+
<article className="min-w-0 flex-1">
|
|
109
|
+
<Markdown
|
|
110
|
+
className="sixb-document"
|
|
111
|
+
components={components}
|
|
112
|
+
rehypePlugins={REHYPE_PLUGINS}
|
|
113
|
+
urlTransform={artifactUrlTransform}
|
|
114
|
+
>
|
|
115
|
+
{file.text}
|
|
116
|
+
</Markdown>
|
|
117
|
+
</article>
|
|
118
|
+
{headings.length > 1 ? (
|
|
119
|
+
<nav
|
|
120
|
+
aria-label={messages.document.outline}
|
|
121
|
+
className="sticky top-14 hidden w-48 shrink-0 self-start @4xl:block"
|
|
122
|
+
>
|
|
123
|
+
<p className="mb-3 text-xs font-semibold text-muted-foreground">
|
|
124
|
+
{messages.document.outline}
|
|
125
|
+
</p>
|
|
126
|
+
<ol className="flex flex-col gap-2 text-[13px] leading-snug">
|
|
127
|
+
{headings.map((heading) => (
|
|
128
|
+
<li key={heading.slug} className={heading.depth === 3 ? "pl-3" : undefined}>
|
|
129
|
+
<a
|
|
130
|
+
href={`#${heading.slug}`}
|
|
131
|
+
onClick={(event) => {
|
|
132
|
+
event.preventDefault()
|
|
133
|
+
scrollToSection(heading.slug)
|
|
134
|
+
}}
|
|
135
|
+
aria-current={activeSlug === heading.slug ? "location" : undefined}
|
|
136
|
+
className={cn(
|
|
137
|
+
"block transition-colors hover:text-foreground",
|
|
138
|
+
activeSlug === heading.slug
|
|
139
|
+
? "font-medium text-foreground"
|
|
140
|
+
: "text-muted-foreground"
|
|
141
|
+
)}
|
|
142
|
+
>
|
|
143
|
+
{heading.text}
|
|
144
|
+
</a>
|
|
145
|
+
</li>
|
|
146
|
+
))}
|
|
147
|
+
</ol>
|
|
148
|
+
</nav>
|
|
149
|
+
) : null}
|
|
150
|
+
</div>
|
|
151
|
+
</div>
|
|
152
|
+
)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The section the reader is in: the last heading that has scrolled past the reading line. The
|
|
157
|
+
* document scrolls itself when its host gives it a height, and scrolls with the page when the host
|
|
158
|
+
* lets it grow, so this follows whichever element scrolls.
|
|
159
|
+
*/
|
|
160
|
+
function useActiveHeading(
|
|
161
|
+
scrollRef: { readonly current: HTMLDivElement | null },
|
|
162
|
+
headings: readonly { readonly slug: string }[],
|
|
163
|
+
content: string | undefined
|
|
164
|
+
): string | null {
|
|
165
|
+
const [active, setActive] = useState<string | null>(null)
|
|
166
|
+
useEffect(() => {
|
|
167
|
+
const container = scrollRef.current
|
|
168
|
+
if (!container || content === undefined || headings.length === 0) return
|
|
169
|
+
let frame = 0
|
|
170
|
+
const update = () => {
|
|
171
|
+
frame = 0
|
|
172
|
+
const top = Math.max(container.getBoundingClientRect().top, 0) + 96
|
|
173
|
+
let current: string | null = headings[0]?.slug ?? null
|
|
174
|
+
for (const heading of headings) {
|
|
175
|
+
const element = container.querySelector(`[id="${CSS.escape(heading.slug)}"]`)
|
|
176
|
+
if (element && element.getBoundingClientRect().top <= top) current = heading.slug
|
|
177
|
+
}
|
|
178
|
+
setActive(current)
|
|
179
|
+
}
|
|
180
|
+
const schedule = () => {
|
|
181
|
+
if (!frame) frame = requestAnimationFrame(update)
|
|
182
|
+
}
|
|
183
|
+
update()
|
|
184
|
+
// Scroll events do not bubble; capturing at the document sees every scrolling element.
|
|
185
|
+
document.addEventListener("scroll", schedule, { capture: true, passive: true })
|
|
186
|
+
return () => {
|
|
187
|
+
document.removeEventListener("scroll", schedule, { capture: true })
|
|
188
|
+
if (frame) cancelAnimationFrame(frame)
|
|
189
|
+
}
|
|
190
|
+
}, [scrollRef, headings, content])
|
|
191
|
+
return active
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function documentComponents(
|
|
195
|
+
version: ArtifactVersion,
|
|
196
|
+
latestVersion: number,
|
|
197
|
+
messages: ArtifactMessages["document"],
|
|
198
|
+
scrollToSection: (slug: string) => void
|
|
199
|
+
): NonNullable<MarkdownProps["components"]> {
|
|
200
|
+
const heading =
|
|
201
|
+
(Tag: "h2" | "h3") =>
|
|
202
|
+
({ node: _node, id, children, ...props }: ComponentProps<"h2"> & { node?: unknown }) => (
|
|
203
|
+
<Tag id={id} {...props}>
|
|
204
|
+
{children}
|
|
205
|
+
{id ? (
|
|
206
|
+
<SectionAnchor
|
|
207
|
+
version={version}
|
|
208
|
+
latestVersion={latestVersion}
|
|
209
|
+
slug={id}
|
|
210
|
+
label={messages.copySection(headingText(version, id))}
|
|
211
|
+
/>
|
|
212
|
+
) : null}
|
|
213
|
+
</Tag>
|
|
214
|
+
)
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
h2: heading("h2"),
|
|
218
|
+
h3: heading("h3"),
|
|
219
|
+
img({ node: _node, src, alt, ...props }) {
|
|
220
|
+
const source = typeof src === "string" ? versionImageSource(version, src) : null
|
|
221
|
+
return source ? (
|
|
222
|
+
<img src={source.inlineUrl} alt={alt ?? ""} loading="lazy" {...props} />
|
|
223
|
+
) : null
|
|
224
|
+
},
|
|
225
|
+
a({ node: _node, href, children, ...props }) {
|
|
226
|
+
if (href?.startsWith("#")) {
|
|
227
|
+
return (
|
|
228
|
+
<a
|
|
229
|
+
href={href}
|
|
230
|
+
onClick={(event) => {
|
|
231
|
+
event.preventDefault()
|
|
232
|
+
scrollToSection(decodeURIComponent(href.slice(1)))
|
|
233
|
+
}}
|
|
234
|
+
{...props}
|
|
235
|
+
>
|
|
236
|
+
{children}
|
|
237
|
+
</a>
|
|
238
|
+
)
|
|
239
|
+
}
|
|
240
|
+
return (
|
|
241
|
+
<a href={href} target="_blank" rel="noreferrer noopener" {...props}>
|
|
242
|
+
{children}
|
|
243
|
+
</a>
|
|
244
|
+
)
|
|
245
|
+
},
|
|
246
|
+
aside({ node: _node, children, ...props }) {
|
|
247
|
+
const kind = (props as Record<string, unknown>)["data-callout"]
|
|
248
|
+
if (!isCalloutKind(kind)) return <aside {...props}>{children}</aside>
|
|
249
|
+
return (
|
|
250
|
+
<Callout kind={kind} label={messages.callouts[kind]}>
|
|
251
|
+
{children}
|
|
252
|
+
</Callout>
|
|
253
|
+
)
|
|
254
|
+
},
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function SectionAnchor({
|
|
259
|
+
version,
|
|
260
|
+
latestVersion,
|
|
261
|
+
slug,
|
|
262
|
+
label,
|
|
263
|
+
}: {
|
|
264
|
+
readonly version: ArtifactVersion
|
|
265
|
+
readonly latestVersion: number
|
|
266
|
+
readonly slug: string
|
|
267
|
+
readonly label: string
|
|
268
|
+
}) {
|
|
269
|
+
const { copied, copy } = useCopyFeedback()
|
|
270
|
+
return (
|
|
271
|
+
<button
|
|
272
|
+
type="button"
|
|
273
|
+
className="sixb-section-anchor"
|
|
274
|
+
aria-label={label}
|
|
275
|
+
title={label}
|
|
276
|
+
data-copied={copied ? "" : undefined}
|
|
277
|
+
onClick={() => {
|
|
278
|
+
copy(
|
|
279
|
+
artifactLink(
|
|
280
|
+
{
|
|
281
|
+
type: "artifact",
|
|
282
|
+
artifactId: version.artifactId,
|
|
283
|
+
version: version.version,
|
|
284
|
+
part: slug,
|
|
285
|
+
},
|
|
286
|
+
latestVersion,
|
|
287
|
+
location.origin
|
|
288
|
+
)
|
|
289
|
+
)
|
|
290
|
+
}}
|
|
291
|
+
>
|
|
292
|
+
<Link2 aria-hidden="true" />
|
|
293
|
+
</button>
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const CALLOUT_ICONS: Record<CalloutKind, typeof Info> = {
|
|
298
|
+
note: Info,
|
|
299
|
+
tip: Lightbulb,
|
|
300
|
+
important: MessageSquareWarning,
|
|
301
|
+
warning: AlertTriangle,
|
|
302
|
+
caution: OctagonAlert,
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function Callout({
|
|
306
|
+
kind,
|
|
307
|
+
label,
|
|
308
|
+
children,
|
|
309
|
+
}: {
|
|
310
|
+
readonly kind: CalloutKind
|
|
311
|
+
readonly label: string
|
|
312
|
+
readonly children: ReactNode
|
|
313
|
+
}) {
|
|
314
|
+
const Icon = CALLOUT_ICONS[kind]
|
|
315
|
+
return (
|
|
316
|
+
<aside className="sixb-callout" data-callout={kind}>
|
|
317
|
+
<Icon className="sixb-callout-icon" aria-hidden="true" />
|
|
318
|
+
<div className="sixb-callout-body">
|
|
319
|
+
<p className="sixb-callout-label">{label}</p>
|
|
320
|
+
{children}
|
|
321
|
+
</div>
|
|
322
|
+
</aside>
|
|
323
|
+
)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/** A heading's words as the outline recorded them, for labels that name the section. */
|
|
327
|
+
function headingText(version: ArtifactVersion, slug: string): string {
|
|
328
|
+
const headings = version.outline.kind === "document" ? version.outline.headings : []
|
|
329
|
+
return headings.find((heading) => heading.slug === slug)?.text ?? slug
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function isCalloutKind(value: unknown): value is CalloutKind {
|
|
333
|
+
return typeof value === "string" && (CALLOUT_KINDS as readonly string[]).includes(value)
|
|
334
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { CsvParseError, parseCsv } from "@sixb/core/artifacts"
|
|
2
|
+
import { Button, Markdown } from "@sixb/ui/components"
|
|
3
|
+
import { useLocale } from "@sixb/ui/lib/i18n"
|
|
4
|
+
import { Download, ExternalLink, File as FileIcon } from "lucide-react"
|
|
5
|
+
import { useMemo } from "react"
|
|
6
|
+
import { formatFileSize } from "../file-size"
|
|
7
|
+
import { type FileSource, useFileName } from "../file-source"
|
|
8
|
+
import { fileView } from "../file-view"
|
|
9
|
+
import { useArtifactMessages } from "../messages"
|
|
10
|
+
import { useFileText } from "../queries"
|
|
11
|
+
import { ViewerLoading, ViewerNotice } from "./notice"
|
|
12
|
+
import { fileSheetModel } from "./sheet/model"
|
|
13
|
+
import { SheetTable } from "./sheet/sheet"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* One file as its type allows: images, PDFs, Markdown, tables, and plain text in place, anything
|
|
17
|
+
* else as a card to download it. File artifacts, message attachments, and files a tool returned
|
|
18
|
+
* all open here.
|
|
19
|
+
*/
|
|
20
|
+
export function FileRenderer({ source }: { readonly source: FileSource }) {
|
|
21
|
+
const view = fileView(source.fileRef)
|
|
22
|
+
const name = useFileName(source)
|
|
23
|
+
switch (view) {
|
|
24
|
+
case "image":
|
|
25
|
+
return (
|
|
26
|
+
<div className="flex min-h-0 flex-1 items-center justify-center overflow-auto bg-muted/30 p-4 sm:p-8">
|
|
27
|
+
<img
|
|
28
|
+
src={source.inlineUrl}
|
|
29
|
+
alt={name}
|
|
30
|
+
className="max-h-full max-w-full rounded-md object-contain"
|
|
31
|
+
/>
|
|
32
|
+
</div>
|
|
33
|
+
)
|
|
34
|
+
case "pdf":
|
|
35
|
+
return <PdfFile source={source} name={name} />
|
|
36
|
+
case "markdown":
|
|
37
|
+
case "text":
|
|
38
|
+
return <TextFile source={source} name={name} markdown={view === "markdown"} />
|
|
39
|
+
case "csv":
|
|
40
|
+
case "tsv":
|
|
41
|
+
return <TableFile source={source} name={name} kind={view} />
|
|
42
|
+
case "download":
|
|
43
|
+
return <DownloadCard source={source} name={name} />
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface FileProps {
|
|
48
|
+
readonly source: FileSource
|
|
49
|
+
/** The file's name as the reader sees it. */
|
|
50
|
+
readonly name: string
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function TextFile({ source, name, markdown }: FileProps & { readonly markdown: boolean }) {
|
|
54
|
+
const messages = useArtifactMessages().files
|
|
55
|
+
const file = useFileText(source)
|
|
56
|
+
if (file.tooLarge) return <DownloadCard source={source} name={name} tooLarge />
|
|
57
|
+
if (file.loading) return <ViewerLoading label={messages.loading} />
|
|
58
|
+
if (file.error || file.text === undefined) {
|
|
59
|
+
return <ViewerNotice title={file.error ? messages[file.error] : messages.noText} />
|
|
60
|
+
}
|
|
61
|
+
return (
|
|
62
|
+
<div className="min-h-0 flex-1 overflow-y-auto">
|
|
63
|
+
{markdown ? (
|
|
64
|
+
<Markdown className="mx-auto max-w-3xl px-6 py-8 sm:px-10 sm:py-12">{file.text}</Markdown>
|
|
65
|
+
) : (
|
|
66
|
+
<pre className="m-0 px-6 py-6 font-mono text-[13px] leading-relaxed whitespace-pre-wrap break-words text-foreground">
|
|
67
|
+
{file.text}
|
|
68
|
+
</pre>
|
|
69
|
+
)}
|
|
70
|
+
</div>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function TableFile({ source, name, kind }: FileProps & { readonly kind: "csv" | "tsv" }) {
|
|
75
|
+
const messages = useArtifactMessages().files
|
|
76
|
+
const file = useFileText(source)
|
|
77
|
+
const parsed = useMemo(() => {
|
|
78
|
+
if (file.text === undefined) return null
|
|
79
|
+
try {
|
|
80
|
+
const table = parseCsv(file.text, { delimiter: kind === "tsv" ? "\t" : "," })
|
|
81
|
+
if (table.header.length === 0) return { error: messages.emptyFile }
|
|
82
|
+
return { model: fileSheetModel(name, table) }
|
|
83
|
+
} catch (error) {
|
|
84
|
+
if (error instanceof CsvParseError) return { error: messages.malformedNear(error.line) }
|
|
85
|
+
throw error
|
|
86
|
+
}
|
|
87
|
+
}, [file.text, kind, name, messages])
|
|
88
|
+
|
|
89
|
+
if (file.tooLarge) return <DownloadCard source={source} name={name} tooLarge />
|
|
90
|
+
if (file.loading) return <ViewerLoading label={messages.loading} />
|
|
91
|
+
if (file.error) {
|
|
92
|
+
return <ViewerNotice title={messages.tableFailed} description={messages[file.error]} />
|
|
93
|
+
}
|
|
94
|
+
if (!parsed) return <ViewerLoading label={messages.loading} />
|
|
95
|
+
if ("error" in parsed) {
|
|
96
|
+
return <ViewerNotice title={messages.tableFailed} description={parsed.error} />
|
|
97
|
+
}
|
|
98
|
+
return <SheetTable model={parsed.model} />
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function PdfFile({ source, name }: FileProps) {
|
|
102
|
+
const messages = useArtifactMessages()
|
|
103
|
+
return (
|
|
104
|
+
<object
|
|
105
|
+
data={source.inlineUrl}
|
|
106
|
+
type="application/pdf"
|
|
107
|
+
title={name}
|
|
108
|
+
className="flex min-h-0 w-full flex-1 bg-muted/30"
|
|
109
|
+
>
|
|
110
|
+
<ViewerNotice
|
|
111
|
+
title={messages.files.pdfUnavailable}
|
|
112
|
+
description={messages.files.pdfDescription}
|
|
113
|
+
action={
|
|
114
|
+
<div className="flex justify-center gap-2">
|
|
115
|
+
<Button variant="outline" size="sm" asChild>
|
|
116
|
+
<a href={source.inlineUrl} target="_blank" rel="noreferrer">
|
|
117
|
+
<ExternalLink />
|
|
118
|
+
{messages.viewer.openInNewTab(name)}
|
|
119
|
+
</a>
|
|
120
|
+
</Button>
|
|
121
|
+
<Button variant="outline" size="sm" asChild>
|
|
122
|
+
<a href={source.downloadUrl} download={name}>
|
|
123
|
+
<Download />
|
|
124
|
+
{messages.viewer.downloadLabel}
|
|
125
|
+
</a>
|
|
126
|
+
</Button>
|
|
127
|
+
</div>
|
|
128
|
+
}
|
|
129
|
+
/>
|
|
130
|
+
</object>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function DownloadCard({
|
|
135
|
+
source,
|
|
136
|
+
name,
|
|
137
|
+
tooLarge = false,
|
|
138
|
+
}: FileProps & { readonly tooLarge?: boolean }) {
|
|
139
|
+
const messages = useArtifactMessages()
|
|
140
|
+
const locale = useLocale()
|
|
141
|
+
return (
|
|
142
|
+
<ViewerNotice
|
|
143
|
+
icon={
|
|
144
|
+
<span className="flex size-12 items-center justify-center rounded-xl bg-muted text-muted-foreground">
|
|
145
|
+
<FileIcon className="size-6" aria-hidden="true" />
|
|
146
|
+
</span>
|
|
147
|
+
}
|
|
148
|
+
title={tooLarge ? messages.files.tooLarge : messages.files.noPreview}
|
|
149
|
+
description={`${name} · ${formatFileSize(source.fileRef.sizeBytes, locale, messages.files)}`}
|
|
150
|
+
action={
|
|
151
|
+
<Button size="sm" asChild>
|
|
152
|
+
<a href={source.downloadUrl} download={name} aria-label={messages.viewer.download(name)}>
|
|
153
|
+
<Download />
|
|
154
|
+
{messages.viewer.downloadLabel}
|
|
155
|
+
</a>
|
|
156
|
+
</Button>
|
|
157
|
+
}
|
|
158
|
+
/>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Spinner } from "@sixb/ui/components"
|
|
2
|
+
import { FileWarning } from "lucide-react"
|
|
3
|
+
import type { ReactNode } from "react"
|
|
4
|
+
|
|
5
|
+
/** A centered message in place of content that could not or should not render. */
|
|
6
|
+
export function ViewerNotice({
|
|
7
|
+
title,
|
|
8
|
+
description,
|
|
9
|
+
icon,
|
|
10
|
+
action,
|
|
11
|
+
}: {
|
|
12
|
+
readonly title: string
|
|
13
|
+
readonly description?: string
|
|
14
|
+
readonly icon?: ReactNode
|
|
15
|
+
readonly action?: ReactNode
|
|
16
|
+
}) {
|
|
17
|
+
return (
|
|
18
|
+
<div className="flex min-h-0 flex-1 items-center justify-center p-8 text-center">
|
|
19
|
+
<div className="flex max-w-sm flex-col items-center gap-2">
|
|
20
|
+
{icon ?? <FileWarning className="size-8 text-muted-foreground" aria-hidden="true" />}
|
|
21
|
+
<p className="text-sm font-medium text-foreground">{title}</p>
|
|
22
|
+
{description ? <p className="text-sm text-muted-foreground">{description}</p> : null}
|
|
23
|
+
{action ? <div className="pt-2">{action}</div> : null}
|
|
24
|
+
</div>
|
|
25
|
+
</div>
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function ViewerLoading({ label }: { readonly label: string }) {
|
|
30
|
+
return (
|
|
31
|
+
<div className="flex min-h-0 flex-1 items-center justify-center" aria-busy="true" role="status">
|
|
32
|
+
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
33
|
+
<Spinner className="size-4" />
|
|
34
|
+
{label}
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
)
|
|
38
|
+
}
|