@lukoweb/apitogo 0.1.1 → 0.1.2

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 (91) hide show
  1. package/dist/cli/cli.js +17 -6
  2. package/dist/declarations/lib/ui/Command.d.ts +1 -1
  3. package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
  4. package/package.json +1 -1
  5. package/src/app/main.tsx +150 -150
  6. package/src/config/loader.ts +245 -245
  7. package/src/index.ts +33 -33
  8. package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
  9. package/src/lib/authentication/components/SignIn.tsx +46 -46
  10. package/src/lib/authentication/components/SignUp.tsx +48 -48
  11. package/src/lib/authentication/providers/clerk.tsx +224 -224
  12. package/src/lib/authentication/ui/AuthCard.tsx +7 -7
  13. package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
  14. package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
  15. package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
  16. package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
  17. package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
  18. package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
  19. package/src/lib/components/Autocomplete.tsx +123 -123
  20. package/src/lib/components/DeveloperHint.tsx +39 -35
  21. package/src/lib/components/Header.tsx +260 -260
  22. package/src/lib/components/Layout.tsx +53 -53
  23. package/src/lib/components/Main.tsx +51 -51
  24. package/src/lib/components/Mermaid.tsx +74 -70
  25. package/src/lib/components/MobileTopNavigation.tsx +276 -276
  26. package/src/lib/components/MultiSelect.tsx +86 -82
  27. package/src/lib/components/TopNavigation.tsx +103 -103
  28. package/src/lib/components/index.ts +26 -26
  29. package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
  30. package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
  31. package/src/lib/components/navigation/NavigationItem.tsx +181 -177
  32. package/src/lib/core/RouteGuard.tsx +193 -193
  33. package/src/lib/errors/ErrorMessage.tsx +50 -46
  34. package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
  35. package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
  36. package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
  37. package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
  38. package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
  39. package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
  40. package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
  41. package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
  42. package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
  43. package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
  44. package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
  45. package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
  46. package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
  47. package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
  48. package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
  49. package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
  50. package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
  51. package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
  52. package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
  53. package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
  54. package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
  55. package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
  56. package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
  57. package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
  58. package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
  59. package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
  60. package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
  61. package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
  62. package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
  63. package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
  64. package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
  65. package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
  66. package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
  67. package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
  68. package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
  69. package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
  70. package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
  71. package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
  72. package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
  73. package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
  74. package/src/lib/ui/ActionButton.tsx +21 -21
  75. package/src/lib/ui/Command.tsx +191 -191
  76. package/src/lib/util/MdxComponents.tsx +150 -150
  77. package/src/vite/config.ts +234 -231
  78. package/src/vite/dev-server.ts +291 -291
  79. package/src/vite/plugin-api-keys.ts +50 -50
  80. package/src/vite/plugin-api.ts +320 -318
  81. package/src/vite/plugin-auth.ts +38 -38
  82. package/src/vite/plugin-component.ts +45 -39
  83. package/src/vite/plugin-config.ts +75 -75
  84. package/src/vite/plugin-custom-pages.ts +36 -36
  85. package/src/vite/plugin-docs.ts +202 -202
  86. package/src/vite/plugin-markdown-export.ts +214 -214
  87. package/src/vite/plugin-mdx.ts +149 -149
  88. package/src/vite/plugin-navigation.ts +106 -106
  89. package/src/vite/plugin-search.ts +47 -47
  90. package/src/vite/plugin.ts +42 -42
  91. package/src/vite/prerender/prerender.ts +233 -233
@@ -1,202 +1,202 @@
1
- import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
2
- import { keepPreviousData, useQuery } from "@tanstack/react-query";
3
- import { ListPlusIcon } from "lucide-react";
4
- import { lazy, useEffect, useRef, useState } from "react";
5
- import { Button } from "@lukoweb/apitogo/ui/Button.js";
6
- import {
7
- CommandDialog,
8
- CommandEmpty,
9
- CommandInput,
10
- } from "@lukoweb/apitogo/ui/Command.js";
11
- import { DialogTitle } from "@lukoweb/apitogo/ui/Dialog.js";
12
- import { Kbd, KbdGroup } from "@lukoweb/apitogo/ui/Kbd.js";
13
- import { useAuthState } from "../../authentication/state.js";
14
- import { useZudoku } from "../../components/context/ZudokuContext.js";
15
- import { SEARCH_PROTECTED_SECTION } from "../../core/RouteGuard.js";
16
- import { joinUrl } from "../../util/joinUrl.js";
17
- import { getResults } from "./get-results.js";
18
- import type { PagefindOptions } from "./index.js";
19
- import { ResultList } from "./ResultList.js";
20
- import type { Pagefind } from "./types.js";
21
-
22
- const IndexingDialog = lazy(() => import("./IndexingDialog.js"));
23
-
24
- const DEFAULT_RANKING = {
25
- // Slightly lower than default because API docs tend to have repetitive terms (parameter names, HTTP methods, etc.)
26
- termFrequency: 0.8,
27
- // Lower than default because API documentation pages tend to be longer due to comprehensive endpoint documentation
28
- pageLength: 0.6,
29
- // Slightly higher than default because in technical documentation, exact matches should be prioritized
30
- termSimilarity: 1.2,
31
- // Slightly lower than default because API docs might have legitimate repetition of terms
32
- termSaturation: 1.2,
33
- };
34
-
35
- const importPagefind = (basePath?: string): Promise<Pagefind> =>
36
- import.meta.env.DEV
37
- ? // @ts-expect-error TypeScript can't resolve the import
38
- import(/* @vite-ignore */ "/pagefind/pagefind.js")
39
- : import(/* @vite-ignore */ joinUrl(basePath, "/pagefind/pagefind.js"));
40
-
41
- const usePagefind = (options: PagefindOptions) => {
42
- const {
43
- options: { basePath },
44
- } = useZudoku();
45
- const { data: pagefind, ...result } = useQuery<Pagefind>({
46
- queryKey: ["pagefind", options.ranking],
47
- retry: false,
48
- queryFn: async () => {
49
- const pagefind = await importPagefind(basePath);
50
- await pagefind.init();
51
- await pagefind.options({
52
- ranking: {
53
- termFrequency:
54
- options.ranking?.termFrequency ?? DEFAULT_RANKING.termFrequency,
55
- pageLength: options.ranking?.pageLength ?? DEFAULT_RANKING.pageLength,
56
- termSimilarity:
57
- options.ranking?.termSimilarity ?? DEFAULT_RANKING.termSimilarity,
58
- termSaturation:
59
- options.ranking?.termSaturation ?? DEFAULT_RANKING.termSaturation,
60
- },
61
- });
62
-
63
- return pagefind;
64
- },
65
- enabled: typeof window !== "undefined",
66
- });
67
-
68
- if (result.isError && result.error.message !== "NOT_BUILT_YET") {
69
- // biome-ignore lint/suspicious/noConsole: Logging allowed here
70
- console.error(result.error);
71
- }
72
-
73
- return { ...result, pagefind };
74
- };
75
-
76
- export const PagefindSearch = ({
77
- isOpen,
78
- onClose,
79
- options,
80
- }: {
81
- isOpen: boolean;
82
- onClose: () => void;
83
- options: PagefindOptions;
84
- }) => {
85
- const { pagefind, error, isError } = usePagefind(options);
86
- const [searchTerm, setSearchTerm] = useState("");
87
- const [selectedValue, setSelectedValue] = useState<string>("");
88
- const auth = useAuthState();
89
- const context = useZudoku();
90
- const inputRef = useRef<HTMLInputElement>(null);
91
-
92
- const { data: searchResults } = useQuery({
93
- queryKey: ["pagefind-search", searchTerm, auth.isAuthenticated],
94
- queryFn: async () => {
95
- const filters = auth.isAuthenticated
96
- ? undefined
97
- : { not: { section: SEARCH_PROTECTED_SECTION } };
98
-
99
- const search = await pagefind?.search(searchTerm, { filters });
100
- if (!search) return [];
101
- return getResults({ search, options, auth, context });
102
- },
103
- placeholderData: keepPreviousData,
104
- enabled: !!pagefind && !!searchTerm,
105
- });
106
-
107
- useEffect(() => {
108
- if (!searchResults?.length) return;
109
-
110
- const firstResult = searchResults.at(0);
111
- if (!firstResult) return;
112
-
113
- const firstValue = `${firstResult.meta.title}-${firstResult.url}`;
114
- setSelectedValue(firstValue);
115
- }, [searchResults]);
116
-
117
- return (
118
- <CommandDialog
119
- command={{
120
- shouldFilter: false,
121
- loop: true,
122
- value: selectedValue,
123
- onValueChange: setSelectedValue,
124
- }}
125
- content={{ className: "max-w-[750px]" }}
126
- open={isOpen}
127
- onOpenChange={onClose}
128
- >
129
- <VisuallyHidden>
130
- <DialogTitle>Search</DialogTitle>
131
- </VisuallyHidden>
132
- <CommandInput
133
- ref={inputRef}
134
- placeholder="Search..."
135
- value={searchTerm}
136
- onValueChange={setSearchTerm}
137
- disabled={isError}
138
- />
139
- <CommandEmpty>
140
- {searchTerm ? (
141
- <div className="flex flex-col items-center">
142
- No results found.
143
- <Button
144
- variant="link"
145
- onClick={() => {
146
- setSearchTerm("");
147
- inputRef.current?.focus();
148
- }}
149
- >
150
- Clear search
151
- </Button>
152
- </div>
153
- ) : (
154
- "Start typing to search"
155
- )}
156
- </CommandEmpty>
157
- {isError && error.message !== "NOT_BUILT_YET" ? (
158
- <div className="p-4 text-sm">
159
- An error occurred while loading search.
160
- </div>
161
- ) : (
162
- <>
163
- <ResultList
164
- basePath={context.options.basePath}
165
- searchResults={searchResults ?? []}
166
- searchTerm={searchTerm}
167
- onClose={onClose}
168
- maxSubResults={options.maxSubResults}
169
- />
170
- <div className="flex justify-between p-2 items-center">
171
- <div className="flex items-center text-xs text-muted-foreground">
172
- <KbdGroup className="ms-0 me-1">
173
- <Kbd>↑</Kbd>
174
- <Kbd>↓</Kbd>
175
- </KbdGroup>
176
- Navigate
177
- <KbdGroup className="ms-4 me-1">
178
- <Kbd>↵</Kbd>
179
- </KbdGroup>
180
- Select
181
- <KbdGroup className="ms-4 me-1">
182
- <Kbd>Esc</Kbd>
183
- </KbdGroup>
184
- Close dialog
185
- </div>
186
- {import.meta.env.DEV && (
187
- <IndexingDialog>
188
- <Button
189
- variant="outline"
190
- className="h-7 text-xs text-muted-foreground"
191
- >
192
- <ListPlusIcon />
193
- Build Search Index
194
- </Button>
195
- </IndexingDialog>
196
- )}
197
- </div>
198
- </>
199
- )}
200
- </CommandDialog>
201
- );
202
- };
1
+ import { Button } from "@lukoweb/apitogo/ui/Button.js";
2
+ import {
3
+ CommandDialog,
4
+ CommandEmpty,
5
+ CommandInput,
6
+ } from "@lukoweb/apitogo/ui/Command.js";
7
+ import { DialogTitle } from "@lukoweb/apitogo/ui/Dialog.js";
8
+ import { Kbd, KbdGroup } from "@lukoweb/apitogo/ui/Kbd.js";
9
+ import { VisuallyHidden } from "@radix-ui/react-visually-hidden";
10
+ import { keepPreviousData, useQuery } from "@tanstack/react-query";
11
+ import { ListPlusIcon } from "lucide-react";
12
+ import { lazy, useEffect, useRef, useState } from "react";
13
+ import { useAuthState } from "../../authentication/state.js";
14
+ import { useZudoku } from "../../components/context/ZudokuContext.js";
15
+ import { SEARCH_PROTECTED_SECTION } from "../../core/RouteGuard.js";
16
+ import { joinUrl } from "../../util/joinUrl.js";
17
+ import { getResults } from "./get-results.js";
18
+ import type { PagefindOptions } from "./index.js";
19
+ import { ResultList } from "./ResultList.js";
20
+ import type { Pagefind } from "./types.js";
21
+
22
+ const IndexingDialog = lazy(() => import("./IndexingDialog.js"));
23
+
24
+ const DEFAULT_RANKING = {
25
+ // Slightly lower than default because API docs tend to have repetitive terms (parameter names, HTTP methods, etc.)
26
+ termFrequency: 0.8,
27
+ // Lower than default because API documentation pages tend to be longer due to comprehensive endpoint documentation
28
+ pageLength: 0.6,
29
+ // Slightly higher than default because in technical documentation, exact matches should be prioritized
30
+ termSimilarity: 1.2,
31
+ // Slightly lower than default because API docs might have legitimate repetition of terms
32
+ termSaturation: 1.2,
33
+ };
34
+
35
+ const importPagefind = (basePath?: string): Promise<Pagefind> =>
36
+ import.meta.env.DEV
37
+ ? // @ts-expect-error TypeScript can't resolve the import
38
+ import(/* @vite-ignore */ "/pagefind/pagefind.js")
39
+ : import(/* @vite-ignore */ joinUrl(basePath, "/pagefind/pagefind.js"));
40
+
41
+ const usePagefind = (options: PagefindOptions) => {
42
+ const {
43
+ options: { basePath },
44
+ } = useZudoku();
45
+ const { data: pagefind, ...result } = useQuery<Pagefind>({
46
+ queryKey: ["pagefind", options.ranking],
47
+ retry: false,
48
+ queryFn: async () => {
49
+ const pagefind = await importPagefind(basePath);
50
+ await pagefind.init();
51
+ await pagefind.options({
52
+ ranking: {
53
+ termFrequency:
54
+ options.ranking?.termFrequency ?? DEFAULT_RANKING.termFrequency,
55
+ pageLength: options.ranking?.pageLength ?? DEFAULT_RANKING.pageLength,
56
+ termSimilarity:
57
+ options.ranking?.termSimilarity ?? DEFAULT_RANKING.termSimilarity,
58
+ termSaturation:
59
+ options.ranking?.termSaturation ?? DEFAULT_RANKING.termSaturation,
60
+ },
61
+ });
62
+
63
+ return pagefind;
64
+ },
65
+ enabled: typeof window !== "undefined",
66
+ });
67
+
68
+ if (result.isError && result.error.message !== "NOT_BUILT_YET") {
69
+ // biome-ignore lint/suspicious/noConsole: Logging allowed here
70
+ console.error(result.error);
71
+ }
72
+
73
+ return { ...result, pagefind };
74
+ };
75
+
76
+ export const PagefindSearch = ({
77
+ isOpen,
78
+ onClose,
79
+ options,
80
+ }: {
81
+ isOpen: boolean;
82
+ onClose: () => void;
83
+ options: PagefindOptions;
84
+ }) => {
85
+ const { pagefind, error, isError } = usePagefind(options);
86
+ const [searchTerm, setSearchTerm] = useState("");
87
+ const [selectedValue, setSelectedValue] = useState<string>("");
88
+ const auth = useAuthState();
89
+ const context = useZudoku();
90
+ const inputRef = useRef<HTMLInputElement>(null);
91
+
92
+ const { data: searchResults } = useQuery({
93
+ queryKey: ["pagefind-search", searchTerm, auth.isAuthenticated],
94
+ queryFn: async () => {
95
+ const filters = auth.isAuthenticated
96
+ ? undefined
97
+ : { not: { section: SEARCH_PROTECTED_SECTION } };
98
+
99
+ const search = await pagefind?.search(searchTerm, { filters });
100
+ if (!search) return [];
101
+ return getResults({ search, options, auth, context });
102
+ },
103
+ placeholderData: keepPreviousData,
104
+ enabled: !!pagefind && !!searchTerm,
105
+ });
106
+
107
+ useEffect(() => {
108
+ if (!searchResults?.length) return;
109
+
110
+ const firstResult = searchResults.at(0);
111
+ if (!firstResult) return;
112
+
113
+ const firstValue = `${firstResult.meta.title}-${firstResult.url}`;
114
+ setSelectedValue(firstValue);
115
+ }, [searchResults]);
116
+
117
+ return (
118
+ <CommandDialog
119
+ command={{
120
+ shouldFilter: false,
121
+ loop: true,
122
+ value: selectedValue,
123
+ onValueChange: setSelectedValue,
124
+ }}
125
+ content={{ className: "max-w-[750px]" }}
126
+ open={isOpen}
127
+ onOpenChange={onClose}
128
+ >
129
+ <VisuallyHidden>
130
+ <DialogTitle>Search</DialogTitle>
131
+ </VisuallyHidden>
132
+ <CommandInput
133
+ ref={inputRef}
134
+ placeholder="Search..."
135
+ value={searchTerm}
136
+ onValueChange={setSearchTerm}
137
+ disabled={isError}
138
+ />
139
+ <CommandEmpty>
140
+ {searchTerm ? (
141
+ <div className="flex flex-col items-center">
142
+ No results found.
143
+ <Button
144
+ variant="link"
145
+ onClick={() => {
146
+ setSearchTerm("");
147
+ inputRef.current?.focus();
148
+ }}
149
+ >
150
+ Clear search
151
+ </Button>
152
+ </div>
153
+ ) : (
154
+ "Start typing to search"
155
+ )}
156
+ </CommandEmpty>
157
+ {isError && error.message !== "NOT_BUILT_YET" ? (
158
+ <div className="p-4 text-sm">
159
+ An error occurred while loading search.
160
+ </div>
161
+ ) : (
162
+ <>
163
+ <ResultList
164
+ basePath={context.options.basePath}
165
+ searchResults={searchResults ?? []}
166
+ searchTerm={searchTerm}
167
+ onClose={onClose}
168
+ maxSubResults={options.maxSubResults}
169
+ />
170
+ <div className="flex justify-between p-2 items-center">
171
+ <div className="flex items-center text-xs text-muted-foreground">
172
+ <KbdGroup className="ms-0 me-1">
173
+ <Kbd>↑</Kbd>
174
+ <Kbd>↓</Kbd>
175
+ </KbdGroup>
176
+ Navigate
177
+ <KbdGroup className="ms-4 me-1">
178
+ <Kbd>↵</Kbd>
179
+ </KbdGroup>
180
+ Select
181
+ <KbdGroup className="ms-4 me-1">
182
+ <Kbd>Esc</Kbd>
183
+ </KbdGroup>
184
+ Close dialog
185
+ </div>
186
+ {import.meta.env.DEV && (
187
+ <IndexingDialog>
188
+ <Button
189
+ variant="outline"
190
+ className="h-7 text-xs text-muted-foreground"
191
+ >
192
+ <ListPlusIcon />
193
+ Build Search Index
194
+ </Button>
195
+ </IndexingDialog>
196
+ )}
197
+ </div>
198
+ </>
199
+ )}
200
+ </CommandDialog>
201
+ );
202
+ };
@@ -1,118 +1,122 @@
1
- import { BracketsIcon, FileTextIcon } from "lucide-react";
2
- import { useLayoutEffect, useRef } from "react";
3
- import { Link, useNavigate } from "react-router";
4
- import { CommandGroup, CommandItem, CommandList } from "@lukoweb/apitogo/ui/Command.js";
5
- import { joinUrl } from "../../util/joinUrl.js";
6
- import type { PagefindSearchFragment, PagefindSubResult } from "./types.js";
7
-
8
- const sortSubResults = (a: PagefindSubResult, b: PagefindSubResult) => {
9
- const aScore = a.weighted_locations.reduce(
10
- (sum, loc) => sum + loc.balanced_score,
11
- 0,
12
- );
13
- const bScore = b.weighted_locations.reduce(
14
- (sum, loc) => sum + loc.balanced_score,
15
- 0,
16
- );
17
- return bScore - aScore;
18
- };
19
-
20
- const hoverClassname = `cursor-pointer border border-transparent data-[selected=true]:border-border`;
21
-
22
- export const ResultList = ({
23
- basePath,
24
- searchResults,
25
- searchTerm,
26
- onClose,
27
- maxSubResults = 4,
28
- }: {
29
- basePath?: string;
30
- searchResults: PagefindSearchFragment[];
31
- searchTerm: string;
32
- onClose: () => void;
33
- maxSubResults?: number;
34
- }) => {
35
- const navigate = useNavigate();
36
- const commandListRef = useRef<HTMLDivElement | null>(null);
37
-
38
- useLayoutEffect(() => {
39
- if (!searchTerm) return;
40
- requestAnimationFrame(() => {
41
- commandListRef.current?.scrollTo({ top: 0 });
42
- });
43
- }, [searchTerm]);
44
-
45
- const stripBasePath = (url: string) => {
46
- if (basePath && url.startsWith(basePath)) {
47
- return joinUrl(url.slice(basePath.length));
48
- }
49
- return url;
50
- };
51
-
52
- return (
53
- <CommandList className="max-h-[450px]" ref={commandListRef}>
54
- {searchTerm && searchResults.length > 0 && (
55
- <CommandGroup
56
- className="text-sm text-muted-foreground"
57
- heading={`${searchResults.length} results for "${searchTerm}"`}
58
- />
59
- )}
60
- {searchTerm &&
61
- searchResults.map((result) => (
62
- <CommandGroup
63
- key={[result.meta.title ?? result.excerpt, result.url].join("-")}
64
- >
65
- <CommandItem
66
- asChild
67
- value={`${result.meta.title}-${result.url}`}
68
- className={hoverClassname}
69
- onSelect={() => {
70
- void navigate(stripBasePath(result.url));
71
- onClose();
72
- }}
73
- >
74
- <Link to={stripBasePath(result.url)}>
75
- {result.meta.section === "openapi" ? (
76
- <BracketsIcon />
77
- ) : (
78
- <FileTextIcon />
79
- )}
80
- {result.meta.category && (
81
- <span className="text-muted-foreground">
82
- {result.meta.category} &rsaquo;{" "}
83
- </span>
84
- )}
85
- {result.meta.title}
86
- </Link>
87
- </CommandItem>
88
- {result.sub_results
89
- .sort(sortSubResults)
90
- .slice(0, maxSubResults)
91
- .map((subResult) => (
92
- <CommandItem
93
- asChild
94
- key={`sub-${result.meta.title}-${subResult.url}`}
95
- value={`sub-${result.meta.title}-${subResult.url}`}
96
- className={hoverClassname}
97
- onSelect={() => {
98
- void navigate(stripBasePath(subResult.url));
99
- onClose();
100
- }}
101
- >
102
- <Link to={stripBasePath(subResult.url)} onClick={onClose}>
103
- <div className="flex flex-col items-start gap-2 ms-2.5 ps-5 border-l border-muted-foreground/50">
104
- <span className="font-bold">{subResult.title}</span>
105
- <span
106
- className="line-clamp-2 text-[13px] [&_mark]:bg-primary [&_mark]:text-primary-foreground"
107
- // biome-ignore lint/security/noDangerouslySetInnerHtml: Pagefind provides sanitized HTML
108
- dangerouslySetInnerHTML={{ __html: subResult.excerpt }}
109
- />
110
- </div>
111
- </Link>
112
- </CommandItem>
113
- ))}
114
- </CommandGroup>
115
- ))}
116
- </CommandList>
117
- );
118
- };
1
+ import {
2
+ CommandGroup,
3
+ CommandItem,
4
+ CommandList,
5
+ } from "@lukoweb/apitogo/ui/Command.js";
6
+ import { BracketsIcon, FileTextIcon } from "lucide-react";
7
+ import { useLayoutEffect, useRef } from "react";
8
+ import { Link, useNavigate } from "react-router";
9
+ import { joinUrl } from "../../util/joinUrl.js";
10
+ import type { PagefindSearchFragment, PagefindSubResult } from "./types.js";
11
+
12
+ const sortSubResults = (a: PagefindSubResult, b: PagefindSubResult) => {
13
+ const aScore = a.weighted_locations.reduce(
14
+ (sum, loc) => sum + loc.balanced_score,
15
+ 0,
16
+ );
17
+ const bScore = b.weighted_locations.reduce(
18
+ (sum, loc) => sum + loc.balanced_score,
19
+ 0,
20
+ );
21
+ return bScore - aScore;
22
+ };
23
+
24
+ const hoverClassname = `cursor-pointer border border-transparent data-[selected=true]:border-border`;
25
+
26
+ export const ResultList = ({
27
+ basePath,
28
+ searchResults,
29
+ searchTerm,
30
+ onClose,
31
+ maxSubResults = 4,
32
+ }: {
33
+ basePath?: string;
34
+ searchResults: PagefindSearchFragment[];
35
+ searchTerm: string;
36
+ onClose: () => void;
37
+ maxSubResults?: number;
38
+ }) => {
39
+ const navigate = useNavigate();
40
+ const commandListRef = useRef<HTMLDivElement | null>(null);
41
+
42
+ useLayoutEffect(() => {
43
+ if (!searchTerm) return;
44
+ requestAnimationFrame(() => {
45
+ commandListRef.current?.scrollTo({ top: 0 });
46
+ });
47
+ }, [searchTerm]);
48
+
49
+ const stripBasePath = (url: string) => {
50
+ if (basePath && url.startsWith(basePath)) {
51
+ return joinUrl(url.slice(basePath.length));
52
+ }
53
+ return url;
54
+ };
55
+
56
+ return (
57
+ <CommandList className="max-h-[450px]" ref={commandListRef}>
58
+ {searchTerm && searchResults.length > 0 && (
59
+ <CommandGroup
60
+ className="text-sm text-muted-foreground"
61
+ heading={`${searchResults.length} results for "${searchTerm}"`}
62
+ />
63
+ )}
64
+ {searchTerm &&
65
+ searchResults.map((result) => (
66
+ <CommandGroup
67
+ key={[result.meta.title ?? result.excerpt, result.url].join("-")}
68
+ >
69
+ <CommandItem
70
+ asChild
71
+ value={`${result.meta.title}-${result.url}`}
72
+ className={hoverClassname}
73
+ onSelect={() => {
74
+ void navigate(stripBasePath(result.url));
75
+ onClose();
76
+ }}
77
+ >
78
+ <Link to={stripBasePath(result.url)}>
79
+ {result.meta.section === "openapi" ? (
80
+ <BracketsIcon />
81
+ ) : (
82
+ <FileTextIcon />
83
+ )}
84
+ {result.meta.category && (
85
+ <span className="text-muted-foreground">
86
+ {result.meta.category} &rsaquo;{" "}
87
+ </span>
88
+ )}
89
+ {result.meta.title}
90
+ </Link>
91
+ </CommandItem>
92
+ {result.sub_results
93
+ .sort(sortSubResults)
94
+ .slice(0, maxSubResults)
95
+ .map((subResult) => (
96
+ <CommandItem
97
+ asChild
98
+ key={`sub-${result.meta.title}-${subResult.url}`}
99
+ value={`sub-${result.meta.title}-${subResult.url}`}
100
+ className={hoverClassname}
101
+ onSelect={() => {
102
+ void navigate(stripBasePath(subResult.url));
103
+ onClose();
104
+ }}
105
+ >
106
+ <Link to={stripBasePath(subResult.url)} onClick={onClose}>
107
+ <div className="flex flex-col items-start gap-2 ms-2.5 ps-5 border-l border-muted-foreground/50">
108
+ <span className="font-bold">{subResult.title}</span>
109
+ <span
110
+ className="line-clamp-2 text-[13px] [&_mark]:bg-primary [&_mark]:text-primary-foreground"
111
+ // biome-ignore lint/security/noDangerouslySetInnerHtml: Pagefind provides sanitized HTML
112
+ dangerouslySetInnerHTML={{ __html: subResult.excerpt }}
113
+ />
114
+ </div>
115
+ </Link>
116
+ </CommandItem>
117
+ ))}
118
+ </CommandGroup>
119
+ ))}
120
+ </CommandList>
121
+ );
122
+ };