@blocklet/labels 2.4.35 → 2.4.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/label/context.d.ts +3 -2
- package/dist/components/label/context.mjs +87 -0
- package/dist/components/label/index.mjs +5 -0
- package/dist/components/label/label-picker.d.ts +2 -1
- package/dist/components/label/label-picker.mjs +175 -0
- package/dist/components/label/label-tree.d.ts +2 -1
- package/dist/components/label/label-tree.mjs +124 -0
- package/dist/components/label/labels.d.ts +3 -3
- package/dist/components/label/labels.mjs +82 -0
- package/dist/components/label/types.mjs +0 -0
- package/dist/components/label/utils.mjs +52 -0
- package/dist/global.d.ts +1 -0
- package/dist/index.mjs +7 -0
- package/dist/label2/github-label-picker.d.ts +3 -2
- package/dist/label2/github-label-picker.mjs +372 -0
- package/dist/label2/labels-context.d.ts +8 -8
- package/dist/label2/labels-context.mjs +137 -0
- package/dist/label2/labels-input.d.ts +3 -2
- package/dist/label2/labels-input.mjs +54 -0
- package/dist/label2/labels2.d.ts +5 -5
- package/dist/label2/labels2.mjs +222 -0
- package/dist/label2/session.d.ts +1 -3
- package/dist/label2/session.mjs +17 -0
- package/dist/label2/tree.d.ts +1 -1
- package/dist/label2/tree.mjs +130 -0
- package/dist/request-cache.mjs +82 -0
- package/dist/test/fixtures/index.d.ts +1 -1
- package/dist/test/fixtures/index.mjs +73 -0
- package/dist/type-override.d.ts +7 -0
- package/dist/types.mjs +0 -0
- package/dist/vite-env.d.ts +1 -0
- package/package.json +11 -18
- package/dist/index.es.js +0 -1463
- package/dist/index.umd.js +0 -1450
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import {
|
|
4
|
+
Box,
|
|
5
|
+
Popper,
|
|
6
|
+
ClickAwayListener,
|
|
7
|
+
Autocomplete,
|
|
8
|
+
autocompleteClasses,
|
|
9
|
+
InputBase,
|
|
10
|
+
ButtonBase,
|
|
11
|
+
alpha
|
|
12
|
+
} from "@mui/material";
|
|
13
|
+
import TagPlusOutlineIcon from "virtual:icons/mdi/tag-plus-outline";
|
|
14
|
+
import { Fragment as Fragment2, useMemo, useState } from "react";
|
|
15
|
+
import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
|
|
16
|
+
import { LabelsContainer } from "./labels-context.mjs";
|
|
17
|
+
import { useSessionContext } from "./session.mjs";
|
|
18
|
+
import { LabelChip } from "./labels2.mjs";
|
|
19
|
+
function PopperComponent({
|
|
20
|
+
disablePortal,
|
|
21
|
+
anchorEl,
|
|
22
|
+
open,
|
|
23
|
+
...other
|
|
24
|
+
}) {
|
|
25
|
+
return /* @__PURE__ */ jsx(
|
|
26
|
+
Box,
|
|
27
|
+
{
|
|
28
|
+
...other,
|
|
29
|
+
sx: (theme) => ({
|
|
30
|
+
[`& .${autocompleteClasses.paper}`]: {
|
|
31
|
+
width: 360,
|
|
32
|
+
boxShadow: "none",
|
|
33
|
+
margin: 0,
|
|
34
|
+
color: "inherit",
|
|
35
|
+
fontSize: 13
|
|
36
|
+
},
|
|
37
|
+
[`& .${autocompleteClasses.listbox}`]: {
|
|
38
|
+
bgcolor: theme.palette.background.paper,
|
|
39
|
+
padding: 1,
|
|
40
|
+
[`& .${autocompleteClasses.option}`]: {
|
|
41
|
+
minHeight: "auto"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
[`&.${autocompleteClasses.popperDisablePortal}`]: {
|
|
45
|
+
position: "relative"
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
export function GithubLabelPicker({
|
|
52
|
+
value = [],
|
|
53
|
+
onChange,
|
|
54
|
+
noLabelsText,
|
|
55
|
+
buttonSx,
|
|
56
|
+
actions,
|
|
57
|
+
trigger,
|
|
58
|
+
multiple = true,
|
|
59
|
+
updateImmediately = true,
|
|
60
|
+
excludes = [],
|
|
61
|
+
disabled = false,
|
|
62
|
+
enableAccessControl = true,
|
|
63
|
+
disablePortal
|
|
64
|
+
}) {
|
|
65
|
+
const { t, locale } = useLocaleContext();
|
|
66
|
+
const { isAdmin, hasAnyPassport } = useSessionContext();
|
|
67
|
+
const {
|
|
68
|
+
flattenedLabels: labels,
|
|
69
|
+
getLabelsByIds,
|
|
70
|
+
loading,
|
|
71
|
+
createLabel,
|
|
72
|
+
recentLabels,
|
|
73
|
+
addRecentLabel
|
|
74
|
+
} = LabelsContainer.useContainer();
|
|
75
|
+
const [anchorEl, setAnchorEl] = useState(null);
|
|
76
|
+
const selectedLabelIds = value;
|
|
77
|
+
const selectedLabels = useMemo(() => getLabelsByIds(selectedLabelIds), [getLabelsByIds, selectedLabelIds]);
|
|
78
|
+
const [inputValue, setInputValue] = useState("");
|
|
79
|
+
const exists = useMemo(
|
|
80
|
+
() => labels.some((x) => x.data.getName(locale).toLowerCase() === inputValue.toLowerCase()),
|
|
81
|
+
[inputValue, labels, locale]
|
|
82
|
+
);
|
|
83
|
+
const filteredLabels = useMemo(() => {
|
|
84
|
+
const excludesSet = new Set(excludes);
|
|
85
|
+
return labels.filter((x) => !excludesSet.has(x.data.id));
|
|
86
|
+
}, [labels]);
|
|
87
|
+
const initialRecentLabels = useMemo(() => recentLabels, [anchorEl]);
|
|
88
|
+
const handleClick = (event) => {
|
|
89
|
+
if (disabled) {
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
event.preventDefault();
|
|
93
|
+
setAnchorEl(event.currentTarget);
|
|
94
|
+
};
|
|
95
|
+
const handleClose = () => {
|
|
96
|
+
if (!updateImmediately) {
|
|
97
|
+
onChange?.(selectedLabelIds);
|
|
98
|
+
}
|
|
99
|
+
setInputValue("");
|
|
100
|
+
if (anchorEl) {
|
|
101
|
+
anchorEl.focus();
|
|
102
|
+
}
|
|
103
|
+
setAnchorEl(null);
|
|
104
|
+
};
|
|
105
|
+
const handleCreate = async () => {
|
|
106
|
+
const { id: id2 } = await createLabel(inputValue);
|
|
107
|
+
if (!multiple) {
|
|
108
|
+
onChange?.([id2]);
|
|
109
|
+
} else if (updateImmediately) {
|
|
110
|
+
onChange([...selectedLabelIds, id2]);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const handleLabelSelectionChange = (newValue) => {
|
|
114
|
+
if (!multiple) {
|
|
115
|
+
onChange?.(newValue.length ? [newValue[newValue.length - 1]] : []);
|
|
116
|
+
setAnchorEl(null);
|
|
117
|
+
} else if (updateImmediately) {
|
|
118
|
+
onChange(newValue);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const open = Boolean(anchorEl);
|
|
122
|
+
const id = open ? "github-label-picker" : void 0;
|
|
123
|
+
const getSelectedSx = (selected) => {
|
|
124
|
+
if (selected) {
|
|
125
|
+
return {
|
|
126
|
+
bgcolor: (theme) => `${theme.palette.primary.main} !important`,
|
|
127
|
+
color: (theme) => `${theme.palette.primary.contrastText} !important`,
|
|
128
|
+
WebkitTextFillColor: (theme) => `${theme.palette.primary.contrastText} !important`
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
return void 0;
|
|
132
|
+
};
|
|
133
|
+
const isLabelPermitted = (node) => {
|
|
134
|
+
const label = node.data;
|
|
135
|
+
const passportsToCheck = [...label.passports || [], ...label.type === "system" ? ["admin", "owner"] : []];
|
|
136
|
+
const isPermitted = !enableAccessControl || isAdmin || hasAnyPassport(passportsToCheck);
|
|
137
|
+
return isPermitted;
|
|
138
|
+
};
|
|
139
|
+
if (loading) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
function renderRecentLabels() {
|
|
143
|
+
const recentLabelNodes = getLabelsByIds(initialRecentLabels).filter(isLabelPermitted);
|
|
144
|
+
if (!recentLabelNodes?.length) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return /* @__PURE__ */ jsxs("li", { children: [
|
|
148
|
+
/* @__PURE__ */ jsx(Box, { sx: { mb: 0.5, fontSize: 12, fontWeight: "medium" }, children: t("label.recently") }),
|
|
149
|
+
/* @__PURE__ */ jsx(Box, { sx: { display: "flex", flexWrap: "wrap", gap: 0.75, mb: 2 }, children: recentLabelNodes.map((x) => {
|
|
150
|
+
const labelId = x.data.id;
|
|
151
|
+
const selected = value?.includes(labelId);
|
|
152
|
+
return /* @__PURE__ */ jsx(
|
|
153
|
+
LabelChip,
|
|
154
|
+
{
|
|
155
|
+
label: x,
|
|
156
|
+
fullName: false,
|
|
157
|
+
onClick: () => {
|
|
158
|
+
handleLabelSelectionChange(
|
|
159
|
+
selected ? selectedLabelIds.filter((y) => y !== labelId) : [...selectedLabelIds, labelId]
|
|
160
|
+
);
|
|
161
|
+
},
|
|
162
|
+
sx: {
|
|
163
|
+
height: 24,
|
|
164
|
+
lineHeight: "24px",
|
|
165
|
+
".MuiChip-label": { maxHeight: 24 },
|
|
166
|
+
...getSelectedSx(selected)
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
labelId
|
|
170
|
+
);
|
|
171
|
+
}) })
|
|
172
|
+
] });
|
|
173
|
+
}
|
|
174
|
+
function renderGroup(params) {
|
|
175
|
+
return /* @__PURE__ */ jsxs("li", { children: [
|
|
176
|
+
/* @__PURE__ */ jsx(Box, { sx: { mb: 0.5, fontSize: 12, fontWeight: "medium" }, children: t("label.title") }),
|
|
177
|
+
/* @__PURE__ */ jsx(
|
|
178
|
+
Box,
|
|
179
|
+
{
|
|
180
|
+
component: "ul",
|
|
181
|
+
sx: {
|
|
182
|
+
display: "flex",
|
|
183
|
+
justifyContent: "flex-start",
|
|
184
|
+
flexWrap: "wrap",
|
|
185
|
+
gap: 0.75,
|
|
186
|
+
p: 0
|
|
187
|
+
},
|
|
188
|
+
children: params.children
|
|
189
|
+
}
|
|
190
|
+
)
|
|
191
|
+
] });
|
|
192
|
+
}
|
|
193
|
+
function renderAutocomplete() {
|
|
194
|
+
return /* @__PURE__ */ jsx(
|
|
195
|
+
Autocomplete,
|
|
196
|
+
{
|
|
197
|
+
open: true,
|
|
198
|
+
multiple: true,
|
|
199
|
+
inputValue,
|
|
200
|
+
onInputChange: (event, newInputValue, reason) => {
|
|
201
|
+
if (reason === "reset") {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
setInputValue(newInputValue);
|
|
205
|
+
},
|
|
206
|
+
onClose: (event, reason) => {
|
|
207
|
+
if (reason === "escape") {
|
|
208
|
+
handleClose();
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
value: selectedLabels,
|
|
212
|
+
onChange: (event, newValue, reason) => {
|
|
213
|
+
event.preventDefault();
|
|
214
|
+
if (event.type === "keydown" && event.key === "Backspace" && reason === "removeOption") {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
if (reason === "selectOption") {
|
|
218
|
+
addRecentLabel(newValue[newValue.length - 1].data.id);
|
|
219
|
+
}
|
|
220
|
+
handleLabelSelectionChange(newValue.map((x) => x.data.id));
|
|
221
|
+
},
|
|
222
|
+
disableCloseOnSelect: true,
|
|
223
|
+
renderTags: () => null,
|
|
224
|
+
noOptionsText: noLabelsText || "No labels",
|
|
225
|
+
renderOption: (props, option, { selected }) => {
|
|
226
|
+
const isPermitted = isLabelPermitted(option);
|
|
227
|
+
if (!isPermitted) {
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
const label = option.data;
|
|
231
|
+
return /* @__PURE__ */ createElement(Box, { component: "li", ...props, key: label.id, sx: { "&.MuiAutocomplete-option": { p: "0 !important" } } }, /* @__PURE__ */ jsx(
|
|
232
|
+
LabelChip,
|
|
233
|
+
{
|
|
234
|
+
label: option,
|
|
235
|
+
sx: {
|
|
236
|
+
height: 24,
|
|
237
|
+
lineHeight: "24px",
|
|
238
|
+
".MuiChip-label": { maxHeight: 24 },
|
|
239
|
+
...getSelectedSx(selected)
|
|
240
|
+
},
|
|
241
|
+
fullName: false
|
|
242
|
+
}
|
|
243
|
+
));
|
|
244
|
+
},
|
|
245
|
+
groupBy: () => "x",
|
|
246
|
+
renderGroup: (params) => {
|
|
247
|
+
return /* @__PURE__ */ jsxs(Fragment2, { children: [
|
|
248
|
+
renderRecentLabels(),
|
|
249
|
+
renderGroup(params)
|
|
250
|
+
] }, params.key);
|
|
251
|
+
},
|
|
252
|
+
options: filteredLabels,
|
|
253
|
+
getOptionLabel: (option) => option.data.getName(locale),
|
|
254
|
+
renderInput: (params) => /* @__PURE__ */ jsx(
|
|
255
|
+
Box,
|
|
256
|
+
{
|
|
257
|
+
component: InputBase,
|
|
258
|
+
ref: params.InputProps.ref,
|
|
259
|
+
inputProps: params.inputProps,
|
|
260
|
+
autoFocus: true,
|
|
261
|
+
placeholder: "Filter labels",
|
|
262
|
+
sx: (theme) => ({
|
|
263
|
+
padding: 1,
|
|
264
|
+
width: "100%",
|
|
265
|
+
bgcolor: "background.paper",
|
|
266
|
+
"& input": {
|
|
267
|
+
borderRadius: 1,
|
|
268
|
+
backgroundColor: "background.paper",
|
|
269
|
+
padding: 1,
|
|
270
|
+
transition: theme.transitions.create(["border-color", "box-shadow"]),
|
|
271
|
+
border: `1px solid ${theme.palette.divider}`,
|
|
272
|
+
fontSize: 14,
|
|
273
|
+
"&:focus": {
|
|
274
|
+
boxShadow: `0px 0px 0px 3px ${alpha(theme.palette.primary.light, 0.4)}`,
|
|
275
|
+
borderColor: theme.palette.primary.light
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
),
|
|
281
|
+
slots: {
|
|
282
|
+
popper: PopperComponent
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
function renderActions() {
|
|
288
|
+
return /* @__PURE__ */ jsxs(Box, { children: [
|
|
289
|
+
isAdmin && inputValue && !exists && /* @__PURE__ */ jsx(
|
|
290
|
+
Box,
|
|
291
|
+
{
|
|
292
|
+
sx: {
|
|
293
|
+
display: "flex",
|
|
294
|
+
alignItems: "center",
|
|
295
|
+
gap: 1,
|
|
296
|
+
width: "100%",
|
|
297
|
+
height: 36,
|
|
298
|
+
px: 2,
|
|
299
|
+
borderTop: "1px solid",
|
|
300
|
+
borderColor: "divider",
|
|
301
|
+
fontSize: 14,
|
|
302
|
+
color: "grey.600",
|
|
303
|
+
cursor: "pointer",
|
|
304
|
+
textDecoration: "none"
|
|
305
|
+
},
|
|
306
|
+
onClick: handleCreate,
|
|
307
|
+
children: /* @__PURE__ */ jsxs("span", { children: [
|
|
308
|
+
'Create new label "',
|
|
309
|
+
inputValue,
|
|
310
|
+
'"'
|
|
311
|
+
] })
|
|
312
|
+
}
|
|
313
|
+
),
|
|
314
|
+
actions
|
|
315
|
+
] });
|
|
316
|
+
}
|
|
317
|
+
const labelInput2 = trigger;
|
|
318
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
319
|
+
/* @__PURE__ */ jsx(Box, { onClick: handleClick, sx: { display: "flex", alignItems: "center" }, children: labelInput2 || /* @__PURE__ */ jsx(
|
|
320
|
+
Box,
|
|
321
|
+
{
|
|
322
|
+
component: ButtonBase,
|
|
323
|
+
disableRipple: true,
|
|
324
|
+
"aria-describedby": id,
|
|
325
|
+
className: "label-picker-trigger",
|
|
326
|
+
sx: [
|
|
327
|
+
(theme) => ({
|
|
328
|
+
width: "100%",
|
|
329
|
+
fontSize: 13,
|
|
330
|
+
color: theme.palette.mode === "light" ? "#586069" : "#8b949e",
|
|
331
|
+
fontWeight: 600,
|
|
332
|
+
"&:hover": {
|
|
333
|
+
color: theme.palette.mode === "light" ? "primary.main" : "#58a6ff"
|
|
334
|
+
},
|
|
335
|
+
"& svg": {
|
|
336
|
+
width: 22,
|
|
337
|
+
height: 22
|
|
338
|
+
}
|
|
339
|
+
}),
|
|
340
|
+
...Array.isArray(buttonSx) ? buttonSx : [buttonSx]
|
|
341
|
+
],
|
|
342
|
+
children: /* @__PURE__ */ jsx(TagPlusOutlineIcon, {})
|
|
343
|
+
}
|
|
344
|
+
) }),
|
|
345
|
+
/* @__PURE__ */ jsx(
|
|
346
|
+
Box,
|
|
347
|
+
{
|
|
348
|
+
component: Popper,
|
|
349
|
+
id,
|
|
350
|
+
open,
|
|
351
|
+
anchorEl,
|
|
352
|
+
placement: "bottom-start",
|
|
353
|
+
disablePortal,
|
|
354
|
+
sx: (theme) => ({
|
|
355
|
+
border: `1px solid ${theme.palette.divider}`,
|
|
356
|
+
boxShadow: `0 8px 24px ${theme.palette.mode === "light" ? "rgba(149, 157, 165, 0.2)" : "rgb(1, 4, 9)"}`,
|
|
357
|
+
borderRadius: 1,
|
|
358
|
+
width: 360,
|
|
359
|
+
overflow: "hidden",
|
|
360
|
+
zIndex: theme.zIndex.modal,
|
|
361
|
+
fontSize: 13,
|
|
362
|
+
color: "text.secondary",
|
|
363
|
+
backgroundColor: "background.paper"
|
|
364
|
+
}),
|
|
365
|
+
children: /* @__PURE__ */ jsx(ClickAwayListener, { onClickAway: handleClose, children: /* @__PURE__ */ jsxs(Box, { onClick: (e) => e.preventDefault(), children: [
|
|
366
|
+
renderAutocomplete(),
|
|
367
|
+
renderActions()
|
|
368
|
+
] }) })
|
|
369
|
+
}
|
|
370
|
+
)
|
|
371
|
+
] });
|
|
372
|
+
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
import { LabelTreeNode } from './tree';
|
|
2
|
-
import { CreateLabel, LabelPayload, LabelsResponse } from '../types';
|
|
2
|
+
import type { CreateLabel, LabelPayload, LabelsResponse } from '../types';
|
|
3
3
|
interface InitialState {
|
|
4
4
|
loading: boolean;
|
|
5
5
|
data?: LabelsResponse;
|
|
6
6
|
createLabel: CreateLabel;
|
|
7
7
|
}
|
|
8
|
-
export declare const LabelsContainer: import(
|
|
8
|
+
export declare const LabelsContainer: import("unstated-next").Container<{
|
|
9
9
|
flattenedLabels: LabelTreeNode[];
|
|
10
10
|
popularLabels: LabelTreeNode[];
|
|
11
11
|
counts: Record<string, number>;
|
|
12
12
|
getLabelsByIds: (ids: string[]) => LabelTreeNode[];
|
|
13
13
|
labelExists: (id: string) => true;
|
|
14
14
|
getLabelName: (id: string) => string;
|
|
15
|
-
getRelatedLabels: (id: string) => import(
|
|
15
|
+
getRelatedLabels: (id: string) => import("./tree").TreeNode<{
|
|
16
16
|
id: string;
|
|
17
17
|
name: string;
|
|
18
18
|
desc?: string | undefined;
|
|
19
19
|
image?: string | undefined;
|
|
20
20
|
icon?: string | undefined;
|
|
21
|
-
type?: import(
|
|
21
|
+
type?: import("../types").LabelType;
|
|
22
22
|
color: string;
|
|
23
23
|
passports: string[];
|
|
24
24
|
translation?: {
|
|
@@ -29,13 +29,13 @@ export declare const LabelsContainer: import('unstated-next').Container<{
|
|
|
29
29
|
}): any;
|
|
30
30
|
getName(locale: string): string;
|
|
31
31
|
}>[];
|
|
32
|
-
getRecommended: (id?: string) => import(
|
|
32
|
+
getRecommended: (id?: string) => import("./tree").TreeNode<{
|
|
33
33
|
id: string;
|
|
34
34
|
name: string;
|
|
35
35
|
desc?: string | undefined;
|
|
36
36
|
image?: string | undefined;
|
|
37
37
|
icon?: string | undefined;
|
|
38
|
-
type?: import(
|
|
38
|
+
type?: import("../types").LabelType;
|
|
39
39
|
color: string;
|
|
40
40
|
passports: string[];
|
|
41
41
|
translation?: {
|
|
@@ -52,7 +52,7 @@ export declare const LabelsContainer: import('unstated-next').Container<{
|
|
|
52
52
|
desc?: string | undefined;
|
|
53
53
|
image?: string | undefined;
|
|
54
54
|
icon?: string | undefined;
|
|
55
|
-
type?: import(
|
|
55
|
+
type?: import("../types").LabelType;
|
|
56
56
|
color: string;
|
|
57
57
|
passports: string[];
|
|
58
58
|
translation?: {
|
|
@@ -69,7 +69,7 @@ export declare const LabelsContainer: import('unstated-next').Container<{
|
|
|
69
69
|
getFullLabelName: (id: string) => string;
|
|
70
70
|
parseLabelsStr: (labelIdsStr: string) => string[];
|
|
71
71
|
recentLabels: string[];
|
|
72
|
-
setRecentLabels: (this: unknown, value: import(
|
|
72
|
+
setRecentLabels: (this: unknown, value: import("ahooks/lib/createUseStorageState").SetState<string[]>) => void;
|
|
73
73
|
addRecentLabel: (label?: string) => void;
|
|
74
74
|
loading: boolean;
|
|
75
75
|
tree: LabelTreeNode;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { createContainer } from "unstated-next";
|
|
2
|
+
import { useLocalStorageState, useReactive } from "ahooks";
|
|
3
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
4
|
+
import { useLocaleContext } from "@arcblock/ux/lib/Locale/context";
|
|
5
|
+
import { LabelTreeNode, createEmptyLabelTree, initLabelTree } from "./tree.mjs";
|
|
6
|
+
const useLabels = ({ loading, data, createLabel: createLabelAPI }) => {
|
|
7
|
+
const { locale } = useLocaleContext();
|
|
8
|
+
const state = useReactive({
|
|
9
|
+
loading: true,
|
|
10
|
+
tree: createEmptyLabelTree(),
|
|
11
|
+
stats: []
|
|
12
|
+
});
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
try {
|
|
15
|
+
if (data && !loading) {
|
|
16
|
+
const { labels, stats } = data;
|
|
17
|
+
const tree = initLabelTree(labels || []);
|
|
18
|
+
state.loading = false;
|
|
19
|
+
state.tree = tree;
|
|
20
|
+
state.stats = stats.sort((a, b) => b.count - a.count);
|
|
21
|
+
}
|
|
22
|
+
} catch (e) {
|
|
23
|
+
console.error(e);
|
|
24
|
+
state.loading = false;
|
|
25
|
+
}
|
|
26
|
+
}, [data, loading]);
|
|
27
|
+
const flattened = useMemo(() => state.tree.flatten(), [state.tree]);
|
|
28
|
+
const counts = useMemo(
|
|
29
|
+
() => state.stats.reduce((acc, cur) => ({ ...acc, [cur.id]: cur.count }), {}),
|
|
30
|
+
[state.stats]
|
|
31
|
+
);
|
|
32
|
+
const nodesKeyById = useMemo(() => {
|
|
33
|
+
return flattened.reduce((acc, cur) => ({ ...acc, [cur.data.id]: cur }), {});
|
|
34
|
+
}, [flattened]);
|
|
35
|
+
const popularLabels = useMemo(
|
|
36
|
+
() => state.stats.slice(0, 12).map((x) => nodesKeyById[x.id]),
|
|
37
|
+
[nodesKeyById, state.stats]
|
|
38
|
+
);
|
|
39
|
+
const getLabelsByIds = useCallback(
|
|
40
|
+
(ids) => {
|
|
41
|
+
return ids.map((id) => nodesKeyById[id]).filter(Boolean);
|
|
42
|
+
},
|
|
43
|
+
[nodesKeyById]
|
|
44
|
+
);
|
|
45
|
+
const labelExists = useCallback((id) => !!nodesKeyById[id], [nodesKeyById]);
|
|
46
|
+
const getLabelName = (id) => {
|
|
47
|
+
return nodesKeyById[id]?.data.getName(locale);
|
|
48
|
+
};
|
|
49
|
+
const getRelatedLabels = (id) => {
|
|
50
|
+
const node = nodesKeyById[id];
|
|
51
|
+
if (!node || node.parent === state.tree) {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
const siblings = node.getAllSiblings();
|
|
55
|
+
return siblings;
|
|
56
|
+
};
|
|
57
|
+
const getRecommended = (id) => {
|
|
58
|
+
const related = id ? getRelatedLabels(id) : [];
|
|
59
|
+
const set = new Set([...related.map((x) => x.data.id), id].filter(Boolean));
|
|
60
|
+
return [...related, ...popularLabels.filter((x) => !set.has(x.data.id))].filter((x) => !!counts[x.data.id]);
|
|
61
|
+
};
|
|
62
|
+
const createLabel = async (name) => {
|
|
63
|
+
const saved = await createLabelAPI({ name });
|
|
64
|
+
const node = new LabelTreeNode({ data: new LabelTreeNode.Label(saved) });
|
|
65
|
+
state.tree.add(node);
|
|
66
|
+
state.tree = state.tree.clone();
|
|
67
|
+
return node.data;
|
|
68
|
+
};
|
|
69
|
+
const addLabelNode = (payload) => {
|
|
70
|
+
const parent = payload.parentId ? nodesKeyById[payload.parentId] : state.tree;
|
|
71
|
+
const node = new LabelTreeNode({ data: new LabelTreeNode.Label(payload) });
|
|
72
|
+
parent.add(node);
|
|
73
|
+
state.tree = state.tree.clone();
|
|
74
|
+
};
|
|
75
|
+
const updateLabelNode = (id, payload) => {
|
|
76
|
+
const existing = nodesKeyById[id];
|
|
77
|
+
if (existing && existing.parent) {
|
|
78
|
+
const node = new LabelTreeNode({ data: new LabelTreeNode.Label(payload) });
|
|
79
|
+
node.add(...existing.children);
|
|
80
|
+
if (existing.parent.data.id == payload.parentId) {
|
|
81
|
+
existing.parent.children = existing.parent.children.map((x) => {
|
|
82
|
+
if (x.data.id === id) {
|
|
83
|
+
node.setParent(existing.parent);
|
|
84
|
+
return node;
|
|
85
|
+
}
|
|
86
|
+
return x;
|
|
87
|
+
});
|
|
88
|
+
} else {
|
|
89
|
+
const parent = nodesKeyById[payload.parentId];
|
|
90
|
+
if (parent) {
|
|
91
|
+
existing.parent.children = existing.parent.children.filter((x) => x.data.id !== id);
|
|
92
|
+
parent.add(node);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
state.tree = state.tree.clone();
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const removeLabelNode = (id) => {
|
|
99
|
+
const node = nodesKeyById[id];
|
|
100
|
+
if (node?.parent) {
|
|
101
|
+
node.parent.removeChild(node);
|
|
102
|
+
state.tree = state.tree.clone();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const getFullLabelName = (id) => {
|
|
106
|
+
return nodesKeyById[id]?.getFullName(locale) ?? id;
|
|
107
|
+
};
|
|
108
|
+
const parseLabelsStr = useCallback((labelIdsStr) => {
|
|
109
|
+
return labelIdsStr?.split(",").filter(Boolean) || [];
|
|
110
|
+
}, []);
|
|
111
|
+
const [recentLabels = [], setRecentLabels] = useLocalStorageState("recent-labels", { defaultValue: [] });
|
|
112
|
+
const addRecentLabel = (label) => {
|
|
113
|
+
if (!label || labelExists(label)) return;
|
|
114
|
+
setRecentLabels([label, ...recentLabels].slice(0, 10));
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
...state,
|
|
118
|
+
flattenedLabels: flattened,
|
|
119
|
+
popularLabels,
|
|
120
|
+
counts,
|
|
121
|
+
getLabelsByIds,
|
|
122
|
+
labelExists,
|
|
123
|
+
getLabelName,
|
|
124
|
+
getRelatedLabels,
|
|
125
|
+
getRecommended,
|
|
126
|
+
createLabel,
|
|
127
|
+
addLabelNode,
|
|
128
|
+
updateLabelNode,
|
|
129
|
+
removeLabelNode,
|
|
130
|
+
getFullLabelName,
|
|
131
|
+
parseLabelsStr,
|
|
132
|
+
recentLabels,
|
|
133
|
+
setRecentLabels,
|
|
134
|
+
addRecentLabel
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
export const LabelsContainer = createContainer(useLabels);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
1
2
|
import { TextFieldProps } from '@mui/material';
|
|
2
|
-
import { GithubLabelPickerProps } from './github-label-picker';
|
|
3
|
+
import { type GithubLabelPickerProps } from './github-label-picker';
|
|
3
4
|
export interface LabelsInputProps {
|
|
4
5
|
value: string[];
|
|
5
6
|
onChange: (value: string[]) => void;
|
|
@@ -8,4 +9,4 @@ export interface LabelsInputProps {
|
|
|
8
9
|
/**
|
|
9
10
|
* @deprecated, use `LabelsInput2` instead
|
|
10
11
|
*/
|
|
11
|
-
export declare function LabelsInput({ value, onChange, LabelPickerProps, sx, ...rest }: LabelsInputProps & Omit<TextFieldProps, 'onChange'>): import("react
|
|
12
|
+
export declare function LabelsInput({ value, onChange, LabelPickerProps, sx, ...rest }: LabelsInputProps & Omit<TextFieldProps, 'onChange'>): import("react").JSX.Element | null;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { TextField } from "@mui/material";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
import { LabelsContainer } from "./labels-context.mjs";
|
|
5
|
+
import { GithubLabelPicker } from "./github-label-picker.mjs";
|
|
6
|
+
import { LabelChip } from "./labels2.mjs";
|
|
7
|
+
export function LabelsInput({
|
|
8
|
+
value = [],
|
|
9
|
+
onChange,
|
|
10
|
+
LabelPickerProps,
|
|
11
|
+
sx,
|
|
12
|
+
...rest
|
|
13
|
+
}) {
|
|
14
|
+
const { getLabelsByIds, loading } = LabelsContainer.useContainer();
|
|
15
|
+
const valueOptions = useMemo(() => getLabelsByIds(value), [getLabelsByIds, value]);
|
|
16
|
+
const mergedSx = [
|
|
17
|
+
{
|
|
18
|
+
"& .MuiInputBase-root": { flexWrap: "wrap", gap: 0.5, py: 0.75 },
|
|
19
|
+
"& .MuiInputBase-input": { width: !value?.length ? "initial" : "20px", p: 0 }
|
|
20
|
+
},
|
|
21
|
+
...Array.isArray(sx) ? sx : [sx]
|
|
22
|
+
];
|
|
23
|
+
const placeholder = !value?.length ? rest.placeholder || "Select labels" : "";
|
|
24
|
+
if (loading) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
return /* @__PURE__ */ jsx(
|
|
28
|
+
GithubLabelPicker,
|
|
29
|
+
{
|
|
30
|
+
value,
|
|
31
|
+
onChange,
|
|
32
|
+
trigger: /* @__PURE__ */ jsx(
|
|
33
|
+
TextField,
|
|
34
|
+
{
|
|
35
|
+
...rest,
|
|
36
|
+
placeholder,
|
|
37
|
+
fullWidth: true,
|
|
38
|
+
sx: mergedSx,
|
|
39
|
+
slotProps: {
|
|
40
|
+
input: {
|
|
41
|
+
autoComplete: "off",
|
|
42
|
+
...rest.InputProps,
|
|
43
|
+
startAdornment: [
|
|
44
|
+
rest.InputProps?.startAdornment,
|
|
45
|
+
...valueOptions.map((x, index) => /* @__PURE__ */ jsx(LabelChip, { label: x }, index))
|
|
46
|
+
].filter(Boolean)
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
),
|
|
51
|
+
...LabelPickerProps
|
|
52
|
+
}
|
|
53
|
+
);
|
|
54
|
+
}
|
package/dist/label2/labels2.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { Theme, SxProps } from '@mui/material';
|
|
2
|
-
import { JSX } from 'react';
|
|
3
|
-
import { LabelTreeNode } from './tree';
|
|
1
|
+
import { type Theme, type SxProps } from '@mui/material';
|
|
2
|
+
import { type JSX } from 'react';
|
|
3
|
+
import type { LabelTreeNode } from './tree';
|
|
4
4
|
export interface LabelsProps {
|
|
5
5
|
labels: string[] | undefined | null;
|
|
6
6
|
sx?: SxProps;
|
|
@@ -20,5 +20,5 @@ export interface LabelChipProps {
|
|
|
20
20
|
disabled?: boolean;
|
|
21
21
|
renderName?: (label: LabelTreeNode | string) => React.ReactNode | string;
|
|
22
22
|
}
|
|
23
|
-
export declare function LabelChip({ label, onDelete, sx, fullName, onClick, disabled, renderName, }: LabelChipProps):
|
|
24
|
-
export declare function Labels2({ compact, labels, sx, renderLabel, renderWhenEmpty, prepend, displaySystemLabels, onItemClick, }: LabelsProps):
|
|
23
|
+
export declare function LabelChip({ label, onDelete, sx, fullName, onClick, disabled, renderName, }: LabelChipProps): JSX.Element;
|
|
24
|
+
export declare function Labels2({ compact, labels, sx, renderLabel, renderWhenEmpty, prepend, displaySystemLabels, onItemClick, }: LabelsProps): JSX.Element | null;
|