diff --git a/ui-react/src/lib/__tests__/result-preview.test.ts b/ui-react/src/lib/__tests__/result-preview.test.ts index d6ca9565..2bb9b988 100644 --- a/ui-react/src/lib/__tests__/result-preview.test.ts +++ b/ui-react/src/lib/__tests__/result-preview.test.ts @@ -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(); + }); +}); diff --git a/ui-react/src/lib/result-preview.ts b/ui-react/src/lib/result-preview.ts index 73c4e33c..430b55a7 100644 --- a/ui-react/src/lib/result-preview.ts +++ b/ui-react/src/lib/result-preview.ts @@ -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); diff --git a/ui-react/src/pages/modeling/thread/OutputBlock.tsx b/ui-react/src/pages/modeling/thread/OutputBlock.tsx index bd401251..641a129b 100644 --- a/ui-react/src/pages/modeling/thread/OutputBlock.tsx +++ b/ui-react/src/pages/modeling/thread/OutputBlock.tsx @@ -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'; @@ -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({ @@ -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(null); // A report is what the run was for, so it opens by itself, but only when the @@ -84,9 +86,9 @@ export function OutputBlock({ >
{label} - {typeLabel(format, name) && ( + {typeLabel(format, name, url) && ( - {typeLabel(format, name)} + {typeLabel(format, name, url)} )} {name} diff --git a/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx b/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx index 6cf4bf91..65eddb75 100644 --- a/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx +++ b/ui-react/src/pages/modeling/thread/__tests__/MintResultsCards.test.tsx @@ -66,9 +66,10 @@ const run = (id: string, over: Partial = {}): 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, });