-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
group-actions.tsx
93 lines (79 loc) · 2.62 KB
/
group-actions.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
import { Center, Loader } from "@mantine/core";
import { useWindowEvent } from "@mantine/hooks";
import type { TranslationObject } from "@homarr/translation";
import { useI18n } from "@homarr/translation/client";
import type { SearchGroup } from "../../lib/group";
import type { inferSearchInteractionOptions } from "../../lib/interaction";
import { SpotlightNoResults } from "../no-results";
import { SpotlightGroupActionItem } from "./items/group-action-item";
interface GroupActionsProps<TOption extends Record<string, unknown>> {
group: SearchGroup<TOption>;
query: string;
setMode: (mode: keyof TranslationObject["search"]["mode"]) => void;
setChildrenOptions: (options: inferSearchInteractionOptions<"children">) => void;
}
export const SpotlightGroupActions = <TOption extends Record<string, unknown>>({
group,
query,
setMode,
setChildrenOptions,
}: GroupActionsProps<TOption>) => {
// This does work as the same amount of hooks is called on every render
const useOptions =
"options" in group ? () => group.options : "useOptions" in group ? group.useOptions : group.useQueryOptions;
const options = useOptions(query);
const t = useI18n();
useWindowEvent("keydown", (event) => {
const optionsArray = Array.isArray(options) ? options : (options.data ?? []);
group.onKeyDown?.(event, optionsArray, query, { setChildrenOptions });
});
if (Array.isArray(options)) {
const filteredOptions = options
.filter((option) => ("filter" in group ? group.filter(query, option) : false))
.sort((optionA, optionB) => {
if ("sort" in group) {
return group.sort?.(query, [optionA, optionB]) ?? 0;
}
return 0;
});
if (filteredOptions.length === 0) {
return <SpotlightNoResults />;
}
return filteredOptions.map((option) => (
<SpotlightGroupActionItem
key={option[group.keyPath] as never}
option={option}
group={group}
query={query}
setMode={setMode}
setChildrenOptions={setChildrenOptions}
/>
));
}
if (options.isLoading) {
return (
<Center w="100%" py="sm">
<Loader size="sm" />
</Center>
);
}
if (options.isError) {
return <Center py="sm">{t("search.error.fetch")}</Center>;
}
if (!options.data) {
return null;
}
if (options.data.length === 0) {
return <SpotlightNoResults />;
}
return options.data.map((option) => (
<SpotlightGroupActionItem
key={option[group.keyPath] as never}
option={option}
group={group}
query={query}
setMode={setMode}
setChildrenOptions={setChildrenOptions}
/>
));
};