-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAssignmentCodeCell.tsx
More file actions
82 lines (70 loc) · 2.02 KB
/
AssignmentCodeCell.tsx
File metadata and controls
82 lines (70 loc) · 2.02 KB
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
import classNames from 'classnames';
import { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { AssignmentsMap, SupportedAssignmentCode } from '@/lib/assignments';
import { getAssignmentColorClasses } from '@/lib/colors';
interface AssignmentCodeCellProps<T extends React.ElementType> {
children?: React.ReactNode;
className?: string;
assignmentCode?: SupportedAssignmentCode | string;
letter?: boolean;
as?: T;
border?: boolean;
grammar?: 'verb' | 'noun' | 'plural-noun';
count?: number;
}
export function AssignmentCodeCell<T extends React.ElementType = 'td'>({
children,
className,
assignmentCode,
letter = false,
as,
border = false,
grammar = 'noun',
count,
...props
}: AssignmentCodeCellProps<T> & React.ComponentProps<T>) {
const { t } = useTranslation();
const assignment = assignmentCode && AssignmentsMap[assignmentCode];
const content = useMemo(() => {
if (!assignmentCode) {
return '';
}
if (children) {
return children;
}
if (!assignment) {
return letter ? assignmentCode[0] : assignmentCode.split('-')[1];
}
if (letter) {
return assignment.letter;
}
const translationKey = `common.assignments.${assignmentCode}`;
if (grammar === 'plural-noun') {
return t(`${translationKey}.noun_other`);
}
if (grammar === 'verb') {
return t(`${translationKey}.verb`);
}
return t(`${translationKey}.noun`);
}, [assignment, assignmentCode, children, grammar, letter, t]);
const Component = as || 'td';
const colorClasses = assignmentCode ? getAssignmentColorClasses(assignmentCode) : null;
return (
<Component
className={classNames(
className,
colorClasses && !border && colorClasses.bgMuted,
colorClasses && border && [colorClasses.border, 'border-b-4'],
)}
{...props}>
{content}
{count ? (
<>
{' '}
<span className="type-body-sm">({count})</span>
</>
) : null}
</Component>
);
}