import { useState } from 'react'; import { useQuery } from '@tanstack/react-query'; import { File, FileCode2, FileText, Folder, X } from 'lucide-react'; import { pluginsApi } from '../../api/plugins'; import CodeView from '../CodeView'; import DialogShell from '../DialogShell'; import IconButton from '../IconButton'; import MarkdownView, { ViewToggle } from '../MarkdownView'; import Spinner from '../Spinner'; import { useT } from '../../i18n'; import { CODE_EXT, fileTree } from '../../lib/fileTree'; import { isMarkdown } from '../../lib/highlight'; import { shortenPath } from '../../lib/paths'; import { queryKeys } from '../../lib/queryKeys'; const NUL = String.fromCharCode(0); /** The files of a plugin, read only, from the local copy Skillshare reviewed: the same two panes as a skill's Files tab. */ export default function PluginFilesDialog({ name, source, onClose, }: { name: string; source?: string; onClose: () => void; }) { const t = useT(); const [picked, setPicked] = useState(''); const [raw, setRaw] = useState(false); const list = useQuery({ queryKey: queryKeys.pluginFiles(name), queryFn: () => pluginsApi.files(name), }); const files = list.data?.files ?? []; const selected = files.includes(picked) ? picked : (files.find((f) => /^readme\.md$/i.test(f)) ?? files[0]); const file = useQuery({ queryKey: queryKeys.pluginFile(name, selected), queryFn: () => pluginsApi.file(name, selected), enabled: !!selected, }); const content = file.data?.content; const markdown = isMarkdown(selected ?? ''); const error = list.error ?? file.error; return (

{name}

{source && (

{shortenPath(source)}

)}
} label={t('common.close')} onClick={onClose} />
{error ? (
{(error as Error).message}
) : list.isPending ? (
) : files.length === 0 ? (

{t('plugins.noFiles')}

) : ( // A fixed height, so each pane scrolls by itself and the list stays beside a long file.
{fileTree(files).map((row) => { const Icon = row.folder ? Folder : row.label.endsWith('.md') ? FileText : CODE_EXT.test(row.label) ? FileCode2 : File; const inner = ( <> {row.label} ); const style = { paddingLeft: 10 + row.depth * 20 }; return row.folder ? (
{inner}
) : ( ); })}
{selected} {markdown && content !== undefined && }
{content === undefined ? (
) : content.includes(NUL) ? (
{t('resourceDetail.files.binary')}
) : !markdown || raw ? ( ) : (
{content}
)}
)}
); }