import { useEffect, useState } from 'react' import { getFiles, fileDownloadUrl, type ChannelKindKey, type FileView, type Org } from '../api/client' const dateFormatter = new Intl.DateTimeFormat('ko-KR', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }) function humanizeSize(bytes: number): string { if (bytes < 1024) { return `${bytes} B` } const kb = bytes / 1024 if (kb < 1024) { return `${kb.toFixed(1)} KB` } return `${(kb / 1024).toFixed(1)} MB` } function formatDate(createAt: number): string { // Intl 기본 포맷은 "2026. 07. 22. 14:05" 형태라 화면 요구사항(yyyy-MM-dd HH:mm)에 맞게 다듬는다. const parts = dateFormatter.formatToParts(new Date(createAt)) const get = (type: string) => parts.find((p) => p.type === type)?.value ?? '' return `${get('year')}-${get('month')}-${get('day')} ${get('hour')}:${get('minute')}` } /** 기관관리 상세의 자료 탭. 선택한 채널에 올라온 첨부파일 전체를 목록으로 보여준다(폴링 없음). */ export default function ChannelFiles({ org }: { org: Org }) { const bothMissing = !org.channelIdMj && !org.channelIdLaw const [channel, setChannel] = useState(org.channelIdMj ? 'mj' : 'law') const [files, setFiles] = useState([]) const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [reloadKey, setReloadKey] = useState(0) useEffect(() => { if (bothMissing) { return } let cancelled = false setLoading(true) setError(null) getFiles(org.id, channel) .then((data) => { if (!cancelled) { setFiles(data) } }) .catch((e: Error) => { if (!cancelled) { setError(e.message) } }) .finally(() => { if (!cancelled) { setLoading(false) } }) return () => { cancelled = true } // eslint-disable-next-line react-hooks/exhaustive-deps }, [channel, org.id, reloadKey]) return (
{!bothMissing && ( )}
{bothMissing ? (

채널이 아직 생성되지 않았습니다.

) : (
{loading &&

불러오는 중…

} {!loading && error &&

{error}

} {!loading && !error && files.length === 0 && (

등록된 자료가 없습니다.

)} {!loading && !error && files.length > 0 && ( {files.map((file) => ( ))}
파일명 크기 올린 사람 올린 시각
{file.name} {humanizeSize(file.size)} {file.uploader} {formatDate(file.createAt)}
)}
)}
) }