-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathNlpEntity.tsx
249 lines (242 loc) · 7.71 KB
/
NlpEntity.tsx
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
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import AddIcon from "@mui/icons-material/Add";
import DatasetLinkedIcon from "@mui/icons-material/DatasetLinked";
import DeleteIcon from "@mui/icons-material/Delete";
import { Button, Chip, Grid, Paper } from "@mui/material";
import { GridColDef, GridRowSelectionModel } from "@mui/x-data-grid";
import { useRouter } from "next/router";
import { useState } from "react";
import { DeleteDialog } from "@/app-components/dialogs";
import { FilterTextfield } from "@/app-components/inputs/FilterTextfield";
import {
ActionColumnLabel,
useActionColumns,
} from "@/app-components/tables/columns/getColumns";
import { renderHeader } from "@/app-components/tables/columns/renderHeader";
import { DataGrid } from "@/app-components/tables/DataGrid";
import { useDelete } from "@/hooks/crud/useDelete";
import { useDeleteMany } from "@/hooks/crud/useDeleteMany";
import { useFind } from "@/hooks/crud/useFind";
import { getDisplayDialogs, useDialog } from "@/hooks/useDialog";
import { useHasPermission } from "@/hooks/useHasPermission";
import { useSearch } from "@/hooks/useSearch";
import { useToast } from "@/hooks/useToast";
import { useTranslate } from "@/hooks/useTranslate";
import { PageHeader } from "@/layout/content/PageHeader";
import { EntityType, Format } from "@/services/types";
import { INlpEntity } from "@/types/nlp-entity.types";
import { PermissionAction } from "@/types/permission.types";
import { getDateTimeFormatter } from "@/utils/date";
import { NlpEntityDialog } from "./NlpEntityDialog";
const NlpEntity = () => {
const router = useRouter();
const deleteEntityDialogCtl = useDialog<string>(false);
const hasPermission = useHasPermission();
const editEntityDialogCtl = useDialog<INlpEntity>(false);
const { mutateAsync: deleteNlpEntity } = useDelete(EntityType.NLP_ENTITY, {
onError: () => {
toast.error(t("message.internal_server_error"));
},
onSuccess() {
deleteEntityDialogCtl.closeDialog();
toast.success(t("message.item_delete_success"));
},
});
const { mutateAsync: deleteNlpEntities } = useDeleteMany(
EntityType.NLP_ENTITY,
{
onError: (error) => {
toast.error(error);
},
onSuccess: () => {
deleteEntityDialogCtl.closeDialog();
setSelectedNlpEntities([]);
toast.success(t("message.item_delete_success"));
},
},
);
const [selectedNlpEntities, setSelectedNlpEntities] = useState<string[]>([]);
const addDialogCtl = useDialog<INlpEntity>(false);
const { t } = useTranslate();
const { toast } = useToast();
const { onSearch, searchPayload } = useSearch<INlpEntity>({
$or: ["name", "doc"],
});
const { dataGridProps: nlpEntityGrid } = useFind(
{
entity: EntityType.NLP_ENTITY,
format: Format.FULL,
},
{
params: searchPayload,
},
);
const actionEntityColumns = useActionColumns<INlpEntity>(
EntityType.NLP_ENTITY,
[
{
label: ActionColumnLabel.Values,
action: (row) =>
router.push(
{
pathname: "/nlp/entities/[id]/values",
query: { id: row.id },
},
undefined,
{
shallow: true,
scroll: false,
},
),
requires: [PermissionAction.READ],
},
{
label: ActionColumnLabel.Edit,
action: (row) => editEntityDialogCtl.openDialog(row),
requires: [PermissionAction.UPDATE],
},
{
label: ActionColumnLabel.Delete,
action: (row) => deleteEntityDialogCtl.openDialog(row.id),
requires: [PermissionAction.DELETE],
},
],
t("label.operations"),
);
const nlpEntityColumns: GridColDef<INlpEntity>[] = [
{
flex: 1,
field: "name",
headerName: t("label.name"),
sortable: true,
disableColumnMenu: true,
renderHeader,
},
{
flex: 2,
field: "doc",
headerName: t("label.doc"),
sortable: true,
disableColumnMenu: true,
renderHeader,
},
{
maxWidth: 210,
field: "lookups",
headerName: t("label.lookups"),
renderCell: (val) => <Chip label={val.value} variant="title" />,
sortable: true,
disableColumnMenu: true,
resizable: false,
renderHeader,
},
{
maxWidth: 90,
field: "builtin",
headerName: t("label.builtin"),
sortable: true,
disableColumnMenu: true,
resizable: false,
renderHeader,
},
{
maxWidth: 140,
field: "createdAt",
headerName: t("label.createdAt"),
disableColumnMenu: true,
renderHeader,
resizable: false,
headerAlign: "left",
valueGetter: (params) =>
t("datetime.created_at", getDateTimeFormatter(params)),
},
{
maxWidth: 140,
field: "updatedAt",
headerName: t("label.updatedAt"),
disableColumnMenu: true,
renderHeader,
resizable: false,
headerAlign: "left",
valueGetter: (params) =>
t("datetime.updated_at", getDateTimeFormatter(params)),
},
actionEntityColumns,
];
const handleSelectionChange = (selection: GridRowSelectionModel) => {
setSelectedNlpEntities(selection as string[]);
};
return (
<Grid container gap={3} flexDirection="column">
<NlpEntityDialog {...getDisplayDialogs(addDialogCtl)} />
<NlpEntityDialog {...editEntityDialogCtl} />
<DeleteDialog
{...deleteEntityDialogCtl}
callback={() => {
if (selectedNlpEntities.length > 0) {
deleteNlpEntities(selectedNlpEntities);
setSelectedNlpEntities([]);
deleteEntityDialogCtl.closeDialog();
} else if (deleteEntityDialogCtl.data) {
deleteNlpEntity(deleteEntityDialogCtl.data);
}
}}
/>
<PageHeader title={t("title.nlp_entities")} icon={DatasetLinkedIcon}>
<Grid
justifyContent="flex-end"
gap={1}
container
alignItems="center"
flexShrink={0}
width="max-content"
>
<Grid item>
<FilterTextfield onChange={onSearch} />
</Grid>
{hasPermission(EntityType.NLP_ENTITY, PermissionAction.CREATE) ? (
<Grid item>
<Button
startIcon={<AddIcon />}
variant="contained"
sx={{ float: "right" }}
onClick={() => addDialogCtl.openDialog()}
>
{t("button.add")}
</Button>
</Grid>
) : null}
{selectedNlpEntities.length > 0 && (
<Grid item>
<Button
startIcon={<DeleteIcon />}
variant="contained"
color="error"
onClick={() => deleteEntityDialogCtl.openDialog(undefined)}
>
{t("button.delete")}
</Button>
</Grid>
)}
</Grid>
</PageHeader>
<Grid item xs={12}>
<Paper sx={{ padding: 2 }}>
<DataGrid
columns={nlpEntityColumns}
{...nlpEntityGrid}
checkboxSelection
onRowSelectionModelChange={handleSelectionChange}
/>
</Paper>
</Grid>
</Grid>
);
};
export default NlpEntity;