Skip to content
Merged
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
11 changes: 11 additions & 0 deletions ui-react/src/lib/__tests__/result-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,14 @@ describe('prettyJson', () => {
expect(prettyJson(ok).note).toBeUndefined();
});
});

describe('outputPreviewKind', () => {
it('reads the kind from the location first, as a result is named by its label', async () => {
const { outputPreviewKind } = await import('@/lib/result-preview');
expect(outputPreviewKind('tapis://s/out/summary.md', 'Sensemaking report')).toBe('markdown');
expect(outputPreviewKind('tapis://s/out/x', 'weights.csv')).toBe('csv');
expect(outputPreviewKind('tapis://s/out/x', 'Lexicon', 'json')).toBe('json');
expect(outputPreviewKind('tapis://s/out/x', 'Report', 'Markdown')).toBe('markdown');
expect(outputPreviewKind('tapis://s/out/model.tif', 'Raster', 'GeoTIFF')).toBeNull();
});
});
22 changes: 22 additions & 0 deletions ui-react/src/lib/result-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ export function previewKind(name: string): PreviewKind | null {
return null;
}

/** The preview a declared output format allows ("md", "Markdown", "csv", "json"), or null. */
export function previewKindFromFormat(format: string | null | undefined): PreviewKind | null {
const value = (format ?? '').trim().toLowerCase();
if (value === 'md' || value === 'markdown' || value === 'text/markdown') return 'markdown';
if (value === 'csv' || value === 'text/csv') return 'csv';
if (value === 'json' || value === 'application/json') return 'json';
return null;
}

/**
* What an output can be previewed as. A result's name is usually its label
* ("Sensemaking report"), so the file's own location decides first, then the
* name, then the format the output declares.
*/
export function outputPreviewKind(
url: string,
name: string,
format?: string | null,
): PreviewKind | null {
return previewKind(url) ?? previewKind(name) ?? previewKindFromFormat(format);
}

/** Decode the bytes of a file's start as text (a cut multi-byte character becomes a replacement mark). */
export function decodePreviewText(bytes: Uint8Array): string {
return new TextDecoder('utf-8').decode(bytes);
Expand Down
14 changes: 8 additions & 6 deletions ui-react/src/pages/modeling/thread/OutputBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { ChevronRight, Copy, Download, FolderOpen } from 'lucide-react';
import { useState } from 'react';

import { PREVIEW_AUTO_OPEN_MAX_BYTES, previewKind } from '@/lib/result-preview';
import { PREVIEW_AUTO_OPEN_MAX_BYTES, outputPreviewKind } from '@/lib/result-preview';
import { FileDownloadButton } from './FileDownloadButton';
import { ResultPreview } from './ResultPreview';
import { useTapisResultInfo } from './useTapisResultKind';
Expand All @@ -26,9 +26,11 @@ export interface OutputBlockProps {
onOpenFolder: (executionId: string) => void;
}

function typeLabel(format: string | null | undefined, name: string): string {
function typeLabel(format: string | null | undefined, name: string, url: string): string {
if (format) return format;
return /\.([a-z0-9]+)$/i.exec(name)?.[1]?.toUpperCase() ?? '';
// A result is usually named by its label, so the file's own name is the better guide.
const extension = (text: string) => /\.([a-z0-9]+)(?:[?#].*)?$/i.exec(text)?.[1]?.toUpperCase();
return extension(url) ?? extension(name) ?? '';
}

export function OutputBlock({
Expand All @@ -48,7 +50,7 @@ export function OutputBlock({
url,
scheme === 'tapis',
);
const kind = previewKind(name);
const kind = outputPreviewKind(url, name, format);
const previewable = Boolean(kind) && scheme === 'tapis' && tapisKind === 'file';
const [open, setOpen] = useState<boolean | null>(null);
// A report is what the run was for, so it opens by itself, but only when the
Expand Down Expand Up @@ -84,9 +86,9 @@ export function OutputBlock({
>
<div className="flex flex-wrap items-center gap-2">
<span className="text-sm font-medium text-gray-900">{label}</span>
{typeLabel(format, name) && (
{typeLabel(format, name, url) && (
<span className="rounded bg-gray-100 px-1.5 py-0.5 text-gray-600">
{typeLabel(format, name)}
{typeLabel(format, name, url)}
</span>
)}
<span className="text-gray-500">{name}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@ const run = (id: string, over: Partial<Execution> = {}): Execution => ({
status: 'SUCCESS',
bindings: { 'in-1': { id: 'r1', name: 'a.txt', url: 'https://x/a.txt' }, 'p-mode': 'shortest' },
results: {
'o-report': { id: 'o-report', name: 'report.md', url: REPORT },
'o-table': { id: 'o-table', name: 'scores.csv', url: TABLE },
'o-lexicon': { id: 'o-lexicon', name: 'lexicon.json', url: LEXICON },
// Named by their labels, as runs name them: the preview kind comes from the location.
'o-report': { id: 'o-report', name: 'Sensemaking report', url: REPORT },
'o-table': { id: 'o-table', name: 'Scores', url: TABLE },
'o-lexicon': { id: 'o-lexicon', name: 'Lexicon', url: LEXICON },
},
...over,
});
Expand Down
Loading