@dxos/plugin-search 0.8.4-main.f5c0578 → 0.8.4-main.fcfe5033a5

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 (141) hide show
  1. package/README.md +0 -1
  2. package/dist/lib/browser/index.mjs +328 -82
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/types/src/SearchPlugin.d.ts +2 -1
  6. package/dist/types/src/SearchPlugin.d.ts.map +1 -1
  7. package/dist/types/src/capabilities/app-graph-builder.d.ts +4 -2
  8. package/dist/types/src/capabilities/app-graph-builder.d.ts.map +1 -1
  9. package/dist/types/src/capabilities/index.d.ts +5 -3
  10. package/dist/types/src/capabilities/index.d.ts.map +1 -1
  11. package/dist/types/src/capabilities/operation-handler.d.ts +6 -0
  12. package/dist/types/src/capabilities/operation-handler.d.ts.map +1 -0
  13. package/dist/types/src/capabilities/react-surface.d.ts +3 -2
  14. package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
  15. package/dist/types/src/components/SearchResultStack/SearchResultStack.d.ts +9 -0
  16. package/dist/types/src/components/SearchResultStack/SearchResultStack.d.ts.map +1 -0
  17. package/dist/types/src/components/SearchResultStack/index.d.ts +2 -0
  18. package/dist/types/src/components/SearchResultStack/index.d.ts.map +1 -0
  19. package/dist/types/src/components/index.d.ts +1 -3
  20. package/dist/types/src/components/index.d.ts.map +1 -1
  21. package/dist/types/src/constants.d.ts +2 -0
  22. package/dist/types/src/constants.d.ts.map +1 -0
  23. package/dist/types/src/containers/SearchArticle/SearchArticle.d.ts +7 -0
  24. package/dist/types/src/containers/SearchArticle/SearchArticle.d.ts.map +1 -0
  25. package/dist/types/src/containers/SearchArticle/SearchArticle.stories.d.ts +15 -0
  26. package/dist/types/src/containers/SearchArticle/SearchArticle.stories.d.ts.map +1 -0
  27. package/dist/types/src/containers/SearchArticle/index.d.ts +2 -0
  28. package/dist/types/src/containers/SearchArticle/index.d.ts.map +1 -0
  29. package/dist/types/src/containers/SearchDialog/SearchDialog.d.ts +8 -0
  30. package/dist/types/src/containers/SearchDialog/SearchDialog.d.ts.map +1 -0
  31. package/dist/types/src/containers/SearchDialog/SearchDialog.stories.d.ts +17 -0
  32. package/dist/types/src/containers/SearchDialog/SearchDialog.stories.d.ts.map +1 -0
  33. package/dist/types/src/containers/SearchDialog/index.d.ts +3 -0
  34. package/dist/types/src/containers/SearchDialog/index.d.ts.map +1 -0
  35. package/dist/types/src/containers/index.d.ts +5 -0
  36. package/dist/types/src/containers/index.d.ts.map +1 -0
  37. package/dist/types/src/hooks/index.d.ts +1 -0
  38. package/dist/types/src/hooks/index.d.ts.map +1 -1
  39. package/dist/types/src/hooks/sync.d.ts +4 -3
  40. package/dist/types/src/hooks/sync.d.ts.map +1 -1
  41. package/dist/types/src/hooks/useGlobalSearch.d.ts +8 -4
  42. package/dist/types/src/hooks/useGlobalSearch.d.ts.map +1 -1
  43. package/dist/types/src/hooks/useSearchResults.d.ts +3 -2
  44. package/dist/types/src/hooks/useSearchResults.d.ts.map +1 -1
  45. package/dist/types/src/hooks/useWebSearch.d.ts +7 -4
  46. package/dist/types/src/hooks/useWebSearch.d.ts.map +1 -1
  47. package/dist/types/src/meta.d.ts +3 -4
  48. package/dist/types/src/meta.d.ts.map +1 -1
  49. package/dist/types/src/operations/definitions.d.ts +3 -0
  50. package/dist/types/src/operations/definitions.d.ts.map +1 -0
  51. package/dist/types/src/operations/index.d.ts +4 -0
  52. package/dist/types/src/operations/index.d.ts.map +1 -0
  53. package/dist/types/src/operations/open-search.d.ts +5 -0
  54. package/dist/types/src/operations/open-search.d.ts.map +1 -0
  55. package/dist/types/src/search/exa.d.ts +4 -2
  56. package/dist/types/src/search/exa.d.ts.map +1 -1
  57. package/dist/types/src/translations.d.ts +15 -9
  58. package/dist/types/src/translations.d.ts.map +1 -1
  59. package/dist/types/src/types/index.d.ts +0 -1
  60. package/dist/types/src/types/index.d.ts.map +1 -1
  61. package/dist/types/src/types/search.d.ts +1 -20
  62. package/dist/types/src/types/search.d.ts.map +1 -1
  63. package/dist/types/tsconfig.tsbuildinfo +1 -1
  64. package/package.json +66 -47
  65. package/src/SearchPlugin.tsx +30 -47
  66. package/src/capabilities/app-graph-builder.ts +56 -69
  67. package/src/capabilities/index.ts +8 -4
  68. package/src/capabilities/operation-handler.ts +16 -0
  69. package/src/capabilities/react-surface.tsx +50 -47
  70. package/src/components/SearchResultStack/SearchResultStack.tsx +118 -0
  71. package/src/components/SearchResultStack/index.ts +5 -0
  72. package/src/components/index.ts +1 -3
  73. package/src/constants.ts +7 -0
  74. package/src/containers/SearchArticle/SearchArticle.stories.tsx +80 -0
  75. package/src/containers/SearchArticle/SearchArticle.tsx +63 -0
  76. package/src/containers/SearchArticle/index.ts +5 -0
  77. package/src/containers/SearchDialog/SearchDialog.stories.tsx +109 -0
  78. package/src/containers/SearchDialog/SearchDialog.tsx +96 -0
  79. package/src/containers/SearchDialog/index.ts +6 -0
  80. package/src/containers/index.ts +10 -0
  81. package/src/hooks/index.ts +2 -0
  82. package/src/hooks/sync.test.ts +9 -7
  83. package/src/hooks/sync.ts +28 -32
  84. package/src/hooks/useGlobalSearch.tsx +32 -13
  85. package/src/hooks/useSearchResults.ts +9 -6
  86. package/src/hooks/useWebSearch.ts +22 -11
  87. package/src/meta.ts +10 -7
  88. package/src/operations/definitions.ts +17 -0
  89. package/src/operations/index.ts +9 -0
  90. package/src/operations/open-search.ts +24 -0
  91. package/src/search/exa.test.ts +9 -11
  92. package/src/search/exa.ts +11 -8
  93. package/src/translations.ts +10 -9
  94. package/src/types/command-score.d.ts +16 -0
  95. package/src/types/index.ts +0 -1
  96. package/src/types/search.ts +1 -25
  97. package/dist/lib/browser/app-graph-builder-D2QQTIIR.mjs +0 -77
  98. package/dist/lib/browser/app-graph-builder-D2QQTIIR.mjs.map +0 -7
  99. package/dist/lib/browser/chunk-JVMYGOKS.mjs +0 -21
  100. package/dist/lib/browser/chunk-JVMYGOKS.mjs.map +0 -7
  101. package/dist/lib/browser/chunk-KNS62YL2.mjs +0 -20
  102. package/dist/lib/browser/chunk-KNS62YL2.mjs.map +0 -7
  103. package/dist/lib/browser/chunk-VKV5ZDB4.mjs +0 -190
  104. package/dist/lib/browser/chunk-VKV5ZDB4.mjs.map +0 -7
  105. package/dist/lib/browser/intent-resolver-NXFDPFHC.mjs +0 -22
  106. package/dist/lib/browser/intent-resolver-NXFDPFHC.mjs.map +0 -7
  107. package/dist/lib/browser/react-surface-B2XYD6JN.mjs +0 -468
  108. package/dist/lib/browser/react-surface-B2XYD6JN.mjs.map +0 -7
  109. package/dist/types/src/capabilities/intent-resolver.d.ts +0 -4
  110. package/dist/types/src/capabilities/intent-resolver.d.ts.map +0 -1
  111. package/dist/types/src/components/Search.stories.d.ts +0 -15
  112. package/dist/types/src/components/Search.stories.d.ts.map +0 -1
  113. package/dist/types/src/components/SearchDialog.d.ts +0 -7
  114. package/dist/types/src/components/SearchDialog.d.ts.map +0 -1
  115. package/dist/types/src/components/SearchMain.d.ts +0 -7
  116. package/dist/types/src/components/SearchMain.d.ts.map +0 -1
  117. package/dist/types/src/components/SearchResults/SearchResults.d.ts +0 -20
  118. package/dist/types/src/components/SearchResults/SearchResults.d.ts.map +0 -1
  119. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts +0 -12
  120. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts.map +0 -1
  121. package/dist/types/src/components/SearchResults/index.d.ts +0 -2
  122. package/dist/types/src/components/SearchResults/index.d.ts.map +0 -1
  123. package/dist/types/src/components/Searchbar/Searchbar.d.ts +0 -14
  124. package/dist/types/src/components/Searchbar/Searchbar.d.ts.map +0 -1
  125. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts +0 -11
  126. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts.map +0 -1
  127. package/dist/types/src/components/Searchbar/index.d.ts +0 -2
  128. package/dist/types/src/components/Searchbar/index.d.ts.map +0 -1
  129. package/dist/types/src/types/action.d.ts +0 -13
  130. package/dist/types/src/types/action.d.ts.map +0 -1
  131. package/src/capabilities/intent-resolver.ts +0 -18
  132. package/src/components/Search.stories.tsx +0 -67
  133. package/src/components/SearchDialog.tsx +0 -124
  134. package/src/components/SearchMain.tsx +0 -68
  135. package/src/components/SearchResults/SearchResults.stories.tsx +0 -58
  136. package/src/components/SearchResults/SearchResults.tsx +0 -86
  137. package/src/components/SearchResults/index.ts +0 -5
  138. package/src/components/Searchbar/Searchbar.stories.tsx +0 -28
  139. package/src/components/Searchbar/Searchbar.tsx +0 -67
  140. package/src/components/Searchbar/index.ts +0 -5
  141. package/src/types/action.tsx +0 -16
@@ -0,0 +1,80 @@
1
+ //
2
+ // Copyright 2026 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import * as Effect from 'effect/Effect';
7
+ import React from 'react';
8
+
9
+ import { Capability } from '@dxos/app-framework';
10
+ import { withPluginManager } from '@dxos/app-framework/testing';
11
+ import { AppCapabilities } from '@dxos/app-toolkit';
12
+ import { ClientPlugin } from '@dxos/plugin-client';
13
+ import { initializeIdentity } from '@dxos/plugin-client/testing';
14
+ import { corePlugins, StorybookPlugin } from '@dxos/plugin-testing';
15
+ import { random } from '@dxos/random';
16
+ import { useSpaces } from '@dxos/react-client/echo';
17
+ import { Loading, withLayout } from '@dxos/react-ui/testing';
18
+ import { createObjectFactory } from '@dxos/schema/testing';
19
+ import { Organization, Person } from '@dxos/types';
20
+
21
+ import { SearchContextProvider } from '#hooks';
22
+
23
+ import { translations } from '../../translations';
24
+ import { SearchArticle } from './SearchArticle';
25
+
26
+ random.seed(0);
27
+
28
+ const DefaultStory = () => {
29
+ const spaces = useSpaces();
30
+ const space = spaces[spaces.length - 1];
31
+ if (!space) {
32
+ return <Loading />;
33
+ }
34
+
35
+ return (
36
+ <SearchContextProvider>
37
+ <SearchArticle space={space} />
38
+ </SearchContextProvider>
39
+ );
40
+ };
41
+
42
+ const meta = {
43
+ title: 'plugins/plugin-search/containers/SearchArticle',
44
+ component: SearchArticle,
45
+ render: DefaultStory,
46
+ decorators: [
47
+ withLayout({ layout: 'column' }),
48
+ withPluginManager({
49
+ capabilities: [Capability.contributes(AppCapabilities.Translations, translations)],
50
+ plugins: [
51
+ ...corePlugins(),
52
+ StorybookPlugin({}),
53
+ ClientPlugin({
54
+ types: [Organization.Organization, Person.Person],
55
+ onClientInitialized: ({ client }) =>
56
+ Effect.gen(function* () {
57
+ const { personalSpace } = yield* initializeIdentity(client);
58
+
59
+ const factory = createObjectFactory(personalSpace.db, random as any);
60
+ yield* Effect.promise(() =>
61
+ factory([
62
+ { type: Organization.Organization, count: 10 },
63
+ { type: Person.Person, count: 50 },
64
+ ]),
65
+ );
66
+ }),
67
+ }),
68
+ ],
69
+ }),
70
+ ],
71
+ parameters: {
72
+ layout: 'fullscreen',
73
+ },
74
+ } satisfies Meta<typeof SearchArticle>;
75
+
76
+ export default meta;
77
+
78
+ type Story = StoryObj<typeof meta>;
79
+
80
+ export const Default: Story = {};
@@ -0,0 +1,63 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import React, { useCallback, useMemo, useState } from 'react';
6
+
7
+ import { Entity, Query } from '@dxos/echo';
8
+ import { Filter, type Space, useQuery } from '@dxos/react-client/echo';
9
+ import { Panel, Toolbar } from '@dxos/react-ui';
10
+ import { SearchList } from '@dxos/react-ui-search';
11
+ import { Text } from '@dxos/schema';
12
+ import { isTauri, getHostPlatform } from '@dxos/util';
13
+
14
+ import { SearchResultStack } from '#components';
15
+ import { useGlobalSearch, useGlobalSearchResults, useWebSearch } from '#hooks';
16
+
17
+ export type SearchArticleProps = {
18
+ space?: Space;
19
+ };
20
+
21
+ export const SearchArticle = ({ space }: SearchArticleProps) => {
22
+ // TODO(burdon): Option to query across spaces.
23
+ const [query, setQuery] = useState<string>();
24
+ // TODO(burdon): Re-enable full-text search when indexer is available in all environments.
25
+ const objects = useQuery(
26
+ space?.db,
27
+ query === undefined ? Query.select(Filter.nothing()) : Query.select(Filter.not(Filter.type(Text.Text))),
28
+ );
29
+
30
+ const { setMatch } = useGlobalSearch();
31
+ const results = useGlobalSearchResults(objects);
32
+ const { results: webResults } = useWebSearch({ query });
33
+ const allResults = useMemo(
34
+ () => [...results, ...webResults].filter(({ object }) => object && Entity.getLabel(object)),
35
+ [results, webResults],
36
+ );
37
+
38
+ const handleSearch = useCallback(
39
+ (text: string) => {
40
+ setQuery(text);
41
+ setMatch?.(text);
42
+ },
43
+ [setMatch],
44
+ );
45
+
46
+ const autoFocus = !isTauri() || getHostPlatform() !== 'ios';
47
+
48
+ // TODO(burdon): Move current up/down without losing focus.
49
+ return (
50
+ <SearchList.Root onSearch={handleSearch}>
51
+ <Panel.Root>
52
+ <Panel.Content asChild>
53
+ <SearchResultStack results={allResults} />
54
+ </Panel.Content>
55
+ <Panel.Statusbar asChild>
56
+ <Toolbar.Root>
57
+ <SearchList.Input placeholder='Search...' autoFocus={autoFocus} />
58
+ </Toolbar.Root>
59
+ </Panel.Statusbar>
60
+ </Panel.Root>
61
+ </SearchList.Root>
62
+ );
63
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export { SearchArticle as default } from './SearchArticle';
@@ -0,0 +1,109 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import * as Effect from 'effect/Effect';
7
+ import React from 'react';
8
+ import { expect, userEvent, waitFor, within } from 'storybook/test';
9
+
10
+ import { Capability } from '@dxos/app-framework';
11
+ import { withPluginManager } from '@dxos/app-framework/testing';
12
+ import { AppCapabilities } from '@dxos/app-toolkit';
13
+ import { ClientPlugin } from '@dxos/plugin-client';
14
+ import { initializeIdentity } from '@dxos/plugin-client/testing';
15
+ import { corePlugins, StorybookPlugin } from '@dxos/plugin-testing';
16
+ import { random } from '@dxos/random';
17
+ import { useSpaces } from '@dxos/react-client/echo';
18
+ import { Dialog } from '@dxos/react-ui';
19
+ import { Loading, withLayout } from '@dxos/react-ui/testing';
20
+ import { createObjectFactory } from '@dxos/schema/testing';
21
+ import { Organization, Person } from '@dxos/types';
22
+
23
+ import { SearchContextProvider } from '#hooks';
24
+
25
+ import { translations } from '../../translations';
26
+ import { SearchDialog } from './SearchDialog';
27
+
28
+ random.seed(0);
29
+
30
+ const DefaultStory = () => {
31
+ const spaces = useSpaces();
32
+ const space = spaces[spaces.length - 1];
33
+ if (!space) {
34
+ return <Loading />;
35
+ }
36
+
37
+ return (
38
+ <SearchContextProvider>
39
+ <Dialog.Root defaultOpen>
40
+ <Dialog.Overlay>
41
+ <SearchDialog pivotId='storybook' space={space} />
42
+ </Dialog.Overlay>
43
+ </Dialog.Root>
44
+ </SearchContextProvider>
45
+ );
46
+ };
47
+
48
+ const meta = {
49
+ title: 'plugins/plugin-search/containers/SearchDialog',
50
+ component: SearchDialog,
51
+ render: DefaultStory,
52
+ decorators: [
53
+ withLayout({ layout: 'fullscreen' }),
54
+ withPluginManager({
55
+ capabilities: [Capability.contributes(AppCapabilities.Translations, translations)],
56
+ plugins: [
57
+ ...corePlugins(),
58
+ StorybookPlugin({}),
59
+ ClientPlugin({
60
+ types: [Organization.Organization, Person.Person],
61
+ onClientInitialized: ({ client }) =>
62
+ Effect.gen(function* () {
63
+ const { personalSpace } = yield* initializeIdentity(client);
64
+
65
+ const factory = createObjectFactory(personalSpace.db, random as any);
66
+ yield* Effect.promise(() =>
67
+ factory([
68
+ { type: Organization.Organization, count: 10 },
69
+ { type: Person.Person, count: 50 },
70
+ ]),
71
+ );
72
+ }),
73
+ }),
74
+ ],
75
+ }),
76
+ ],
77
+ tags: ['test'],
78
+ parameters: {
79
+ layout: 'fullscreen',
80
+ },
81
+ } satisfies Meta<typeof SearchDialog>;
82
+
83
+ export default meta;
84
+
85
+ type Story = StoryObj<typeof meta>;
86
+
87
+ export const Default: Story = {};
88
+
89
+ export const Test: Story = {
90
+ play: async () => {
91
+ const body = within(document.body);
92
+
93
+ // Wait for the dialog to render and the search input to appear.
94
+ const searchInput = await body.findByRole('textbox', undefined, { timeout: 10_000 });
95
+ await expect(searchInput).toBeInTheDocument();
96
+
97
+ // Type a search term that should match generated Person/Organization names.
98
+ await userEvent.type(searchInput, 'a');
99
+
100
+ // Wait for search results to appear as options in the listbox.
101
+ await waitFor(
102
+ async () => {
103
+ const options = body.queryAllByRole('option');
104
+ await expect(options.length).toBeGreaterThan(0);
105
+ },
106
+ { timeout: 10_000 },
107
+ );
108
+ },
109
+ };
@@ -0,0 +1,96 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import React, { useCallback, useMemo, useState } from 'react';
6
+
7
+ import { useOperationInvoker } from '@dxos/app-framework/ui';
8
+ import { getObjectPathFromObject, LayoutOperation } from '@dxos/app-toolkit';
9
+ import { useLayout } from '@dxos/app-toolkit/ui';
10
+ import { useActiveSpace } from '@dxos/app-toolkit/ui';
11
+ import { Entity, Obj, Query } from '@dxos/echo';
12
+ import { Filter, type Space, useQuery } from '@dxos/react-client/echo';
13
+ import { Dialog, useTranslation } from '@dxos/react-ui';
14
+ import { SearchList } from '@dxos/react-ui-search';
15
+ import { Text } from '@dxos/schema';
16
+
17
+ import { useGlobalSearch, useGlobalSearchResults } from '#hooks';
18
+ import { meta } from '#meta';
19
+ import { type SearchResult } from '#types';
20
+
21
+ export type SearchDialogProps = {
22
+ pivotId?: string;
23
+ space?: Space;
24
+ };
25
+
26
+ export const SearchDialog = ({ pivotId: pivotIdProp, space: spaceProp }: SearchDialogProps) => {
27
+ const { t } = useTranslation(meta.id);
28
+ const layout = useLayout();
29
+ const pivotId = pivotIdProp ?? layout.active[layout.active.length - 1];
30
+ const { invokePromise } = useOperationInvoker();
31
+ const { setMatch } = useGlobalSearch();
32
+ const [query, setQuery] = useState<string>();
33
+
34
+ const activeSpace = useActiveSpace();
35
+ const space = spaceProp ?? activeSpace;
36
+ // TODO(burdon): Re-enable full-text search when indexer is available in all environments.
37
+ const objects = useQuery(
38
+ space?.db,
39
+ query === undefined ? Query.select(Filter.nothing()) : Query.select(Filter.not(Filter.type(Text.Text))),
40
+ );
41
+
42
+ const results = useGlobalSearchResults(objects);
43
+ const allResults = useMemo(() => results.filter(({ object }) => object && Entity.getLabel(object)), [results]);
44
+
45
+ const handleSearch = useCallback(
46
+ (text: string) => {
47
+ setQuery(text);
48
+ setMatch?.(text);
49
+ },
50
+ [setMatch],
51
+ );
52
+
53
+ const handleSelect = useCallback(
54
+ async (result: SearchResult) => {
55
+ if (!result.object || !Obj.isObject(result.object)) {
56
+ return;
57
+ }
58
+
59
+ const qualifiedPath = getObjectPathFromObject(result.object);
60
+ await invokePromise(LayoutOperation.UpdateDialog, { state: false });
61
+ await invokePromise(LayoutOperation.Open, {
62
+ subject: [qualifiedPath],
63
+ pivotId,
64
+ positioning: 'end',
65
+ });
66
+ },
67
+ [pivotId, invokePromise],
68
+ );
69
+
70
+ return (
71
+ <Dialog.Content>
72
+ <Dialog.Header>
73
+ <Dialog.Title>{t('search-dialog.title')}</Dialog.Title>
74
+ <Dialog.Close asChild>
75
+ <Dialog.CloseIconButton />
76
+ </Dialog.Close>
77
+ </Dialog.Header>
78
+ <SearchList.Root onSearch={handleSearch}>
79
+ <SearchList.Input classNames='px-0' autoFocus placeholder={t('search.placeholder')} />
80
+ <SearchList.Viewport classNames='max-h-[24rem]'>
81
+ {allResults.map((result) => (
82
+ <SearchList.Item
83
+ key={result.id}
84
+ classNames='flex gap-2 items-center'
85
+ value={result.id}
86
+ label={result.label ?? (result.object ? Entity.getLabel(result.object) : undefined) ?? result.id}
87
+ icon={result.icon}
88
+ onSelect={() => void handleSelect(result)}
89
+ />
90
+ ))}
91
+ {query && allResults.length === 0 && <SearchList.Empty />}
92
+ </SearchList.Viewport>
93
+ </SearchList.Root>
94
+ </Dialog.Content>
95
+ );
96
+ };
@@ -0,0 +1,6 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ export { SearchDialog, type SearchDialogProps } from './SearchDialog';
6
+ export { SearchDialog as default } from './SearchDialog';
@@ -0,0 +1,10 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type ComponentType, lazy } from 'react';
6
+
7
+ export type { SearchDialogProps } from './SearchDialog';
8
+
9
+ export const SearchDialog: ComponentType<any> = lazy(() => import('./SearchDialog'));
10
+ export const SearchArticle: ComponentType<any> = lazy(() => import('./SearchArticle'));
@@ -3,5 +3,7 @@
3
3
  //
4
4
 
5
5
  export * from './sync';
6
+
6
7
  export * from './useGlobalSearch';
7
8
  export * from './useSearchResults';
9
+ export * from './useWebSearch';
@@ -5,17 +5,19 @@
5
5
  import { describe, expect, test } from 'vitest';
6
6
 
7
7
  import { Client } from '@dxos/client';
8
- import { Filter, Obj, Type } from '@dxos/echo';
9
- import { faker } from '@dxos/random';
8
+ import { Filter, Obj } from '@dxos/echo';
9
+ import { TestSchema } from '@dxos/echo/testing';
10
+ import { random } from '@dxos/random';
10
11
 
11
12
  import { filterObjectsSync } from './sync';
12
13
 
13
- faker.seed(1);
14
+ random.seed(1);
14
15
 
15
16
  // TODO(burdon): Reconcile with agent/minisearch.
16
17
 
17
18
  describe('Search', () => {
18
- test('Prefix text search', async () => {
19
+ // TODO(mykola): Fix FTS index to filter by fields.
20
+ test.skip('Prefix text search', async () => {
19
21
  const client = new Client();
20
22
  await client.initialize();
21
23
  await client.halo.createIdentity();
@@ -24,11 +26,11 @@ describe('Search', () => {
24
26
  const space = await client.spaces.create();
25
27
  Array.from({ length: 20 }).map((_, i) => {
26
28
  const content =
27
- i === 10 ? faker.lorem.sentence() + ` ${match}}. ` + faker.lorem.sentence() : faker.lorem.sentences();
28
- return space.db.add(Obj.make(Type.Expando, { title: faker.lorem.sentence(), content }));
29
+ i === 10 ? random.lorem.sentence() + ` ${match}}. ` + random.lorem.sentence() : random.lorem.sentences();
30
+ return space.db.add(Obj.make(TestSchema.Expando, { title: random.lorem.sentence(), content }));
29
31
  });
30
32
 
31
- const { objects } = await space.db.query(Filter.everything()).run();
33
+ const objects = await space.db.query(Filter.everything()).run();
32
34
  const results = filterObjectsSync(objects, new RegExp(match, 'i'));
33
35
  expect(results).to.have.length(1);
34
36
  });
package/src/hooks/sync.ts CHANGED
@@ -2,12 +2,13 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Schema, SchemaAST } from 'effect';
5
+ import type * as Schema from 'effect/Schema';
6
+ import * as SchemaAST from 'effect/SchemaAST';
6
7
 
7
- import { Obj } from '@dxos/echo';
8
- import { DataType } from '@dxos/schema';
8
+ import { type Entity, Obj } from '@dxos/echo';
9
+ import { Text } from '@dxos/schema';
9
10
 
10
- import { type SearchResult } from '../types';
11
+ import { type SearchResult } from '#types';
11
12
 
12
13
  export const queryStringToMatch = (queryString?: string): RegExp | undefined => {
13
14
  const trimmed = queryString?.trim();
@@ -38,39 +39,34 @@ const getIcon = (schema: Schema.Schema.AnyNoContext | undefined): string | undef
38
39
  export type TextFields = Record<string, string>;
39
40
 
40
41
  // TODO(thure): This returns `SearchResult[]` which is not just a narrower set of objects as the name implies.
41
- export const filterObjectsSync = <T extends Record<string, any>>(objects: T[], match?: RegExp): SearchResult[] => {
42
+ export const filterObjectsSync = <T extends Entity.Unknown>(objects: T[], match?: RegExp): SearchResult<T>[] => {
42
43
  if (!match) {
43
44
  return [];
44
45
  }
45
46
 
46
- return objects.reduce<SearchResult[]>((results, object) => {
47
+ return objects.reduce<SearchResult<T>[]>((results, object) => {
47
48
  // TODO(burdon): Hack to ignore Text objects.
48
- if (object instanceof DataType.Text) {
49
+ if (object instanceof Text.Text) {
49
50
  return results;
50
51
  }
51
52
 
52
53
  const fields = mapObjectToTextFields(object);
53
54
  Object.entries(fields).some(([, value]) => {
54
- const result = value.match(match);
55
- if (result) {
56
- // TODO(burdon): Use schema.
57
- const label = getStringProperty(object, ['label', 'name', 'title']);
58
-
59
- results.push({
60
- id: object.id,
61
- type: getIcon(Obj.getSchema(object)),
62
- label,
63
- match,
64
- // TODO(burdon): Truncate.
65
- // TODO(burdon): Issue with sketch documents.
66
- snippet: value !== label ? value : (fields.content ?? fields.description ?? undefined),
67
- object,
68
- });
69
-
70
- return true;
71
- }
55
+ // TODO(burdon): Use schema.
56
+ const label = Obj.getLabel(object as any);
57
+
58
+ results.push({
59
+ id: object.id,
60
+ type: getIcon(Obj.getSchema(object)),
61
+ label,
62
+ match,
63
+ // TODO(burdon): Truncate.
64
+ // TODO(burdon): Issue with sketch documents.
65
+ snippet: value !== label ? value : (fields.content ?? fields.description ?? undefined),
66
+ object,
67
+ });
72
68
 
73
- return false;
69
+ return true;
74
70
  });
75
71
 
76
72
  return results;
@@ -94,13 +90,13 @@ export const getStringProperty = (object: any, keys: string[]): string | undefin
94
90
  };
95
91
 
96
92
  // TODO(burdon): Filter system fields.
97
- const getKeys = (object: Record<string, unknown>): string[] => {
93
+ const getKeys = <T extends Entity.Unknown>(object: T): string[] => {
98
94
  try {
99
95
  const obj = JSON.parse(JSON.stringify(object));
100
96
  return Object.keys(obj).filter(
101
97
  (key) => key !== 'id' && key !== 'identityKey' && key[0] !== '_' && key[0] !== '@',
102
98
  ) as string[];
103
- } catch (err) {
99
+ } catch {
104
100
  // TODO(burdon): Error with TLDraw sketch.
105
101
  // Uncaught Error: Type with the name content has already been defined with a different constructor
106
102
  return [];
@@ -108,13 +104,13 @@ const getKeys = (object: Record<string, unknown>): string[] => {
108
104
  };
109
105
 
110
106
  // TODO(burdon): Use ECHO schema.
111
- export const mapObjectToTextFields = <T extends Record<string, unknown>>(object: T): TextFields => {
107
+ export const mapObjectToTextFields = <T extends Entity.Unknown>(object: T): TextFields => {
112
108
  return getKeys(object).reduce<TextFields>((fields, key) => {
113
- const value = object[key] as any;
114
- if (typeof value === 'string' || value instanceof DataType.Text) {
109
+ const value = (object as any)[key];
110
+ if (typeof value === 'string' || value instanceof Text.Text) {
115
111
  try {
116
112
  fields[key] = String(value);
117
- } catch (err) {
113
+ } catch {
118
114
  // TODO(burdon): Exception when parsing sketch document.
119
115
  }
120
116
  }
@@ -2,14 +2,19 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import React, { type PropsWithChildren, createContext, useContext, useMemo, useState } from 'react';
5
+ import React, { type PropsWithChildren, createContext, useCallback, useContext, useState } from 'react';
6
6
 
7
7
  import { raise } from '@dxos/debug';
8
+ import { type Entity } from '@dxos/echo';
9
+ import { GlobalFilterProvider } from '@dxos/react-ui-search';
8
10
 
9
- import { type SearchResult } from '../types';
11
+ import { type SearchResult } from '#types';
10
12
 
11
13
  import { filterObjectsSync, queryStringToMatch } from './sync';
12
14
 
15
+ // Re-export for backward compatibility.
16
+ export { useGlobalFilteredObjects, GlobalFilterProvider } from '@dxos/react-ui-search';
17
+
13
18
  type SearchContextType = {
14
19
  match?: RegExp;
15
20
  setMatch?: (text?: string) => void;
@@ -17,26 +22,40 @@ type SearchContextType = {
17
22
 
18
23
  const SearchContext = createContext<SearchContextType>({});
19
24
 
25
+ /**
26
+ * Provider for global search context.
27
+ * Also provides the GlobalFilterProvider for useGlobalFilteredObjects to work.
28
+ */
20
29
  export const SearchContextProvider = ({ children }: PropsWithChildren) => {
21
30
  const [match, setMatch] = useState<RegExp>();
22
31
  const handleMatch = (text?: string) => setMatch(queryStringToMatch(text));
23
- return <SearchContext.Provider value={{ match, setMatch: handleMatch }}>{children}</SearchContext.Provider>;
32
+
33
+ // Provide a filter function for useGlobalFilteredObjects.
34
+ const filterFn = useCallback(
35
+ <T extends Entity.Any>(objects: T[]): T[] => {
36
+ if (!match) {
37
+ return objects;
38
+ }
39
+
40
+ return filterObjectsSync(objects, match)
41
+ .filter((result) => result.object)
42
+ .map((result) => result.object!);
43
+ },
44
+ [match],
45
+ );
46
+
47
+ return (
48
+ <SearchContext.Provider value={{ match, setMatch: handleMatch }}>
49
+ <GlobalFilterProvider filter={filterFn}>{children}</GlobalFilterProvider>
50
+ </SearchContext.Provider>
51
+ );
24
52
  };
25
53
 
26
54
  export const useGlobalSearch = () => {
27
55
  return useContext(SearchContext) ?? raise(new Error('Missing SearchContext.'));
28
56
  };
29
57
 
30
- export const useGlobalSearchResults = <T extends Record<string, any>>(objects?: T[]): SearchResult[] => {
58
+ export const useGlobalSearchResults = <T extends Entity.Unknown>(objects?: T[]): SearchResult<T>[] => {
31
59
  const { match } = useGlobalSearch();
32
60
  return objects && match ? filterObjectsSync(objects, match) : [];
33
61
  };
34
-
35
- export const useGlobalFilteredObjects = <T extends Record<string, any>>(objects?: T[]): T[] => {
36
- const { match } = useGlobalSearch();
37
- if (!match || !objects) {
38
- return objects ?? [];
39
- }
40
-
41
- return useMemo(() => filterObjectsSync(objects, match).map((result) => result.object), [objects, match]);
42
- };
@@ -5,12 +5,13 @@
5
5
  import { yieldOrContinue } from 'main-thread-scheduling';
6
6
  import { useEffect, useState } from 'react';
7
7
 
8
- import { DataType } from '@dxos/schema';
8
+ import { type Entity } from '@dxos/echo';
9
+ import { Text } from '@dxos/schema';
9
10
 
10
11
  // TODO(thure): Deprecate search-sync, move still-relevant utilities elsewhere (here, probably).
11
12
  import { mapObjectToTextFields, queryStringToMatch } from './sync';
12
13
 
13
- export const filterObjects = async <T extends Record<string, any>>(
14
+ export const filterObjects = async <T extends Entity.Unknown>(
14
15
  objects: T[],
15
16
  match?: RegExp,
16
17
  ): Promise<Map<T, string[][]>> => {
@@ -18,9 +19,10 @@ export const filterObjects = async <T extends Record<string, any>>(
18
19
  if (!match) {
19
20
  return result;
20
21
  }
22
+
21
23
  await Promise.all(
22
24
  objects
23
- .filter((object) => !(object instanceof DataType.Text))
25
+ .filter((object) => !(object instanceof Text.Text))
24
26
  .map(async (object) => {
25
27
  await yieldOrContinue('interactive');
26
28
  const fields = mapObjectToTextFields<T>(object);
@@ -34,10 +36,11 @@ export const filterObjects = async <T extends Record<string, any>>(
34
36
  });
35
37
  }),
36
38
  );
39
+
37
40
  return result;
38
41
  };
39
42
 
40
- export const useSearchResults = <T extends Record<string, any>>(
43
+ export const useSearchResults = <T extends Entity.Unknown>(
41
44
  queryString?: string,
42
45
  objects?: T[],
43
46
  delay: number = 400,
@@ -47,14 +50,14 @@ export const useSearchResults = <T extends Record<string, any>>(
47
50
 
48
51
  useEffect(() => {
49
52
  setPending(!!(objects && queryString));
50
- const timeoutId = setTimeout(async () => {
53
+ const timeout = setTimeout(async () => {
51
54
  const nextResults =
52
55
  objects && queryString ? await filterObjects(objects, queryStringToMatch(queryString)) : new Map();
53
56
  setResults(nextResults);
54
57
  setPending(false);
55
58
  }, delay);
56
59
 
57
- return () => clearTimeout(timeoutId);
60
+ return () => clearTimeout(timeout);
58
61
  }, [queryString, objects]);
59
62
 
60
63
  return [pending, results];