import { useCallback, useEffect, useState } from 'react'; import Editor from './components/Editor'; import { loadFromBytebin } from './util/storage'; const INITIAL = Symbol(); const LOADING = Symbol(); const LOADED = Symbol(); type LoadingState = typeof INITIAL | typeof LOADING | typeof LOADED; export default function App() { const [pasteId] = useState(getPasteIdFromUrl); const [state, setState] = useState(INITIAL); const [forcedContent, setForcedContent] = useState(''); const [actualContent, setActualContent] = useState(''); const [contentType, setContentType] = useState(); const setContent = useCallback( (content: string) => { setActualContent(content); setForcedContent(content); }, [setActualContent, setForcedContent] ); useEffect(() => { if (pasteId && state === INITIAL) { setState(LOADING); setForcedContent('Loading...'); loadFromBytebin(pasteId).then(({ ok, content, type }) => { if (ok) { setContent(content); if (type) { setContentType(type); } } else { setContent(get404Message(pasteId)); } setState(LOADED); }); } }, [pasteId, state, setContent]); return ( ); } function get404Message(pasteId: string) { return ` ██╗ ██╗ ██████╗ ██╗ ██╗ ██║ ██║██╔═████╗██║ ██║ ███████║██║██╔██║███████║ ╚════██║████╔╝██║╚════██║ ██║╚██████╔╝ ██║ ╚═╝ ╚═════╝ ╚═╝ not found: '${pasteId}' maybe the paste expired? `; } function getPasteIdFromUrl() { const path = window.location.pathname; if (path && /^\/[a-zA-Z0-9]+$/.test(path)) { return path.substring(1); } else { return undefined; } }