@btst/plugin-search 1.0.0-beta.2 → 1.1.0

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 (48) hide show
  1. package/dist/api/index.cjs +2 -0
  2. package/dist/api/index.d.cts +3 -2
  3. package/dist/api/index.d.mts +3 -2
  4. package/dist/api/index.d.ts +3 -2
  5. package/dist/api/index.mjs +1 -1
  6. package/dist/api/operations.cjs +20 -18
  7. package/dist/api/operations.mjs +15 -13
  8. package/dist/client/components/index.cjs +2 -0
  9. package/dist/client/components/index.d.cts +15 -7
  10. package/dist/client/components/index.d.mts +15 -7
  11. package/dist/client/components/index.d.ts +15 -7
  12. package/dist/client/components/index.mjs +1 -0
  13. package/dist/client/components/pages/search-page.internal.cjs +12 -4
  14. package/dist/client/components/pages/search-page.internal.mjs +12 -4
  15. package/dist/client/components/search-dialog.cjs +93 -57
  16. package/dist/client/components/search-dialog.mjs +94 -58
  17. package/dist/client/components/search-highlight.cjs +22 -0
  18. package/dist/client/components/search-highlight.mjs +20 -0
  19. package/dist/client/components/search-results.cjs +31 -24
  20. package/dist/client/components/search-results.mjs +32 -25
  21. package/dist/client/hooks/use-search-state.cjs +1 -1
  22. package/dist/client/hooks/use-search-state.mjs +1 -1
  23. package/dist/client/index.d.cts +3 -12
  24. package/dist/client/index.d.mts +3 -12
  25. package/dist/client/index.d.ts +3 -12
  26. package/dist/constants.cjs +11 -0
  27. package/dist/constants.mjs +6 -0
  28. package/dist/providers.cjs +21 -2
  29. package/dist/providers.d.cts +10 -3
  30. package/dist/providers.d.mts +10 -3
  31. package/dist/providers.d.ts +10 -3
  32. package/dist/providers.mjs +20 -3
  33. package/dist/query-keys.cjs +2 -1
  34. package/dist/query-keys.mjs +2 -1
  35. package/dist/response-schema.cjs +57 -0
  36. package/dist/response-schema.mjs +40 -0
  37. package/dist/schemas.cjs +10 -9
  38. package/dist/schemas.d.cts +39 -8
  39. package/dist/schemas.d.mts +39 -8
  40. package/dist/schemas.d.ts +39 -8
  41. package/dist/schemas.mjs +5 -5
  42. package/dist/shared/plugin-search.BDlgoPEb.d.ts +18 -0
  43. package/dist/shared/plugin-search.QfsGS8no.d.cts +18 -0
  44. package/dist/shared/plugin-search.WwLtD6lV.d.mts +18 -0
  45. package/dist/types.d.cts +4 -2
  46. package/dist/types.d.mts +4 -2
  47. package/dist/types.d.ts +4 -2
  48. package/package.json +4 -4
@@ -13,5 +13,7 @@ exports.searchInputSchema = schemas.searchInputSchema;
13
13
  exports.blogSearchProvider = providers.blogSearchProvider;
14
14
  exports.cmsSearchProvider = providers.cmsSearchProvider;
15
15
  exports.ecommerceSearchProvider = providers.ecommerceSearchProvider;
16
+ exports.normalizeSearchText = providers.normalizeSearchText;
16
17
  exports.schoolSearchProvider = providers.schoolSearchProvider;
18
+ exports.searchRelevance = providers.searchRelevance;
17
19
  exports.searchSnippet = providers.searchSnippet;
@@ -7,7 +7,8 @@ import { SearchBackendConfig, SearchResponse } from '../types.cjs';
7
7
  export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.cjs';
8
8
  export { searchPermissions } from '../permissions.cjs';
9
9
  export { searchInputSchema } from '../schemas.cjs';
10
- export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.cjs';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, normalizeSearchText, schoolSearchProvider, searchRelevance, searchSnippet } from '../providers.cjs';
11
+ import 'zod/mini';
11
12
 
12
13
  declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
14
  search: better_call.StrictEndpoint<"/search", {
@@ -28,8 +29,8 @@ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack
28
29
  }>;
29
30
  }, {
30
31
  readonly items: readonly {
31
- readonly resourceId: string;
32
32
  readonly title: string;
33
+ readonly resourceId: string;
33
34
  readonly snippet: string;
34
35
  readonly href: string;
35
36
  readonly key: string;
@@ -7,7 +7,8 @@ import { SearchBackendConfig, SearchResponse } from '../types.mjs';
7
7
  export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.mjs';
8
8
  export { searchPermissions } from '../permissions.mjs';
9
9
  export { searchInputSchema } from '../schemas.mjs';
10
- export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.mjs';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, normalizeSearchText, schoolSearchProvider, searchRelevance, searchSnippet } from '../providers.mjs';
11
+ import 'zod/mini';
11
12
 
12
13
  declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
14
  search: better_call.StrictEndpoint<"/search", {
@@ -28,8 +29,8 @@ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack
28
29
  }>;
29
30
  }, {
30
31
  readonly items: readonly {
31
- readonly resourceId: string;
32
32
  readonly title: string;
33
+ readonly resourceId: string;
33
34
  readonly snippet: string;
34
35
  readonly href: string;
35
36
  readonly key: string;
@@ -7,7 +7,8 @@ import { SearchBackendConfig, SearchResponse } from '../types.js';
7
7
  export { SearchInput, SearchProvider, SearchProviderResult, SearchResult, SearchSource } from '../types.js';
8
8
  export { searchPermissions } from '../permissions.js';
9
9
  export { searchInputSchema } from '../schemas.js';
10
- export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.js';
10
+ export { BlogSearchOperations, EcommerceSearchOperations, SchoolSearchOperations, blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, normalizeSearchText, schoolSearchProvider, searchRelevance, searchSnippet } from '../providers.js';
11
+ import 'zod/mini';
11
12
 
12
13
  declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack_plugins_api.BackendPlugin<{
13
14
  search: better_call.StrictEndpoint<"/search", {
@@ -28,8 +29,8 @@ declare const searchBackendPlugin: (config?: SearchBackendConfig) => _btst_stack
28
29
  }>;
29
30
  }, {
30
31
  readonly items: readonly {
31
- readonly resourceId: string;
32
32
  readonly title: string;
33
+ readonly resourceId: string;
33
34
  readonly snippet: string;
34
35
  readonly href: string;
35
36
  readonly key: string;
@@ -1,4 +1,4 @@
1
1
  export { searchBackendPlugin } from './plugin.mjs';
2
2
  export { searchPermissions } from '../permissions.mjs';
3
3
  export { searchInputSchema } from '../schemas.mjs';
4
- export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, schoolSearchProvider, searchSnippet } from '../providers.mjs';
4
+ export { blogSearchProvider, cmsSearchProvider, ecommerceSearchProvider, normalizeSearchText, schoolSearchProvider, searchRelevance, searchSnippet } from '../providers.mjs';
@@ -5,19 +5,13 @@ const zod = require('zod');
5
5
  const permissions = require('../permissions.cjs');
6
6
  const providers = require('../providers.cjs');
7
7
  const schemas = require('../schemas.cjs');
8
+ const constants = require('../constants.cjs');
9
+ const responseSchema = require('../response-schema.cjs');
8
10
 
9
- const resultSchema = zod.z.object({
10
- resourceId: zod.z.string().min(1).max(200),
11
- title: zod.z.string().min(1).max(500),
12
- snippet: zod.z.string().max(500),
13
- href: zod.z.string().max(2e3).refine(
14
- (value) => value.startsWith("/") && !value.startsWith("//") && !/[\\\u0000-\u0020]/.test(value),
15
- "Search destinations must be application-relative paths"
16
- )
17
- });
11
+ const aliasesSchema = zod.z.array(zod.z.string().max(500)).max(100).optional();
18
12
  function createSearchOperations(providers$1) {
19
- if (providers$1.length > schemas.SEARCH_MAX_SOURCES) {
20
- throw new Error(`Search supports at most ${schemas.SEARCH_MAX_SOURCES} providers`);
13
+ if (providers$1.length > constants.SEARCH_MAX_SOURCES) {
14
+ throw new Error(`Search supports at most ${constants.SEARCH_MAX_SOURCES} providers`);
21
15
  }
22
16
  const sorted = [...providers$1].sort(
23
17
  (a, b) => a.id < b.id ? -1 : a.id > b.id ? 1 : 0
@@ -68,23 +62,29 @@ function createSearchOperations(providers$1) {
68
62
  };
69
63
  if (!input.q) return { ...empty, items: [] };
70
64
  let truncated = false;
65
+ const relevance = /* @__PURE__ */ new Map();
71
66
  const pages = await Promise.all(
72
67
  sorted.filter((provider) => facts.sources.includes(provider.id)).map(async (provider) => {
73
68
  try {
74
69
  const page = await provider.search({
75
70
  q: input.q,
76
- limit: schemas.SEARCH_MAX_RESULTS + 1,
71
+ limit: constants.SEARCH_MAX_RESULTS + 1,
77
72
  request,
78
73
  identity
79
74
  });
80
- if (page.items.length > schemas.SEARCH_MAX_RESULTS + 1 || typeof page.hasMore !== "boolean")
75
+ if (page.items.length > constants.SEARCH_MAX_RESULTS + 1 || typeof page.hasMore !== "boolean")
81
76
  throw new Error("Invalid provider page");
82
77
  truncated ||= page.hasMore;
83
78
  const seen = /* @__PURE__ */ new Set();
84
79
  return page.items.flatMap((record) => {
85
- const item = resultSchema.parse(record);
80
+ const item = responseSchema.searchProviderResultSchema.parse(record);
81
+ const aliases = aliasesSchema.parse(record.aliases);
86
82
  if (seen.has(item.resourceId)) return [];
87
83
  seen.add(item.resourceId);
84
+ relevance.set(
85
+ `${provider.id}:${item.resourceId}`,
86
+ providers.searchRelevance({ ...item, aliases }, input.q)
87
+ );
88
88
  return [
89
89
  {
90
90
  ...item,
@@ -106,18 +106,20 @@ function createSearchOperations(providers$1) {
106
106
  }
107
107
  })
108
108
  );
109
- const items = pages.flat();
109
+ const items = pages.flat().sort(
110
+ (a, b) => relevance.get(a.key) - relevance.get(b.key) || (a.sourceId < b.sourceId ? -1 : a.sourceId > b.sourceId ? 1 : 0) || (a.resourceId < b.resourceId ? -1 : a.resourceId > b.resourceId ? 1 : 0)
111
+ );
110
112
  const end = input.page * input.limit;
111
- const hasMore = input.page < schemas.SEARCH_MAX_PAGES && end < Math.min(items.length, schemas.SEARCH_MAX_RESULTS);
113
+ const hasMore = input.page < constants.SEARCH_MAX_PAGES && end < Math.min(items.length, constants.SEARCH_MAX_RESULTS);
112
114
  return {
113
115
  ...empty,
114
116
  items: items.slice(
115
117
  end - input.limit,
116
- Math.min(end, schemas.SEARCH_MAX_RESULTS)
118
+ Math.min(end, constants.SEARCH_MAX_RESULTS)
117
119
  ),
118
120
  hasMore,
119
121
  nextPage: hasMore ? input.page + 1 : null,
120
- truncated: truncated || items.length > Math.min(schemas.SEARCH_MAX_RESULTS, input.limit * schemas.SEARCH_MAX_PAGES)
122
+ truncated: truncated || items.length > Math.min(constants.SEARCH_MAX_RESULTS, input.limit * constants.SEARCH_MAX_PAGES)
121
123
  };
122
124
  }
123
125
  })
@@ -1,18 +1,12 @@
1
1
  import { defineOperation, OperationHttpError } from '@btst/stack/plugins/api';
2
2
  import { z } from 'zod';
3
3
  import { searchPermissions } from '../permissions.mjs';
4
- import { searchSnippet } from '../providers.mjs';
5
- import { SEARCH_MAX_SOURCES, searchSourceIdSchema, searchInputSchema, SEARCH_MAX_RESULTS, SEARCH_MAX_PAGES } from '../schemas.mjs';
4
+ import { searchRelevance, searchSnippet } from '../providers.mjs';
5
+ import { searchSourceIdSchema, searchInputSchema } from '../schemas.mjs';
6
+ import { SEARCH_MAX_SOURCES, SEARCH_MAX_RESULTS, SEARCH_MAX_PAGES } from '../constants.mjs';
7
+ import { searchProviderResultSchema } from '../response-schema.mjs';
6
8
 
7
- const resultSchema = z.object({
8
- resourceId: z.string().min(1).max(200),
9
- title: z.string().min(1).max(500),
10
- snippet: z.string().max(500),
11
- href: z.string().max(2e3).refine(
12
- (value) => value.startsWith("/") && !value.startsWith("//") && !/[\\\u0000-\u0020]/.test(value),
13
- "Search destinations must be application-relative paths"
14
- )
15
- });
9
+ const aliasesSchema = z.array(z.string().max(500)).max(100).optional();
16
10
  function createSearchOperations(providers) {
17
11
  if (providers.length > SEARCH_MAX_SOURCES) {
18
12
  throw new Error(`Search supports at most ${SEARCH_MAX_SOURCES} providers`);
@@ -66,6 +60,7 @@ function createSearchOperations(providers) {
66
60
  };
67
61
  if (!input.q) return { ...empty, items: [] };
68
62
  let truncated = false;
63
+ const relevance = /* @__PURE__ */ new Map();
69
64
  const pages = await Promise.all(
70
65
  sorted.filter((provider) => facts.sources.includes(provider.id)).map(async (provider) => {
71
66
  try {
@@ -80,9 +75,14 @@ function createSearchOperations(providers) {
80
75
  truncated ||= page.hasMore;
81
76
  const seen = /* @__PURE__ */ new Set();
82
77
  return page.items.flatMap((record) => {
83
- const item = resultSchema.parse(record);
78
+ const item = searchProviderResultSchema.parse(record);
79
+ const aliases = aliasesSchema.parse(record.aliases);
84
80
  if (seen.has(item.resourceId)) return [];
85
81
  seen.add(item.resourceId);
82
+ relevance.set(
83
+ `${provider.id}:${item.resourceId}`,
84
+ searchRelevance({ ...item, aliases }, input.q)
85
+ );
86
86
  return [
87
87
  {
88
88
  ...item,
@@ -104,7 +104,9 @@ function createSearchOperations(providers) {
104
104
  }
105
105
  })
106
106
  );
107
- const items = pages.flat();
107
+ const items = pages.flat().sort(
108
+ (a, b) => relevance.get(a.key) - relevance.get(b.key) || (a.sourceId < b.sourceId ? -1 : a.sourceId > b.sourceId ? 1 : 0) || (a.resourceId < b.resourceId ? -1 : a.resourceId > b.resourceId ? 1 : 0)
109
+ );
108
110
  const end = input.page * input.limit;
109
111
  const hasMore = input.page < SEARCH_MAX_PAGES && end < Math.min(items.length, SEARCH_MAX_RESULTS);
110
112
  return {
@@ -3,9 +3,11 @@
3
3
 
4
4
  const searchDialog = require('./search-dialog.cjs');
5
5
  const searchPage = require('./pages/search-page.cjs');
6
+ const searchHighlight = require('./search-highlight.cjs');
6
7
 
7
8
 
8
9
 
9
10
  exports.SearchDialog = searchDialog.SearchDialog;
10
11
  exports.SearchInput = searchDialog.SearchInput;
11
12
  exports.SearchPageComponent = searchPage.SearchPageComponent;
13
+ exports.SearchHighlight = searchHighlight.SearchHighlight;
@@ -1,23 +1,23 @@
1
1
  'use client';
2
2
  import * as react from 'react';
3
+ import { SearchPluginOverrides } from '../index.cjs';
3
4
  import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.cjs';
5
+ import '../../types.cjs';
6
+ import '@btst/stack/plugins/api';
4
7
  import '@btst/stack/client';
5
8
 
6
9
  /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
- interface SearchPresentationProps {
10
+ interface SearchPresentationProps extends SearchPluginOverrides {
8
11
  /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
12
  sources?: readonly string[];
10
13
  className?: string;
11
- placeholder?: string;
12
- emptyMessage?: string;
13
- title?: string;
14
14
  }
15
15
  /** Controlled discovery dialog with keyboard selection and router navigation. */
16
16
  interface SearchDialogProps extends SearchPresentationProps {
17
17
  open: boolean;
18
18
  onOpenChange: (open: boolean) => void;
19
19
  }
20
- declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, renderResult, renderLoading, }: SearchDialogProps): react.JSX.Element;
21
21
  /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
22
  interface SearchInputProps extends SearchPresentationProps {
23
23
  triggerClassName?: string;
@@ -31,5 +31,13 @@ declare function SearchPageComponent({ initialSearch, }: {
31
31
  initialSearch?: SearchListState;
32
32
  }): react.JSX.Element;
33
33
 
34
- export { SearchDialog, SearchInput, SearchPageComponent };
35
- export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
34
+ /** Plain result text and the literal phrase to emphasize. */
35
+ interface SearchHighlightProps {
36
+ text: string;
37
+ query: string;
38
+ }
39
+ /** Highlights case-insensitive phrase matches while preserving the original text. */
40
+ declare function SearchHighlight({ text, query }: SearchHighlightProps): string | (string | react.JSX.Element)[];
41
+
42
+ export { SearchDialog, SearchHighlight, SearchInput, SearchPageComponent };
43
+ export type { SearchDialogProps, SearchHighlightProps, SearchInputProps, SearchPresentationProps };
@@ -1,23 +1,23 @@
1
1
  'use client';
2
2
  import * as react from 'react';
3
+ import { SearchPluginOverrides } from '../index.mjs';
3
4
  import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.mjs';
5
+ import '../../types.mjs';
6
+ import '@btst/stack/plugins/api';
4
7
  import '@btst/stack/client';
5
8
 
6
9
  /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
- interface SearchPresentationProps {
10
+ interface SearchPresentationProps extends SearchPluginOverrides {
8
11
  /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
12
  sources?: readonly string[];
10
13
  className?: string;
11
- placeholder?: string;
12
- emptyMessage?: string;
13
- title?: string;
14
14
  }
15
15
  /** Controlled discovery dialog with keyboard selection and router navigation. */
16
16
  interface SearchDialogProps extends SearchPresentationProps {
17
17
  open: boolean;
18
18
  onOpenChange: (open: boolean) => void;
19
19
  }
20
- declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, renderResult, renderLoading, }: SearchDialogProps): react.JSX.Element;
21
21
  /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
22
  interface SearchInputProps extends SearchPresentationProps {
23
23
  triggerClassName?: string;
@@ -31,5 +31,13 @@ declare function SearchPageComponent({ initialSearch, }: {
31
31
  initialSearch?: SearchListState;
32
32
  }): react.JSX.Element;
33
33
 
34
- export { SearchDialog, SearchInput, SearchPageComponent };
35
- export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
34
+ /** Plain result text and the literal phrase to emphasize. */
35
+ interface SearchHighlightProps {
36
+ text: string;
37
+ query: string;
38
+ }
39
+ /** Highlights case-insensitive phrase matches while preserving the original text. */
40
+ declare function SearchHighlight({ text, query }: SearchHighlightProps): string | (string | react.JSX.Element)[];
41
+
42
+ export { SearchDialog, SearchHighlight, SearchInput, SearchPageComponent };
43
+ export type { SearchDialogProps, SearchHighlightProps, SearchInputProps, SearchPresentationProps };
@@ -1,23 +1,23 @@
1
1
  'use client';
2
2
  import * as react from 'react';
3
+ import { SearchPluginOverrides } from '../index.js';
3
4
  import { S as SearchListState } from '../../shared/plugin-search.5GvThkZW.js';
5
+ import '../../types.js';
6
+ import '@btst/stack/plugins/api';
4
7
  import '@btst/stack/client';
5
8
 
6
9
  /** Shared discovery options. Supplying sources fixes the allowed UI scope. */
7
- interface SearchPresentationProps {
10
+ interface SearchPresentationProps extends SearchPluginOverrides {
8
11
  /** Fixed provider IDs; URL source filters cannot expand this selection. */
9
12
  sources?: readonly string[];
10
13
  className?: string;
11
- placeholder?: string;
12
- emptyMessage?: string;
13
- title?: string;
14
14
  }
15
15
  /** Controlled discovery dialog with keyboard selection and router navigation. */
16
16
  interface SearchDialogProps extends SearchPresentationProps {
17
17
  open: boolean;
18
18
  onOpenChange: (open: boolean) => void;
19
19
  }
20
- declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, }: SearchDialogProps): react.JSX.Element;
20
+ declare function SearchDialog({ open, onOpenChange, sources, className, placeholder, emptyMessage, title, renderResult, renderLoading, }: SearchDialogProps): react.JSX.Element;
21
21
  /** Trigger for the shared dialog, including the optional Cmd/Ctrl+K shortcut. */
22
22
  interface SearchInputProps extends SearchPresentationProps {
23
23
  triggerClassName?: string;
@@ -31,5 +31,13 @@ declare function SearchPageComponent({ initialSearch, }: {
31
31
  initialSearch?: SearchListState;
32
32
  }): react.JSX.Element;
33
33
 
34
- export { SearchDialog, SearchInput, SearchPageComponent };
35
- export type { SearchDialogProps, SearchInputProps, SearchPresentationProps };
34
+ /** Plain result text and the literal phrase to emphasize. */
35
+ interface SearchHighlightProps {
36
+ text: string;
37
+ query: string;
38
+ }
39
+ /** Highlights case-insensitive phrase matches while preserving the original text. */
40
+ declare function SearchHighlight({ text, query }: SearchHighlightProps): string | (string | react.JSX.Element)[];
41
+
42
+ export { SearchDialog, SearchHighlight, SearchInput, SearchPageComponent };
43
+ export type { SearchDialogProps, SearchHighlightProps, SearchInputProps, SearchPresentationProps };
@@ -1,3 +1,4 @@
1
1
  'use client';
2
2
  export { SearchDialog, SearchInput } from './search-dialog.mjs';
3
3
  export { SearchPageComponent } from './pages/search-page.mjs';
4
+ export { SearchHighlight } from './search-highlight.mjs';
@@ -64,7 +64,7 @@ function SearchPage({
64
64
  "aria-label": "Search results",
65
65
  "aria-busy": loading,
66
66
  children: [
67
- /* @__PURE__ */ jsxRuntime.jsx("div", { "aria-live": "polite", children: /* @__PURE__ */ jsxRuntime.jsx(
67
+ /* @__PURE__ */ jsxRuntime.jsx(
68
68
  searchResults.SearchStatus,
69
69
  {
70
70
  query: state.q,
@@ -72,10 +72,18 @@ function SearchPage({
72
72
  loading,
73
73
  error: result.error,
74
74
  emptyMessage: overrides.emptyMessage,
75
- onRetry: () => void result.refetch()
75
+ onRetry: () => void result.refetch(),
76
+ renderLoading: overrides.renderLoading
76
77
  }
77
- ) }),
78
- !result.error && !loading && result.data && /* @__PURE__ */ jsxRuntime.jsx(searchResults.SearchResults, { data: result.data }),
78
+ ),
79
+ !result.error && !loading && result.data && /* @__PURE__ */ jsxRuntime.jsx(
80
+ searchResults.SearchResults,
81
+ {
82
+ data: result.data,
83
+ query: state.q,
84
+ renderResult: overrides.renderResult
85
+ }
86
+ ),
79
87
  !result.error && state.q.trim() && result.data && /* @__PURE__ */ jsxRuntime.jsx(
80
88
  searchResults.SearchPagination,
81
89
  {
@@ -62,7 +62,7 @@ function SearchPage({
62
62
  "aria-label": "Search results",
63
63
  "aria-busy": loading,
64
64
  children: [
65
- /* @__PURE__ */ jsx("div", { "aria-live": "polite", children: /* @__PURE__ */ jsx(
65
+ /* @__PURE__ */ jsx(
66
66
  SearchStatus,
67
67
  {
68
68
  query: state.q,
@@ -70,10 +70,18 @@ function SearchPage({
70
70
  loading,
71
71
  error: result.error,
72
72
  emptyMessage: overrides.emptyMessage,
73
- onRetry: () => void result.refetch()
73
+ onRetry: () => void result.refetch(),
74
+ renderLoading: overrides.renderLoading
74
75
  }
75
- ) }),
76
- !result.error && !loading && result.data && /* @__PURE__ */ jsx(SearchResults, { data: result.data }),
76
+ ),
77
+ !result.error && !loading && result.data && /* @__PURE__ */ jsx(
78
+ SearchResults,
79
+ {
80
+ data: result.data,
81
+ query: state.q,
82
+ renderResult: overrides.renderResult
83
+ }
84
+ ),
77
85
  !result.error && state.q.trim() && result.data && /* @__PURE__ */ jsx(
78
86
  SearchPagination,
79
87
  {
@@ -3,12 +3,14 @@
3
3
 
4
4
  const jsxRuntime = require('react/jsx-runtime');
5
5
  const react = require('react');
6
+ const utils = require('../../ui/src/lib/utils.cjs');
6
7
  const context = require('@btst/stack/context');
7
8
  const button = require('../../ui/src/components/button.cjs');
8
9
  const command = require('../../ui/src/components/command.cjs');
9
10
  const dialog = require('../../ui/src/components/dialog.cjs');
10
11
  const searchResults = require('./search-results.cjs');
11
12
  const sourceFilter = require('./source-filter.cjs');
13
+ const searchHighlight = require('./search-highlight.cjs');
12
14
  const useSearchState = require('../hooks/use-search-state.cjs');
13
15
 
14
16
  function SearchDialog({
@@ -18,9 +20,12 @@ function SearchDialog({
18
20
  className,
19
21
  placeholder = "Search content\u2026",
20
22
  emptyMessage,
21
- title = "Search"
23
+ title = "Search",
24
+ renderResult,
25
+ renderLoading
22
26
  }) {
23
27
  const { router } = context.useStack();
28
+ const inputRef = react.useRef(null);
24
29
  const { state, setState, result, fixedSources, loading } = useSearchState.useSearchState({
25
30
  sources,
26
31
  enabled: open
@@ -30,32 +35,71 @@ function SearchDialog({
30
35
  else window.location.assign(href);
31
36
  onOpenChange(false);
32
37
  };
33
- return /* @__PURE__ */ jsxRuntime.jsx(dialog.Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogContent, { className, "data-testid": "search-modal", children: [
34
- /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogHeader, { children: [
35
- /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogTitle, { children: title }),
36
- /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogDescription, { children: "Find content across the available sources." })
37
- ] }),
38
- /* @__PURE__ */ jsxRuntime.jsxs(command.Command, { shouldFilter: false, children: [
39
- /* @__PURE__ */ jsxRuntime.jsx(
40
- command.CommandInput,
41
- {
42
- "aria-label": "Search",
43
- "data-testid": "search-input",
44
- placeholder,
45
- value: state.q,
46
- maxLength: 200,
47
- onValueChange: (q) => setState({ q, page: 1 }, { replace: true })
48
- }
38
+ return /* @__PURE__ */ jsxRuntime.jsx(dialog.Dialog, { open, onOpenChange, children: /* @__PURE__ */ jsxRuntime.jsxs(
39
+ dialog.DialogContent,
40
+ {
41
+ className: utils.cn(
42
+ "max-h-[calc(100dvh-2rem)] overflow-y-auto motion-reduce:animate-none",
43
+ className
49
44
  ),
50
- !fixedSources && result.data && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-3", children: /* @__PURE__ */ jsxRuntime.jsx(
51
- sourceFilter.SourceFilter,
52
- {
53
- sources: result.data.sources,
54
- value: state.sources,
55
- onChange: (value) => setState({ sources: value, page: 1 })
56
- }
57
- ) }),
58
- /* @__PURE__ */ jsxRuntime.jsxs(command.CommandList, { "aria-busy": loading, className: "max-h-[400px]", children: [
45
+ "data-testid": "search-modal",
46
+ children: [
47
+ /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogHeader, { children: [
48
+ /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogTitle, { children: title }),
49
+ /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogDescription, { children: "Find content across the available sources." })
50
+ ] }),
51
+ /* @__PURE__ */ jsxRuntime.jsxs(command.Command, { shouldFilter: false, className: "h-auto min-w-0", children: [
52
+ /* @__PURE__ */ jsxRuntime.jsx(
53
+ command.CommandInput,
54
+ {
55
+ ref: inputRef,
56
+ "aria-label": "Search",
57
+ "data-testid": "search-input",
58
+ placeholder,
59
+ value: state.q,
60
+ maxLength: 200,
61
+ onValueChange: (q) => setState({ q, page: 1 }, { replace: true })
62
+ }
63
+ ),
64
+ !fixedSources && result.data && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "p-3", children: /* @__PURE__ */ jsxRuntime.jsx(
65
+ sourceFilter.SourceFilter,
66
+ {
67
+ sources: result.data.sources,
68
+ value: state.sources,
69
+ onChange: (value) => setState({ sources: value, page: 1 })
70
+ }
71
+ ) }),
72
+ /* @__PURE__ */ jsxRuntime.jsx(
73
+ command.CommandList,
74
+ {
75
+ "aria-label": "Search results",
76
+ "aria-busy": loading,
77
+ className: "max-h-[min(400px,40dvh)]",
78
+ children: !result.error && !loading && result.data?.items.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
79
+ command.CommandItem,
80
+ {
81
+ value: item.key,
82
+ "aria-label": `${item.title}, ${item.sourceLabel}`,
83
+ "data-testid": "search-result",
84
+ onSelect: () => void navigate(item.href),
85
+ className: "flex min-w-0 flex-col items-start gap-2 border-b px-4 py-3 [overflow-wrap:anywhere]",
86
+ children: renderResult ? renderResult(item, state.q) : /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
87
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-xs text-muted-foreground", children: item.sourceLabel }),
88
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "font-medium", children: /* @__PURE__ */ jsxRuntime.jsx(searchHighlight.SearchHighlight, { text: item.title, query: state.q }) }),
89
+ item.snippet && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-sm text-muted-foreground", children: /* @__PURE__ */ jsxRuntime.jsx(
90
+ searchHighlight.SearchHighlight,
91
+ {
92
+ text: item.snippet,
93
+ query: state.q
94
+ }
95
+ ) })
96
+ ] })
97
+ },
98
+ item.key
99
+ ))
100
+ }
101
+ )
102
+ ] }),
59
103
  /* @__PURE__ */ jsxRuntime.jsx(
60
104
  searchResults.SearchStatus,
61
105
  {
@@ -64,39 +108,28 @@ function SearchDialog({
64
108
  loading,
65
109
  error: result.error,
66
110
  emptyMessage,
67
- onRetry: () => void result.refetch()
111
+ onRetry: () => {
112
+ void result.refetch();
113
+ inputRef.current?.focus();
114
+ },
115
+ renderLoading
68
116
  }
69
117
  ),
70
- !result.error && !loading && result.data?.items.map((item) => /* @__PURE__ */ jsxRuntime.jsxs(
71
- command.CommandItem,
72
- {
73
- value: item.key,
74
- "data-testid": "search-result",
75
- onSelect: () => void navigate(item.href),
76
- className: "flex flex-col items-start gap-2 border-b px-4 py-3",
77
- children: [
78
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-xs text-muted-foreground", children: item.sourceLabel }),
79
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "font-medium", children: item.title }),
80
- item.snippet && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-sm text-muted-foreground", children: item.snippet })
81
- ]
82
- },
83
- item.key
84
- ))
85
- ] })
86
- ] }),
87
- !result.error && state.q.trim() && result.data && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
88
- result.data.truncated && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-muted-foreground", children: "Showing a limited result window. Refine your query to find more content." }),
89
- /* @__PURE__ */ jsxRuntime.jsx(
90
- searchResults.SearchPagination,
91
- {
92
- page: state.page,
93
- hasMore: result.data.hasMore,
94
- loading,
95
- onPageChange: (page) => setState({ page })
96
- }
97
- )
98
- ] })
99
- ] }) });
118
+ !result.error && state.q.trim() && result.data && /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
119
+ result.data.truncated && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-xs text-muted-foreground", children: "Showing a limited result window. Refine your query to find more content." }),
120
+ /* @__PURE__ */ jsxRuntime.jsx(
121
+ searchResults.SearchPagination,
122
+ {
123
+ page: state.page,
124
+ hasMore: result.data.hasMore,
125
+ loading,
126
+ onPageChange: (page) => setState({ page })
127
+ }
128
+ )
129
+ ] })
130
+ ]
131
+ }
132
+ ) });
100
133
  }
101
134
  function SearchInput({
102
135
  triggerClassName,
@@ -105,6 +138,8 @@ function SearchInput({
105
138
  ...props
106
139
  }) {
107
140
  const [open, setOpen] = react.useState(false);
141
+ const [hydrated, setHydrated] = react.useState(false);
142
+ react.useEffect(() => setHydrated(true), []);
108
143
  const trigger = react.useRef(null);
109
144
  const onOpenChange = (nextOpen) => {
110
145
  setOpen(nextOpen);
@@ -131,6 +166,7 @@ function SearchInput({
131
166
  variant: "outline",
132
167
  className: triggerClassName,
133
168
  "data-testid": "search-button",
169
+ disabled: !hydrated,
134
170
  "aria-haspopup": "dialog",
135
171
  "aria-expanded": open,
136
172
  onClick: () => setOpen(true),