import React, { useEffect, useMemo, useState } from 'react'; import { useSearchParams } from 'react-router-dom'; import { IcFileText, IcNavBack } from '../res/icons'; import * as FileSystem from '@/os/FileSystemService'; import { useFileManagerGestures } from '../hooks/useFileManagerGestures'; import { strings } from '../res/strings'; import { stringsEn } from '../res/strings.en'; import { useAppStrings } from '@/os/useAppStrings'; type LoadState = | { status: 'loading'; text: '' } | { status: 'ready'; text: string } | { status: 'error'; text: '' }; export const TextPreviewPage: React.FC = () => { const [searchParams] = useSearchParams(); const { bindBack } = useFileManagerGestures(); const s = useAppStrings(strings, stringsEn); const path = searchParams.get('path') || ''; const [loadState, setLoadState] = useState({ status: 'loading', text: '' }); const fileName = useMemo(() => { if (!path) return s.text_preview_no_file; const parts = path.split('/').filter(Boolean); return parts[parts.length - 1] || s.text_preview_no_file; }, [path, s.text_preview_no_file]); const file = useMemo(() => (path ? FileSystem.getNode(path) : null), [path]); useEffect(() => { let cancelled = false; async function loadText() { if (!path || !file || file.type !== 'file') { setLoadState({ status: 'error', text: '' }); return; } setLoadState({ status: 'loading', text: '' }); const blob = await FileSystem.readFile(path); if (cancelled) return; if (!blob) { setLoadState({ status: 'error', text: '' }); return; } const text = await blob.text(); if (!cancelled) setLoadState({ status: 'ready', text }); } loadText(); return () => { cancelled = true; }; }, [file, path]); return (

{fileName}

{file && (
{FileSystem.formatFileSize(file.size)}
)}
{loadState.status === 'loading' && (
{s.text_preview_loading}
)} {loadState.status === 'error' && (
{s.text_preview_failed}
)} {loadState.status === 'ready' && (
            {loadState.text || s.text_preview_empty}
          
)}
); }; export default TextPreviewPage;