import { useEffect, useState } from 'react' import { Helmet } from 'react-helmet-async' import { useParams } from 'react-router-dom' import TagLink from '@/components/TagLink' import WikiBody from '@/components/WikiBody' import Label from '@/components/common/Label' import PageTitle from '@/components/common/PageTitle' import TabGroup, { Tab } from '@/components/common/TabGroup' import TagInput from '@/components/common/TagInput' import MainArea from '@/components/layout/MainArea' import { Button } from '@/components/ui/button' import { toast } from '@/components/ui/use-toast' import { SITE_TITLE } from '@/config' import { apiGet, apiPut } from '@/lib/api' import type { FC } from 'react' import type { Material, Tag } from '@/types' type MaterialWithTag = Material & { tag: Tag } const MaterialDetailPage: FC = () => { const { id } = useParams () const [file, setFile] = useState (null) const [filePreview, setFilePreview] = useState ('') const [loading, setLoading] = useState (false) const [material, setMaterial] = useState (null) const [sending, setSending] = useState (false) const [tag, setTag] = useState ('') const [url, setURL] = useState ('') const handleSubmit = async () => { const formData = new FormData if (tag.trim ()) formData.append ('tag', tag) if (file) formData.append ('file', file) if (url.trim ()) formData.append ('url', url) try { setSending (true) const data = await apiPut (`/materials/${ id }`, formData) setMaterial (data) toast ({ title: '更新成功!' }) } catch { toast ({ title: '更新失敗……', description: '入力を見直してください.' }) } finally { setSending (false) } } useEffect (() => { if (!(id)) return void (async () => { try { setLoading (true) const data = await apiGet (`/materials/${ id }`) setMaterial (data) setTag (data.tag.name) if (data.file && data.contentType) { setFilePreview (data.file) setFile (new File ([await (await fetch (data.file)).blob ()], data.file, { type: data.contentType })) } setURL (data.url ?? '') } finally { setLoading (false) } }) () }, [id]) return ( {material && ( {`${ material.tag.name } 素材照会 | ${ SITE_TITLE }`} )} {loading ? 'Loading...' : (material && ( <> {(material.file && material.contentType) && ( (/image\/.*/.test (material.contentType) && ( {material.tag.name)) || (/video\/.*/.test (material.contentType) && ( ) } export default MaterialDetailPage