import { useContext, useEffect, useRef, useState } from 'preact/hooks' import { Markdown } from '../components/Markdown.jsx' import { Problem } from '../components/Problem.jsx' import { Solution } from '../components/Solution.jsx' import { MetadataContext, useCurrentUser, useReadResource } from '../hooks.jsx' export const ProblemPage = ({ id }) => { const metadata = useContext(MetadataContext) metadata.title = `Problem ${id}` const [user] = useCurrentUser() const [source, setSource] = useState('') const editorRef = useRef() const [{ content }] = useReadResource(`/api/problem/${id}`, { content: '' }) const [solutions] = useReadResource(`/api/solutions?problem=${id}`, []) const sendSolution = async () => { const res = await fetch(`/api/solution`, { method: 'POST', credentials: 'include', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ problemId: id, content: source, }), }) location.reload() } useEffect(() => { if (editorRef.current) { // settare questo ad "auto" toglie l'altezza al contenitore che passa alla sua // dimensione minima iniziale, ciò serve per permettere all'autosize della textarea di // crescere e ridursi ma ha il problema che resetta lo scroll della pagina che deve // essere preservato a mano const oldScrollY = window.scrollY editorRef.current.style.height = 'auto' editorRef.current.style.height = editorRef.current.scrollHeight + 'px' window.scrollTo(0, oldScrollY) } }, [source]) return (
{user && (
Logged in as {user.role} @{user.username}
)}
Testo del problema
{solutions.length > 0 && (
Soluzioni
{solutions.map(s => ( ))}
)} {user ? ( <>
Invia una soluzione al problema

Editor

Preview

{source.trim().length ? ( ) : (
Scrivi una nuova soluzione...
)}
) : (
Accedi per inviare una soluzione
)}
) }