-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsites-table.tsx
146 lines (131 loc) · 4.65 KB
/
sites-table.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import React, { useState, useEffect } from 'react';
import Papa from 'papaparse';
import { Search } from 'lucide-react';
const SitesDashboard = () => {
const [sites, setSites] = useState([]);
const [searchTerm, setSearchTerm] = useState('');
const [sortConfig, setSortConfig] = useState({ key: 'Site name', direction: 'asc' });
useEffect(() => {
const loadData = async () => {
try {
const response = await window.fs.readFile('Sites_20241201194031240.csv', { encoding: 'utf8' });
const result = Papa.parse(response, {
header: true,
dynamicTyping: true,
skipEmptyLines: true
});
setSites(result.data);
} catch (error) {
console.error('Error loading data:', error);
}
};
loadData();
}, []);
const formatValue = (value) => {
if (value === null || value === undefined) return '-';
if (typeof value === 'boolean') return value ? 'Yes' : 'No';
if (typeof value === 'number') {
if (value === 0) return '0';
return value.toLocaleString();
}
return value;
};
const sortData = (data, sortConfig) => {
if (!sortConfig.key) return data;
return [...data].sort((a, b) => {
const aValue = a[sortConfig.key];
const bValue = b[sortConfig.key];
if (aValue === null || aValue === undefined) return sortConfig.direction === 'asc' ? 1 : -1;
if (bValue === null || bValue === undefined) return sortConfig.direction === 'asc' ? -1 : 1;
if (aValue < bValue) return sortConfig.direction === 'asc' ? -1 : 1;
if (aValue > bValue) return sortConfig.direction === 'asc' ? 1 : -1;
return 0;
});
};
const requestSort = (key) => {
const direction = sortConfig.key === key && sortConfig.direction === 'asc' ? 'desc' : 'asc';
setSortConfig({ key, direction });
};
const filteredSites = sites.filter(site =>
Object.values(site).some(value =>
String(value).toLowerCase().includes(searchTerm.toLowerCase())
)
);
const sortedSites = sortData(filteredSites, sortConfig);
const importantColumns = [
'Site name',
'URL',
'Primary admin',
'Template',
'Date created',
'Storage used (GB)',
'External sharing'
];
return (
<div className="w-full p-4 space-y-4">
<div className="flex items-center space-x-2 mb-4">
<Search className="w-4 h-4 text-gray-500" />
<input
type="text"
placeholder="Search sites..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="w-64 px-3 py-2 border rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
/>
</div>
<div className="overflow-x-auto rounded-lg border border-gray-200">
<table className="min-w-full divide-y divide-gray-200">
<thead className="bg-gray-50">
<tr>
{importantColumns.map((column) => (
<th
key={column}
onClick={() => requestSort(column)}
className="px-4 py-3 text-left text-sm font-medium text-gray-500 cursor-pointer hover:bg-gray-100"
>
<div className="flex items-center space-x-1">
<span>{column}</span>
{sortConfig.key === column && (
<span className="ml-1">
{sortConfig.direction === 'asc' ? '↑' : '↓'}
</span>
)}
</div>
</th>
))}
</tr>
</thead>
<tbody className="bg-white divide-y divide-gray-200">
{sortedSites.map((site, index) => (
<tr
key={index}
className="hover:bg-gray-50"
>
{importantColumns.map((column) => (
<td key={column} className="px-4 py-3 text-sm text-gray-500 whitespace-nowrap">
{column === 'URL' ? (
<a
href={site[column]}
target="_blank"
rel="noopener noreferrer"
className="text-blue-600 hover:underline"
>
{site[column]}
</a>
) : (
formatValue(site[column])
)}
</td>
))}
</tr>
))}
</tbody>
</table>
</div>
<div className="text-sm text-gray-500 mt-4">
Showing {sortedSites.length} of {sites.length} sites
</div>
</div>
);
};
export default SitesDashboard;