Presidentlin commited on
Commit
a7865fc
1 Parent(s): 35a1853
Files changed (1) hide show
  1. src/App.tsx +43 -31
src/App.tsx CHANGED
@@ -43,6 +43,7 @@ const App: React.FC = () => {
43
  const calculateComparison = (modelPrice: number, comparisonPrice: number): string => {
44
  return (((modelPrice - comparisonPrice) / comparisonPrice) * 100).toFixed(2)
45
  }
 
46
  const filteredData = data
47
  .filter((provider) => selectedProviders.length === 0 || selectedProviders.includes(provider.provider))
48
  .map((provider) => ({
@@ -51,36 +52,51 @@ const App: React.FC = () => {
51
  }))
52
  .filter((provider) => provider.models.length > 0)
53
 
54
- const sortedData = React.useMemo(() => {
55
- let sortableData = [...filteredData];
 
 
 
 
 
 
 
 
 
 
56
  if (sortConfig !== null) {
57
- if (sortConfig.key === 'provider') {
58
- sortableData.sort((a, b) => {
59
- if (a.provider < b.provider) {
60
- return sortConfig.direction === 'ascending' ? -1 : 1;
61
- }
62
- if (a.provider > b.provider) {
63
- return sortConfig.direction === 'ascending' ? 1 : -1;
64
- }
65
- return 0;
66
- });
67
- } else if (sortConfig.key === 'model' || sortConfig.key === 'inputPrice' || sortConfig.key === 'outputPrice') {
68
- sortableData.forEach(provider => {
69
- provider.models.sort((a, b) => {
70
- if (a[sortConfig.key] < b[sortConfig.key]) {
71
- return sortConfig.direction === 'ascending' ? -1 : 1;
72
- }
73
- if (a[sortConfig.key] > b[sortConfig.key]) {
74
- return sortConfig.direction === 'ascending' ? 1 : -1;
75
- }
76
- return 0;
77
- });
78
- });
79
- }
80
  }
81
  return sortableData;
82
  }, [filteredData, sortConfig]);
83
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  const requestSort = (key: string) => {
85
  let direction = 'ascending';
86
  if (sortConfig && sortConfig.key === key && sortConfig.direction === 'ascending') {
@@ -90,7 +106,6 @@ const App: React.FC = () => {
90
  };
91
 
92
 
93
- console.log(filteredData)
94
 
95
  const toggleProviderExpansion = (provider: string) => {
96
  setExpandedProviders((prev) => (prev.includes(provider) ? prev.filter((p) => p !== provider) : [...prev, provider]))
@@ -191,8 +206,8 @@ const App: React.FC = () => {
191
  </button>
192
  </TableHead>
193
  <TableHead>
194
- <button type="button" onClick={() => requestSort('model')}>
195
- Model {sortConfig?.key === 'model' ? (sortConfig.direction === 'ascending' ? '▲' : '▼') : null}
196
  </button>
197
  </TableHead>
198
  <TableHead>
@@ -304,9 +319,6 @@ const App: React.FC = () => {
304
  )}
305
  </TableBody>
306
  </Table>
307
-
308
-
309
-
310
  </CardContent>
311
  </Card>
312
  )
 
43
  const calculateComparison = (modelPrice: number, comparisonPrice: number): string => {
44
  return (((modelPrice - comparisonPrice) / comparisonPrice) * 100).toFixed(2)
45
  }
46
+
47
  const filteredData = data
48
  .filter((provider) => selectedProviders.length === 0 || selectedProviders.includes(provider.provider))
49
  .map((provider) => ({
 
52
  }))
53
  .filter((provider) => provider.models.length > 0)
54
 
55
+ const flattenData = (data) => {
56
+ return data.flatMap(provider =>
57
+ provider.models.map(model => ({
58
+ provider: provider.provider,
59
+ uri: provider.uri,
60
+ ...model
61
+ }))
62
+ );
63
+ }
64
+
65
+ const sortedFlattenedData = React.useMemo(() => {
66
+ let sortableData = flattenData(filteredData);
67
  if (sortConfig !== null) {
68
+ sortableData.sort((a, b) => {
69
+ if (a[sortConfig.key] < b[sortConfig.key]) {
70
+ return sortConfig.direction === 'ascending' ? -1 : 1;
71
+ }
72
+ if (a[sortConfig.key] > b[sortConfig.key]) {
73
+ return sortConfig.direction === 'ascending' ? 1 : -1;
74
+ }
75
+ return 0;
76
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  }
78
  return sortableData;
79
  }, [filteredData, sortConfig]);
80
 
81
+ const reassembleData = (sortedFlattenedData) => {
82
+ const reassembledData = sortedFlattenedData.reduce((acc, item) => {
83
+ const providerIndex = acc.findIndex(provider => provider.provider === item.provider);
84
+ if (providerIndex === -1) {
85
+ acc.push({
86
+ provider: item.provider,
87
+ uri: item.uri,
88
+ models: [{ name: item.name, inputPrice: item.inputPrice, outputPrice: item.outputPrice }]
89
+ });
90
+ } else {
91
+ acc[providerIndex].models.push({ name: item.name, inputPrice: item.inputPrice, outputPrice: item.outputPrice });
92
+ }
93
+ return acc;
94
+ }, []);
95
+ return reassembledData;
96
+ }
97
+
98
+ const sortedData = reassembleData(sortedFlattenedData);
99
+
100
  const requestSort = (key: string) => {
101
  let direction = 'ascending';
102
  if (sortConfig && sortConfig.key === key && sortConfig.direction === 'ascending') {
 
106
  };
107
 
108
 
 
109
 
110
  const toggleProviderExpansion = (provider: string) => {
111
  setExpandedProviders((prev) => (prev.includes(provider) ? prev.filter((p) => p !== provider) : [...prev, provider]))
 
206
  </button>
207
  </TableHead>
208
  <TableHead>
209
+ <button type="button" onClick={() => requestSort('name')}>
210
+ Model {sortConfig?.key === 'name' ? (sortConfig.direction === 'ascending' ? '▲' : '▼') : null}
211
  </button>
212
  </TableHead>
213
  <TableHead>
 
319
  )}
320
  </TableBody>
321
  </Table>
 
 
 
322
  </CardContent>
323
  </Card>
324
  )