|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useParams } from "next/navigation"; |
| 4 | +import Link from 'next/link'; |
| 5 | +import { useCallback, useEffect, useState } from 'react'; |
| 6 | +import { useHTML } from '@/contexts/HTMLContext'; |
| 7 | +import { DocumentSkeleton } from '@/components/DocumentSkeleton'; |
| 8 | +import { HTMLViewer } from '@/components/HTMLViewer'; |
| 9 | +import { Button } from '@headlessui/react'; |
| 10 | +import { DocumentSettings } from '@/components/DocumentSettings'; |
| 11 | +import { SettingsIcon } from '@/components/icons/Icons'; |
| 12 | +import { useTTS } from "@/contexts/TTSContext"; |
| 13 | + |
| 14 | +export default function HTMLPage() { |
| 15 | + const { id } = useParams(); |
| 16 | + const { setCurrentDocument, currDocName, clearCurrDoc } = useHTML(); |
| 17 | + const { stop } = useTTS(); |
| 18 | + const [error, setError] = useState<string | null>(null); |
| 19 | + const [isLoading, setIsLoading] = useState(true); |
| 20 | + const [isSettingsOpen, setIsSettingsOpen] = useState(false); |
| 21 | + |
| 22 | + const loadDocument = useCallback(async () => { |
| 23 | + if (!isLoading) return; |
| 24 | + console.log('Loading new HTML document (from page.tsx)'); |
| 25 | + stop(); |
| 26 | + try { |
| 27 | + if (!id) { |
| 28 | + setError('Document not found'); |
| 29 | + return; |
| 30 | + } |
| 31 | + await setCurrentDocument(id as string); |
| 32 | + } catch (err) { |
| 33 | + console.error('Error loading document:', err); |
| 34 | + setError('Failed to load document'); |
| 35 | + } finally { |
| 36 | + setIsLoading(false); |
| 37 | + } |
| 38 | + }, [isLoading, id, setCurrentDocument, stop]); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + loadDocument(); |
| 42 | + }, [loadDocument]); |
| 43 | + |
| 44 | + if (error) { |
| 45 | + return ( |
| 46 | + <div className="flex flex-col items-center justify-center min-h-screen"> |
| 47 | + <p className="text-red-500 mb-4">{error}</p> |
| 48 | + <Link |
| 49 | + href="/" |
| 50 | + onClick={() => {clearCurrDoc();}} |
| 51 | + className="inline-flex items-center px-3 py-1 bg-base text-foreground rounded-lg hover:bg-offbase transition-colors" |
| 52 | + > |
| 53 | + <svg className="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 54 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" /> |
| 55 | + </svg> |
| 56 | + Back to Documents |
| 57 | + </Link> |
| 58 | + </div> |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + return ( |
| 63 | + <> |
| 64 | + <div className="p-2 pb-2 border-b border-offbase"> |
| 65 | + <div className="flex flex-wrap items-center justify-between"> |
| 66 | + <div className="flex items-center gap-1"> |
| 67 | + <Link |
| 68 | + href="/" |
| 69 | + onClick={() => {clearCurrDoc();}} |
| 70 | + className="inline-flex items-center px-3 py-1 bg-base text-foreground rounded-lg hover:bg-offbase transform transition-transform duration-200 ease-in-out hover:scale-[1.02]" |
| 71 | + > |
| 72 | + <svg className="w-4 h-4 mr-2" fill="currentColor" stroke="currentColor" viewBox="0 0 24 24"> |
| 73 | + <path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" /> |
| 74 | + </svg> |
| 75 | + Documents |
| 76 | + </Link> |
| 77 | + <Button |
| 78 | + onClick={() => setIsSettingsOpen(true)} |
| 79 | + className="rounded-full p-1 text-foreground hover:bg-offbase transform transition-transform duration-200 ease-in-out hover:scale-[1.1] hover:text-accent" |
| 80 | + aria-label="View Settings" |
| 81 | + > |
| 82 | + <SettingsIcon className="w-5 h-5 hover:animate-spin-slow" /> |
| 83 | + </Button> |
| 84 | + </div> |
| 85 | + <h1 className="ml-2 mr-2 text-md font-semibold text-foreground truncate"> |
| 86 | + {isLoading ? 'Loading...' : currDocName} |
| 87 | + </h1> |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + {isLoading ? ( |
| 91 | + <div className="p-4"> |
| 92 | + <DocumentSkeleton /> |
| 93 | + </div> |
| 94 | + ) : ( |
| 95 | + <HTMLViewer className="p-4" /> |
| 96 | + )} |
| 97 | + <DocumentSettings html isOpen={isSettingsOpen} setIsOpen={setIsSettingsOpen} /> |
| 98 | + </> |
| 99 | + ); |
| 100 | +} |
0 commit comments