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
|
import { FunctionComponent, useCallback, useMemo } from "react";
import { Badge, Button, Group } from "@mantine/core";
import { faTrash, faWrench } from "@fortawesome/free-solid-svg-icons";
import { ColumnDef } from "@tanstack/react-table";
import { cloneDeep } from "lodash";
import { Action } from "@/components";
import {
anyCutoff,
ProfileEditModal,
} from "@/components/forms/ProfileEditForm";
import SimpleTable from "@/components/tables/SimpleTable";
import { useModals } from "@/modules/modals";
import { languageProfileKey } from "@/pages/Settings/keys";
import { useFormActions } from "@/pages/Settings/utilities/FormValues";
import { BuildKey, useArrayAction } from "@/utilities";
import { useLatestEnabledLanguages, useLatestProfiles } from ".";
const Table: FunctionComponent = () => {
const profiles = useLatestProfiles();
const languages = useLatestEnabledLanguages();
const nextProfileId = useMemo(
() =>
1 +
profiles.reduce<number>((val, prof) => Math.max(prof.profileId, val), 0),
[profiles],
);
const { setValue } = useFormActions();
const modals = useModals();
const submitProfiles = useCallback(
(list: Language.Profile[]) => {
setValue(list, languageProfileKey, (value) => JSON.stringify(value));
},
[setValue],
);
const updateProfile = useCallback(
(profile: Language.Profile) => {
const list = [...profiles];
const idx = list.findIndex((v) => v.profileId === profile.profileId);
if (idx !== -1) {
list[idx] = profile;
} else {
list.push(profile);
}
submitProfiles(list);
},
[profiles, submitProfiles],
);
const action = useArrayAction<Language.Profile>((fn) => {
const list = [...profiles];
submitProfiles(fn(list));
});
const columns = useMemo<ColumnDef<Language.Profile>[]>(
() => [
{
header: "Name",
accessorKey: "name",
},
{
header: "Languages",
accessorKey: "items",
cell: ({
row: {
original: { items, cutoff },
},
}) => {
return (
<Group gap="xs" wrap="nowrap">
{items.map((v) => {
const isCutoff = v.id === cutoff || cutoff === anyCutoff;
return (
<ItemBadge key={v.id} cutoff={isCutoff} item={v}></ItemBadge>
);
})}
</Group>
);
},
},
{
header: "Must contain",
accessorKey: "mustContain",
cell: ({
row: {
original: { mustContain },
},
}) => {
if (!mustContain) {
return null;
}
return (
<>
{mustContain.map((v, idx) => {
return (
<Badge key={BuildKey(idx, v)} color="gray">
{v}
</Badge>
);
})}
</>
);
},
},
{
header: "Must not contain",
accessorKey: "mustNotContain",
cell: ({
row: {
original: { mustNotContain },
},
}) => {
if (!mustNotContain) {
return null;
}
return (
<>
{mustNotContain.map((v, idx) => {
return (
<Badge key={BuildKey(idx, v)} color="gray">
{v}
</Badge>
);
})}
</>
);
},
},
{
id: "profileId",
cell: ({ row }) => {
const profile = row.original;
return (
<Group gap="xs" wrap="nowrap">
<Action
label="Edit Profile"
icon={faWrench}
c="gray"
onClick={() => {
modals.openContextModal(ProfileEditModal, {
languages,
profile: cloneDeep(profile),
onComplete: updateProfile,
});
}}
></Action>
<Action
label="Remove"
icon={faTrash}
c="red"
onClick={() => action.remove(row.index)}
></Action>
</Group>
);
},
},
],
// TODO: Optimize this
[action, languages, modals, updateProfile],
);
const canAdd = languages.length !== 0;
return (
<>
<SimpleTable columns={columns} data={[...profiles]}></SimpleTable>
<Button
fullWidth
disabled={!canAdd}
onClick={() => {
const profile = {
profileId: nextProfileId,
name: "",
items: [],
cutoff: null,
mustContain: [],
mustNotContain: [],
originalFormat: false,
};
modals.openContextModal(ProfileEditModal, {
languages,
profile,
onComplete: updateProfile,
});
}}
>
{canAdd ? "Add New Profile" : "No Enabled Languages"}
</Button>
</>
);
};
interface ItemProps {
item: Language.ProfileItem;
cutoff: boolean;
}
const ItemBadge: FunctionComponent<ItemProps> = ({ cutoff, item }) => {
const text = useMemo(() => {
let result = item.language;
if (item.hi === "True") {
result += ":HI";
} else if (item.forced === "True") {
result += ":Forced";
}
return result;
}, [item.hi, item.forced, item.language]);
return (
<Badge
title={cutoff ? "Ignore others if this one is available" : undefined}
color={cutoff ? "primary" : "secondary"}
>
{text}
</Badge>
);
};
export default Table;
|