import React, { useState } from "react"; import { PolicyData } from "../types"; interface AIPoliciesTableProps { policies: PolicyData[]; } const AIPoliciesTable: React.FC = ({ policies }) => { const initialOpenYears = policies.reduce((acc, policy) => { const year = new Date(policy.releaseDate).getFullYear(); acc[year] = true; return acc; }, {} as { [key: number]: boolean }); const [openYears, setOpenYears] = useState<{ [key: number]: boolean }>(initialOpenYears); const toggleYear = (year: number) => { setOpenYears((prev) => ({ ...prev, [year]: !prev[year] })); }; const groupedPolicies = policies.reduce((acc, policy) => { const year = new Date(policy.releaseDate).getFullYear(); if (!acc[year]) { acc[year] = []; } acc[year].push(policy); return acc; }, {} as { [key: number]: PolicyData[] }); const sortedYears = Object.keys(groupedPolicies) .map(Number) .sort((a, b) => b - a); const formatDate = (dateString: string) => { const date = new Date(dateString); const options: Intl.DateTimeFormatOptions = { month: 'short', day: 'numeric', year: 'numeric' }; return date.toLocaleDateString('en-US', options); }; return (
{sortedYears.map((year) => (
{openYears[year] && ( {groupedPolicies[year].map((policy, index) => ( ))}
{policy.zh}
{policy.en}
{formatDate(policy.releaseDate)}
{policy.link.zh && ( 中文 )} {policy.link.en && ( English )}
)}
))}
); }; export default AIPoliciesTable;