Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion browser/data-browser/src/chunks/AppPage/hostStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,24 @@ vi.mock('@tomic/react', async () => {

// Signing needs a real key and a real agent; what these tests are about is
// which requests leave and which are refused before they do.
return { ...actual, signRequest: async () => ({}) };
return {
...actual,
signRequest: async () => ({}),
// Reading an importer's stored config is tested in @tomic/lib
// (plugin-destination.test.ts); here only what `data` passes on.
destinationTablesFor: async (
_store: unknown,
_drive: string,
table: string,
) => (table === 'did:ad:transactions' ? DESTINATION_TABLES : undefined),
};
});

const DESTINATION_TABLES = {
statements: { table: 'did:ad:statements', rowClass: 'did:ad:statement' },
closingBalances: { table: 'did:ad:balances', rowClass: 'did:ad:balance' },
};

const APP = 'did:ad:app';
const DRIVE = 'did:ad:drive';

Expand Down Expand Up @@ -177,6 +192,22 @@ describe('writing as the app', () => {
expect(viewing.table).toBe('did:ad:someone-elses-table');
});

it('names the other tables of a multi-class destination by their keys', async () => {
const store = fakeStore();

await expect(
handleRequest(store, APP, DRIVE, req('data'), 'did:ad:transactions'),
).resolves.toEqual({
table: 'did:ad:transactions',
rowClass: undefined,
tables: DESTINATION_TABLES,
});
// A single-table destination, or any other table, answers as before.
await expect(
handleRequest(store, APP, DRIVE, req('data'), 'did:ad:other'),
).resolves.toEqual({ table: 'did:ad:other', rowClass: undefined });
});

it('refuses an operation it does not implement', async () => {
const store = fakeStore();

Expand Down
6 changes: 6 additions & 0 deletions browser/data-browser/src/chunks/AppPage/hostStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ProxyHost } from '@helpers/proxyConnections';
import {
CollectionBuilder,
core,
destinationTablesFor,
errorMessageFromResponse,
findSchema,
pluginSchema,
Expand Down Expand Up @@ -105,12 +106,17 @@ export async function handleRequest(
// names what its rows are, and duplicating that on the app would be two
// places to disagree.
const tableResource = await store.getResource(subject);
// A table an importer's Set up created together with others (a
// manifest `destination.tables`): its siblings, by the keys the
// manifest declared, so a view of transactions can find statements.
const tables = await destinationTablesFor(store, drive, subject);

return {
table: subject,
rowClass: tableResource.get(core.properties.classtype) as
| string
| undefined,
...(tables ? { tables } : {}),
};
}

Expand Down
305 changes: 305 additions & 0 deletions browser/data-browser/src/chunks/PluginRuns/FileImport.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
import { useEffect, useId, useState } from 'react';
import {
executeServerPlugin,
provisionDestination,
pluginConfigFor,
pluginConfigProblems,
useStore,
DEFAULT_ACCEPT_MAX_BYTES,
type DeclaredAccept,
type DeclaredDestination,
type JSONObject,
type PluginManifest,
type Resource,
type Store,
} from '@tomic/react';
import { Button } from '@components/Button';
import { Column } from '@components/Row';
import Field from '@components/forms/Field';
import { Input, ErrMessage } from '@components/forms/InputStyles';
import { AtomicLink } from '@components/AtomicLink';
import { pluginClassesFor } from './runScript';
import { RunPluginDialog } from './RunPluginDialog';

/**
* The generic entry point for a plugin that declares `accepts`: choose a file,
* preview what the plugin proposes for it, approve.
*
* The host owns acquisition: it enforces the declared size, decodes the file
* and hands it over as `input.upload`. The plugin runs on the server (it may
* need `ctx.query`/`ctx.read` to recognise earlier imports), and nothing is
* written until the proposal is approved in {@link RunPluginDialog}. A plugin
* that also declares a `destination` gets a Set up step first, which creates
* its tables and stores them as the plugin's config.
*/
export function FileImport({
resource,
drive,
source,
manifest,
}: {
resource: Resource;
drive: string;
source: string | undefined;
manifest: PluginManifest;
}): React.JSX.Element {
const store = useStore();
const fileId = useId();
const accepts = manifest.accepts ?? [];
const [config, setConfig] = useState<JSONObject>();
const [missing, setMissing] = useState<string[]>([]);
const [file, setFile] = useState<File>();
const [error, setError] = useState('');
const [busy, setBusy] = useState<'setup' | 'preview'>();
const [verdict, setVerdict] = useState<string>();
const [imported, setImported] = useState(false);
const [refresh, setRefresh] = useState(0);

useEffect(() => {
let active = true;
void storedConfig(store, drive, resource.subject, manifest)
.then(found => {
if (!active) return;
setConfig(found);
setMissing(
pluginConfigProblems(found, manifest.config).map(p => p.message),
);
})
.catch(reason => {
if (active) setError(String(reason));
});

return () => {
active = false;
};
}, [store, drive, resource.subject, manifest, refresh]);

const setUp = async () => {
if (!manifest.destination) return;
setBusy('setup');
setError('');

try {
await provisionDestination(
store,
drive,
resource.subject,
manifest.destination,
manifest.config?.key,
);
setRefresh(n => n + 1);
} catch (reason) {
setError(`Could not set up this importer: ${String(reason)}`);
} finally {
setBusy(undefined);
}
};

const preview = async () => {
if (!file || !source || !config) return;
setBusy('preview');
setError('');
setImported(false);

try {
const max = maxBytes(accepts);
if (file.size > max)
throw new Error(
`This file is ${formatBytes(file.size)}; this importer accepts at most ${formatBytes(max)}. Export a shorter period.`,
);
const text = decode(await file.arrayBuffer());
const result = await executeServerPlugin(store, {
drive,
plugin: resource.subject,
source,
input: {
upload: {
name: file.name,
mediaType: file.type,
size: file.size,
text,
},
config,
trigger: {
kind: 'manual',
at: Date.now(),
subject: resource.subject,
},
},
});
if (result.error || !result.verdict)
throw new Error(result.error ?? 'The importer returned no preview.');
setVerdict(result.verdict);
} catch (reason) {
setError(reason instanceof Error ? reason.message : String(reason));
} finally {
setBusy(undefined);
}
};

const table = firstTable(config);

if (config === undefined && !error) return <p>Loading importer…</p>;

return (
<Column gap='0.75rem'>
{missing.length > 0 ? (
manifest.destination ? (
<>
<SetupSummary destination={manifest.destination} />
<Button disabled={busy !== undefined} onClick={setUp}>
{busy === 'setup' ? 'Setting up…' : 'Set up'}
</Button>
</>
) : (
missing.map(message => <p key={message}>{message}</p>)
)
) : (
<>
<Field fieldId={fileId} label='File to import'>
<Input
id={fileId}
type='file'
accept={acceptAttribute(accepts)}
disabled={busy !== undefined}
onChange={event => {
setFile(event.target.files?.[0]);
setError('');
setImported(false);
}}
/>
</Field>
<p>
The file is read by this plugin on your AtomicServer, up to{' '}
{formatBytes(maxBytes(accepts))}. Review the proposed changes before
anything is saved.
</p>
<Button disabled={busy !== undefined || !file} onClick={preview}>
{busy === 'preview' ? 'Preparing preview…' : 'Preview import'}
</Button>
</>
)}
{error && <ErrMessage role='alert'>{error}</ErrMessage>}
{imported && table && (
<AtomicLink subject={table}>Open imported data</AtomicLink>
)}
{verdict && (
<RunPluginDialog
resource={resource}
drive={drive}
show
verdict={verdict}
triggerKind='manual'
onShowChange={open => {
if (!open) setVerdict(undefined);
}}
onReviewed={() => {
setImported(true);
setVerdict(undefined);
}}
/>
)}
</Column>
);
}

/** What Set up is about to create, named the way the sidebar will show it. */
function SetupSummary({
destination,
}: {
destination: DeclaredDestination;
}): React.JSX.Element {
const names = [
...(destination.table ? [destination.table.name] : []),
...Object.values(destination.tables ?? {}).map(table => table.name),
];

if (names.length === 1)
return (
<p>
Set up creates a {names[0]} table for this importer. Nothing is imported
yet.
</p>
);

return (
<>
<p>
Set up creates {names.length} tables for this importer. Nothing is
imported yet.
</p>
<ul>
{names.map(name => (
<li key={name}>{name}</li>
))}
</ul>
</>
);
}

/** Where "Open imported data" goes: the table a single-table importer writes, or its first. */
function firstTable(config: JSONObject | undefined): string | undefined {
if (typeof config?.table === 'string') return config.table;
const tables = config?.tables;
if (!tables || typeof tables !== 'object' || Array.isArray(tables))
return undefined;
const first = Object.values(tables)[0];

return first &&
typeof first === 'object' &&
!Array.isArray(first) &&
typeof first.table === 'string'
? first.table
: undefined;
}

function maxBytes(accepts: DeclaredAccept[]): number {
return Math.max(
0,
...accepts.map(accept => accept.maxBytes ?? DEFAULT_ACCEPT_MAX_BYTES),
);
}

function acceptAttribute(accepts: DeclaredAccept[]): string | undefined {
const values = accepts.flatMap(accept => [
...(accept.extensions ?? []),
...(accept.mediaTypes ?? []),
]);

return values.length ? values.join(',') : undefined;
}

function formatBytes(bytes: number): string {
if (bytes >= 1024 * 1024)
return `${Math.round((bytes / (1024 * 1024)) * 10) / 10} MB`;
if (bytes >= 1024) return `${Math.round(bytes / 1024)} KB`;

return `${bytes} bytes`;
}

/** UTF-8 when the file is valid UTF-8; older bank exports are often Windows-1252. */
export function decode(bytes: ArrayBuffer): string {
try {
return new TextDecoder('utf-8', { fatal: true }).decode(bytes);
} catch {
return new TextDecoder('windows-1252').decode(bytes);
}
}

async function storedConfig(
store: Store,
drive: string,
plugin: string,
manifest: PluginManifest,
): Promise<JSONObject> {
const terms = await pluginClassesFor(store, drive);
const resource = await store.getResource(plugin);

return pluginConfigFor(
{
schemas: resource.get(terms.properties['plugin-schemas']),
connection: resource.get(terms.properties['plugin-connection']),
},
manifest.config,
);
}
Loading
Loading