A bajillion type errors
This commit is contained in:
parent
dfcb95bd0d
commit
9fecf5bbfa
10 changed files with 36 additions and 45 deletions
|
@ -4,14 +4,14 @@ import { useState, useEffect } from 'react'
|
|||
|
||||
import NetworkError from './types/error/network';
|
||||
import ContentTypeError from './types/error/content_type';
|
||||
import Image from './types/image';
|
||||
import ImageContent from './types/image';
|
||||
import Terminal from './types/terminal';
|
||||
import Text from './types/text';
|
||||
|
||||
type ContentType<T> = {
|
||||
content_type: RegExp,
|
||||
extension: RegExp,
|
||||
emit: () => undefined | Processor<T>,
|
||||
path?: RegExp,
|
||||
emit: () => void | Processor<T>,
|
||||
};
|
||||
type Processor<T> = {
|
||||
process: (response: Response) => Promise<T>,
|
||||
|
@ -22,8 +22,8 @@ function not_match(regex: RegExp | undefined, str: string) {
|
|||
return !(regex ?? /(?:)/).test(str);
|
||||
}
|
||||
|
||||
function is_type(response: Response, type: ContentType<Any>) {
|
||||
if (not_match(type.content_type, response.headers.get('Content-Type').split(';')[0])) {
|
||||
function is_type(response: Response, type: ContentType<any>) {
|
||||
if (not_match(type.content_type, response.headers.get('Content-Type')!.split(';')[0])) {
|
||||
return false;
|
||||
} else if (not_match(type.path, window.location.pathname)) {
|
||||
return false;
|
||||
|
@ -32,12 +32,12 @@ function is_type(response: Response, type: ContentType<Any>) {
|
|||
}
|
||||
|
||||
export default function Content({ src }: { src: string}) {
|
||||
const [content, set_content] = useState();
|
||||
const [content, set_content] = useState<JSX.Element>();
|
||||
|
||||
const recognized_types: ContentType<Any>[] = [{
|
||||
const recognized_types: ContentType<any>[] = [{
|
||||
content_type: /image\/\w+/,
|
||||
emit: () => {
|
||||
set_content(<Image src={src} />);
|
||||
set_content(<ImageContent src={src} />);
|
||||
},
|
||||
}, {
|
||||
content_type: /application\/octet-stream/,
|
||||
|
@ -70,9 +70,8 @@ export default function Content({ src }: { src: string}) {
|
|||
if (content == undefined) {
|
||||
const result = fetch(src)
|
||||
.then(response => {
|
||||
const content_type = response.headers.get('Content-Type').split(';')[0];
|
||||
for (const type of recognized_types) {
|
||||
if (type.content_type.test(content_type)) {
|
||||
if (is_type(response, type)) {
|
||||
const emitted = type.emit();
|
||||
if (emitted != undefined) {
|
||||
result.then(emitted.postprocess);
|
||||
|
@ -81,13 +80,13 @@ export default function Content({ src }: { src: string}) {
|
|||
return;
|
||||
}
|
||||
}
|
||||
set_content(<ContentTypeError content_type={content_type} />);
|
||||
set_content(<ContentTypeError content_type={response.headers.get('Content-Type')!.split(';')[0]} />);
|
||||
})
|
||||
.catch(err => {
|
||||
set_content(<NetworkError err={err} />);
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
});
|
||||
|
||||
return content ?? <p>Loading...</p>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue