📦 EqualifyEverything / equalify-v2-dashboard-mocks

📄 AuditDashboard.tsx · 329 lines
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329import React, { useState, useEffect } from 'react';
import { keepPreviousData, useQuery } from '@tanstack/react-query';
import { Link } from '@tanstack/react-router';
import { fetchAudits } from '../api/api';

// Search Options Component
const SearchOptions: React.FC<{
  options: AuditSearchOptions;
  onApply: (options: AuditSearchOptions) => void;
  isLoading: boolean;
}> = ({ options, onApply, isLoading }) => {
  const [localOptions, setLocalOptions] = useState<AuditSearchOptions>(options);
  
  // Reset local options when parent options change
  useEffect(() => {
    setLocalOptions(options);
  }, [options]);
  
  const handleChange = (field: keyof AuditSearchOptions, value: any) => {
    setLocalOptions(prev => ({ ...prev, [field]: value }));
  };

  // Conditionally show/hide options based on checkbox status
  const showOptionsStorageKey = 'showSearchOptions';
  const [isChecked, setIsChecked] = useState(() => {
    const savedState = localStorage.getItem(showOptionsStorageKey);
    return savedState !== null ? JSON.parse(savedState) : false;
  });

  useEffect(() => {
    localStorage.setItem(showOptionsStorageKey, JSON.stringify(isChecked));
  }, [isChecked, showOptionsStorageKey]);

  const handleCheckboxChange = () => {
    setIsChecked(!isChecked);
  };
  
  const sortableColumns: Array<{ key: keyof Audit; label: string }> = [
    { key: 'name', label: 'Audit Name' },
    { key: 'progress', label: 'Progress' },
    { key: 'created', label: 'Created' },
    { key: 'lastRun', label: 'Last Run' },
  ];
  
  const limitOptions = [5, 10, 20];
  
  return (
    <>
      <input 
        type="checkbox" 
        name="toggle" 
        id="toggle_view_options"
        checked={isChecked}
        onChange={handleCheckboxChange}
      />
      <label htmlFor="toggle_view_options">Show Search Options</label>
      <section id="search_options"
        aria-hidden={!isChecked}
        style={{ display: isChecked ? 'block' : 'none' }}
      >
        <h3>Search Options</h3>
        
        <form action={() => onApply(localOptions)}>
          {/* Name Filter */}
          <div role="group" aria-labelledby="filter-group">
            <h4 id="filter-group">Filter</h4>
            <label htmlFor="nameFilter">Audit Name: </label>
            <input
              type="text"
              id="nameFilter"
              value={localOptions.nameFilter}
              onChange={(e) => handleChange('nameFilter', e.target.value)}
              placeholder="Enter audit name..."
            />
          </div>
          
          {/* Sort Options */}
          <div role="group" aria-labelledby="sort-group">
            <h4 id="sort-group">Sort</h4>
            <div>
              {sortableColumns.map((column) => (
                <React.Fragment key={column.key}>
                  <input
                    type="radio"
                    id={`sort-${column.key}`}
                    name="sortBy"
                    checked={localOptions.sortBy === column.key}
                    onChange={() => handleChange('sortBy', column.key)}
                  />
                  <label htmlFor={`sort-${column.key}`}>
                    {column.label}
                  </label>
                </React.Fragment>
              ))}
            </div>
            
            <div>
              <input
                type="radio"
                id="sort-asc"
                name="sortDirection"
                checked={localOptions.sortDirection === 'asc'}
                onChange={() => handleChange('sortDirection', 'asc')}
              />
              <label htmlFor="sort-asc">
                Ascending
              </label>
              <input
                type="radio"
                id="sort-desc"
                name="sortDirection"
                checked={localOptions.sortDirection === 'desc'}
                onChange={() => handleChange('sortDirection', 'desc')}
              />
              <label htmlFor="sort-desc">
                Descending
              </label>
            </div>
          </div>
          
          {/* Limit Options */}
          <div role="group" aria-labelledby="limit-group">
            <h4 id="limit-group">Limit</h4>
            <label htmlFor="limit">Change Number of Audits Displayed: </label>
            <select
              id="limit"
              value={localOptions.limit}
              onChange={(e) => handleChange('limit', Number(e.target.value))}
            >
              {limitOptions.map((limit) => (
                <option key={limit} value={limit}>
                  Up to {limit}
                </option>
              ))}
            </select>
          </div>
          
          {/* Apply Button */}
          <button
            type="button"
            onClick={() => onApply(localOptions)}
            disabled={isLoading}
          >
            {isLoading ? 'Loading...' : 'Save Search Options'}
          </button>
        </form>
      </section>
    </>
  );
};

// Audit Table Component
const AuditTable: React.FC = () => {
  const [selectedAudits, setSelectedAudits] = useState<Set<string>>(new Set());
  const [selectAll, setSelectAll] = useState(false);
  const [searchOptions, setSearchOptions] = useState<AuditSearchOptions>({
    nameFilter: '',
    sortBy: 'created',
    sortDirection: 'desc',
    limit: 10,
  });
  
  // Using Tanstack Query with better loading state management
  const { 
    data: audits = [], 
    isLoading, 
    isFetching, 
    isError, 
    refetch 
  } = useQuery({
    queryKey: ['audits', searchOptions],
    queryFn: () => fetchAudits(searchOptions),
    placeholderData: keepPreviousData, // Keep showing the previous data while loading new data
    staleTime: 30000, // Consider data fresh for 30 seconds
  });
  
  // Only reset selections when data source actually changes (not during loading)
  useEffect(() => {
    if (!isFetching) {
      setSelectedAudits(new Set());
      setSelectAll(false);
    }
  }, [audits, isFetching]);
  
  const toggleSelectAll = () => {
    if (selectAll) {
      setSelectedAudits(new Set());
    } else {
      setSelectedAudits(new Set(audits.map(audit => audit.id)));
    }
    setSelectAll(!selectAll);
  };
  
  const toggleAuditSelection = (id: string) => {
    const newSelection = new Set(selectedAudits);
    if (newSelection.has(id)) {
      newSelection.delete(id);
    } else {
      newSelection.add(id);
    }
    setSelectedAudits(newSelection);
    setSelectAll(newSelection.size === audits.length);
  };
  
  const handleApplySearchOptions = (newOptions: AuditSearchOptions) => {
    setSearchOptions(newOptions);
  };
  
  const formatDate = (dateString: string) => {
    return new Date(dateString).toLocaleString();
  };
  
  if (isError) {
    return <div>Error loading audits. Please try again.</div>;
  }
  
  // Show loading indicator only on initial load, not on refetch
  const showInitialLoading = isLoading && !isFetching;
  
  return (
    <>
      <h2>All Audits</h2>
      
      <SearchOptions 
        options={searchOptions} 
        onApply={handleApplySearchOptions} 
        isLoading={isFetching}
      />

      <section>
        <h3>Audits Table</h3>
        
        {/* Show loading state indicator during fetch but keep table visible */}
        {isFetching && !isLoading && (
          <div>Refreshing data...</div>
        )}
        
        <div>
          {showInitialLoading ? (
            <div>Loading...</div>
          ) : (
            <>
              <input
                id='all-audits'
                type="checkbox"
                checked={selectAll}
                onChange={toggleSelectAll}
              />
              <label htmlFor="all-audits">Select All Audits</label>
              <table>
                <thead>
                  <tr>
                    <th></th>
                    <th>Name</th>
                    <th>Pages</th>
                    <th>Checks</th>
                    <th>Progress</th>
                    <th>Status</th>
                    <th>Created</th>
                    <th>Last Run</th>
                  </tr>
                </thead>
                <tbody>
                  {audits.map((audit) => (
                    <tr key={audit.id}>
                      <td>
                        <input
                          type="checkbox"
                          checked={selectedAudits.has(audit.id)}
                          onChange={() => toggleAuditSelection(audit.id)}
                        />
                      </td>
                      <td>
                        <Link 
                          to="/audits/$auditId" 
                          params={{ auditId: audit.id }}
                        >
                          {audit.name}
                        </Link>
                      </td>
                      <td>{audit.pages}</td>
                      <td>{audit.checks}</td>
                      <td>{audit.progress}%</td>
                      <td>{audit.status.charAt(0).toUpperCase() + audit.status.slice(1)}</td>
                      <td>{formatDate(audit.created)}</td>
                      <td>{formatDate(audit.lastRun)}</td>
                    </tr>
                  ))}
                  {audits.length === 0 && (
                    <tr>
                      <td colSpan={8}>
                        No audits found. Try adjusting your search criteria.
                      </td>
                    </tr>
                  )}
                </tbody>
              </table>
            </>
          )}
        </div>
        
        {selectedAudits.size > 0 && (
          <div>
            <p>
              {selectedAudits.size} {selectedAudits.size === 1 ? 'audit' : 'audits'} selected
            </p>
          </div>
        )}
      </section>
      <section>
        <h3>Manage Selected Audits</h3>
        <nav aria-label="Manage Selected Audits">
          <button>Run Audit</button>    
        </nav>
      </section>
    </>
  );
};

// Main Page Component for Tanstack Router
export const AuditDashboard: React.FC = () => {
  return (
    <>
      <AuditTable />
    </>
  );
};

export default AuditDashboard;