@arbidocs/react 0.5.146 → 0.5.148
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/dist/artifacts/index.cjs +14 -14
- package/dist/calls/livekit/index.cjs +48 -48
- package/dist/chat/conversation-cache.js +5 -0
- package/dist/chat/conversation-cache.js.map +2 -2
- package/dist/chat/useConversation.js +10 -5
- package/dist/chat/useConversation.js.map +2 -2
- package/dist/chat/useConversationActions.js +4 -0
- package/dist/chat/useConversationActions.js.map +2 -2
- package/dist/chat/useConversationSettings.js +4 -10
- package/dist/chat/useConversationSettings.js.map +2 -2
- package/dist/{chunk-FE6ZSNZE.cjs → chunk-5FQB5F2F.cjs} +9 -4
- package/dist/chunk-5FQB5F2F.cjs.map +1 -0
- package/dist/{chunk-26SZ3DS4.cjs → chunk-MJIXYLI4.cjs} +66 -66
- package/dist/{chunk-26SZ3DS4.cjs.map → chunk-MJIXYLI4.cjs.map} +1 -1
- package/dist/{chunk-ZZZYVNFW.cjs → chunk-VBUEDJMK.cjs} +57 -57
- package/dist/{chunk-ZZZYVNFW.cjs.map → chunk-VBUEDJMK.cjs.map} +1 -1
- package/dist/{chunk-2F24LLVZ.cjs → chunk-VVPB7NKX.cjs} +38 -38
- package/dist/{chunk-2F24LLVZ.cjs.map → chunk-VVPB7NKX.cjs.map} +1 -1
- package/dist/{chunk-FDYZA6NO.cjs → chunk-XBY4RLC6.cjs} +4 -4
- package/dist/{chunk-FDYZA6NO.cjs.map → chunk-XBY4RLC6.cjs.map} +1 -1
- package/dist/{chunk-VO7OWQP6.cjs → chunk-ZFTWH6B6.cjs} +44 -44
- package/dist/{chunk-VO7OWQP6.cjs.map → chunk-ZFTWH6B6.cjs.map} +1 -1
- package/dist/databases.js +9 -6
- package/dist/databases.js.map +2 -2
- package/dist/datasets/index.js +25 -21
- package/dist/datasets/index.js.map +2 -2
- package/dist/datasets/pages.js +208 -0
- package/dist/datasets/pages.js.map +7 -0
- package/dist/datasets/registry.js.map +2 -2
- package/dist/datasets/sources/database.js +21 -23
- package/dist/datasets/sources/database.js.map +2 -2
- package/dist/document-tools/core.cjs +98 -98
- package/dist/document-tools/core.d.ts +2 -2
- package/dist/document-tools/index.cjs +100 -100
- package/dist/document-tools/index.d.ts +1 -1
- package/dist/documents/documents-writes.js +45 -14
- package/dist/documents/documents-writes.js.map +2 -2
- package/dist/documents/index.js +8 -0
- package/dist/documents/index.js.map +2 -2
- package/dist/documents/tag-when-processed.js +47 -0
- package/dist/documents/tag-when-processed.js.map +7 -0
- package/dist/host/config.js.map +1 -1
- package/dist/index.cjs +1893 -1596
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +319 -97
- package/dist/index.js +27 -5
- package/dist/index.js.map +2 -2
- package/dist/notifications/notification-types.js +7 -0
- package/dist/notifications/notification-types.js.map +2 -2
- package/dist/notifications/server-notifications.js +66 -10
- package/dist/notifications/server-notifications.js.map +2 -2
- package/dist/{query-keys.d-d2M4FAq2.d.ts → query-keys.d-pgq-4jyZ.d.ts} +7 -2
- package/dist/query-keys.js +7 -2
- package/dist/query-keys.js.map +2 -2
- package/dist/table-views.js +166 -0
- package/dist/table-views.js.map +7 -0
- package/dist/ui/index.cjs +13 -13
- package/dist/viewers/index.cjs +9 -9
- package/package.json +3 -3
- package/dist/account/table-views.js +0 -16
- package/dist/account/table-views.js.map +0 -7
- package/dist/chunk-FE6ZSNZE.cjs.map +0 -1
- package/dist/datasets/store.js +0 -194
- package/dist/datasets/store.js.map +0 -7
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/datasets/index.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * Datasets and dashboards: data source adapters returning one normalized `Dataset`, the\n * serialisable `ChartSpec` / `PanelSpec` / `DashboardSpec`, and
|
|
5
|
-
"mappings": "AAKA,cAAc;AACd;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,wBAAmE;AAC5E,SAAS,cAAc,uBAA+C;AACtE;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,
|
|
4
|
+
"sourcesContent": ["/**\n * Datasets and dashboards: data source adapters returning one normalized `Dataset`, the\n * serialisable `ChartSpec` / `PanelSpec` / `DashboardSpec`, and pages (`/v1/pages`). The\n * drawing is `@arbidocs/blocks/dashboards`'.\n */\nexport * from './types'\nexport {\n EMPTY_GROUP_LABEL,\n MAX_SERIES,\n aggregateDataset,\n bucketOf,\n bucketRange,\n fillTimeGaps,\n filterRows,\n recordsOf,\n} from './aggregate'\nexport { defineRowsSource, type RowsSourceDefinition, type RowsState } from './rows-source'\nexport { queryOfChart, usePanelDataset, type PanelDatasetState } from './panel'\nexport {\n BUILTIN_DATA_SOURCES,\n createDataSourceRegistry,\n useDataSources,\n type DashboardsHost,\n type DataSourceRegistry,\n} from './registry'\nexport {\n ANALYTICS_CHART_VIEW,\n DEFAULT_BLOCK_HEIGHT,\n PAGE_GRID_COLUMNS,\n blockOfPanel,\n packBlocks,\n pageFromDashboard,\n panelOfBlock,\n parsePageSpec,\n savedPageOf,\n useDeletePage,\n usePages,\n useSavePage,\n useSharePage,\n type AnalyticsChartRequest,\n type BlockLayout,\n type PageBlock,\n type PageBlockKind,\n type PageSpec,\n type PanelBlockConfig,\n type SavedPage,\n} from './pages'\nexport {\n DEFAULT_ACTIVITY_SOURCE_LABELS,\n createActivitySource,\n type ActivitySourceLabels,\n} from './sources/activity'\nexport {\n DEFAULT_CONVERSATIONS_SOURCE_LABELS,\n createConversationsSource,\n type ConversationsSourceLabels,\n} from './sources/conversations'\nexport {\n DEFAULT_DATABASE_SOURCE_LABELS,\n createDatabaseSource,\n type DatabaseSourceLabels,\n} from './sources/database'\nexport {\n DEFAULT_DOCUMENTS_SOURCE_LABELS,\n createDocumentsSource,\n type DocumentsSourceLabels,\n} from './sources/documents'\nexport {\n DEFAULT_FACTS_SOURCE_LABELS,\n createFactsSource,\n type FactsSourceLabels,\n} from './sources/facts'\nexport {\n DEFAULT_PROJECTS_SOURCE_LABELS,\n createProjectsSource,\n type ProjectsSourceLabels,\n type ProjectsSourceOptions,\n} from './sources/projects'\nexport { DEFAULT_TAGS_SOURCE_LABELS, createTagsSource, type TagsSourceLabels } from './sources/tags'\nexport {\n DEFAULT_WORKFLOWS_SOURCE_LABELS,\n createWorkflowsSource,\n type WorkflowsSourceLabels,\n} from './sources/workflows'\nexport {\n DEFAULT_WORKSPACES_SOURCE_LABELS,\n createWorkspacesSource,\n type WorkspacesSourceLabels,\n} from './sources/workspaces'\nexport {\n DEFAULT_USAGE_BREAKDOWN_SOURCE_LABELS,\n DEFAULT_USAGE_SOURCE_LABELS,\n MODEL_GROUP_LABELS,\n createUsageBreakdownSource,\n createUsageSource,\n type UsageBreakdownBy,\n type UsageBreakdownKey,\n type UsageBreakdownSourceLabels,\n type UsageSourceLabels,\n} from './sources/usage'\n"],
|
|
5
|
+
"mappings": "AAKA,cAAc;AACd;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,wBAAmE;AAC5E,SAAS,cAAc,uBAA+C;AACtE;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OAGK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAQK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAGK;AACP,SAAS,4BAA4B,wBAA+C;AACpF;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,OAEK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAKK;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
2
|
+
import { useArbi } from "../useArbi.js";
|
|
3
|
+
import { arbiQueryKeys } from "../query-keys.js";
|
|
4
|
+
const ANALYTICS_CHART_VIEW = "analytics.chartDatabase";
|
|
5
|
+
const PAGE_GRID_COLUMNS = 12;
|
|
6
|
+
const DEFAULT_BLOCK_HEIGHT = {
|
|
7
|
+
metric: 2,
|
|
8
|
+
text: 2,
|
|
9
|
+
chart: 4,
|
|
10
|
+
table: 6,
|
|
11
|
+
document: 8,
|
|
12
|
+
gallery: 8
|
|
13
|
+
};
|
|
14
|
+
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
15
|
+
const clamp = (n, lo, hi, fallback) => typeof n === "number" && Number.isFinite(n) ? Math.min(hi, Math.max(lo, Math.round(n))) : fallback;
|
|
16
|
+
function packBlocks(blocks) {
|
|
17
|
+
let x = 0;
|
|
18
|
+
let rowTop = 0;
|
|
19
|
+
let bottom = 0;
|
|
20
|
+
return blocks.map((b) => {
|
|
21
|
+
const w = clamp(b.layout.w, 1, PAGE_GRID_COLUMNS, PAGE_GRID_COLUMNS);
|
|
22
|
+
const h = clamp(b.layout.h, 1, 100, 4);
|
|
23
|
+
if (x + w > PAGE_GRID_COLUMNS) {
|
|
24
|
+
x = 0;
|
|
25
|
+
rowTop = bottom;
|
|
26
|
+
}
|
|
27
|
+
const layout = { x, y: rowTop, w, h };
|
|
28
|
+
x += w;
|
|
29
|
+
bottom = Math.max(bottom, rowTop + h);
|
|
30
|
+
return { ...b, layout };
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function blockOfPanel(panel, h) {
|
|
34
|
+
const kind = panel.chart.type === "metric" ? "metric" : "chart";
|
|
35
|
+
return {
|
|
36
|
+
id: panel.id,
|
|
37
|
+
kind,
|
|
38
|
+
layout: { x: 0, y: 0, w: panel.width ?? PAGE_GRID_COLUMNS, h: h ?? DEFAULT_BLOCK_HEIGHT[kind] },
|
|
39
|
+
config: {
|
|
40
|
+
title: panel.title,
|
|
41
|
+
...panel.description ? { description: panel.description } : {},
|
|
42
|
+
source: panel.source,
|
|
43
|
+
chart: panel.chart
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function panelOfBlock(block) {
|
|
48
|
+
return {
|
|
49
|
+
id: block.id,
|
|
50
|
+
title: block.config.title,
|
|
51
|
+
...block.config.description ? { description: block.config.description } : {},
|
|
52
|
+
source: block.config.source,
|
|
53
|
+
chart: block.config.chart,
|
|
54
|
+
width: block.layout.w
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function pageFromDashboard(dashboard) {
|
|
58
|
+
return {
|
|
59
|
+
version: 1,
|
|
60
|
+
title: dashboard.title,
|
|
61
|
+
...dashboard.description ? { description: dashboard.description } : {},
|
|
62
|
+
blocks: packBlocks(dashboard.panels.map((p) => blockOfPanel(p)))
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function isPanelConfig(v) {
|
|
66
|
+
return isObject(v) && typeof v.title === "string" && isObject(v.source) && typeof v.source.id === "string" && isObject(v.chart) && typeof v.chart.type === "string";
|
|
67
|
+
}
|
|
68
|
+
function blockOf(v) {
|
|
69
|
+
if (!isObject(v) || typeof v.id !== "string" || !isObject(v.config)) return null;
|
|
70
|
+
const l = isObject(v.layout) ? v.layout : {};
|
|
71
|
+
const layout = {
|
|
72
|
+
x: clamp(l.x, 0, PAGE_GRID_COLUMNS - 1, 0),
|
|
73
|
+
y: clamp(l.y, 0, 1e4, 0),
|
|
74
|
+
w: clamp(l.w, 1, PAGE_GRID_COLUMNS, PAGE_GRID_COLUMNS),
|
|
75
|
+
h: clamp(l.h, 1, 100, 4)
|
|
76
|
+
};
|
|
77
|
+
const config = v.config;
|
|
78
|
+
switch (v.kind) {
|
|
79
|
+
case "chart":
|
|
80
|
+
case "metric":
|
|
81
|
+
return isPanelConfig(config) ? { id: v.id, kind: v.kind, layout, config } : null;
|
|
82
|
+
case "text":
|
|
83
|
+
return { id: v.id, kind: "text", layout, config: { text: String(config.text ?? "") } };
|
|
84
|
+
case "table":
|
|
85
|
+
return typeof config.view === "string" ? { id: v.id, kind: "table", layout, config: { view: config.view } } : null;
|
|
86
|
+
case "document":
|
|
87
|
+
return typeof config.document === "string" ? { id: v.id, kind: "document", layout, config: { document: config.document } } : null;
|
|
88
|
+
case "gallery": {
|
|
89
|
+
const documents = Array.isArray(config.documents) ? config.documents.filter((d) => typeof d === "string") : [];
|
|
90
|
+
return documents.length ? { id: v.id, kind: "gallery", layout, config: { documents } } : null;
|
|
91
|
+
}
|
|
92
|
+
default:
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
function parsePageSpec(value) {
|
|
97
|
+
if (!isObject(value) || value.version !== 1 || typeof value.title !== "string") return null;
|
|
98
|
+
return {
|
|
99
|
+
version: 1,
|
|
100
|
+
title: value.title,
|
|
101
|
+
...typeof value.description === "string" && value.description ? { description: value.description } : {},
|
|
102
|
+
blocks: (Array.isArray(value.blocks) ? value.blocks : []).map(blockOf).filter((b) => b !== null)
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function parseDashboardJson(json) {
|
|
106
|
+
if (json === null) return null;
|
|
107
|
+
let parsed;
|
|
108
|
+
try {
|
|
109
|
+
parsed = JSON.parse(json);
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
if (!isObject(parsed) || typeof parsed.title !== "string") return null;
|
|
114
|
+
const panels = (Array.isArray(parsed.panels) ? parsed.panels : []).filter(
|
|
115
|
+
(p) => isObject(p) && typeof p.id === "string" && typeof p.title === "string" && isPanelConfig({ ...p, title: p.title })
|
|
116
|
+
);
|
|
117
|
+
return {
|
|
118
|
+
version: 1,
|
|
119
|
+
title: parsed.title,
|
|
120
|
+
...typeof parsed.description === "string" ? { description: parsed.description } : {},
|
|
121
|
+
panels
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const EMPTY = { version: 1, title: "", blocks: [] };
|
|
125
|
+
function savedPageOf(page) {
|
|
126
|
+
const legacy = page.workspaceId === null;
|
|
127
|
+
const dashboard = legacy ? parseDashboardJson(page.legacyJson) : null;
|
|
128
|
+
const spec = legacy ? dashboard && pageFromDashboard(dashboard) : parsePageSpec(page.spec);
|
|
129
|
+
return {
|
|
130
|
+
id: page.id,
|
|
131
|
+
workspaceId: page.workspaceId,
|
|
132
|
+
shared: page.shared,
|
|
133
|
+
spec: spec ?? EMPTY,
|
|
134
|
+
viewIds: page.viewIds,
|
|
135
|
+
documentIds: page.documentIds,
|
|
136
|
+
createdBy: page.createdBy,
|
|
137
|
+
updatedAt: page.updatedAt,
|
|
138
|
+
legacy,
|
|
139
|
+
unreadable: page.unreadable || spec === null
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function openWorkspaceId(arbi) {
|
|
143
|
+
return arbi.hasWorkspace ? arbi.getWorkspaceId() : "";
|
|
144
|
+
}
|
|
145
|
+
function usePages() {
|
|
146
|
+
const arbi = useArbi();
|
|
147
|
+
const workspaceId = openWorkspaceId(arbi);
|
|
148
|
+
return useQuery({
|
|
149
|
+
queryKey: arbiQueryKeys.pages(workspaceId),
|
|
150
|
+
queryFn: () => arbi.readInWorkspace(workspaceId, async () => (await arbi.pages.list()).map(savedPageOf)),
|
|
151
|
+
enabled: !!workspaceId && arbi.isLoggedIn,
|
|
152
|
+
staleTime: 1e3 * 30
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function usePageCache() {
|
|
156
|
+
const arbi = useArbi();
|
|
157
|
+
const queryClient = useQueryClient();
|
|
158
|
+
const key = arbiQueryKeys.pages(openWorkspaceId(arbi));
|
|
159
|
+
return {
|
|
160
|
+
put: (page) => queryClient.setQueryData(
|
|
161
|
+
key,
|
|
162
|
+
(old = []) => old.some((p) => p.id === page.id) ? old.map((p) => p.id === page.id ? page : p) : [...old, page]
|
|
163
|
+
),
|
|
164
|
+
drop: (id) => queryClient.setQueryData(key, (old = []) => old.filter((p) => p.id !== id))
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function useSavePage() {
|
|
168
|
+
const arbi = useArbi();
|
|
169
|
+
const cache = usePageCache();
|
|
170
|
+
return useMutation({
|
|
171
|
+
mutationFn: async ({ id, spec, shared }) => savedPageOf(
|
|
172
|
+
id ? await arbi.pages.update(id, spec) : await arbi.pages.create({ spec, shared })
|
|
173
|
+
),
|
|
174
|
+
onSuccess: cache.put
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
function useSharePage() {
|
|
178
|
+
const arbi = useArbi();
|
|
179
|
+
const cache = usePageCache();
|
|
180
|
+
return useMutation({
|
|
181
|
+
mutationFn: async (id) => savedPageOf(await arbi.pages.share(id)),
|
|
182
|
+
onSuccess: cache.put
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
function useDeletePage() {
|
|
186
|
+
const arbi = useArbi();
|
|
187
|
+
const cache = usePageCache();
|
|
188
|
+
return useMutation({
|
|
189
|
+
mutationFn: (id) => arbi.pages.remove(id),
|
|
190
|
+
onSuccess: (_void, id) => cache.drop(id)
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
export {
|
|
194
|
+
ANALYTICS_CHART_VIEW,
|
|
195
|
+
DEFAULT_BLOCK_HEIGHT,
|
|
196
|
+
PAGE_GRID_COLUMNS,
|
|
197
|
+
blockOfPanel,
|
|
198
|
+
packBlocks,
|
|
199
|
+
pageFromDashboard,
|
|
200
|
+
panelOfBlock,
|
|
201
|
+
parsePageSpec,
|
|
202
|
+
savedPageOf,
|
|
203
|
+
useDeletePage,
|
|
204
|
+
usePages,
|
|
205
|
+
useSavePage,
|
|
206
|
+
useSharePage
|
|
207
|
+
};
|
|
208
|
+
//# sourceMappingURL=pages.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/datasets/pages.ts"],
|
|
4
|
+
"sourcesContent": ["/**\n * Pages: the open workspace's layouts of live blocks (charts, metrics, text and table views), kept\n * on the server (`arbi.pages`, `/v1/pages`) under the workspace key. A page is its creator's until\n * shared. Sharing is one-way: a shared page is every member's to read and every editor's to edit,\n * and only a workspace owner deletes it. A shared page never shows a private view (the server\n * refuses: `PagePrivateContentError`).\n *\n * A page is a 12-column grid: each block has a place and a size (`layout`). Chart and metric blocks\n * carry an Analytics panel's config (source, chart); a table block carries only the view it shows.\n * Dashboards saved before pages come back as private pages (`legacy`), opened with the user's key;\n * saving one places it in the open workspace.\n */\nimport { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'\nimport type { Page, PageSpecWire } from '@arbidocs/sdk/browser'\nimport { useArbi } from '../useArbi'\nimport { arbiQueryKeys } from '../query-keys'\nimport type { ChartSpec, DashboardSpec, DataSourceRef, PanelSpec } from './types'\n\n/**\n * The transient view through which a host asks for a chart of a database (\"Chart this\"): set it to\n * `{ id, name, workspaceId }` and open the pages, which start a new page on that database and clear it.\n */\nexport const ANALYTICS_CHART_VIEW = 'analytics.chartDatabase'\n\n/** A database to chart. */\nexport interface AnalyticsChartRequest {\n id: string\n name: string\n /** The database's workspace, so the new blocks read it from anywhere. */\n workspaceId?: string\n}\n\n/** The columns of a page's grid. */\nexport const PAGE_GRID_COLUMNS = 12\n\nexport type PageBlockKind = 'chart' | 'metric' | 'text' | 'table' | 'document' | 'gallery'\n\n/** Where a block sits: column `x` (0\u201311) and row `y`, `w` columns wide and `h` rows high. */\nexport interface BlockLayout {\n x: number\n y: number\n w: number\n h: number\n}\n\n/** A chart or metric block: what an Analytics panel shows. */\nexport interface PanelBlockConfig {\n title: string\n description?: string\n source: DataSourceRef\n chart: ChartSpec\n}\n\ninterface BlockBase {\n id: string\n layout: BlockLayout\n}\n\nexport type PageBlock =\n | (BlockBase & { kind: 'chart'; config: PanelBlockConfig })\n | (BlockBase & { kind: 'metric'; config: PanelBlockConfig })\n | (BlockBase & { kind: 'text'; config: { text: string } })\n /** A saved table view, nothing else: its columns, filters and sort are the view's. */\n | (BlockBase & { kind: 'table'; config: { view: string } })\n /** One document (a PDF, a Word file, an image, a generated report), read-only. */\n | (BlockBase & { kind: 'document'; config: { document: string } })\n /** Documents shown one at a time, in this order, with previous / next and thumbnails. */\n | (BlockBase & { kind: 'gallery'; config: { documents: string[] } })\n\n/** A page: a titled grid of blocks. Plain JSON (`version` for later migrations). */\nexport interface PageSpec {\n version: 1\n title: string\n description?: string\n blocks: PageBlock[]\n}\n\n/** A page as read. */\nexport interface SavedPage {\n id: string\n /** `null`: a dashboard from before pages, not yet saved into a workspace. */\n workspaceId: string | null\n shared: boolean\n spec: PageSpec\n /** The views its table blocks show (the server's record). */\n viewIds: string[]\n /** The documents its document and gallery blocks show (the server's record). */\n documentIds: string[]\n createdBy: string | null\n updatedAt: string\n /** A dashboard from before pages (sealed to the user): saving it places it in this workspace. */\n legacy: boolean\n /** It could not be opened (sealed under another key, or damaged): it can only be deleted. */\n unreadable: boolean\n}\n\n/** A new block's height in rows, by kind. */\nexport const DEFAULT_BLOCK_HEIGHT: Record<PageBlockKind, number> = {\n metric: 2,\n text: 2,\n chart: 4,\n table: 6,\n document: 8,\n gallery: 8,\n}\n\nconst isObject = (v: unknown): v is Record<string, unknown> =>\n typeof v === 'object' && v !== null && !Array.isArray(v)\n\nconst clamp = (n: unknown, lo: number, hi: number, fallback: number) =>\n typeof n === 'number' && Number.isFinite(n) ? Math.min(hi, Math.max(lo, Math.round(n))) : fallback\n\n/**\n * Place blocks in order, left to right in rows: a block goes beside the last one when it fits in\n * the 12 columns, else below everything placed so far. Each keeps its size.\n */\nexport function packBlocks<B extends { layout: BlockLayout }>(blocks: readonly B[]): B[] {\n let x = 0\n let rowTop = 0\n let bottom = 0\n return blocks.map((b) => {\n const w = clamp(b.layout.w, 1, PAGE_GRID_COLUMNS, PAGE_GRID_COLUMNS)\n const h = clamp(b.layout.h, 1, 100, 4)\n if (x + w > PAGE_GRID_COLUMNS) {\n x = 0\n rowTop = bottom\n }\n const layout = { x, y: rowTop, w, h }\n x += w\n bottom = Math.max(bottom, rowTop + h)\n return { ...b, layout }\n })\n}\n\n/** A chart or metric block of an Analytics panel. */\nexport function blockOfPanel(panel: PanelSpec, h?: number): PageBlock {\n const kind = panel.chart.type === 'metric' ? 'metric' : 'chart'\n return {\n id: panel.id,\n kind,\n layout: { x: 0, y: 0, w: panel.width ?? PAGE_GRID_COLUMNS, h: h ?? DEFAULT_BLOCK_HEIGHT[kind] },\n config: {\n title: panel.title,\n ...(panel.description ? { description: panel.description } : {}),\n source: panel.source,\n chart: panel.chart,\n },\n }\n}\n\n/** The Analytics panel a chart or metric block draws. */\nexport function panelOfBlock(block: Extract<PageBlock, { kind: 'chart' | 'metric' }>): PanelSpec {\n return {\n id: block.id,\n title: block.config.title,\n ...(block.config.description ? { description: block.config.description } : {}),\n source: block.config.source,\n chart: block.config.chart,\n width: block.layout.w,\n }\n}\n\n/** A dashboard (a template, or one saved before pages) as a page: its panels as blocks, in order. */\nexport function pageFromDashboard(dashboard: DashboardSpec): PageSpec {\n return {\n version: 1,\n title: dashboard.title,\n ...(dashboard.description ? { description: dashboard.description } : {}),\n blocks: packBlocks(dashboard.panels.map((p) => blockOfPanel(p))),\n }\n}\n\nfunction isPanelConfig(v: unknown): v is PanelBlockConfig {\n return (\n isObject(v) &&\n typeof v.title === 'string' &&\n isObject(v.source) &&\n typeof v.source.id === 'string' &&\n isObject(v.chart) &&\n typeof v.chart.type === 'string'\n )\n}\n\nfunction blockOf(v: unknown): PageBlock | null {\n if (!isObject(v) || typeof v.id !== 'string' || !isObject(v.config)) return null\n const l = isObject(v.layout) ? v.layout : {}\n const layout = {\n x: clamp(l.x, 0, PAGE_GRID_COLUMNS - 1, 0),\n y: clamp(l.y, 0, 10_000, 0),\n w: clamp(l.w, 1, PAGE_GRID_COLUMNS, PAGE_GRID_COLUMNS),\n h: clamp(l.h, 1, 100, 4),\n }\n const config = v.config\n switch (v.kind) {\n case 'chart':\n case 'metric':\n return isPanelConfig(config) ? { id: v.id, kind: v.kind, layout, config } : null\n case 'text':\n return { id: v.id, kind: 'text', layout, config: { text: String(config.text ?? '') } }\n case 'table':\n return typeof config.view === 'string'\n ? { id: v.id, kind: 'table', layout, config: { view: config.view } }\n : null\n case 'document':\n return typeof config.document === 'string'\n ? { id: v.id, kind: 'document', layout, config: { document: config.document } }\n : null\n case 'gallery': {\n const documents = Array.isArray(config.documents)\n ? config.documents.filter((d): d is string => typeof d === 'string')\n : []\n return documents.length ? { id: v.id, kind: 'gallery', layout, config: { documents } } : null\n }\n default:\n return null\n }\n}\n\n/** A stored spec, checked: `null` when it is not a page (malformed blocks are dropped). */\nexport function parsePageSpec(value: unknown): PageSpec | null {\n if (!isObject(value) || value.version !== 1 || typeof value.title !== 'string') return null\n return {\n version: 1,\n title: value.title,\n ...(typeof value.description === 'string' && value.description\n ? { description: value.description }\n : {}),\n blocks: (Array.isArray(value.blocks) ? value.blocks : [])\n .map(blockOf)\n .filter((b): b is PageBlock => b !== null),\n }\n}\n\n/** A dashboard spec saved before pages (its JSON), checked. */\nfunction parseDashboardJson(json: string | null): DashboardSpec | null {\n if (json === null) return null\n let parsed: unknown\n try {\n parsed = JSON.parse(json)\n } catch {\n return null\n }\n if (!isObject(parsed) || typeof parsed.title !== 'string') return null\n const panels = (Array.isArray(parsed.panels) ? parsed.panels : []).filter(\n (p): p is PanelSpec =>\n isObject(p) &&\n typeof p.id === 'string' &&\n typeof p.title === 'string' &&\n isPanelConfig({ ...p, title: p.title })\n )\n return {\n version: 1,\n title: parsed.title,\n ...(typeof parsed.description === 'string' ? { description: parsed.description } : {}),\n panels,\n }\n}\n\nconst EMPTY: PageSpec = { version: 1, title: '', blocks: [] }\n\n/** A page as the hooks give it. */\nexport function savedPageOf(page: Page): SavedPage {\n const legacy = page.workspaceId === null\n const dashboard = legacy ? parseDashboardJson(page.legacyJson) : null\n const spec = legacy\n ? dashboard && pageFromDashboard(dashboard)\n : parsePageSpec(page.spec as unknown)\n return {\n id: page.id,\n workspaceId: page.workspaceId,\n shared: page.shared,\n spec: spec ?? EMPTY,\n viewIds: page.viewIds,\n documentIds: page.documentIds,\n createdBy: page.createdBy,\n updatedAt: page.updatedAt,\n legacy,\n unreadable: page.unreadable || spec === null,\n }\n}\n\n/** The open workspace's id, or '' with none open. */\nfunction openWorkspaceId(arbi: ReturnType<typeof useArbi>): string {\n return arbi.hasWorkspace ? arbi.getWorkspaceId() : ''\n}\n\n/** The open workspace's pages the user can see (shared ones and their own), oldest first. */\nexport function usePages() {\n const arbi = useArbi()\n const workspaceId = openWorkspaceId(arbi)\n return useQuery({\n queryKey: arbiQueryKeys.pages(workspaceId),\n queryFn: () =>\n arbi.readInWorkspace(workspaceId, async () => (await arbi.pages.list()).map(savedPageOf)),\n enabled: !!workspaceId && arbi.isLoggedIn,\n staleTime: 1000 * 30,\n })\n}\n\nfunction usePageCache() {\n const arbi = useArbi()\n const queryClient = useQueryClient()\n const key = arbiQueryKeys.pages(openWorkspaceId(arbi))\n return {\n put: (page: SavedPage) =>\n queryClient.setQueryData<SavedPage[]>(key, (old = []) =>\n old.some((p) => p.id === page.id)\n ? old.map((p) => (p.id === page.id ? page : p))\n : [...old, page]\n ),\n drop: (id: string) =>\n queryClient.setQueryData<SavedPage[]>(key, (old = []) => old.filter((p) => p.id !== id)),\n }\n}\n\n/** Save a page: new without `id` (private unless `shared`), else its spec replaced. */\nexport function useSavePage() {\n const arbi = useArbi()\n const cache = usePageCache()\n return useMutation({\n mutationFn: async ({ id, spec, shared }: { id?: string; spec: PageSpec; shared?: boolean }) =>\n savedPageOf(\n id\n ? await arbi.pages.update(id, spec as unknown as PageSpecWire)\n : await arbi.pages.create({ spec: spec as unknown as PageSpecWire, shared })\n ),\n onSuccess: cache.put,\n })\n}\n\n/** Share a page with the workspace. One-way: a shared page stays shared (only an owner deletes it). */\nexport function useSharePage() {\n const arbi = useArbi()\n const cache = usePageCache()\n return useMutation({\n mutationFn: async (id: string) => savedPageOf(await arbi.pages.share(id)),\n onSuccess: cache.put,\n })\n}\n\nexport function useDeletePage() {\n const arbi = useArbi()\n const cache = usePageCache()\n return useMutation({\n mutationFn: (id: string) => arbi.pages.remove(id),\n onSuccess: (_void, id) => cache.drop(id),\n })\n}\n"],
|
|
5
|
+
"mappings": "AAYA,SAAS,aAAa,UAAU,sBAAsB;AAEtD,SAAS,eAAe;AACxB,SAAS,qBAAqB;AAOvB,MAAM,uBAAuB;AAW7B,MAAM,oBAAoB;AAgE1B,MAAM,uBAAsD;AAAA,EACjE,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AAAA,EACP,UAAU;AAAA,EACV,SAAS;AACX;AAEA,MAAM,WAAW,CAAC,MAChB,OAAO,MAAM,YAAY,MAAM,QAAQ,CAAC,MAAM,QAAQ,CAAC;AAEzD,MAAM,QAAQ,CAAC,GAAY,IAAY,IAAY,aACjD,OAAO,MAAM,YAAY,OAAO,SAAS,CAAC,IAAI,KAAK,IAAI,IAAI,KAAK,IAAI,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI;AAMrF,SAAS,WAA8C,QAA2B;AACvF,MAAI,IAAI;AACR,MAAI,SAAS;AACb,MAAI,SAAS;AACb,SAAO,OAAO,IAAI,CAAC,MAAM;AACvB,UAAM,IAAI,MAAM,EAAE,OAAO,GAAG,GAAG,mBAAmB,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MAAO,EAAE,OAAO,KAAK,KAAK,OAAO,CAAE,IAC5C,CAAC,GAAG,KAAK,IAAI;AAAA,IACnB;AAAA,IACF,MAAM,CAAC,OACL,YAAY,aAA0B,KAAK,CAAC,MAAM,CAAC,MAAM,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;AAAA,EAC3F;AACF;AAGO,SAAS,cAAc;AAC5B,QAAM,OAAO,QAAQ;AACrB,QAAM,QAAQ,aAAa;AAC3B,SAAO,YAAY;AAAA,IACjB,YAAY,OAAO,EAAE,IAAI,MAAM,OAAO,MACpC;AAAA,MACE,KACI,MAAM,KAAK,MAAM,OAAO,IAAI,IAA+B,IAC3D,MAAM,KAAK,MAAM,OAAO,EAAE,MAAuC,OAAO,CAAC;AAAA,IAC/E;AAAA,IACF,WAAW,MAAM;AAAA,EACnB,CAAC;AACH;AAGO,SAAS,eAAe;AAC7B,QAAM,OAAO,QAAQ;AACrB,QAAM,QAAQ,aAAa;AAC3B,SAAO,YAAY;AAAA,IACjB,YAAY,OAAO,OAAe,YAAY,MAAM,KAAK,MAAM,MAAM,EAAE,CAAC;AAAA,IACxE,WAAW,MAAM;AAAA,EACnB,CAAC;AACH;AAEO,SAAS,gBAAgB;AAC9B,QAAM,OAAO,QAAQ;AACrB,QAAM,QAAQ,aAAa;AAC3B,SAAO,YAAY;AAAA,IACjB,YAAY,CAAC,OAAe,KAAK,MAAM,OAAO,EAAE;AAAA,IAChD,WAAW,CAAC,OAAO,OAAO,MAAM,KAAK,EAAE;AAAA,EACzC,CAAC;AACH;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/datasets/registry.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * The data sources a host offers, by id: the built-in adapters (AI usage and its breakdown, documents, threads,\n * projects, workspaces, tags, facts, workflows, recent activity, databases) plus the host's own from `<ArbiHost config={{ dashboards: { sources } }}>`\n * (a source with a built-in id replaces it, which is how a site relabels one in its own words).\n */\nimport { useMemo } from 'react'\nimport { useArbiHost } from '../host/config'\nimport { createActivitySource } from './sources/activity'\nimport { createConversationsSource } from './sources/conversations'\nimport { createDatabaseSource } from './sources/database'\nimport { createDocumentsSource } from './sources/documents'\nimport { createFactsSource } from './sources/facts'\nimport { createProjectsSource } from './sources/projects'\nimport { createTagsSource } from './sources/tags'\nimport { createUsageBreakdownSource, createUsageSource } from './sources/usage'\nimport { createWorkflowsSource } from './sources/workflows'\nimport { createWorkspacesSource } from './sources/workspaces'\nimport type { DataSource } from './types'\
|
|
5
|
-
"mappings": "AAKA,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAC1C,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AACtC,SAAS,yBAAyB;AAClC,SAAS,4BAA4B;AACrC,SAAS,wBAAwB;AACjC,SAAS,4BAA4B,yBAAyB;AAC9D,SAAS,6BAA6B;AACtC,SAAS,8BAA8B;
|
|
4
|
+
"sourcesContent": ["/**\n * The data sources a host offers, by id: the built-in adapters (AI usage and its breakdown, documents, threads,\n * projects, workspaces, tags, facts, workflows, recent activity, databases) plus the host's own from `<ArbiHost config={{ dashboards: { sources } }}>`\n * (a source with a built-in id replaces it, which is how a site relabels one in its own words).\n */\nimport { useMemo } from 'react'\nimport { useArbiHost } from '../host/config'\nimport { createActivitySource } from './sources/activity'\nimport { createConversationsSource } from './sources/conversations'\nimport { createDatabaseSource } from './sources/database'\nimport { createDocumentsSource } from './sources/documents'\nimport { createFactsSource } from './sources/facts'\nimport { createProjectsSource } from './sources/projects'\nimport { createTagsSource } from './sources/tags'\nimport { createUsageBreakdownSource, createUsageSource } from './sources/usage'\nimport { createWorkflowsSource } from './sources/workflows'\nimport { createWorkspacesSource } from './sources/workspaces'\nimport type { DataSource } from './types'\n\n/** What a host configures for the charts of pages: the data sources offered. */\nexport interface DashboardsHost {\n /** More sources, or replacements for built-ins (same `id`). */\n sources?: readonly DataSource[]\n /** Leave the built-in sources out (only `sources` are offered). */\n builtins?: false\n}\n\n/** A lookup of sources by id, in the order offered. */\nexport interface DataSourceRegistry {\n list: readonly DataSource[]\n get: (id: string) => DataSource | undefined\n}\n\n/** Built-in sources with their default (neutral) labels. */\nexport const BUILTIN_DATA_SOURCES: readonly DataSource[] = [\n createUsageSource(),\n createUsageBreakdownSource(),\n createDocumentsSource(),\n createConversationsSource(),\n createProjectsSource(),\n createWorkspacesSource(),\n createTagsSource(),\n createFactsSource(),\n createWorkflowsSource(),\n createActivitySource(),\n createDatabaseSource(),\n]\n\n/** A registry over `sources`; a later source replaces an earlier one with the same id. */\nexport function createDataSourceRegistry(sources: readonly DataSource[]): DataSourceRegistry {\n const byId = new Map<string, DataSource>()\n for (const source of sources) byId.set(source.id, source)\n const list = [...byId.values()]\n return { list, get: (id) => byId.get(id) }\n}\n\n/** The sources offered here: the built-ins and the host's (`dashboards.sources`). */\nexport function useDataSources(): DataSourceRegistry {\n const { dashboards } = useArbiHost()\n return useMemo(\n () =>\n createDataSourceRegistry([\n ...(dashboards?.builtins === false ? [] : BUILTIN_DATA_SOURCES),\n ...(dashboards?.sources ?? []),\n ]),\n [dashboards]\n )\n}\n"],
|
|
5
|
+
"mappings": "AAKA,SAAS,eAAe;AACxB,SAAS,mBAAmB;AAC5B,SAAS,4BAA4B;AACrC,SAAS,iCAAiC;AAC1C,SAAS,4BAA4B;AACrC,SAAS,6BAA6B;AACtC,SAAS,yBAAyB;AAClC,SAAS,4BAA4B;AACrC,SAAS,wBAAwB;AACjC,SAAS,4BAA4B,yBAAyB;AAC9D,SAAS,6BAA6B;AACtC,SAAS,8BAA8B;AAkBhC,MAAM,uBAA8C;AAAA,EACzD,kBAAkB;AAAA,EAClB,2BAA2B;AAAA,EAC3B,sBAAsB;AAAA,EACtB,0BAA0B;AAAA,EAC1B,qBAAqB;AAAA,EACrB,uBAAuB;AAAA,EACvB,iBAAiB;AAAA,EACjB,kBAAkB;AAAA,EAClB,sBAAsB;AAAA,EACtB,qBAAqB;AAAA,EACrB,qBAAqB;AACvB;AAGO,SAAS,yBAAyB,SAAoD;AAC3F,QAAM,OAAO,oBAAI,IAAwB;AACzC,aAAW,UAAU,QAAS,MAAK,IAAI,OAAO,IAAI,MAAM;AACxD,QAAM,OAAO,CAAC,GAAG,KAAK,OAAO,CAAC;AAC9B,SAAO,EAAE,MAAM,KAAK,CAAC,OAAO,KAAK,IAAI,EAAE,EAAE;AAC3C;AAGO,SAAS,iBAAqC;AACnD,QAAM,EAAE,WAAW,IAAI,YAAY;AACnC,SAAO;AAAA,IACL,MACE,yBAAyB;AAAA,MACvB,GAAI,YAAY,aAAa,QAAQ,CAAC,IAAI;AAAA,MAC1C,GAAI,YAAY,WAAW,CAAC;AAAA,IAC9B,CAAC;AAAA,IACH,CAAC,UAAU;AAAA,EACb;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { useMemo } from "react";
|
|
2
|
-
import {
|
|
3
|
-
import { viewsOfTable } from "../../account/table-views.js";
|
|
4
|
-
import { useArbi } from "../../useArbi.js";
|
|
2
|
+
import { useTableViewsQuery } from "../../table-views.js";
|
|
5
3
|
import {
|
|
6
4
|
useDatabase,
|
|
7
5
|
useDatabaseAggregate,
|
|
@@ -95,37 +93,42 @@ function datasetRowOf(row, fields) {
|
|
|
95
93
|
function createDatabaseSource(labels) {
|
|
96
94
|
const l = { ...DEFAULT_DATABASE_SOURCE_LABELS, ...labels };
|
|
97
95
|
function useView(params) {
|
|
98
|
-
const arbi = useArbi();
|
|
99
96
|
const databaseId = text(params.databaseId);
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const workspaceId = arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
|
|
97
|
+
const wanted = text(params.view);
|
|
98
|
+
const views = useTableViewsQuery(databaseId ?? null, { enabled: Boolean(wanted && databaseId) });
|
|
103
99
|
return useMemo(
|
|
104
|
-
() =>
|
|
105
|
-
|
|
106
|
-
) : void 0,
|
|
107
|
-
[settings.data?.tableviews, workspaceId, databaseId, viewName]
|
|
100
|
+
() => wanted && databaseId ? (views.data ?? []).find((v) => v.id === wanted) ?? (views.data ?? []).find((v) => v.spec.name === wanted) : void 0,
|
|
101
|
+
[views.data, databaseId, wanted]
|
|
108
102
|
);
|
|
109
103
|
}
|
|
110
104
|
function useFields(params) {
|
|
111
105
|
const databaseId = text(params.databaseId) ?? null;
|
|
112
106
|
const database = useDatabase(databaseId, { workspaceId: text(params.workspaceId) });
|
|
113
107
|
const view = useView(params);
|
|
114
|
-
const fields = useMemo(() => databaseFields(database.data, view), [database.data, view]);
|
|
108
|
+
const fields = useMemo(() => databaseFields(database.data, view?.spec), [database.data, view]);
|
|
115
109
|
return { fields, isLoading: database.isLoading && !!databaseId, error: database.error };
|
|
116
110
|
}
|
|
117
111
|
function useDataset(params, query, options) {
|
|
118
112
|
const databaseId = text(params.databaseId) ?? null;
|
|
119
113
|
const enabled = options?.enabled ?? true;
|
|
120
114
|
const fields = useFields(params);
|
|
115
|
+
const view = useView(params);
|
|
121
116
|
const grouped = !query.records;
|
|
122
|
-
const request = useMemo(() =>
|
|
117
|
+
const request = useMemo(() => {
|
|
118
|
+
if (!grouped) return {};
|
|
119
|
+
const req = aggregateRequest(query);
|
|
120
|
+
const own = view?.spec.filters ?? [];
|
|
121
|
+
return own.length ? { ...req, filters: [...own, ...req.filters ?? []] } : req;
|
|
122
|
+
}, [grouped, query, view]);
|
|
123
123
|
const workspaceId = text(params.workspaceId);
|
|
124
124
|
const aggregate = useDatabaseAggregate(databaseId, request, {
|
|
125
125
|
enabled: enabled && grouped,
|
|
126
126
|
workspaceId
|
|
127
127
|
});
|
|
128
|
-
const rows = useDatabaseRows(enabled && !grouped ? databaseId : null, "", {
|
|
128
|
+
const rows = useDatabaseRows(enabled && !grouped ? databaseId : null, "", {
|
|
129
|
+
workspaceId,
|
|
130
|
+
view: view?.id
|
|
131
|
+
});
|
|
129
132
|
const dataset = useMemo(() => {
|
|
130
133
|
if (grouped) {
|
|
131
134
|
return aggregate.data ? datasetOfAggregate(aggregate.data, fields.fields, query) : void 0;
|
|
@@ -178,18 +181,13 @@ function createDatabaseSource(labels) {
|
|
|
178
181
|
label: l.view,
|
|
179
182
|
placeholder: l.allColumns,
|
|
180
183
|
useOptions(params) {
|
|
181
|
-
const arbi = useArbi();
|
|
182
184
|
const databaseId = text(params.databaseId);
|
|
183
|
-
const
|
|
184
|
-
const workspaceId = arbi.hasWorkspace ? arbi.getWorkspaceId() : null;
|
|
185
|
+
const views = useTableViewsQuery(databaseId ?? null, { enabled: Boolean(databaseId) });
|
|
185
186
|
const options = useMemo(
|
|
186
|
-
() => databaseId ?
|
|
187
|
-
|
|
188
|
-
label: v.name
|
|
189
|
-
})) : [],
|
|
190
|
-
[settings.data?.tableviews, workspaceId, databaseId]
|
|
187
|
+
() => databaseId ? (views.data ?? []).map((v) => ({ value: v.id, label: v.spec.name })) : [],
|
|
188
|
+
[views.data, databaseId]
|
|
191
189
|
);
|
|
192
|
-
return { options, isLoading:
|
|
190
|
+
return { options, isLoading: views.isLoading && !!databaseId };
|
|
193
191
|
}
|
|
194
192
|
}
|
|
195
193
|
],
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../../src/datasets/sources/database.ts"],
|
|
4
|
-
"sourcesContent": ["/**\n * A database of the open workspace (one the user created or imported from a spreadsheet) as a\n * dataset: `params.databaseId` picks it, `params.view` one of its saved views (the grid's\n * \"Columns\" menu), whose columns are then the fields offered. Grouping and measuring run on the\n * server (`POST /v1/databases/{id}/aggregate`, over every row, so totals are exact however large the\n * database is); a table of records reads the first page of rows.\n */\nimport { useMemo } from 'react'\nimport { useUserSettings } from '../../account/settings'\nimport { viewsOfTable, type TableView } from '../../account/table-views'\nimport { useArbi } from '../../useArbi'\nimport {\n useDatabase,\n useDatabaseAggregate,\n useDatabaseRows,\n useDatabases,\n type Database,\n type DatabaseAggregateRequest,\n type DatabaseAggregateResult,\n type DatabaseColumn,\n type DatabaseRow,\n} from '../../databases'\nimport { aggregateFields, recordsOf } from '../aggregate'\nimport {\n AGGREGATE_FIELDS,\n type DataSource,\n type DataSourceFieldsState,\n type DataSourceParams,\n type Dataset,\n type DatasetField,\n type DatasetQuery,\n type DatasetRow,\n type DatasetState,\n type DatasetValue,\n} from '../types'\n\nexport interface DatabaseSourceLabels {\n source: string\n description: string\n database: string\n view: string\n allColumns: string\n}\n\nexport const DEFAULT_DATABASE_SOURCE_LABELS: DatabaseSourceLabels = {\n source: 'Database',\n description: 'A database, or one of its saved views.',\n database: 'Database',\n view: 'Saved view',\n allColumns: 'All columns',\n}\n\nconst MULTI = new Set(['document', 'relation', 'lookup', 'tag'])\n\n/** A database column as a dataset field (ids are the column ids the server groups by). */\nexport function fieldOfColumn(column: DatabaseColumn): DatasetField {\n const type =\n column.type === 'number' || column.type === 'rollup'\n ? 'number'\n : column.type === 'date'\n ? 'date'\n : column.type === 'checkbox'\n ? 'boolean'\n : 'string'\n return {\n id: column.id,\n label: column.name,\n type,\n ...(column.options?.length ? { options: column.options } : {}),\n ...(MULTI.has(column.type) ? { multiple: true } : {}),\n }\n}\n\nconst text = (v: DatasetValue | undefined) => (typeof v === 'string' && v ? v : undefined)\n\n/** The fields of a database, limited to a saved view's columns (in its order) when one is named. */\nexport function databaseFields(\n database: Pick<Database, 'columns'> | undefined,\n view?: Pick<TableView, 'columns'>\n): DatasetField[] {\n const columns = database?.columns ?? []\n if (!view?.columns?.length) return columns.map(fieldOfColumn)\n const byId = new Map(columns.map((c) => [c.id, c]))\n return view.columns.flatMap((id) => {\n const column = byId.get(id)\n return column ? [fieldOfColumn(column)] : []\n })\n}\n\n/** The server request for a query. */\nexport function aggregateRequest(query: DatasetQuery): Partial<DatabaseAggregateRequest> {\n return {\n group_by: query.groupBy ?? null,\n split_by: query.splitBy ?? null,\n time_bucket: query.bucket ?? 'month',\n measure: { op: query.measure?.op ?? 'count', column: query.measure?.field ?? null },\n filters: (query.filters ?? []).map((f) => ({ column: f.field, op: f.op, value: f.value })),\n limit: query.limit ?? 50,\n }\n}\n\n/** The server's labels and series as a long-form aggregated dataset, re-sorted when asked. */\nexport function datasetOfAggregate(\n result: DatabaseAggregateResult,\n fields: readonly DatasetField[],\n query: DatasetQuery\n): Dataset {\n const split = Boolean(query.splitBy)\n let order = result.keys.map((_, i) => i)\n const total = (i: number) => result.series.reduce((sum, s) => sum + (s.values[i] ?? 0), 0)\n if (query.sort === 'label') {\n order = [...order].sort((a, b) =>\n result.keys[a] === ''\n ? 1\n : result.keys[b] === ''\n ? -1\n : result.labels[a].localeCompare(result.labels[b])\n )\n } else if (query.sort === 'value-asc') {\n order = [...order].sort((a, b) => total(a) - total(b))\n } else if (query.sort === 'value-desc') {\n order = [...order].sort((a, b) => total(b) - total(a))\n }\n const rows: DatasetRow[] = order.flatMap((i) =>\n result.series.map((s) => ({\n [AGGREGATE_FIELDS.group]: result.labels[i],\n [AGGREGATE_FIELDS.key]: result.keys[i],\n ...(split ? { [AGGREGATE_FIELDS.series]: s.label } : {}),\n [AGGREGATE_FIELDS.value]: s.values[i] ?? null,\n }))\n )\n return {\n fields: aggregateFields(fields, query),\n rows,\n totalRows: result.total_rows,\n truncated: result.truncated,\n }\n}\n\n/** A database row as a dataset row: linked ids read as their titles. */\nexport function datasetRowOf(row: DatabaseRow, fields: readonly DatasetField[]): DatasetRow {\n const titles = row.titles ?? {}\n const out: Record<string, DatasetValue> = {}\n for (const field of fields) {\n const raw = row.values[field.id]\n if (raw === null || raw === undefined) out[field.id] = null\n else if (Array.isArray(raw)) out[field.id] = raw.map((v) => titles[String(v)] ?? String(v))\n else if (typeof raw === 'string')\n out[field.id] = field.type === 'string' ? (titles[raw] ?? raw) : raw\n else if (typeof raw === 'number' || typeof raw === 'boolean') out[field.id] = raw\n else out[field.id] = String(raw)\n }\n return out\n}\n\nexport function createDatabaseSource(labels?: Partial<DatabaseSourceLabels>): DataSource {\n const l = { ...DEFAULT_DATABASE_SOURCE_LABELS, ...labels }\n\n function useView(params: DataSourceParams): TableView | undefined {\n const arbi = useArbi()\n const databaseId = text(params.databaseId)\n const viewName = text(params.view)\n const settings = useUserSettings({ enabled: Boolean(viewName) })\n const workspaceId = arbi.hasWorkspace ? arbi.getWorkspaceId() : null\n return useMemo(\n () =>\n viewName && databaseId\n ? viewsOfTable(settings.data?.tableviews, workspaceId, databaseId).find(\n (v) => v.name === viewName\n )\n : undefined,\n [settings.data?.tableviews, workspaceId, databaseId, viewName]\n )\n }\n\n function useFields(params: DataSourceParams): DataSourceFieldsState {\n const databaseId = text(params.databaseId) ?? null\n const database = useDatabase(databaseId, { workspaceId: text(params.workspaceId) })\n const view = useView(params)\n const fields = useMemo(() => databaseFields(database.data, view), [database.data, view])\n return { fields, isLoading: database.isLoading && !!databaseId, error: database.error }\n }\n\n function useDataset(\n params: DataSourceParams,\n query: DatasetQuery,\n options?: { enabled?: boolean }\n ): DatasetState {\n const databaseId = text(params.databaseId) ?? null\n const enabled = options?.enabled ?? true\n const fields = useFields(params)\n const grouped = !query.records\n const request = useMemo(() => (grouped ? aggregateRequest(query) : {}), [grouped, query])\n const workspaceId = text(params.workspaceId)\n const aggregate = useDatabaseAggregate(databaseId, request, {\n enabled: enabled && grouped,\n workspaceId,\n })\n const rows = useDatabaseRows(enabled && !grouped ? databaseId : null, '', { workspaceId })\n const dataset = useMemo(() => {\n if (grouped) {\n return aggregate.data ? datasetOfAggregate(aggregate.data, fields.fields, query) : undefined\n }\n if (!rows.data) return undefined\n const records = rows.data.rows.map((r) => datasetRowOf(r, fields.fields))\n return recordsOf({ fields: fields.fields, rows: records }, query)\n }, [grouped, query, aggregate.data, rows.data, fields.fields])\n const active = grouped ? aggregate : rows\n return {\n dataset,\n isLoading: !!databaseId && (active.isLoading || fields.isLoading),\n error: active.error ?? fields.error,\n }\n }\n\n return {\n id: 'database',\n label: l.source,\n description: l.description,\n // A panel that names its database's workspace reads it anywhere; an older one, only in the open one.\n scope: (params) => (text(params.workspaceId) ? undefined : 'workspace'),\n params: [\n {\n id: 'databaseId',\n label: l.database,\n required: true,\n // The open workspace's databases (the list is read there), and the panel's own database\n // wherever it is (read by id in its workspace), so its name shows with none or another open.\n useOptions(params) {\n const databases = useDatabases()\n const currentId = text(params.databaseId) ?? null\n const listed = (databases.data ?? []).some((d) => d.id === currentId)\n const current = useDatabase(listed ? null : currentId, {\n workspaceId: text(params.workspaceId),\n })\n const options = useMemo(() => {\n const all = [...(databases.data ?? [])]\n if (current.data && !listed) all.push(current.data)\n return all.map((d) => ({\n value: d.id,\n label: d.name,\n params: { workspaceId: d.workspace_id },\n }))\n }, [databases.data, current.data, listed])\n return { options, isLoading: databases.isLoading || current.isLoading }\n },\n },\n {\n id: 'view',\n label: l.view,\n placeholder: l.allColumns,\n useOptions(params) {\n const arbi = useArbi()\n const databaseId = text(params.databaseId)\n const settings = useUserSettings({ enabled: Boolean(databaseId) })\n const workspaceId = arbi.hasWorkspace ? arbi.getWorkspaceId() : null\n const options = useMemo(\n () =>\n databaseId\n ? viewsOfTable(settings.data?.tableviews, workspaceId, databaseId).map((v) => ({\n value: v.name,\n label: v.name,\n }))\n : [],\n [settings.data?.tableviews, workspaceId, databaseId]\n )\n return { options, isLoading: settings.isLoading && !!databaseId }\n },\n },\n ],\n useFields,\n useDataset,\n }\n}\n"],
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["/**\n * A database of the open workspace (one the user created or imported from a spreadsheet) as a\n * dataset: `params.databaseId` picks it, `params.view` one of its saved views (the grid's\n * \"Columns\" menu; its id, or its name for a panel saved before views had ids), whose columns are\n * then the fields offered and whose filters narrow every query. Grouping and measuring run on the\n * server (`POST /v1/databases/{id}/aggregate`, over every row, so totals are exact however large the\n * database is); a table of records reads the first page of rows.\n */\nimport { useMemo } from 'react'\nimport { useTableViewsQuery, type TableViewRecord, type TableViewSpec } from '../../table-views'\nimport {\n useDatabase,\n useDatabaseAggregate,\n useDatabaseRows,\n useDatabases,\n type Database,\n type DatabaseAggregateRequest,\n type DatabaseAggregateResult,\n type DatabaseColumn,\n type DatabaseRow,\n} from '../../databases'\nimport { aggregateFields, recordsOf } from '../aggregate'\nimport {\n AGGREGATE_FIELDS,\n type DataSource,\n type DataSourceFieldsState,\n type DataSourceParams,\n type Dataset,\n type DatasetField,\n type DatasetQuery,\n type DatasetRow,\n type DatasetState,\n type DatasetValue,\n} from '../types'\n\nexport interface DatabaseSourceLabels {\n source: string\n description: string\n database: string\n view: string\n allColumns: string\n}\n\nexport const DEFAULT_DATABASE_SOURCE_LABELS: DatabaseSourceLabels = {\n source: 'Database',\n description: 'A database, or one of its saved views.',\n database: 'Database',\n view: 'Saved view',\n allColumns: 'All columns',\n}\n\nconst MULTI = new Set(['document', 'relation', 'lookup', 'tag'])\n\n/** A database column as a dataset field (ids are the column ids the server groups by). */\nexport function fieldOfColumn(column: DatabaseColumn): DatasetField {\n const type =\n column.type === 'number' || column.type === 'rollup'\n ? 'number'\n : column.type === 'date'\n ? 'date'\n : column.type === 'checkbox'\n ? 'boolean'\n : 'string'\n return {\n id: column.id,\n label: column.name,\n type,\n ...(column.options?.length ? { options: column.options } : {}),\n ...(MULTI.has(column.type) ? { multiple: true } : {}),\n }\n}\n\nconst text = (v: DatasetValue | undefined) => (typeof v === 'string' && v ? v : undefined)\n\n/** The fields of a database, limited to a saved view's columns (in its order) when one is named. */\nexport function databaseFields(\n database: Pick<Database, 'columns'> | undefined,\n view?: Pick<TableViewSpec, 'columns'>\n): DatasetField[] {\n const columns = database?.columns ?? []\n if (!view?.columns?.length) return columns.map(fieldOfColumn)\n const byId = new Map(columns.map((c) => [c.id, c]))\n return view.columns.flatMap((id) => {\n const column = byId.get(id)\n return column ? [fieldOfColumn(column)] : []\n })\n}\n\n/** The server request for a query. */\nexport function aggregateRequest(query: DatasetQuery): Partial<DatabaseAggregateRequest> {\n return {\n group_by: query.groupBy ?? null,\n split_by: query.splitBy ?? null,\n time_bucket: query.bucket ?? 'month',\n measure: { op: query.measure?.op ?? 'count', column: query.measure?.field ?? null },\n filters: (query.filters ?? []).map((f) => ({ column: f.field, op: f.op, value: f.value })),\n limit: query.limit ?? 50,\n }\n}\n\n/** The server's labels and series as a long-form aggregated dataset, re-sorted when asked. */\nexport function datasetOfAggregate(\n result: DatabaseAggregateResult,\n fields: readonly DatasetField[],\n query: DatasetQuery\n): Dataset {\n const split = Boolean(query.splitBy)\n let order = result.keys.map((_, i) => i)\n const total = (i: number) => result.series.reduce((sum, s) => sum + (s.values[i] ?? 0), 0)\n if (query.sort === 'label') {\n order = [...order].sort((a, b) =>\n result.keys[a] === ''\n ? 1\n : result.keys[b] === ''\n ? -1\n : result.labels[a].localeCompare(result.labels[b])\n )\n } else if (query.sort === 'value-asc') {\n order = [...order].sort((a, b) => total(a) - total(b))\n } else if (query.sort === 'value-desc') {\n order = [...order].sort((a, b) => total(b) - total(a))\n }\n const rows: DatasetRow[] = order.flatMap((i) =>\n result.series.map((s) => ({\n [AGGREGATE_FIELDS.group]: result.labels[i],\n [AGGREGATE_FIELDS.key]: result.keys[i],\n ...(split ? { [AGGREGATE_FIELDS.series]: s.label } : {}),\n [AGGREGATE_FIELDS.value]: s.values[i] ?? null,\n }))\n )\n return {\n fields: aggregateFields(fields, query),\n rows,\n totalRows: result.total_rows,\n truncated: result.truncated,\n }\n}\n\n/** A database row as a dataset row: linked ids read as their titles. */\nexport function datasetRowOf(row: DatabaseRow, fields: readonly DatasetField[]): DatasetRow {\n const titles = row.titles ?? {}\n const out: Record<string, DatasetValue> = {}\n for (const field of fields) {\n const raw = row.values[field.id]\n if (raw === null || raw === undefined) out[field.id] = null\n else if (Array.isArray(raw)) out[field.id] = raw.map((v) => titles[String(v)] ?? String(v))\n else if (typeof raw === 'string')\n out[field.id] = field.type === 'string' ? (titles[raw] ?? raw) : raw\n else if (typeof raw === 'number' || typeof raw === 'boolean') out[field.id] = raw\n else out[field.id] = String(raw)\n }\n return out\n}\n\nexport function createDatabaseSource(labels?: Partial<DatabaseSourceLabels>): DataSource {\n const l = { ...DEFAULT_DATABASE_SOURCE_LABELS, ...labels }\n\n function useView(params: DataSourceParams): TableViewRecord | undefined {\n const databaseId = text(params.databaseId)\n const wanted = text(params.view)\n const views = useTableViewsQuery(databaseId ?? null, { enabled: Boolean(wanted && databaseId) })\n return useMemo(\n () =>\n wanted && databaseId\n ? ((views.data ?? []).find((v) => v.id === wanted) ??\n (views.data ?? []).find((v) => v.spec.name === wanted))\n : undefined,\n [views.data, databaseId, wanted]\n )\n }\n\n function useFields(params: DataSourceParams): DataSourceFieldsState {\n const databaseId = text(params.databaseId) ?? null\n const database = useDatabase(databaseId, { workspaceId: text(params.workspaceId) })\n const view = useView(params)\n const fields = useMemo(() => databaseFields(database.data, view?.spec), [database.data, view])\n return { fields, isLoading: database.isLoading && !!databaseId, error: database.error }\n }\n\n function useDataset(\n params: DataSourceParams,\n query: DatasetQuery,\n options?: { enabled?: boolean }\n ): DatasetState {\n const databaseId = text(params.databaseId) ?? null\n const enabled = options?.enabled ?? true\n const fields = useFields(params)\n const view = useView(params)\n const grouped = !query.records\n // A view's filters narrow every query (its conditions first, the panel's after them).\n const request = useMemo(() => {\n if (!grouped) return {}\n const req = aggregateRequest(query)\n const own = view?.spec.filters ?? []\n return own.length ? { ...req, filters: [...own, ...(req.filters ?? [])] } : req\n }, [grouped, query, view])\n const workspaceId = text(params.workspaceId)\n const aggregate = useDatabaseAggregate(databaseId, request, {\n enabled: enabled && grouped,\n workspaceId,\n })\n const rows = useDatabaseRows(enabled && !grouped ? databaseId : null, '', {\n workspaceId,\n view: view?.id,\n })\n const dataset = useMemo(() => {\n if (grouped) {\n return aggregate.data ? datasetOfAggregate(aggregate.data, fields.fields, query) : undefined\n }\n if (!rows.data) return undefined\n const records = rows.data.rows.map((r) => datasetRowOf(r, fields.fields))\n return recordsOf({ fields: fields.fields, rows: records }, query)\n }, [grouped, query, aggregate.data, rows.data, fields.fields])\n const active = grouped ? aggregate : rows\n return {\n dataset,\n isLoading: !!databaseId && (active.isLoading || fields.isLoading),\n error: active.error ?? fields.error,\n }\n }\n\n return {\n id: 'database',\n label: l.source,\n description: l.description,\n // A panel that names its database's workspace reads it anywhere; an older one, only in the open one.\n scope: (params) => (text(params.workspaceId) ? undefined : 'workspace'),\n params: [\n {\n id: 'databaseId',\n label: l.database,\n required: true,\n // The open workspace's databases (the list is read there), and the panel's own database\n // wherever it is (read by id in its workspace), so its name shows with none or another open.\n useOptions(params) {\n const databases = useDatabases()\n const currentId = text(params.databaseId) ?? null\n const listed = (databases.data ?? []).some((d) => d.id === currentId)\n const current = useDatabase(listed ? null : currentId, {\n workspaceId: text(params.workspaceId),\n })\n const options = useMemo(() => {\n const all = [...(databases.data ?? [])]\n if (current.data && !listed) all.push(current.data)\n return all.map((d) => ({\n value: d.id,\n label: d.name,\n params: { workspaceId: d.workspace_id },\n }))\n }, [databases.data, current.data, listed])\n return { options, isLoading: databases.isLoading || current.isLoading }\n },\n },\n {\n id: 'view',\n label: l.view,\n placeholder: l.allColumns,\n useOptions(params) {\n const databaseId = text(params.databaseId)\n const views = useTableViewsQuery(databaseId ?? null, { enabled: Boolean(databaseId) })\n const options = useMemo(\n () =>\n databaseId\n ? (views.data ?? []).map((v) => ({ value: v.id, label: v.spec.name }))\n : [],\n [views.data, databaseId]\n )\n return { options, isLoading: views.isLoading && !!databaseId }\n },\n },\n ],\n useFields,\n useDataset,\n }\n}\n"],
|
|
5
|
+
"mappings": "AAQA,SAAS,eAAe;AACxB,SAAS,0BAAoE;AAC7E;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAMK;AACP,SAAS,iBAAiB,iBAAiB;AAC3C;AAAA,EACE;AAAA,OAUK;AAUA,MAAM,iCAAuD;AAAA,EAClE,QAAQ;AAAA,EACR,aAAa;AAAA,EACb,UAAU;AAAA,EACV,MAAM;AAAA,EACN,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