Skip to content
Snippets Groups Projects

feat: handle text/plain specifically in file-viewer

Merged Elias Künstler requested to merge racct/textplain-file-viewer-handling into master
1 file
+ 16
1
Compare changes
  • Side-by-side
  • Inline
import { subscribeByID } from '@adornis/baseql/operations/mongo.js';
import { stateful } from '@adornis/functional-lit/stateful.js';
import { stateful, useState, useEffect } from '@adornis/functional-lit/stateful.js';
import { useQuery } from '@adornis/functional-lit/use-query.js';
import { html } from 'lit';
import { AdornisFile } from '../db/files.js';
@@ -82,7 +82,22 @@ export const FileViewer = stateful(({ fileID }: { fileID: string }) => {
</iframe>
</object>
`;
} else if (file.meta.mimetype === 'text/plain') {
// Fetch and display plain text files as readable text
const [textContent, setTextContent] = useState<string | null>(null);
useEffect(() => {
fetch(file.getServeLink())
.then(res => res.text())
.then(setTextContent)
.catch(() => setTextContent('Failed to load file content.'));
}, [fileID]);
return html`
<div class="w-full h-[60vh] overflow-auto bg-base-200 rounded shadow p-4">
<pre class="whitespace-pre-wrap break-words font-mono text-base-content">${textContent ?? 'Loading...'}</pre>
</div>
`;
} else if (file.meta.mimetype.includes('text') || file.meta.mimetype.includes('json')) {
// For other text/* and json, fallback to iframe
return html`
<iframe
src="${file.getServeLink()}"
Loading