Convert to typescript

This commit is contained in:
Luck
2022-11-06 22:46:13 +00:00
parent e995261d87
commit 042bace2c6
18 changed files with 593 additions and 321 deletions

77
src/App.tsx Normal file
View File

@@ -0,0 +1,77 @@
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<string | undefined>(getPasteIdFromUrl);
const [state, setState] = useState<LoadingState>(INITIAL);
const [forcedContent, setForcedContent] = useState<string>('');
const [actualContent, setActualContent] = useState<string>('');
const [contentType, setContentType] = useState<string>();
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 (
<Editor
forcedContent={forcedContent}
setForcedContent={setContent}
actualContent={actualContent}
setActualContent={setActualContent}
contentType={contentType}
pasteId={pasteId}
/>
);
}
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;
}
}