2022-01-08 19:10:27 +00:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2021-03-26 22:00:12 +00:00
|
|
|
import Editor from './components/Editor';
|
|
|
|
|
import parseContentType from 'content-type-parser';
|
2021-04-02 18:54:37 +01:00
|
|
|
import { languageIds } from './util/highlighting';
|
2021-10-27 23:37:42 +01:00
|
|
|
import { bytebinUrl } from './util/constants';
|
2021-03-26 22:00:12 +00:00
|
|
|
|
|
|
|
|
function getPasteIdFromUrl() {
|
|
|
|
|
const path = window.location.pathname;
|
|
|
|
|
if (path && /^\/[a-zA-Z0-9]+$/.test(path)) {
|
|
|
|
|
return path.substring(1);
|
|
|
|
|
} else {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function loadFromBytebin(id) {
|
|
|
|
|
try {
|
2021-10-27 23:37:42 +01:00
|
|
|
const resp = await fetch(bytebinUrl + id);
|
2021-03-26 22:00:12 +00:00
|
|
|
if (resp.ok) {
|
|
|
|
|
const content = await resp.text();
|
2021-04-02 13:05:15 +01:00
|
|
|
const type = parseLanguageFromContentType(
|
|
|
|
|
resp.headers.get('content-type')
|
|
|
|
|
);
|
|
|
|
|
|
2022-01-08 19:10:27 +00:00
|
|
|
document.title = 'pastes | ' + id;
|
2021-03-27 13:09:03 +00:00
|
|
|
return { ok: true, content, type };
|
2021-03-26 22:00:12 +00:00
|
|
|
} else {
|
|
|
|
|
return { ok: false };
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return { ok: false };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 13:09:03 +00:00
|
|
|
function parseLanguageFromContentType(contentType) {
|
|
|
|
|
const { type, subtype: subType } = parseContentType(contentType);
|
|
|
|
|
if (type === 'application' && subType === 'json') {
|
|
|
|
|
return 'json';
|
|
|
|
|
}
|
|
|
|
|
if (type === 'text' && languageIds.includes(subType.toLowerCase())) {
|
|
|
|
|
return subType.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-26 22:00:12 +00:00
|
|
|
const INITIAL = Symbol();
|
|
|
|
|
const LOADING = Symbol();
|
|
|
|
|
const LOADED = Symbol();
|
|
|
|
|
|
|
|
|
|
export default function App() {
|
|
|
|
|
const [pasteId] = useState(getPasteIdFromUrl);
|
|
|
|
|
const [state, setState] = useState(INITIAL);
|
2022-01-08 19:10:27 +00:00
|
|
|
const [forcedContent, setForcedContent] = useState('');
|
|
|
|
|
const [actualContent, setActualContent] = useState('');
|
2021-03-26 22:00:12 +00:00
|
|
|
const [contentType, setContentType] = useState();
|
|
|
|
|
|
2022-01-08 19:10:27 +00:00
|
|
|
const setContent = useCallback((content) => {
|
|
|
|
|
setActualContent(content);
|
|
|
|
|
setForcedContent(content);
|
|
|
|
|
}, [setActualContent, setForcedContent]);
|
|
|
|
|
|
2021-03-26 22:00:12 +00:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (pasteId && state === INITIAL) {
|
|
|
|
|
setState(LOADING);
|
2022-01-08 19:10:27 +00:00
|
|
|
setForcedContent('Loading...');
|
2021-03-26 22:00:12 +00:00
|
|
|
loadFromBytebin(pasteId).then(({ ok, content, type }) => {
|
|
|
|
|
if (ok) {
|
|
|
|
|
setContent(content);
|
|
|
|
|
if (type) {
|
|
|
|
|
setContentType(type);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2021-03-27 21:23:47 +00:00
|
|
|
setContent(get404Message(pasteId));
|
2021-03-26 22:00:12 +00:00
|
|
|
}
|
|
|
|
|
setState(LOADED);
|
2021-04-02 13:05:15 +01:00
|
|
|
});
|
2021-03-26 22:00:12 +00:00
|
|
|
}
|
2021-04-02 13:05:15 +01:00
|
|
|
}, [pasteId, state, setContent]);
|
2021-03-26 22:00:12 +00:00
|
|
|
|
2021-04-02 13:05:15 +01:00
|
|
|
return (
|
|
|
|
|
<Editor
|
2022-01-08 19:10:27 +00:00
|
|
|
forcedContent={forcedContent}
|
|
|
|
|
setForcedContent={setContent}
|
|
|
|
|
actualContent={actualContent}
|
|
|
|
|
setActualContent={setActualContent}
|
2021-04-02 13:05:15 +01:00
|
|
|
contentType={contentType}
|
2022-04-03 19:47:01 +01:00
|
|
|
pasteId={pasteId}
|
2021-04-02 13:05:15 +01:00
|
|
|
/>
|
|
|
|
|
);
|
2021-03-26 22:00:12 +00:00
|
|
|
}
|
2021-03-27 21:23:47 +00:00
|
|
|
|
|
|
|
|
function get404Message(pasteId) {
|
|
|
|
|
return `
|
|
|
|
|
██╗ ██╗ ██████╗ ██╗ ██╗
|
|
|
|
|
██║ ██║██╔═████╗██║ ██║
|
|
|
|
|
███████║██║██╔██║███████║
|
|
|
|
|
╚════██║████╔╝██║╚════██║
|
|
|
|
|
██║╚██████╔╝ ██║
|
|
|
|
|
╚═╝ ╚═════╝ ╚═╝
|
|
|
|
|
|
|
|
|
|
not found: '${pasteId}'
|
|
|
|
|
maybe the paste expired?
|
|
|
|
|
`;
|
2021-04-02 13:05:15 +01:00
|
|
|
}
|