Xianbao QIAN commited on
Commit
cbc828e
1 Parent(s): f831b65

group by years

Browse files
Files changed (1) hide show
  1. src/components/AIPoliciesTable.tsx +36 -13
src/components/AIPoliciesTable.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import React from 'react';
2
  import Link from 'next/link';
3
 
4
  interface PolicyData {
@@ -14,24 +14,47 @@ interface AIPoliciesTableProps {
14
  }
15
 
16
  const AIPoliciesTable: React.FC<AIPoliciesTableProps> = ({ policies }) => {
17
- const groupedPolicies: { [slug: string]: PolicyData[] } = {};
18
 
19
  policies.forEach((policy) => {
20
- if (!groupedPolicies[policy.slug]) {
21
- groupedPolicies[policy.slug] = [];
 
22
  }
23
- groupedPolicies[policy.slug].push(policy);
 
 
 
24
  });
25
 
26
- // Sort the policies within each group based on releaseDate in descending order
27
- Object.values(groupedPolicies).forEach((group) => {
28
- group.sort((a, b) => new Date(b.releaseDate).getTime() - new Date(a.releaseDate).getTime());
29
- });
30
 
31
  return (
32
  <div className="my-8">
33
- {/* <h2 className="text-2xl font-bold mb-4 dark:text-white">AI Policies from the Chinese government</h2> */}
34
- <div className="overflow-x-auto">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  <table className="w-full border-collapse table-auto">
36
  <tbody>
37
  {Object.entries(groupedPolicies).map(([slug, policies], index) => {
@@ -43,7 +66,7 @@ const AIPoliciesTable: React.FC<AIPoliciesTableProps> = ({ policies }) => {
43
  <td className="py-2 px-4 dark:text-white">
44
  <div>{zhPolicy?.title}</div>
45
  <div className="text-sm text-gray-500 dark:text-gray-400">{enPolicy?.title}</div>
46
- <div className="text-xs text-gray-400 dark:text-gray-500">{zhPolicy?.releaseDate}</div>
47
  </td>
48
  <td className="py-2 px-4">
49
  {policies.map((policy) => (
@@ -65,7 +88,7 @@ const AIPoliciesTable: React.FC<AIPoliciesTableProps> = ({ policies }) => {
65
  })}
66
  </tbody>
67
  </table>
68
- </div>
69
  </div>
70
  );
71
  };
 
1
+ import React, { useState } from 'react';
2
  import Link from 'next/link';
3
 
4
  interface PolicyData {
 
14
  }
15
 
16
  const AIPoliciesTable: React.FC<AIPoliciesTableProps> = ({ policies }) => {
17
+ const groupedPoliciesByYear: { [year: string]: { [slug: string]: PolicyData[] } } = {};
18
 
19
  policies.forEach((policy) => {
20
+ const year = new Date(policy.releaseDate).getFullYear().toString();
21
+ if (!groupedPoliciesByYear[year]) {
22
+ groupedPoliciesByYear[year] = {};
23
  }
24
+ if (!groupedPoliciesByYear[year][policy.slug]) {
25
+ groupedPoliciesByYear[year][policy.slug] = [];
26
+ }
27
+ groupedPoliciesByYear[year][policy.slug].push(policy);
28
  });
29
 
30
+ const sortedYears = Object.keys(groupedPoliciesByYear).sort((a, b) => parseInt(b) - parseInt(a));
 
 
 
31
 
32
  return (
33
  <div className="my-8">
34
+ {sortedYears.map((year) => (
35
+ <YearSection key={year} year={year} groupedPolicies={groupedPoliciesByYear[year]} />
36
+ ))}
37
+ </div>
38
+ );
39
+ };
40
+
41
+ interface YearSectionProps {
42
+ year: string;
43
+ groupedPolicies: { [slug: string]: PolicyData[] };
44
+ }
45
+
46
+ const YearSection: React.FC<YearSectionProps> = ({ year, groupedPolicies }) => {
47
+ const [isOpen, setIsOpen] = useState(true);
48
+
49
+ return (
50
+ <div className="mb-8">
51
+ <button
52
+ className="text-xl font-bold mb-2 focus:outline-none"
53
+ onClick={() => setIsOpen(!isOpen)}
54
+ >
55
+ {year} {isOpen ? '▼' : '▶'}
56
+ </button>
57
+ {isOpen && (
58
  <table className="w-full border-collapse table-auto">
59
  <tbody>
60
  {Object.entries(groupedPolicies).map(([slug, policies], index) => {
 
66
  <td className="py-2 px-4 dark:text-white">
67
  <div>{zhPolicy?.title}</div>
68
  <div className="text-sm text-gray-500 dark:text-gray-400">{enPolicy?.title}</div>
69
+ <div className="text-xs text-gray-400 dark:text-gray-500">{new Date(zhPolicy?.releaseDate || '').toLocaleDateString()}</div>
70
  </td>
71
  <td className="py-2 px-4">
72
  {policies.map((policy) => (
 
88
  })}
89
  </tbody>
90
  </table>
91
+ )}
92
  </div>
93
  );
94
  };