📦 EqualifyEverything / equalify-viewer

📄 TableSection.tsx · 376 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376import { useState, useMemo } from 'react';
import type { Blocker } from '../services/DataManager';
import { format, parseISO } from 'date-fns';
import { Search, ExternalLink, Eye, EyeOff, CheckSquare, Square, ChevronDown, ChevronRight, HelpCircle } from 'lucide-react';
import { parseMessages } from '../utils/MessageParser';

interface TableSectionProps {
    data: Blocker[];
    ignoredIds: string[];
    onToggleIgnore: (id: string) => void;
    onBulkIgnore: (ids: string[], ignore: boolean) => void;
}

type GroupingMode = 'message' | 'url' | 'none';

const TableSection = ({ data, ignoredIds, onToggleIgnore, onBulkIgnore }: TableSectionProps) => {
    const [search, setSearch] = useState('');
    const [showIgnored, setShowIgnored] = useState(false);
    const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
    const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
    const [groupingMode, setGroupingMode] = useState<GroupingMode>('message');

    // Filter Data
    const filteredData = useMemo(() => {
        return data.filter(item => {
            const isIgnored = ignoredIds.includes(item.id);
            if (!showIgnored && isIgnored) return false;
            if (showIgnored && !isIgnored) return false;

            const primaryMessage = item.messages?.[0] || '';
            const matchSearch =
                item.url.toLowerCase().includes(search.toLowerCase()) ||
                primaryMessage.toLowerCase().includes(search.toLowerCase());

            return matchSearch;
        });
    }, [data, ignoredIds, showIgnored, search]);

    // Grouping Logic
    const groupedData = useMemo(() => {
        if (groupingMode === 'none') {
            return { 'All Items': filteredData };
        }

        const groups: Record<string, Blocker[]> = {};
        filteredData.forEach(item => {
            let key = '';
            if (groupingMode === 'message') {
                key = item.messages?.[0] || 'Unknown Issue';
            } else if (groupingMode === 'url') {
                key = item.url || 'Unknown URL';
            }

            if (!groups[key]) groups[key] = [];
            groups[key].push(item);
        });
        return groups;
    }, [filteredData, groupingMode]);

    const sortedGroupKeys = useMemo(() => {
        const keys = Object.keys(groupedData);
        if (groupingMode === 'none') return keys;

        // Sort by count descending
        return keys.sort((a, b) => groupedData[b].length - groupedData[a].length);
    }, [groupedData, groupingMode]);

    // Selection Logic
    const handleSelectGroup = (key: string) => {
        const items = groupedData[key];
        const itemIds = items.map(d => d.id);
        const allSelected = itemIds.every(id => selectedIds.has(id));

        const next = new Set(selectedIds);
        itemIds.forEach(id => {
            if (allSelected) next.delete(id);
            else next.add(id);
        });
        setSelectedIds(next);
    };

    const handleSelectRow = (id: string) => {
        const next = new Set(selectedIds);
        if (next.has(id)) next.delete(id);
        else next.add(id);
        setSelectedIds(next);
    };

    const handleBulkAction = () => {
        onBulkIgnore(Array.from(selectedIds), !showIgnored);
        setSelectedIds(new Set());
    };

    const toggleExpand = (key: string) => {
        const next = new Set(expandedGroups);
        if (next.has(key)) next.delete(key);
        else next.add(key);
        setExpandedGroups(next);
    };

    const renderItemRow = (item: Blocker) => {
        const isSelected = selectedIds.has(item.id);
        const primaryMessage = item.messages?.[0] || 'Unknown Issue';

        return (
            <div key={item.id} style={{ padding: '1rem', borderBottom: '1px solid var(--neutral-100)', display: 'grid', gridTemplateColumns: '40px 1fr auto', gap: '1rem', background: 'white' }}>
                <div onClick={() => handleSelectRow(item.id)} style={{ cursor: 'pointer', paddingTop: '0.25rem' }}>
                    {isSelected ? <CheckSquare size={18} color="var(--color-primary-main)" /> : <Square size={18} color="var(--neutral-400)" />}
                </div>

                <div style={{ overflow: 'hidden' }}>
                    <div style={{ marginBottom: '0.5rem', display: 'flex', alignItems: 'center', gap: '0.5rem', flexWrap: 'wrap' }}>
                        {groupingMode !== 'url' && (
                            <a href={item.url} target="_blank" rel="noopener noreferrer" style={{ fontWeight: 500, fontSize: '0.9rem', display: 'flex', alignItems: 'center', gap: '0.25rem', color: 'var(--color-primary-main)' }}>
                                {item.url.length > 60 ? '...' + item.url.slice(-60) : item.url}
                                <ExternalLink size={12} />
                            </a>
                        )}
                        {groupingMode !== 'message' && (
                            <div style={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}>
                                {parseMessages(primaryMessage).map((v, i) => (
                                    <div key={i} style={{ display: 'flex', alignItems: 'center', gap: '0.4rem' }}>
                                        <span style={{
                                            fontSize: '0.7rem',
                                            color: 'var(--color-primary-main)',
                                            background: 'rgba(255, 69, 0, 0.1)',
                                            padding: '1px 5px',
                                            borderRadius: '4px',
                                            fontWeight: 600,
                                            whiteSpace: 'nowrap'
                                        }}>
                                            {v.rule}
                                        </span>
                                        <span style={{ fontSize: '0.8rem', color: 'var(--neutral-700)' }}>
                                            {v.description.length > 80 ? v.description.slice(0, 80) + '...' : v.description}
                                        </span>
                                    </div>
                                ))}
                            </div>
                        )}
                        <span style={{ fontSize: '0.75rem', color: 'var(--neutral-500)' }}>
                            {format(parseISO(item.created_at), 'MMM d, yyyy')}
                        </span>
                    </div>

                    {/* Code Snippet */}
                    <div style={{ background: 'var(--neutral-900)', color: 'var(--neutral-50)', padding: '0.75rem', borderRadius: '4px', fontFamily: 'monospace', fontSize: '0.8rem', overflowX: 'auto', marginBottom: '0.5rem' }}>
                        <code>{item.content}</code>
                    </div>

                    {/* Categories tag if available */}
                    {item.categories && item.categories.length > 0 && (
                        <div style={{ display: "flex", gap: "0.5rem", marginTop: "0.5rem" }}>
                            {item.categories.map(cat => (
                                <span key={cat} style={{ fontSize: '0.75rem', color: 'var(--neutral-600)', background: 'var(--neutral-100)', padding: '2px 4px', borderRadius: '4px' }}>
                                    {cat}
                                </span>
                            ))}
                        </div>
                    )}
                </div>

                <div>
                    <button className="btn btn-ghost" style={{ padding: '0.25rem 0.5rem', fontSize: '0.8rem' }} onClick={() => onToggleIgnore(item.id)}>
                        {showIgnored ? 'Restore' : 'Ignore'}
                    </button>
                </div>
            </div>
        );
    };

    return (
        <div className="card table-section">
            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '1.5rem', flexWrap: 'wrap', gap: '1rem' }}>
                <div style={{ display: 'flex', alignItems: 'center', gap: '1rem' }}>
                    <h3 style={{ fontSize: '1.125rem', fontWeight: 600, margin: 0 }}>Detailed Findings</h3>

                    {/* Visual Divider */}
                    <div style={{ width: '1px', height: '24px', background: 'var(--neutral-300)' }}></div>

                    {/* Grouping Toggle */}
                    <div style={{ display: 'flex', gap: '0.25rem', background: 'var(--neutral-100)', padding: '0.25rem', borderRadius: 'var(--radius-md)' }}>
                        <button
                            onClick={() => setGroupingMode('message')}
                            style={{
                                padding: '0.4rem 0.75rem',
                                background: groupingMode === 'message' ? 'white' : 'transparent',
                                borderRadius: '4px',
                                boxShadow: groupingMode === 'message' ? '0 1px 2px rgba(0,0,0,0.1)' : 'none',
                                border: 'none',
                                fontSize: '0.8rem',
                                fontWeight: 600,
                                color: groupingMode === 'message' ? 'var(--color-primary-main)' : 'var(--neutral-500)',
                                cursor: 'pointer',
                                transition: 'all 0.2s'
                            }}
                        >
                            Messages
                        </button>
                        <button
                            onClick={() => setGroupingMode('url')}
                            style={{
                                padding: '0.4rem 0.75rem',
                                background: groupingMode === 'url' ? 'white' : 'transparent',
                                borderRadius: '4px',
                                boxShadow: groupingMode === 'url' ? '0 1px 2px rgba(0,0,0,0.1)' : 'none',
                                border: 'none',
                                fontSize: '0.8rem',
                                fontWeight: 600,
                                color: groupingMode === 'url' ? 'var(--color-primary-main)' : 'var(--neutral-500)',
                                cursor: 'pointer',
                                transition: 'all 0.2s'
                            }}
                        >
                            URLs
                        </button>
                        <button
                            onClick={() => setGroupingMode('none')}
                            style={{
                                padding: '0.4rem 0.75rem',
                                background: groupingMode === 'none' ? 'white' : 'transparent',
                                borderRadius: '4px',
                                boxShadow: groupingMode === 'none' ? '0 1px 2px rgba(0,0,0,0.1)' : 'none',
                                border: 'none',
                                fontSize: '0.8rem',
                                fontWeight: 600,
                                color: groupingMode === 'none' ? 'var(--color-primary-main)' : 'var(--neutral-500)',
                                cursor: 'pointer',
                                transition: 'all 0.2s'
                            }}
                        >
                            None
                        </button>
                    </div>
                </div>

                <div style={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}>
                    <div style={{ position: 'relative' }}>
                        <Search size={16} style={{ position: 'absolute', left: '10px', top: '50%', transform: 'translateY(-50%)', color: 'var(--neutral-400)' }} />
                        <input
                            type="text"
                            placeholder="Search URL or messages..."
                            value={search}
                            onChange={e => setSearch(e.target.value)}
                            style={{ width: '250px', paddingLeft: '32px' }}
                        />
                    </div>
                    <button
                        className={`btn ${showIgnored ? 'btn-primary' : 'btn-outline'}`}
                        onClick={() => { setShowIgnored(!showIgnored); setSelectedIds(new Set()); }}
                    >
                        {showIgnored ? <Eye size={16} /> : <EyeOff size={16} />}
                        {showIgnored ? 'Viewing Ignored' : 'View Ignored'}
                    </button>
                </div>
            </div>

            {selectedIds.size > 0 && (
                <div style={{ padding: '0.75rem', background: 'var(--neutral-100)', borderRadius: 'var(--radius-md)', marginBottom: '1rem', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
                    <span style={{ fontWeight: 500, fontSize: '0.875rem' }}>{selectedIds.size} items selected</span>
                    <button className="btn btn-primary" style={{ padding: '0.4rem 0.8rem', fontSize: '0.8rem' }} onClick={handleBulkAction}>
                        {showIgnored ? 'Restore Selected' : 'Ignore Selected'}
                    </button>
                </div>
            )}

            <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
                {groupingMode === 'none' ? (
                    <div style={{ border: '1px solid var(--neutral-200)', borderRadius: 'var(--radius-md)', overflow: 'hidden' }}>
                        {sortedGroupKeys.length > 0 && groupedData[sortedGroupKeys[0]].map(item => renderItemRow(item))}
                        {sortedGroupKeys.length === 0 && (
                            <div style={{ padding: '2rem', textAlign: 'center', color: 'var(--neutral-500)' }}>
                                No findings matching your criteria.
                            </div>
                        )}
                    </div>
                ) : (
                    sortedGroupKeys.map(key => {
                        const items = groupedData[key];
                        const isExpanded = expandedGroups.has(key);
                        const allSelected = items.every(d => selectedIds.has(d.id));
                        const someSelected = items.some(d => selectedIds.has(d.id));
                        // parseMessages helper might expect just a string content, assuming key is the message string
                        const parsedViolations = parseMessages(key);

                        return (
                            <div key={key} style={{ border: '1px solid var(--neutral-200)', borderRadius: 'var(--radius-md)', overflow: 'hidden' }}>
                                <div
                                    style={{
                                        background: 'var(--neutral-50)',
                                        padding: '0.75rem',
                                        display: 'flex',
                                        alignItems: 'flex-start',
                                        gap: '1rem',
                                        cursor: 'pointer',
                                        borderBottom: isExpanded ? '1px solid var(--neutral-200)' : 'none'
                                    }}
                                    onClick={() => toggleExpand(key)}
                                >
                                    <div onClick={(e) => { e.stopPropagation(); handleSelectGroup(key); }} style={{ marginTop: '4px' }}>
                                        {allSelected ? <CheckSquare size={18} color="var(--color-primary-main)" /> :
                                            someSelected ? <CheckSquare size={18} color="var(--neutral-400)" opacity={0.5} /> :
                                                <Square size={18} color="var(--neutral-400)" />
                                        }
                                    </div>

                                    <div style={{ flex: 1 }}>
                                        {groupingMode === 'message' ? (
                                            <div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
                                                {parsedViolations.map((v, i) => (
                                                    <div key={i} style={{ display: 'flex', alignItems: 'flex-start', gap: '0.5rem', fontSize: '0.9rem' }}>
                                                        <div style={{
                                                            display: 'inline-block',
                                                            padding: '0.1rem 0.4rem',
                                                            background: 'rgba(255, 69, 0, 0.1)',
                                                            color: 'var(--color-primary-main)',
                                                            borderRadius: '4px',
                                                            fontSize: '0.75rem',
                                                            fontWeight: 600,
                                                            whiteSpace: 'nowrap',
                                                            marginTop: '2px'
                                                        }}>
                                                            {v.rule}
                                                        </div>
                                                        <span style={{ color: 'var(--neutral-700)' }}>
                                                            {v.description}
                                                        </span>
                                                        {v.helpUrl && (
                                                            <a href={v.helpUrl} target="_blank" rel="noopener noreferrer" onClick={e => e.stopPropagation()} style={{ color: 'var(--neutral-400)', marginTop: '2px' }}>
                                                                <HelpCircle size={14} />
                                                            </a>
                                                        )}
                                                    </div>
                                                ))}
                                            </div>
                                        ) : (
                                            // URL Grouping Header
                                            <div style={{ fontWeight: 600, fontSize: '0.95rem', color: 'var(--neutral-800)', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
                                                {key}
                                                <a href={key} target="_blank" rel="noopener noreferrer" onClick={e => e.stopPropagation()} style={{ color: 'var(--color-primary-main)' }}>
                                                    <ExternalLink size={14} />
                                                </a>
                                            </div>
                                        )}
                                    </div>

                                    <div style={{ display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
                                        <div style={{ fontSize: '0.8rem', color: 'var(--neutral-500)', whiteSpace: 'nowrap' }}>
                                            {items.length} {items.length === 1 ? 'instance' : 'instances'}
                                        </div>
                                        {isExpanded ? <ChevronDown size={18} /> : <ChevronRight size={18} />}
                                    </div>
                                </div>

                                {isExpanded && (
                                    <div style={{ background: 'white' }}>
                                        {items.map(item => renderItemRow(item))}
                                    </div>
                                )}
                            </div>
                        );
                    })
                )}

                {sortedGroupKeys.length === 0 && (
                    <div style={{ padding: '2rem', textAlign: 'center', color: 'var(--neutral-500)' }}>
                        No findings matching your criteria.
                    </div>
                )}
            </div>
        </div>
    );
};

export default TableSection;