startx 0.0.1

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.
Files changed (181) hide show
  1. package/.editorconfig +20 -0
  2. package/.prettierignore +24 -0
  3. package/.prettierrc.js +52 -0
  4. package/.vscode/settings.json +3 -0
  5. package/LICENSE +21 -0
  6. package/apps/core-server/.env.example +24 -0
  7. package/apps/core-server/Dockerfile +61 -0
  8. package/apps/core-server/eslint.config.mjs +47 -0
  9. package/apps/core-server/package.json +73 -0
  10. package/apps/core-server/src/config/custom-type.ts +54 -0
  11. package/apps/core-server/src/events/index.ts +37 -0
  12. package/apps/core-server/src/index.ts +19 -0
  13. package/apps/core-server/src/middlewares/auth-middleware.ts +50 -0
  14. package/apps/core-server/src/middlewares/cors-middleware.ts +6 -0
  15. package/apps/core-server/src/middlewares/error-middleware.ts +23 -0
  16. package/apps/core-server/src/middlewares/logger-middleware.ts +21 -0
  17. package/apps/core-server/src/middlewares/notfound-middleware.ts +14 -0
  18. package/apps/core-server/src/middlewares/serve-static.ts +24 -0
  19. package/apps/core-server/src/routes/files/router.ts +7 -0
  20. package/apps/core-server/src/routes/server.ts +36 -0
  21. package/apps/core-server/tsconfig.json +10 -0
  22. package/apps/core-server/tsdown.config.ts +14 -0
  23. package/biome.json +62 -0
  24. package/configs/eslint-config/package.json +60 -0
  25. package/configs/eslint-config/plugins.d.ts +1 -0
  26. package/configs/eslint-config/src/configs/base.ts +237 -0
  27. package/configs/eslint-config/src/configs/frontend.ts +62 -0
  28. package/configs/eslint-config/src/configs/node.ts +10 -0
  29. package/configs/eslint-config/src/plugin.ts +25 -0
  30. package/configs/eslint-config/src/rules/index.ts +30 -0
  31. package/configs/eslint-config/src/rules/no-argument-spread.test.ts +47 -0
  32. package/configs/eslint-config/src/rules/no-argument-spread.ts +96 -0
  33. package/configs/eslint-config/src/rules/no-dynamic-import-template.ts +32 -0
  34. package/configs/eslint-config/src/rules/no-internal-package-import.ts +40 -0
  35. package/configs/eslint-config/src/rules/no-interpolation-in-regular-string.ts +32 -0
  36. package/configs/eslint-config/src/rules/no-json-parse-json-stringify.test.ts +34 -0
  37. package/configs/eslint-config/src/rules/no-json-parse-json-stringify.ts +49 -0
  38. package/configs/eslint-config/src/rules/no-plain-errors.ts +50 -0
  39. package/configs/eslint-config/src/rules/no-skipped-tests.ts +61 -0
  40. package/configs/eslint-config/src/rules/no-top-level-relative-imports-in-backend-module.ts +27 -0
  41. package/configs/eslint-config/src/rules/no-type-unsafe-event-emitter.ts +33 -0
  42. package/configs/eslint-config/src/rules/no-uncaught-json-parse.test.ts +21 -0
  43. package/configs/eslint-config/src/rules/no-uncaught-json-parse.ts +45 -0
  44. package/configs/eslint-config/src/rules/no-untyped-config-class-field.ts +26 -0
  45. package/configs/eslint-config/src/rules/no-unused-param-catch-clause.ts +33 -0
  46. package/configs/eslint-config/src/rules/no-useless-catch-throw.test.ts +34 -0
  47. package/configs/eslint-config/src/rules/no-useless-catch-throw.ts +47 -0
  48. package/configs/eslint-config/src/utils/json.ts +21 -0
  49. package/configs/eslint-config/tsconfig.json +8 -0
  50. package/configs/eslint-config/tsdown.config.ts +11 -0
  51. package/configs/eslint-config/vitest.config.ts +3 -0
  52. package/configs/tsdown-config/package.json +14 -0
  53. package/configs/tsdown-config/src/config/tsdown.base.ts +13 -0
  54. package/configs/typescript-config/package.json +10 -0
  55. package/configs/typescript-config/tsconfig.common.json +32 -0
  56. package/configs/typescript-config/tsconfig.frontend.json +14 -0
  57. package/configs/typescript-config/tsconfig.node.json +9 -0
  58. package/configs/vitest-config/package.json +25 -0
  59. package/configs/vitest-config/src/base.ts +34 -0
  60. package/configs/vitest-config/src/frontend.ts +15 -0
  61. package/configs/vitest-config/src/node.ts +5 -0
  62. package/configs/vitest-config/tsconfig.json +7 -0
  63. package/package.json +47 -0
  64. package/packages/@repo/constants/eslint.config.mjs +21 -0
  65. package/packages/@repo/constants/package.json +19 -0
  66. package/packages/@repo/constants/src/api.ts +1 -0
  67. package/packages/@repo/constants/src/index.ts +8 -0
  68. package/packages/@repo/constants/src/time.ts +23 -0
  69. package/packages/@repo/constants/tsconfig.json +7 -0
  70. package/packages/@repo/db/eslint.config.mjs +21 -0
  71. package/packages/@repo/db/package.json +30 -0
  72. package/packages/@repo/db/src/functions.ts +122 -0
  73. package/packages/@repo/db/src/index.ts +20 -0
  74. package/packages/@repo/db/src/schema/common.ts +49 -0
  75. package/packages/@repo/db/src/schema/index.ts +1 -0
  76. package/packages/@repo/db/tsconfig.json +13 -0
  77. package/packages/@repo/lib/eslint.config.mjs +49 -0
  78. package/packages/@repo/lib/package.json +57 -0
  79. package/packages/@repo/lib/src/bucket-module/file-storage.ts +49 -0
  80. package/packages/@repo/lib/src/bucket-module/s3-storage.ts +114 -0
  81. package/packages/@repo/lib/src/bucket-module/utils.ts +11 -0
  82. package/packages/@repo/lib/src/command-module.ts +77 -0
  83. package/packages/@repo/lib/src/constants.ts +3 -0
  84. package/packages/@repo/lib/src/cookie-module.ts +42 -0
  85. package/packages/@repo/lib/src/custom-type.ts +54 -0
  86. package/packages/@repo/lib/src/env.ts +13 -0
  87. package/packages/@repo/lib/src/error-handlers-module/index.ts +11 -0
  88. package/packages/@repo/lib/src/file-system/index.ts +90 -0
  89. package/packages/@repo/lib/src/hashing-module.ts +9 -0
  90. package/packages/@repo/lib/src/index.ts +27 -0
  91. package/packages/@repo/lib/src/logger-module/log-config.ts +16 -0
  92. package/packages/@repo/lib/src/logger-module/logger.ts +78 -0
  93. package/packages/@repo/lib/src/logger-module/memory-profiler.ts +65 -0
  94. package/packages/@repo/lib/src/mail-module/api.ts +0 -0
  95. package/packages/@repo/lib/src/mail-module/mock.ts +8 -0
  96. package/packages/@repo/lib/src/mail-module/nodemailer.ts +45 -0
  97. package/packages/@repo/lib/src/notification-module/index.ts +172 -0
  98. package/packages/@repo/lib/src/notification-module/push-notification.ts +90 -0
  99. package/packages/@repo/lib/src/oauth2-client.ts +109 -0
  100. package/packages/@repo/lib/src/otp-module.ts +98 -0
  101. package/packages/@repo/lib/src/pagination-module.ts +49 -0
  102. package/packages/@repo/lib/src/token-module.ts +35 -0
  103. package/packages/@repo/lib/src/user-session.ts +117 -0
  104. package/packages/@repo/lib/src/utils.ts +42 -0
  105. package/packages/@repo/lib/src/validation-module.ts +187 -0
  106. package/packages/@repo/lib/tsconfig.json +7 -0
  107. package/packages/@repo/mail/package.json +29 -0
  108. package/packages/@repo/mail/src/emails/admin/OtpEmail.tsx +168 -0
  109. package/packages/@repo/mail/src/index.ts +13 -0
  110. package/packages/@repo/mail/tsconfig.build.json +14 -0
  111. package/packages/@repo/mail/tsconfig.json +13 -0
  112. package/packages/@repo/mail/tsdown.config.ts +9 -0
  113. package/packages/@repo/redis/eslint.config.mjs +8 -0
  114. package/packages/@repo/redis/package.json +31 -0
  115. package/packages/@repo/redis/src/index.ts +2 -0
  116. package/packages/@repo/redis/src/lib/redis-client.ts +23 -0
  117. package/packages/@repo/redis/src/lib/redis-module.ts +3 -0
  118. package/packages/@repo/redis/tsconfig.json +12 -0
  119. package/packages/ui/components.json +17 -0
  120. package/packages/ui/eslint.config.mjs +18 -0
  121. package/packages/ui/package.json +67 -0
  122. package/packages/ui/postcss.config.mjs +9 -0
  123. package/packages/ui/src/components/custom/form-wrapper.tsx +551 -0
  124. package/packages/ui/src/components/custom/grid-component.tsx +23 -0
  125. package/packages/ui/src/components/custom/hover-tool.tsx +38 -0
  126. package/packages/ui/src/components/custom/image-picker.tsx +109 -0
  127. package/packages/ui/src/components/custom/no-content.tsx +37 -0
  128. package/packages/ui/src/components/custom/page-container.tsx +24 -0
  129. package/packages/ui/src/components/custom/page-section.tsx +59 -0
  130. package/packages/ui/src/components/custom/simple-popover.tsx +29 -0
  131. package/packages/ui/src/components/custom/switch-component.tsx +20 -0
  132. package/packages/ui/src/components/custom/theme-provider.tsx +74 -0
  133. package/packages/ui/src/components/custom/typography.tsx +111 -0
  134. package/packages/ui/src/components/extensions/carousel.tsx +392 -0
  135. package/packages/ui/src/components/hooks/event/use-click.tsx +39 -0
  136. package/packages/ui/src/components/hooks/time/useDebounce.tsx +21 -0
  137. package/packages/ui/src/components/hooks/time/useInterval.tsx +35 -0
  138. package/packages/ui/src/components/hooks/time/useTimeout.tsx +19 -0
  139. package/packages/ui/src/components/hooks/time/useTimer.tsx +51 -0
  140. package/packages/ui/src/components/hooks/use-media-query.tsx +19 -0
  141. package/packages/ui/src/components/hooks/use-persistent-storage.tsx +52 -0
  142. package/packages/ui/src/components/hooks/use-update-effect.tsx +13 -0
  143. package/packages/ui/src/components/hooks/use-window-dimension.tsx +30 -0
  144. package/packages/ui/src/components/lib/utils.ts +242 -0
  145. package/packages/ui/src/components/lucide.tsx +3 -0
  146. package/packages/ui/src/components/sonner.tsx +1 -0
  147. package/packages/ui/src/components/ui/alert-dialog.tsx +116 -0
  148. package/packages/ui/src/components/ui/avatar.tsx +53 -0
  149. package/packages/ui/src/components/ui/badge.tsx +46 -0
  150. package/packages/ui/src/components/ui/breadcrumb.tsx +109 -0
  151. package/packages/ui/src/components/ui/button.tsx +96 -0
  152. package/packages/ui/src/components/ui/card.tsx +92 -0
  153. package/packages/ui/src/components/ui/carousel.tsx +243 -0
  154. package/packages/ui/src/components/ui/checkbox.tsx +32 -0
  155. package/packages/ui/src/components/ui/command.tsx +155 -0
  156. package/packages/ui/src/components/ui/dialog.tsx +127 -0
  157. package/packages/ui/src/components/ui/dropdown-menu.tsx +226 -0
  158. package/packages/ui/src/components/ui/form.tsx +165 -0
  159. package/packages/ui/src/components/ui/input-otp.tsx +76 -0
  160. package/packages/ui/src/components/ui/input.tsx +21 -0
  161. package/packages/ui/src/components/ui/label.tsx +24 -0
  162. package/packages/ui/src/components/ui/multiple-select.tsx +510 -0
  163. package/packages/ui/src/components/ui/popover.tsx +42 -0
  164. package/packages/ui/src/components/ui/select.tsx +170 -0
  165. package/packages/ui/src/components/ui/separator.tsx +28 -0
  166. package/packages/ui/src/components/ui/sheet.tsx +130 -0
  167. package/packages/ui/src/components/ui/skeleton.tsx +13 -0
  168. package/packages/ui/src/components/ui/spinner.tsx +16 -0
  169. package/packages/ui/src/components/ui/switch.tsx +28 -0
  170. package/packages/ui/src/components/ui/table.tsx +116 -0
  171. package/packages/ui/src/components/ui/tabs.tsx +54 -0
  172. package/packages/ui/src/components/ui/textarea.tsx +18 -0
  173. package/packages/ui/src/components/ui/timeline.tsx +118 -0
  174. package/packages/ui/src/components/ui/tooltip.tsx +30 -0
  175. package/packages/ui/src/components/util/n-formattor.ts +22 -0
  176. package/packages/ui/src/components/util/storage.ts +37 -0
  177. package/packages/ui/src/globals.css +87 -0
  178. package/packages/ui/tailwind.config.ts +94 -0
  179. package/packages/ui/tsconfig.json +12 -0
  180. package/pnpm-workspace.yaml +43 -0
  181. package/turbo.json +77 -0
@@ -0,0 +1,510 @@
1
+ /* eslint-disable local-rules/no-json-parse-json-stringify */
2
+ "use client";
3
+
4
+ import { Command as CommandPrimitive, useCommandState } from "cmdk";
5
+ import { X } from "lucide-react";
6
+ import * as React from "react";
7
+ import { forwardRef, useEffect } from "react";
8
+
9
+ import { Command, CommandGroup, CommandItem, CommandList } from "./command";
10
+ import { cn } from "../lib/utils";
11
+ import { Badge } from "../ui/badge";
12
+
13
+ export interface Option {
14
+ value: string;
15
+ label: string;
16
+ disable?: boolean;
17
+ /** fixed option that can't be removed. */
18
+ fixed?: boolean;
19
+ /** Group the options by providing key. */
20
+ [key: string]: string | boolean | undefined;
21
+ }
22
+ interface GroupOption {
23
+ [key: string]: Option[];
24
+ }
25
+
26
+ interface MultipleSelectorProps {
27
+ value?: Option[];
28
+ defaultOptions?: Option[];
29
+ /** manually controlled options */
30
+ options?: Option[];
31
+ placeholder?: string;
32
+ /** Loading component. */
33
+ loadingIndicator?: React.ReactNode;
34
+ /** Empty component. */
35
+ emptyIndicator?: React.ReactNode;
36
+ /** Debounce time for async search. Only work with `onSearch`. */
37
+ delay?: number;
38
+ /**
39
+ * Only work with `onSearch` prop. Trigger search when `onFocus`.
40
+ * For example, when user click on the input, it will trigger the search to get initial options.
41
+ **/
42
+ triggerSearchOnFocus?: boolean;
43
+ /** async search */
44
+ onSearch?: (value: string) => Promise<Option[]>;
45
+ onChange?: (options: Option[]) => void;
46
+ /** Limit the maximum number of selected options. */
47
+ maxSelected?: number;
48
+ /** When the number of selected options exceeds the limit, the onMaxSelected will be called. */
49
+ onMaxSelected?: (maxLimit: number) => void;
50
+ /** Hide the placeholder when there are options selected. */
51
+ hidePlaceholderWhenSelected?: boolean;
52
+ disabled?: boolean;
53
+ /** Group the options base on provided key. */
54
+ groupBy?: string;
55
+ className?: string;
56
+ badgeClassName?: string;
57
+ /**
58
+ * First item selected is a default behavior by cmdk. That is why the default is true.
59
+ * This is a workaround solution by add a dummy item.
60
+ *
61
+ * @reference: https://github.com/pacocoursey/cmdk/issues/171
62
+ */
63
+ selectFirstItem?: boolean;
64
+ /** Allow user to create option when there is no option matched. */
65
+ creatable?: boolean;
66
+ /** Props of `Command` */
67
+ commandProps?: React.ComponentPropsWithoutRef<typeof Command>;
68
+ /** Props of `CommandInput` */
69
+ inputProps?: Omit<
70
+ React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>,
71
+ "value" | "placeholder" | "disabled"
72
+ >;
73
+ }
74
+
75
+ export interface MultipleSelectorRef {
76
+ selectedValue: Option[];
77
+ input: HTMLInputElement;
78
+ }
79
+
80
+ export function useDebounce<T>(value: T, delay?: number): T {
81
+ const [debouncedValue, setDebouncedValue] = React.useState<T>(value);
82
+
83
+ useEffect(() => {
84
+ const timer = setTimeout(() => setDebouncedValue(value), delay ?? 500);
85
+
86
+ return () => {
87
+ clearTimeout(timer);
88
+ };
89
+ }, [value, delay]);
90
+
91
+ return debouncedValue;
92
+ }
93
+
94
+ function transToGroupOption(options: Option[], groupBy?: string) {
95
+ if (options.length === 0) {
96
+ return {};
97
+ }
98
+ if (!groupBy) {
99
+ return {
100
+ "": options,
101
+ };
102
+ }
103
+
104
+ const groupOption: GroupOption = {};
105
+ options.forEach(option => {
106
+ const key = (option[groupBy] as string) || "";
107
+ if (!groupOption[key]) {
108
+ groupOption[key] = [];
109
+ }
110
+ groupOption[key].push(option);
111
+ });
112
+ return groupOption;
113
+ }
114
+
115
+ function removePickedOption(groupOption: GroupOption, picked: Option[]) {
116
+ const cloneOption = JSON.parse(JSON.stringify(groupOption)) as GroupOption;
117
+
118
+ for (const [key, value] of Object.entries(cloneOption)) {
119
+ cloneOption[key] = value.filter(val => !picked.find(p => p.value === val.value));
120
+ }
121
+ return cloneOption;
122
+ }
123
+
124
+ function isOptionsExist(groupOption: GroupOption, targetOption: Option[]) {
125
+ for (const [key, value] of Object.entries(groupOption)) {
126
+ if (value.some(option => targetOption.find(p => p.value === option.value))) {
127
+ return true;
128
+ }
129
+ }
130
+ return false;
131
+ }
132
+
133
+ /**
134
+ * The `CommandEmpty` of shadcn/ui will cause the cmdk empty not rendering correctly.
135
+ * So we create one and copy the `Empty` implementation from `cmdk`.
136
+ *
137
+ * @reference: https://github.com/hsuanyi-chou/shadcn-ui-expansions/issues/34#issuecomment-1949561607
138
+ **/
139
+ const CommandEmpty = forwardRef<
140
+ HTMLDivElement,
141
+ React.ComponentProps<typeof CommandPrimitive.Empty>
142
+ >(({ className, ...props }, forwardedRef) => {
143
+ const render = useCommandState(state => state.filtered.count === 0);
144
+
145
+ if (!render) return null;
146
+
147
+ return (
148
+ <div
149
+ ref={forwardedRef}
150
+ className={cn("py-6 text-center text-sm", className)}
151
+ // cmdk-empty=""
152
+ role="presentation"
153
+ {...props}
154
+ />
155
+ );
156
+ });
157
+
158
+ CommandEmpty.displayName = "CommandEmpty";
159
+
160
+ const MultipleSelector = React.forwardRef<MultipleSelectorRef, MultipleSelectorProps>(
161
+ (
162
+ {
163
+ value,
164
+ onChange,
165
+ placeholder,
166
+ defaultOptions: arrayDefaultOptions = [],
167
+ options: arrayOptions,
168
+ delay,
169
+ onSearch,
170
+ loadingIndicator,
171
+ emptyIndicator,
172
+ maxSelected = Number.MAX_SAFE_INTEGER,
173
+ onMaxSelected,
174
+ hidePlaceholderWhenSelected,
175
+ disabled,
176
+ groupBy,
177
+ className,
178
+ badgeClassName,
179
+ selectFirstItem = true,
180
+ creatable = false,
181
+ triggerSearchOnFocus = false,
182
+ commandProps,
183
+ inputProps,
184
+ }: MultipleSelectorProps,
185
+ ref: React.Ref<MultipleSelectorRef>
186
+ ) => {
187
+ const inputRef = React.useRef<HTMLInputElement>(null);
188
+ const [open, setOpen] = React.useState(false);
189
+ const [isLoading, setIsLoading] = React.useState(false);
190
+
191
+ const [selected, setSelected] = React.useState<Option[]>(value ?? []);
192
+ const [options, setOptions] = React.useState<GroupOption>(
193
+ transToGroupOption(arrayDefaultOptions, groupBy)
194
+ );
195
+ const [inputValue, setInputValue] = React.useState("");
196
+ const debouncedSearchTerm = useDebounce(inputValue, delay ?? 500);
197
+
198
+ React.useImperativeHandle(
199
+ ref,
200
+ () => ({
201
+ selectedValue: [...selected],
202
+ input: inputRef.current as HTMLInputElement,
203
+ focus: () => inputRef.current?.focus(),
204
+ }),
205
+ [selected]
206
+ );
207
+
208
+ const handleUnselect = React.useCallback(
209
+ (option: Option) => {
210
+ const newOptions = selected.filter(s => s.value !== option.value);
211
+ setSelected(newOptions);
212
+ onChange?.(newOptions);
213
+ },
214
+ [onChange, selected]
215
+ );
216
+
217
+ const handleKeyDown = React.useCallback(
218
+ (e: React.KeyboardEvent<HTMLDivElement>) => {
219
+ const input = inputRef.current;
220
+ if (input) {
221
+ if (e.key === "Delete" || e.key === "Backspace") {
222
+ if (input.value === "" && selected.length > 0) {
223
+ const value = selected[selected.length - 1];
224
+ if (value) {
225
+ handleUnselect(value);
226
+ }
227
+ }
228
+ }
229
+ // This is not a default behavior of the <input /> field
230
+ if (e.key === "Escape") {
231
+ input.blur();
232
+ }
233
+ }
234
+ },
235
+ [handleUnselect, selected]
236
+ );
237
+
238
+ useEffect(() => {
239
+ if (value) {
240
+ setSelected(value);
241
+ }
242
+ }, [value]);
243
+
244
+ useEffect(() => {
245
+ /** If `onSearch` is provided, do not trigger options updated. */
246
+ if (!arrayOptions || onSearch) {
247
+ return;
248
+ }
249
+ const newOption = transToGroupOption(arrayOptions || [], groupBy);
250
+ if (JSON.stringify(newOption) !== JSON.stringify(options)) {
251
+ setOptions(newOption);
252
+ }
253
+ }, [arrayDefaultOptions, arrayOptions, groupBy, onSearch, options]);
254
+
255
+ useEffect(() => {
256
+ const doSearch = async () => {
257
+ setIsLoading(true);
258
+ const res = await onSearch?.(debouncedSearchTerm);
259
+ setOptions(transToGroupOption(res ?? [], groupBy));
260
+ setIsLoading(false);
261
+ };
262
+
263
+ const exec = async () => {
264
+ if (!onSearch || !open) return;
265
+
266
+ if (triggerSearchOnFocus) {
267
+ await doSearch();
268
+ }
269
+
270
+ if (debouncedSearchTerm) {
271
+ await doSearch();
272
+ }
273
+ };
274
+
275
+ void exec();
276
+ }, [debouncedSearchTerm, groupBy, open, triggerSearchOnFocus]);
277
+
278
+ const CreatableItem = () => {
279
+ if (!creatable) return undefined;
280
+ if (
281
+ isOptionsExist(options, [{ value: inputValue, label: inputValue }]) ||
282
+ selected.find(s => s.value === inputValue)
283
+ ) {
284
+ return undefined;
285
+ }
286
+
287
+ const Item = (
288
+ <CommandItem
289
+ value={inputValue}
290
+ className="cursor-pointer"
291
+ onMouseDown={e => {
292
+ e.preventDefault();
293
+ e.stopPropagation();
294
+ }}
295
+ onSelect={(value: string) => {
296
+ if (selected.length >= maxSelected) {
297
+ onMaxSelected?.(selected.length);
298
+ return;
299
+ }
300
+ setInputValue("");
301
+ const newOptions = [...selected, { value, label: value }];
302
+ setSelected(newOptions);
303
+ onChange?.(newOptions);
304
+ }}
305
+ >
306
+ {`Create "${inputValue}"`}
307
+ </CommandItem>
308
+ );
309
+
310
+ // For normal creatable
311
+ if (!onSearch && inputValue.length > 0) {
312
+ return Item;
313
+ }
314
+
315
+ // For async search creatable. avoid showing creatable item before loading at first.
316
+ if (onSearch && debouncedSearchTerm.length > 0 && !isLoading) {
317
+ return Item;
318
+ }
319
+
320
+ return undefined;
321
+ };
322
+
323
+ const EmptyItem = React.useCallback(() => {
324
+ if (!emptyIndicator) return undefined;
325
+
326
+ // For async search that showing emptyIndicator
327
+ if (onSearch && !creatable && Object.keys(options).length === 0) {
328
+ return (
329
+ <CommandItem value="-" disabled>
330
+ {emptyIndicator}
331
+ </CommandItem>
332
+ );
333
+ }
334
+
335
+ return <CommandEmpty>{emptyIndicator}</CommandEmpty>;
336
+ }, [creatable, emptyIndicator, onSearch, options]);
337
+
338
+ const selectables = React.useMemo<GroupOption>(
339
+ () => removePickedOption(options, selected),
340
+ [options, selected]
341
+ );
342
+
343
+ /** Avoid Creatable Selector freezing or lagging when paste a long string. */
344
+ const commandFilter = React.useCallback(() => {
345
+ if (commandProps?.filter) {
346
+ return commandProps.filter;
347
+ }
348
+
349
+ if (creatable) {
350
+ return (value: string, search: string) => {
351
+ return value.toLowerCase().includes(search.toLowerCase()) ? 1 : -1;
352
+ };
353
+ }
354
+ // Using default filter in `cmdk`. We don't have to provide it.
355
+ return undefined;
356
+ }, [creatable, commandProps?.filter]);
357
+
358
+ return (
359
+ <Command
360
+ {...commandProps}
361
+ onKeyDown={e => {
362
+ handleKeyDown(e);
363
+ commandProps?.onKeyDown?.(e);
364
+ }}
365
+ className={cn("h-auto max-h-fit overflow-visible bg-transparent", commandProps?.className)}
366
+ shouldFilter={
367
+
368
+ commandProps?.shouldFilter !== undefined ? commandProps.shouldFilter : !onSearch
369
+ } // When onSearch is provided, we don't want to filter the options. You can still override it.
370
+ filter={commandFilter()}
371
+ >
372
+ <div
373
+ className={cn(
374
+ " rounded-md border border-input text-sm ring-offset-background focus-within:ring-2 focus-within:border-primary focus-within:ring-ring/50 focus-within:ring-offset-0",
375
+ " placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input flex w-full min-w-0 rounded-md border bg-transparent text-base shadow-xs transition-[color,box-shadow] outline-none file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
376
+ "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
377
+ "aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive",
378
+ {
379
+ "px-3 py-2": selected.length !== 0,
380
+ "cursor-text": !disabled && selected.length !== 0,
381
+ },
382
+ className
383
+ )}
384
+ onClick={() => {
385
+ if (disabled) return;
386
+ inputRef.current?.focus();
387
+ }}
388
+ >
389
+ <div className="flex flex-wrap gap-1">
390
+ {selected.map(option => {
391
+ return (
392
+ <Badge
393
+ key={option.value}
394
+ className={cn(
395
+ "data-[disabled]:bg-muted-foreground data-[disabled]:text-muted data-[disabled]:hover:bg-muted-foreground cursor-default",
396
+ "data-[fixed]:bg-muted-foreground data-[fixed]:text-muted data-[fixed]:hover:bg-muted-foreground",
397
+ badgeClassName
398
+ )}
399
+ data-fixed={option.fixed}
400
+ data-disabled={disabled ?? undefined}
401
+ >
402
+ {option.label}
403
+ <button
404
+ className={cn(
405
+ "ml-1 rounded-full outline-none ring-offset-background focus:ring-2 focus:ring-ring focus:ring-offset-2 cursor-pointer",
406
+ (disabled ?? option.fixed) && "hidden"
407
+ )}
408
+ onKeyDown={e => {
409
+ if (e.key === "Enter") {
410
+ handleUnselect(option);
411
+ }
412
+ }}
413
+ onMouseDown={e => {
414
+ e.preventDefault();
415
+ e.stopPropagation();
416
+ }}
417
+ onClick={() => handleUnselect(option)}
418
+ >
419
+ <X className="h-3 w-3 text-white hover:text-foreground" />
420
+ </button>
421
+ </Badge>
422
+ );
423
+ })}
424
+ {/* Avoid having the "Search" Icon */}
425
+ <CommandPrimitive.Input
426
+ {...inputProps}
427
+ ref={inputRef}
428
+ value={inputValue}
429
+ disabled={disabled}
430
+ onValueChange={value => {
431
+ setInputValue(value);
432
+ inputProps?.onValueChange?.(value);
433
+ }}
434
+ onBlur={event => {
435
+ setOpen(false);
436
+ inputProps?.onBlur?.(event);
437
+ }}
438
+ onFocus={async event => {
439
+ setOpen(true);
440
+ if (triggerSearchOnFocus) await onSearch?.(debouncedSearchTerm);
441
+ inputProps?.onFocus?.(event);
442
+ }}
443
+ placeholder={hidePlaceholderWhenSelected && selected.length !== 0 ? "" : placeholder}
444
+ className={cn(
445
+ "flex-1 bg-transparent outline-none placeholder:text-muted-foreground",
446
+ {
447
+ "w-full": hidePlaceholderWhenSelected,
448
+ "px-3 py-2": selected.length === 0,
449
+ "ml-1": selected.length !== 0,
450
+ },
451
+ inputProps?.className
452
+ )}
453
+ />
454
+ </div>
455
+ </div>
456
+ <div className="relative">
457
+ {open ? <CommandList className="absolute top-1 z-10 w-full rounded-md border bg-popover text-popover-foreground shadow-md outline-none animate-in">
458
+ {isLoading ? (
459
+ <>{loadingIndicator}</>
460
+ ) : (
461
+ <>
462
+ {EmptyItem()}
463
+ {CreatableItem()}
464
+ {!selectFirstItem && <CommandItem value="-" className="hidden" />}
465
+ {Object.entries(selectables).map(([key, dropdowns]) => (
466
+ <CommandGroup key={key} heading={key} className="h-full overflow-auto">
467
+ <>
468
+ {dropdowns.map(option => {
469
+ return (
470
+ <CommandItem
471
+ key={option.value}
472
+ value={option.value}
473
+ disabled={option.disable}
474
+ onMouseDown={e => {
475
+ e.preventDefault();
476
+ e.stopPropagation();
477
+ }}
478
+ onSelect={() => {
479
+ if (selected.length >= maxSelected) {
480
+ onMaxSelected?.(selected.length);
481
+ return;
482
+ }
483
+ setInputValue("");
484
+ const newOptions = [...selected, option];
485
+ setSelected(newOptions);
486
+ onChange?.(newOptions);
487
+ }}
488
+ className={cn(
489
+ "cursor-pointer",
490
+ option.disable && "cursor-default text-muted-foreground"
491
+ )}
492
+ >
493
+ {option.label}
494
+ </CommandItem>
495
+ );
496
+ })}
497
+ </>
498
+ </CommandGroup>
499
+ ))}
500
+ </>
501
+ )}
502
+ </CommandList> : null}
503
+ </div>
504
+ </Command>
505
+ );
506
+ }
507
+ );
508
+
509
+ MultipleSelector.displayName = "MultipleSelector";
510
+ export { MultipleSelector };
@@ -0,0 +1,42 @@
1
+ 'use client';
2
+
3
+ import * as PopoverPrimitive from '@radix-ui/react-popover';
4
+ import type * as React from 'react';
5
+
6
+ import { cn } from '../lib/utils';
7
+
8
+ function Popover({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Root>) {
9
+ return <PopoverPrimitive.Root data-slot="popover" {...props} />;
10
+ }
11
+
12
+ function PopoverTrigger({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Trigger>) {
13
+ return <PopoverPrimitive.Trigger data-slot="popover-trigger" {...props} />;
14
+ }
15
+
16
+ function PopoverContent({
17
+ className,
18
+ align = 'center',
19
+ sideOffset = 4,
20
+ ...props
21
+ }: React.ComponentProps<typeof PopoverPrimitive.Content>) {
22
+ return (
23
+ <PopoverPrimitive.Portal>
24
+ <PopoverPrimitive.Content
25
+ data-slot="popover-content"
26
+ align={align}
27
+ sideOffset={sideOffset}
28
+ className={cn(
29
+ 'bg-popover text-popover-foreground data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2 z-50 w-72 origin-(--radix-popover-content-transform-origin) rounded-md border p-4 shadow-md outline-hidden',
30
+ className,
31
+ )}
32
+ {...props}
33
+ />
34
+ </PopoverPrimitive.Portal>
35
+ );
36
+ }
37
+
38
+ function PopoverAnchor({ ...props }: React.ComponentProps<typeof PopoverPrimitive.Anchor>) {
39
+ return <PopoverPrimitive.Anchor data-slot="popover-anchor" {...props} />;
40
+ }
41
+
42
+ export { Popover, PopoverTrigger, PopoverContent, PopoverAnchor };