Xianbao QIAN commited on
Commit
58154f8
·
1 Parent(s): c80b461

nice views

Browse files
Files changed (1) hide show
  1. src/app/page.tsx +81 -10
src/app/page.tsx CHANGED
@@ -16,6 +16,8 @@ export default function Home() {
16
  const [currentPage, setCurrentPage] = useState(1);
17
  const [pageSize, setPageSize] = useState(100);
18
  const [filterText, setFilterText] = useState('');
 
 
19
 
20
  useEffect(() => {
21
  async function fetchData() {
@@ -63,6 +65,7 @@ export default function Home() {
63
  await db.terminate();
64
 
65
  setAllModels(data);
 
66
  }
67
  fetchData();
68
  }, []);
@@ -71,15 +74,28 @@ export default function Home() {
71
  model.ancestor.toLowerCase().includes(filterText.toLowerCase())
72
  );
73
 
74
- const totalPages = Math.ceil(filteredModels.length / pageSize);
 
 
 
 
 
 
 
 
75
 
76
- const paginatedModels = filteredModels.slice(
77
  (currentPage - 1) * pageSize,
78
  currentPage * pageSize
79
  );
80
 
 
 
 
 
 
81
  return (
82
- <main className="container mx-auto py-8 text-gray-900 dark:text-white">
83
  <h1 className="text-4xl font-bold mb-4">All Models</h1>
84
  <div className="mb-4">
85
  <input
@@ -87,17 +103,29 @@ export default function Home() {
87
  placeholder="Filter by model name"
88
  value={filterText}
89
  onChange={(e) => setFilterText(e.target.value)}
90
- className="px-4 py-2 border border-gray-300 rounded-md"
91
  />
92
  </div>
93
- {paginatedModels.length > 0 ? (
 
 
94
  <>
95
  <table className="table-auto border-collapse w-full">
96
  <thead>
97
  <tr>
98
  <th className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-left">Model</th>
99
- <th className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-right">Direct Children</th>
100
- <th className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-right">All Children</th>
 
 
 
 
 
 
 
 
 
 
101
  </tr>
102
  </thead>
103
  <tbody>
@@ -110,18 +138,61 @@ export default function Home() {
110
  ))}
111
  </tbody>
112
  </table>
113
- <div className="mt-4">
114
  <button
115
  onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
116
  disabled={currentPage === 1}
117
- className="px-4 py-2 bg-blue-500 text-white rounded-md mr-2"
118
  >
119
  Previous
120
  </button>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  <button
122
  onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages))}
123
  disabled={currentPage === totalPages}
124
- className="px-4 py-2 bg-blue-500 text-white rounded-md"
125
  >
126
  Next
127
  </button>
 
16
  const [currentPage, setCurrentPage] = useState(1);
17
  const [pageSize, setPageSize] = useState(100);
18
  const [filterText, setFilterText] = useState('');
19
+ const [isLoading, setIsLoading] = useState(true);
20
+ const [orderBy, setOrderBy] = useState<'all_children' | 'direct_children'>('all_children');
21
 
22
  useEffect(() => {
23
  async function fetchData() {
 
65
  await db.terminate();
66
 
67
  setAllModels(data);
68
+ setIsLoading(false);
69
  }
70
  fetchData();
71
  }, []);
 
74
  model.ancestor.toLowerCase().includes(filterText.toLowerCase())
75
  );
76
 
77
+ const sortedModels = filteredModels.sort((a, b) => {
78
+ if (orderBy === 'all_children') {
79
+ return b.all_children_count - a.all_children_count;
80
+ } else {
81
+ return (b.direct_children_count ?? 0) - (a.direct_children_count ?? 0);
82
+ }
83
+ });
84
+
85
+ const totalPages = Math.ceil(sortedModels.length / pageSize);
86
 
87
+ const paginatedModels = sortedModels.slice(
88
  (currentPage - 1) * pageSize,
89
  currentPage * pageSize
90
  );
91
 
92
+ const handleOrderByClick = (column: 'all_children' | 'direct_children') => {
93
+ setOrderBy(column);
94
+ setCurrentPage(1);
95
+ };
96
+
97
  return (
98
+ <main className="container mx-auto py-8 bg-white dark:bg-gray-900 text-gray-900 dark:text-white">
99
  <h1 className="text-4xl font-bold mb-4">All Models</h1>
100
  <div className="mb-4">
101
  <input
 
103
  placeholder="Filter by model name"
104
  value={filterText}
105
  onChange={(e) => setFilterText(e.target.value)}
106
+ className="px-4 py-2 border border-gray-300 dark:border-gray-700 rounded-md bg-white dark:bg-gray-800 text-gray-900 dark:text-white"
107
  />
108
  </div>
109
+ {isLoading ? (
110
+ <p>Loading data...</p>
111
+ ) : paginatedModels.length > 0 ? (
112
  <>
113
  <table className="table-auto border-collapse w-full">
114
  <thead>
115
  <tr>
116
  <th className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-left">Model</th>
117
+ <th
118
+ className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-right cursor-pointer"
119
+ onClick={() => handleOrderByClick('direct_children')}
120
+ >
121
+ Direct Children {orderBy === 'direct_children' && '▼'}
122
+ </th>
123
+ <th
124
+ className="px-4 py-2 bg-gray-100 dark:bg-gray-800 text-right cursor-pointer"
125
+ onClick={() => handleOrderByClick('all_children')}
126
+ >
127
+ All Children {orderBy === 'all_children' && '▼'}
128
+ </th>
129
  </tr>
130
  </thead>
131
  <tbody>
 
138
  ))}
139
  </tbody>
140
  </table>
141
+ <div className="mt-4 flex items-center justify-between">
142
  <button
143
  onClick={() => setCurrentPage((prev) => Math.max(prev - 1, 1))}
144
  disabled={currentPage === 1}
145
+ className="px-4 py-2 bg-blue-500 dark:bg-blue-600 text-white rounded-md mr-2"
146
  >
147
  Previous
148
  </button>
149
+ <div className="flex items-center space-x-2">
150
+ {currentPage > 1 && (
151
+ <>
152
+ <button
153
+ onClick={() => setCurrentPage(1)}
154
+ className="px-2 py-1 bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200 rounded-md"
155
+ >
156
+ 1
157
+ </button>
158
+ {currentPage > 2 && <span className="text-gray-500">...</span>}
159
+ </>
160
+ )}
161
+ {[...Array(5)].map((_, i) => {
162
+ const page = currentPage + i - 2;
163
+ if (page >= 1 && page <= totalPages) {
164
+ return (
165
+ <button
166
+ key={page}
167
+ onClick={() => setCurrentPage(page)}
168
+ className={`px-2 py-1 ${
169
+ page === currentPage
170
+ ? 'bg-blue-500 dark:bg-blue-600 text-white'
171
+ : 'bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200'
172
+ } rounded-md`}
173
+ >
174
+ {page}
175
+ </button>
176
+ );
177
+ }
178
+ return null;
179
+ })}
180
+ {currentPage < totalPages && (
181
+ <>
182
+ {currentPage < totalPages - 1 && <span className="text-gray-500">...</span>}
183
+ <button
184
+ onClick={() => setCurrentPage(totalPages)}
185
+ className="px-2 py-1 bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-200 rounded-md"
186
+ >
187
+ {totalPages}
188
+ </button>
189
+ </>
190
+ )}
191
+ </div>
192
  <button
193
  onClick={() => setCurrentPage((prev) => Math.min(prev + 1, totalPages))}
194
  disabled={currentPage === totalPages}
195
+ className="px-4 py-2 bg-blue-500 dark:bg-blue-600 text-white rounded-md"
196
  >
197
  Next
198
  </button>