import { Button } from '@components/common/ui/Button.js';
import { Input } from '@components/common/ui/Input.js';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow
} from '@components/common/ui/Table.js';
import { _ } from '@evershop/evershop/lib/locale/translate/_';
import React, { useEffect, useState } from 'react';
import type { ImportApi, IssuesPage } from '../ui/importApi.js';
import {
codesBySize,
noIssueFilters,
pageCount,
toQuery,
withFilter
} from '../ui/issueFilters.js';
import type { IssueFilters } from '../ui/issueFilters.js';
import { ChoiceSelect } from './ChoiceSelect.js';
import { formatNumber } from './format.js';
import { ToneIcon } from './Notice.js';
export interface ProblemsTableProps {
api: ImportApi;
issuesUrl: string;
}
// A type to filter by: a pill, so that it reads as something to press.
const pillClass =
'inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs transition-colors outline-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]';
/** The severity of a problem, by shape and colour (never colour alone) and for a screen reader. */
function Severity({ severity }: { severity: 'error' | 'warning' }) {
return (
<>
{severity === 'error' ? _('Error') : _('Warning')}
>
);
}
/**
* The problems of the job (ยง 8.2): the totals per type, three filters, and the
* rows a page at a time. The messages come from the server in the admin's
* language. The state of the filters is the table's own; a new filter goes back
* to the first page.
*/
export function ProblemsTable({ api, issuesUrl }: ProblemsTableProps) {
const [filters, setFilters] = useState(noIssueFilters);
const [column, setColumn] = useState('');
const [data, setData] = useState(null);
const [failed, setFailed] = useState(null);
useEffect(() => {
// The cleanup aborts the ask of the filters that were just replaced, and an
// aborted ask never shows: an older answer cannot replace a newer one.
const controller = new AbortController();
api
.issues(issuesUrl, toQuery(filters), controller.signal)
.then((result) => {
if (controller.signal.aborted) return;
if (result.ok) {
setData(result.data);
setFailed(null);
} else {
setFailed(result.error.message);
}
});
return () => controller.abort();
}, [api, issuesUrl, filters]);
// The totals per type are those of the whole job, whatever the filters are.
const types = codesBySize(data?.byCode ?? {});
const pages = data ? pageCount(data.total, data.limit) : 1;
return (
{_('Problems')}
{types.length > 0 && (
{_('Problems by type:')}
{types.map(({ code, count }) => (
))}