|
| 1 | +import React, { useMemo, useState } from 'react'; |
| 2 | +import { ThemeTable, ThemeTableUnavailable } from '../common/ThemeTable'; |
| 3 | +import { dates } from 'shared'; |
| 4 | +import { NoneText } from '../TestPlanVersionsPage'; |
| 5 | +import SortableTableHeader, { |
| 6 | + TABLE_SORT_ORDERS |
| 7 | +} from '../common/SortableTableHeader'; |
| 8 | +import FilterButtons from '../common/FilterButtons'; |
| 9 | +import { IssuePropType } from '../common/proptypes'; |
| 10 | +import PropTypes from 'prop-types'; |
| 11 | + |
| 12 | +const FILTER_OPTIONS = { |
| 13 | + OPEN: 'Open', |
| 14 | + CLOSED: 'Closed', |
| 15 | + ALL: 'All' |
| 16 | +}; |
| 17 | + |
| 18 | +const SORT_FIELDS = { |
| 19 | + AUTHOR: 'author', |
| 20 | + TITLE: 'title', |
| 21 | + STATUS: 'status', |
| 22 | + AT: 'at', |
| 23 | + CREATED_AT: 'createdAt', |
| 24 | + CLOSED_AT: 'closedAt' |
| 25 | +}; |
| 26 | + |
| 27 | +const SortableIssuesTable = ({ issues }) => { |
| 28 | + const [activeSort, setActiveSort] = useState(SORT_FIELDS.STATUS); |
| 29 | + const [sortOrder, setSortOrder] = useState(TABLE_SORT_ORDERS.ASC); |
| 30 | + const [activeFilter, setActiveFilter] = useState('OPEN'); |
| 31 | + |
| 32 | + const issueStats = useMemo(() => { |
| 33 | + const openIssues = issues.filter(issue => issue.isOpen).length; |
| 34 | + const closedIssues = issues.length - openIssues; |
| 35 | + return { openIssues, closedIssues }; |
| 36 | + }, [issues]); |
| 37 | + |
| 38 | + // Helper function to get sortable value from issue |
| 39 | + const getSortableValue = (issue, sortField) => { |
| 40 | + switch (sortField) { |
| 41 | + case SORT_FIELDS.AUTHOR: |
| 42 | + return issue.author; |
| 43 | + case SORT_FIELDS.TITLE: |
| 44 | + return issue.title; |
| 45 | + case SORT_FIELDS.AT: |
| 46 | + return issue.at?.name ?? ''; |
| 47 | + case SORT_FIELDS.CREATED_AT: |
| 48 | + return new Date(issue.createdAt); |
| 49 | + case SORT_FIELDS.CLOSED_AT: |
| 50 | + return issue.closedAt ? new Date(issue.closedAt) : new Date(0); |
| 51 | + default: |
| 52 | + return ''; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + const compareByStatus = (a, b) => { |
| 57 | + if (a.isOpen !== b.isOpen) { |
| 58 | + if (sortOrder === TABLE_SORT_ORDERS.ASC) { |
| 59 | + return a.isOpen ? -1 : 1; // Open first for ascending |
| 60 | + } |
| 61 | + return a.isOpen ? 1 : -1; // Closed first for descending |
| 62 | + } |
| 63 | + // If status is the same, sort by date created (newest first) |
| 64 | + return new Date(b.createdAt) - new Date(a.createdAt); |
| 65 | + }; |
| 66 | + |
| 67 | + const compareValues = (aValue, bValue) => { |
| 68 | + return sortOrder === TABLE_SORT_ORDERS.ASC |
| 69 | + ? aValue < bValue |
| 70 | + ? -1 |
| 71 | + : 1 |
| 72 | + : aValue > bValue |
| 73 | + ? -1 |
| 74 | + : 1; |
| 75 | + }; |
| 76 | + |
| 77 | + const sortedAndFilteredIssues = useMemo(() => { |
| 78 | + // Filter issues |
| 79 | + const filtered = |
| 80 | + activeFilter === 'ALL' |
| 81 | + ? issues |
| 82 | + : issues.filter(issue => issue.isOpen === (activeFilter === 'OPEN')); |
| 83 | + |
| 84 | + // Sort issues |
| 85 | + return filtered.sort((a, b) => { |
| 86 | + // Special handling for status sorting |
| 87 | + if (activeSort === SORT_FIELDS.STATUS) { |
| 88 | + return compareByStatus(a, b); |
| 89 | + } |
| 90 | + |
| 91 | + // Normal sorting for other fields |
| 92 | + const aValue = getSortableValue(a, activeSort); |
| 93 | + const bValue = getSortableValue(b, activeSort); |
| 94 | + return compareValues(aValue, bValue); |
| 95 | + }); |
| 96 | + }, [issues, activeSort, sortOrder, activeFilter]); |
| 97 | + |
| 98 | + const handleSort = column => newSortOrder => { |
| 99 | + setActiveSort(column); |
| 100 | + setSortOrder(newSortOrder); |
| 101 | + }; |
| 102 | + |
| 103 | + const renderTableHeader = () => ( |
| 104 | + <thead> |
| 105 | + <tr> |
| 106 | + {[ |
| 107 | + { field: SORT_FIELDS.AUTHOR, title: 'Author' }, |
| 108 | + { field: SORT_FIELDS.TITLE, title: 'Issue' }, |
| 109 | + { field: SORT_FIELDS.STATUS, title: 'Status' }, |
| 110 | + { field: SORT_FIELDS.AT, title: 'Assistive Technology' }, |
| 111 | + { field: SORT_FIELDS.CREATED_AT, title: 'Created On' }, |
| 112 | + { field: SORT_FIELDS.CLOSED_AT, title: 'Closed On' } |
| 113 | + ].map(({ field, title }) => ( |
| 114 | + <SortableTableHeader |
| 115 | + key={field} |
| 116 | + title={title} |
| 117 | + active={activeSort === field} |
| 118 | + onSort={handleSort(field)} |
| 119 | + data-test={`sort-${field.toLowerCase()}`} |
| 120 | + /> |
| 121 | + ))} |
| 122 | + </tr> |
| 123 | + </thead> |
| 124 | + ); |
| 125 | + |
| 126 | + const renderTableBody = () => ( |
| 127 | + <tbody> |
| 128 | + {sortedAndFilteredIssues.map(issue => ( |
| 129 | + <tr |
| 130 | + key={issue.link} |
| 131 | + data-test="issue-row" |
| 132 | + data-status={issue.isOpen ? 'open' : 'closed'} |
| 133 | + > |
| 134 | + <td> |
| 135 | + <a |
| 136 | + target="_blank" |
| 137 | + rel="noreferrer" |
| 138 | + href={`https://github.com/${issue.author}`} |
| 139 | + > |
| 140 | + {issue.author} |
| 141 | + </a> |
| 142 | + </td> |
| 143 | + <td> |
| 144 | + <a target="_blank" rel="noreferrer" href={issue.link}> |
| 145 | + {issue.title} |
| 146 | + </a> |
| 147 | + </td> |
| 148 | + <td data-test="issue-status">{issue.isOpen ? 'Open' : 'Closed'}</td> |
| 149 | + <td>{issue.at?.name ?? 'AT not specified'}</td> |
| 150 | + <td>{dates.convertDateToString(issue.createdAt, 'MMM D, YYYY')}</td> |
| 151 | + <td> |
| 152 | + {!issue.closedAt ? ( |
| 153 | + <NoneText>N/A</NoneText> |
| 154 | + ) : ( |
| 155 | + dates.convertDateToString(issue.closedAt, 'MMM D, YYYY') |
| 156 | + )} |
| 157 | + </td> |
| 158 | + </tr> |
| 159 | + ))} |
| 160 | + </tbody> |
| 161 | + ); |
| 162 | + |
| 163 | + return ( |
| 164 | + <> |
| 165 | + <h2 id="github-issues"> |
| 166 | + GitHub Issues ({issueStats.openIssues} open, {issueStats.closedIssues} |
| 167 | + closed) |
| 168 | + </h2> |
| 169 | + <FilterButtons |
| 170 | + filterLabel="Filter" |
| 171 | + filterAriaLabel="Filter GitHub issues" |
| 172 | + filterOptions={FILTER_OPTIONS} |
| 173 | + activeFilter={activeFilter} |
| 174 | + onFilterChange={setActiveFilter} |
| 175 | + /> |
| 176 | + {!sortedAndFilteredIssues.length ? ( |
| 177 | + <ThemeTableUnavailable aria-labelledby="github-issues"> |
| 178 | + No GitHub Issues |
| 179 | + </ThemeTableUnavailable> |
| 180 | + ) : ( |
| 181 | + <ThemeTable |
| 182 | + bordered |
| 183 | + aria-labelledby="github-issues" |
| 184 | + data-test="issues-table" |
| 185 | + > |
| 186 | + {renderTableHeader()} |
| 187 | + {renderTableBody()} |
| 188 | + </ThemeTable> |
| 189 | + )} |
| 190 | + </> |
| 191 | + ); |
| 192 | +}; |
| 193 | + |
| 194 | +SortableIssuesTable.propTypes = { |
| 195 | + issues: PropTypes.arrayOf(IssuePropType).isRequired |
| 196 | +}; |
| 197 | + |
| 198 | +export default SortableIssuesTable; |
0 commit comments