'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 = { matcher: RegExp, emit: () => undefined | Processor, }; type Processor = { process: (response: Response) => Promise, postprocess: (data: T) => undefined, }; const recognized_types: ContentType[] = [{ matcher: /image\/\w+/, emit: () => { set_content(); }, }, { matcher: /text\/\w+/, emit: () => { return { process: (response: Response) => { return response.text(); }, postprocess: (data: string) => { set_content(); } }; } }]; 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(); }); } }, []); return content ??

Loading...

; }