Catch all subpaths

This commit is contained in:
Gnarwhal 2024-09-16 19:57:33 +00:00
parent 2c2fd045b3
commit 13c9a7140c
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
10 changed files with 21 additions and 8 deletions

View file

@ -1,61 +0,0 @@
'use client'
import { useState, useEffect } from 'react'
import Error from './types/error';
import Image from './types/image';
import Text from './types/text';
export default function Content({ src }: { src: string}) {
const [content, set_content] = useState();
type ContentType<T> = {
matcher: RegExp,
emit: () => undefined | Processor<T>,
};
type Processor<T> = {
process: (response: Response) => Promise<T>,
postprocess: (data: T) => undefined,
};
const recognized_types: ContentType<Any>[] = [{
matcher: /image\/\w+/,
emit: () => {
set_content(<Image src={src} />);
},
}, {
matcher: /text\/\w+/,
emit: () => {
return {
process: (response: Response) => {
return response.text();
},
postprocess: (data: string) => {
set_content(<Text text={data} />);
}
};
}
}];
useEffect(() => {
if (content == undefined) {
const result = fetch(src)
.then(response => {
const content_type = response.headers.get('Content-Type').split(';')[0];
for (let type of recognized_types) {
if (type.matcher.test(content_type)) {
const emitted = type.emit();
if (emitted != undefined) {
result.then(emitted.postprocess);
return emitted.process(response);
}
return;
}
}
set_content(<Error content_type={content_type} />);
});
}
}, []);
return content ?? <p>Loading...</p>;
}