@dxos/plugin-search 0.8.4-main.ae835ea → 0.8.4-main.bc674ce

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 (128) hide show
  1. package/dist/lib/browser/app-graph-builder-KZ3V2E5U.mjs +81 -0
  2. package/dist/lib/browser/app-graph-builder-KZ3V2E5U.mjs.map +7 -0
  3. package/dist/lib/browser/{chunk-GN2FSWTM.mjs → chunk-HRIWTRJV.mjs} +1 -1
  4. package/dist/lib/browser/chunk-HRIWTRJV.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-I7YWQSUZ.mjs +26 -0
  6. package/dist/lib/browser/chunk-I7YWQSUZ.mjs.map +7 -0
  7. package/dist/lib/browser/chunk-ZRJJXRGF.mjs +282 -0
  8. package/dist/lib/browser/chunk-ZRJJXRGF.mjs.map +7 -0
  9. package/dist/lib/browser/index.mjs +51 -57
  10. package/dist/lib/browser/index.mjs.map +4 -4
  11. package/dist/lib/browser/meta.json +1 -1
  12. package/dist/lib/browser/operation-resolver-6MBOKZQH.mjs +25 -0
  13. package/dist/lib/browser/operation-resolver-6MBOKZQH.mjs.map +7 -0
  14. package/dist/lib/browser/react-surface-RVLK6KJ5.mjs +368 -0
  15. package/dist/lib/browser/react-surface-RVLK6KJ5.mjs.map +7 -0
  16. package/dist/types/src/SearchPlugin.d.ts +2 -1
  17. package/dist/types/src/SearchPlugin.d.ts.map +1 -1
  18. package/dist/types/src/capabilities/app-graph-builder/app-graph-builder.d.ts +6 -0
  19. package/dist/types/src/capabilities/app-graph-builder/app-graph-builder.d.ts.map +1 -0
  20. package/dist/types/src/capabilities/app-graph-builder/index.d.ts +3 -0
  21. package/dist/types/src/capabilities/app-graph-builder/index.d.ts.map +1 -0
  22. package/dist/types/src/capabilities/index.d.ts +3 -3
  23. package/dist/types/src/capabilities/index.d.ts.map +1 -1
  24. package/dist/types/src/capabilities/operation-resolver/index.d.ts +3 -0
  25. package/dist/types/src/capabilities/operation-resolver/index.d.ts.map +1 -0
  26. package/dist/types/src/capabilities/operation-resolver/operation-resolver.d.ts +5 -0
  27. package/dist/types/src/capabilities/operation-resolver/operation-resolver.d.ts.map +1 -0
  28. package/dist/types/src/capabilities/react-surface/index.d.ts +3 -0
  29. package/dist/types/src/capabilities/react-surface/index.d.ts.map +1 -0
  30. package/dist/types/src/capabilities/react-surface/react-surface.d.ts +5 -0
  31. package/dist/types/src/capabilities/react-surface/react-surface.d.ts.map +1 -0
  32. package/dist/types/src/components/SearchDialog.d.ts.map +1 -1
  33. package/dist/types/src/components/SearchMain.d.ts +4 -5
  34. package/dist/types/src/components/SearchMain.d.ts.map +1 -1
  35. package/dist/types/src/components/SearchMain.stories.d.ts +31 -0
  36. package/dist/types/src/components/SearchMain.stories.d.ts.map +1 -0
  37. package/dist/types/src/components/SpaceMain.d.ts +9 -0
  38. package/dist/types/src/components/SpaceMain.d.ts.map +1 -0
  39. package/dist/types/src/components/index.d.ts +1 -1
  40. package/dist/types/src/components/index.d.ts.map +1 -1
  41. package/dist/types/src/hooks/index.d.ts +1 -0
  42. package/dist/types/src/hooks/index.d.ts.map +1 -1
  43. package/dist/types/src/hooks/sync.d.ts +3 -2
  44. package/dist/types/src/hooks/sync.d.ts.map +1 -1
  45. package/dist/types/src/hooks/useGlobalSearch.d.ts +7 -3
  46. package/dist/types/src/hooks/useGlobalSearch.d.ts.map +1 -1
  47. package/dist/types/src/hooks/useSearchResults.d.ts +3 -2
  48. package/dist/types/src/hooks/useSearchResults.d.ts.map +1 -1
  49. package/dist/types/src/hooks/useWebSearch.d.ts +6 -3
  50. package/dist/types/src/hooks/useWebSearch.d.ts.map +1 -1
  51. package/dist/types/src/meta.d.ts +2 -2
  52. package/dist/types/src/meta.d.ts.map +1 -1
  53. package/dist/types/src/search/exa.d.ts +3 -1
  54. package/dist/types/src/search/exa.d.ts.map +1 -1
  55. package/dist/types/src/translations.d.ts +1 -1
  56. package/dist/types/src/types/action.d.ts +13 -10
  57. package/dist/types/src/types/action.d.ts.map +1 -1
  58. package/dist/types/src/types/search.d.ts +3 -7
  59. package/dist/types/src/types/search.d.ts.map +1 -1
  60. package/dist/types/tsconfig.tsbuildinfo +1 -1
  61. package/package.json +53 -45
  62. package/src/SearchPlugin.tsx +24 -42
  63. package/src/capabilities/app-graph-builder/app-graph-builder.ts +70 -0
  64. package/src/capabilities/app-graph-builder/index.ts +7 -0
  65. package/src/capabilities/index.ts +3 -5
  66. package/src/capabilities/operation-resolver/index.ts +7 -0
  67. package/src/capabilities/operation-resolver/operation-resolver.ts +23 -0
  68. package/src/capabilities/react-surface/index.ts +7 -0
  69. package/src/capabilities/react-surface/react-surface.tsx +70 -0
  70. package/src/components/SearchDialog.tsx +67 -61
  71. package/src/components/SearchMain.stories.tsx +90 -0
  72. package/src/components/SearchMain.tsx +63 -48
  73. package/src/components/SpaceMain.tsx +169 -0
  74. package/src/components/index.ts +1 -1
  75. package/src/hooks/index.ts +2 -0
  76. package/src/hooks/sync.test.ts +6 -4
  77. package/src/hooks/sync.ts +25 -30
  78. package/src/hooks/useGlobalSearch.tsx +31 -12
  79. package/src/hooks/useSearchResults.ts +7 -4
  80. package/src/hooks/useWebSearch.ts +20 -9
  81. package/src/meta.ts +2 -2
  82. package/src/search/exa.test.ts +9 -11
  83. package/src/search/exa.ts +6 -5
  84. package/src/translations.ts +1 -1
  85. package/src/types/action.tsx +8 -6
  86. package/src/types/command-score.d.ts +16 -0
  87. package/src/types/search.ts +3 -9
  88. package/dist/lib/browser/app-graph-builder-EVWBWGSY.mjs +0 -78
  89. package/dist/lib/browser/app-graph-builder-EVWBWGSY.mjs.map +0 -7
  90. package/dist/lib/browser/chunk-GN2FSWTM.mjs.map +0 -7
  91. package/dist/lib/browser/chunk-TCVYYI44.mjs +0 -21
  92. package/dist/lib/browser/chunk-TCVYYI44.mjs.map +0 -7
  93. package/dist/lib/browser/chunk-WN3TVQLV.mjs +0 -190
  94. package/dist/lib/browser/chunk-WN3TVQLV.mjs.map +0 -7
  95. package/dist/lib/browser/intent-resolver-UYQA6UUQ.mjs +0 -22
  96. package/dist/lib/browser/intent-resolver-UYQA6UUQ.mjs.map +0 -7
  97. package/dist/lib/browser/react-surface-MKWVVY3M.mjs +0 -470
  98. package/dist/lib/browser/react-surface-MKWVVY3M.mjs.map +0 -7
  99. package/dist/types/src/capabilities/app-graph-builder.d.ts +0 -4
  100. package/dist/types/src/capabilities/app-graph-builder.d.ts.map +0 -1
  101. package/dist/types/src/capabilities/intent-resolver.d.ts +0 -4
  102. package/dist/types/src/capabilities/intent-resolver.d.ts.map +0 -1
  103. package/dist/types/src/capabilities/react-surface.d.ts +0 -4
  104. package/dist/types/src/capabilities/react-surface.d.ts.map +0 -1
  105. package/dist/types/src/components/Search.stories.d.ts +0 -16
  106. package/dist/types/src/components/Search.stories.d.ts.map +0 -1
  107. package/dist/types/src/components/SearchResults/SearchResults.d.ts +0 -20
  108. package/dist/types/src/components/SearchResults/SearchResults.d.ts.map +0 -1
  109. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts +0 -16
  110. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts.map +0 -1
  111. package/dist/types/src/components/SearchResults/index.d.ts +0 -2
  112. package/dist/types/src/components/SearchResults/index.d.ts.map +0 -1
  113. package/dist/types/src/components/Searchbar/Searchbar.d.ts +0 -14
  114. package/dist/types/src/components/Searchbar/Searchbar.d.ts.map +0 -1
  115. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts +0 -13
  116. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts.map +0 -1
  117. package/dist/types/src/components/Searchbar/index.d.ts +0 -2
  118. package/dist/types/src/components/Searchbar/index.d.ts.map +0 -1
  119. package/src/capabilities/app-graph-builder.ts +0 -80
  120. package/src/capabilities/intent-resolver.ts +0 -18
  121. package/src/capabilities/react-surface.tsx +0 -59
  122. package/src/components/Search.stories.tsx +0 -64
  123. package/src/components/SearchResults/SearchResults.stories.tsx +0 -58
  124. package/src/components/SearchResults/SearchResults.tsx +0 -86
  125. package/src/components/SearchResults/index.ts +0 -5
  126. package/src/components/Searchbar/Searchbar.stories.tsx +0 -28
  127. package/src/components/Searchbar/Searchbar.tsx +0 -67
  128. package/src/components/Searchbar/index.ts +0 -5
@@ -0,0 +1,90 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import { type Decorator, type Meta, type StoryObj } from '@storybook/react-vite';
6
+ import * as Effect from 'effect/Effect';
7
+ import React from 'react';
8
+
9
+ import { withPluginManager } from '@dxos/app-framework/testing';
10
+ import { Obj } from '@dxos/echo';
11
+ import { TestSchema } from '@dxos/echo/testing';
12
+ import { ClientPlugin } from '@dxos/plugin-client';
13
+ import { PreviewPlugin } from '@dxos/plugin-preview';
14
+ import { corePlugins } from '@dxos/plugin-testing';
15
+ import { faker } from '@dxos/random';
16
+ import { useSpaces } from '@dxos/react-client/echo';
17
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
18
+ import { withMosaic } from '@dxos/react-ui-mosaic/testing';
19
+
20
+ import { SearchContextProvider } from '../hooks';
21
+ import { translations } from '../translations';
22
+
23
+ import { SearchMain } from './SearchMain';
24
+
25
+ faker.seed(1234);
26
+
27
+ const withSearchContext = (): Decorator => {
28
+ return (Story) => (
29
+ <SearchContextProvider>
30
+ <Story />
31
+ </SearchContextProvider>
32
+ );
33
+ };
34
+
35
+ const DefaultStory = () => {
36
+ const spaces = useSpaces();
37
+ const space = spaces[spaces.length - 1];
38
+ return <SearchMain space={space} />;
39
+ };
40
+
41
+ const meta = {
42
+ title: 'plugins/plugin-search/SearchMain',
43
+ component: SearchMain,
44
+ render: DefaultStory,
45
+ decorators: [
46
+ withTheme,
47
+ withLayout({ layout: 'column' }),
48
+ withMosaic(),
49
+ withSearchContext(),
50
+ withPluginManager({
51
+ plugins: [
52
+ ...corePlugins(),
53
+ ClientPlugin({
54
+ types: [TestSchema.Task, TestSchema.Organization, TestSchema.Person],
55
+ onClientInitialized: ({ client }) =>
56
+ Effect.gen(function* () {
57
+ yield* Effect.promise(() => client.halo.createIdentity());
58
+ yield* Effect.promise(() => client.spaces.waitUntilReady());
59
+ const space = yield* Effect.promise(() => client.spaces.create({ name: 'Test Space' }));
60
+ yield* Effect.promise(() => space.waitUntilReady());
61
+ // Create some test data to search.
62
+ space.db.add(
63
+ Obj.make(TestSchema.Task, { title: 'Project Proposal', description: 'A proposal for a new project' }),
64
+ );
65
+ space.db.add(
66
+ Obj.make(TestSchema.Task, { title: 'Meeting Notes', description: 'Notes from the team meeting' }),
67
+ );
68
+ space.db.add(
69
+ Obj.make(TestSchema.Task, { title: 'Budget Report', description: 'Q3 financial budget report' }),
70
+ );
71
+ space.db.add(Obj.make(TestSchema.Person, { name: 'Alice Smith', email: 'alice@example.com' }));
72
+ space.db.add(Obj.make(TestSchema.Person, { name: 'Bob Jones', email: 'bob@example.com' }));
73
+ space.db.add(Obj.make(TestSchema.Organization, { name: 'Composer Project' }));
74
+ }),
75
+ }),
76
+ PreviewPlugin(),
77
+ ],
78
+ }),
79
+ ],
80
+ parameters: {
81
+ layout: 'fullscreen',
82
+ translations,
83
+ },
84
+ } satisfies Meta<typeof SearchMain>;
85
+
86
+ export default meta;
87
+
88
+ type Story = StoryObj<typeof meta>;
89
+
90
+ export const Default: Story = {};
@@ -2,67 +2,82 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import React, { type FC, useState } from 'react';
5
+ import React, { useCallback, useMemo, useState } from 'react';
6
6
 
7
- import { log } from '@dxos/log';
8
- import { useClient } from '@dxos/react-client';
7
+ import { Surface } from '@dxos/app-framework/react';
8
+ import { Entity, Query } from '@dxos/echo';
9
9
  import { Filter, type Space, useQuery } from '@dxos/react-client/echo';
10
- import { type ThemedClassName, useTranslation } from '@dxos/react-ui';
11
- import { activeSurface, mx } from '@dxos/react-ui-theme';
10
+ import { Toolbar, useTranslation } from '@dxos/react-ui';
11
+ import { Card, Mosaic, type StackTileComponent } from '@dxos/react-ui-mosaic';
12
+ import { Layout } from '@dxos/react-ui-mosaic';
13
+ import { SearchList } from '@dxos/react-ui-searchlist';
14
+ import { Text } from '@dxos/schema';
12
15
 
13
- import { useGlobalSearch, useGlobalSearchResults } from '../hooks';
14
- import { useWebSearch } from '../hooks/useWebSearch';
16
+ import { useGlobalSearch, useGlobalSearchResults, useWebSearch } from '../hooks';
15
17
  import { meta } from '../meta';
18
+ import { type SearchResult } from '../types';
16
19
 
17
- import { Searchbar } from './Searchbar';
18
- import { SearchResults } from './SearchResults';
19
-
20
- export const SearchMain: FC<ThemedClassName<{ space: Space }>> = ({ classNames, space }) => {
20
+ export const SearchMain = ({ space }: { space?: Space }) => {
21
21
  const { t } = useTranslation(meta.id);
22
- const client = useClient();
23
22
  const { setMatch } = useGlobalSearch();
24
23
  const [query, setQuery] = useState<string>();
25
- // TODO(burdon): UX to select all spaces.
26
- const allSpaces = false;
24
+ // TODO(burdon): Option to search across spaces.
25
+ const objects = useQuery(
26
+ space?.db,
27
+ query === undefined
28
+ ? Query.select(Filter.nothing())
29
+ : // TODO(dmaretskyi): Final version would walk the ancestry of the object until we find a non-system type.
30
+ Query.all(
31
+ Query.select(Filter.text(query, { type: 'full-text' })).select(Filter.not(Filter.type(Text.Text))),
32
+ Query.select(Filter.text(query, { type: 'full-text' }))
33
+ .select(Filter.type(Text.Text))
34
+ .referencedBy('dxos.org/type/Document', 'content'),
35
+ ),
36
+ );
27
37
 
28
- // TODO(burdon): Returns ALL objects (e.g., incl. Text objects that are fields of parent objects).
29
- const objects = useQuery(allSpaces ? client.spaces : space, Filter.everything());
30
38
  const results = useGlobalSearchResults(objects);
39
+ const { results: webResults } = useWebSearch({ query });
40
+ const allResults = useMemo(
41
+ () => [...results, ...webResults].filter(({ object }) => object && Entity.getLabel(object)),
42
+ [results, webResults],
43
+ );
31
44
 
32
- const {
33
- runSearch,
34
- results: webResults,
35
- isLoading,
36
- } = useWebSearch({
37
- query,
38
- });
39
-
40
- const [selected, setSelected] = useState<string>();
41
-
42
- // TODO(burdon): Activate and/or filter current main (set context).
43
- const handleSelect = (id: string) => {
44
- setSelected((selected) => (selected === id ? undefined : id));
45
- };
45
+ const handleSearch = useCallback(
46
+ (text: string) => {
47
+ setQuery(text);
48
+ setMatch?.(text);
49
+ },
50
+ [setMatch],
51
+ );
46
52
 
47
- const allResults = [...results, ...webResults];
48
- log.info('results', { results, webResults });
53
+ return (
54
+ <Layout.Main toolbar>
55
+ <SearchList.Root onSearch={handleSearch}>
56
+ <Toolbar.Root>
57
+ <SearchList.Input placeholder={t('search placeholder')} />
58
+ </Toolbar.Root>
59
+ <SearchList.Content>
60
+ <Mosaic.Container asChild>
61
+ <Mosaic.Viewport>
62
+ <Mosaic.Stack items={allResults} getId={(result) => result.object!.id} Tile={SearchResultTile} />
63
+ </Mosaic.Viewport>
64
+ </Mosaic.Container>
65
+ {allResults.length === 0 && <SearchList.Empty>{t('empty results message')}</SearchList.Empty>}
66
+ </SearchList.Content>
67
+ </SearchList.Root>
68
+ </Layout.Main>
69
+ );
70
+ };
49
71
 
72
+ const SearchResultTile: StackTileComponent<SearchResult> = ({ data }) => {
50
73
  return (
51
- <div className={mx('flex flex-col grow h-full overflow-hidden', classNames)}>
52
- <Searchbar
53
- placeholder={t('search placeholder')}
54
- onChange={(text) => {
55
- setQuery(text);
56
- setMatch?.(text);
57
- }}
58
- onSubmit={runSearch}
59
- />
60
- {isLoading && <div className={mx('flex flex-col grow overflow-hidden', activeSurface)}>Loading...</div>}
61
- {allResults.length > 0 && (
62
- <div className={mx('flex flex-col grow overflow-hidden', activeSurface)}>
63
- <SearchResults items={allResults} selected={selected} onSelect={handleSelect} />
64
- </div>
65
- )}
66
- </div>
74
+ <Card.Root key={data.id}>
75
+ <Card.Toolbar>
76
+ <Card.DragHandle />
77
+ <Card.Title>{data.label ?? (data.object && Entity.getLabel(data.object))}</Card.Title>
78
+ <Card.Menu />
79
+ </Card.Toolbar>
80
+ <Surface role='card--content' data={{ subject: data.object }} limit={1} />
81
+ </Card.Root>
67
82
  );
68
83
  };
@@ -0,0 +1,169 @@
1
+ //
2
+ // Copyright 2025 DXOS.org
3
+ //
4
+
5
+ import React, { useCallback, useEffect, useMemo, useState } from 'react';
6
+
7
+ import { Common } from '@dxos/app-framework';
8
+ import { useAppGraph, useCapabilities, useOperationInvoker } from '@dxos/app-framework/react';
9
+ import { Filter, Obj, Query } from '@dxos/echo';
10
+ import { Graph, Node, useActionRunner, useConnections } from '@dxos/plugin-graph';
11
+ import { type Space, useQuery } from '@dxos/react-client/echo';
12
+ import { Toolbar, toLocalizedString, useTranslation } from '@dxos/react-ui';
13
+ import { Card, Mosaic, type StackTileComponent } from '@dxos/react-ui-mosaic';
14
+ import { Layout } from '@dxos/react-ui-mosaic';
15
+ import { SearchList } from '@dxos/react-ui-searchlist';
16
+ import { Text } from '@dxos/schema';
17
+ import { getStyles } from '@dxos/ui-theme';
18
+
19
+ import { meta } from '../meta';
20
+
21
+ /**
22
+ *
23
+ */
24
+ export const SpaceMain = ({ space }: { space: Space }) => {
25
+ const { t } = useTranslation(meta.id);
26
+ const { items, handleSearch } = useSpaceItems(space);
27
+
28
+ return (
29
+ <Layout.Main toolbar>
30
+ <SearchList.Root onSearch={handleSearch}>
31
+ <Toolbar.Root>
32
+ <SearchList.Input placeholder={t('search placeholder')} />
33
+ </Toolbar.Root>
34
+ <SearchList.Content>
35
+ <Mosaic.Container asChild>
36
+ <Mosaic.Viewport padding>
37
+ <Mosaic.Stack items={items} getId={(node) => node.id} Tile={NodeTile} />
38
+ </Mosaic.Viewport>
39
+ </Mosaic.Container>
40
+ </SearchList.Content>
41
+ </SearchList.Root>
42
+ </Layout.Main>
43
+ );
44
+ };
45
+
46
+ const NodeTile: StackTileComponent<Node.Node> = ({ data: node }) => {
47
+ const { t } = useTranslation(meta.id);
48
+ const { graph } = useAppGraph();
49
+ const { invokeSync } = useOperationInvoker();
50
+ const runAction = useActionRunner();
51
+
52
+ const label = toLocalizedString(node.properties.label, t);
53
+ const icon = node.properties.icon ?? 'ph--placeholder--regular';
54
+ const iconHue = node.properties.iconHue;
55
+ const styles = iconHue ? getStyles(iconHue) : undefined;
56
+
57
+ const handleClick = () => {
58
+ if (Node.isAction(node)) {
59
+ // Run action if this is an action node.
60
+ const [parent] = Graph.getConnections(graph, node.id, 'inbound');
61
+ if (parent) {
62
+ void runAction(node, { parent });
63
+ }
64
+ } else {
65
+ // Navigate to the node.
66
+ invokeSync(Common.LayoutOperation.Open, { subject: [node.id] });
67
+ }
68
+ };
69
+
70
+ return (
71
+ <Card.Root fullWidth>
72
+ <Card.Toolbar>
73
+ <Card.ToolbarIconButton variant='ghost' label={label} icon={icon} iconOnly iconClassNames={styles?.icon} />
74
+ <Card.Title onClick={handleClick}>{label}</Card.Title>
75
+ <Card.Menu />
76
+ </Card.Toolbar>
77
+ </Card.Root>
78
+ );
79
+ };
80
+
81
+ /**
82
+ * Hook to resolve metadata (icon, iconHue, etc.) for objects based on their typename.
83
+ */
84
+ // TODO(wittjosiah): Factor out.
85
+ const useMetadataResolver = () => {
86
+ const allMetadata = useCapabilities(Common.Capability.Metadata);
87
+ return useCallback((typename: string) => allMetadata.find((m) => m.id === typename)?.metadata ?? {}, [allMetadata]);
88
+ };
89
+
90
+ /**
91
+ * Hook to get space items with search/filter support.
92
+ * Returns direct children when no query, or full-text search results when searching.
93
+ */
94
+ const useSpaceItems = (space: Space) => {
95
+ const { graph } = useAppGraph();
96
+ const [query, setQuery] = useState<string>();
97
+ const resolveMetadata = useMetadataResolver();
98
+
99
+ // Expand the space node to load its children (needed when navigating directly to a space URL).
100
+ useEffect(() => {
101
+ const frame = requestAnimationFrame(() => {
102
+ Graph.expand(graph, space.id, 'outbound');
103
+ });
104
+ return () => cancelAnimationFrame(frame);
105
+ }, [graph, space.id]);
106
+
107
+ // Get direct children of the space node for the default view.
108
+ const children = useConnections(graph, space.id, 'outbound');
109
+
110
+ const handleSearch = useCallback((text: string) => {
111
+ setQuery(text);
112
+ }, []);
113
+
114
+ // TODO(dmaretskyi): Switch back to QueryEditor once query builder is ready for production.
115
+ // Query results using full-text search.
116
+ const results = useQuery(
117
+ space.db,
118
+ query === undefined
119
+ ? Query.select(Filter.nothing())
120
+ : Query.all(
121
+ Query.select(Filter.text(query, { type: 'full-text' })).select(Filter.not(Filter.type(Text.Text))),
122
+ Query.select(Filter.text(query, { type: 'full-text' }))
123
+ .select(Filter.type(Text.Text))
124
+ .referencedBy('dxos.org/type/Document', 'content'),
125
+ ),
126
+ );
127
+
128
+ // Determine if query is empty (show default view).
129
+ const isQueryEmpty = !query?.trim();
130
+
131
+ // Filter children to those which are objects or actions with disposition 'item'.
132
+ const filteredChildren = useMemo(
133
+ () =>
134
+ children.filter((node) => {
135
+ // Include regular objects.
136
+ if (Obj.isObject(node.data)) {
137
+ return true;
138
+ }
139
+ // Include actions with disposition 'item'.
140
+ return Node.isAction(node) && node.properties.disposition === 'item';
141
+ }),
142
+ [children],
143
+ );
144
+
145
+ // When showing default view, use nodes (which may include actions).
146
+ // When showing search results, wrap objects in a compatible shape with resolved metadata.
147
+ const items = useMemo(() => {
148
+ if (isQueryEmpty) {
149
+ return filteredChildren;
150
+ }
151
+ // Wrap query results as pseudo-nodes for consistent rendering.
152
+ return results.map((obj) => {
153
+ const typename = Obj.getTypename(obj);
154
+ const metadata = typename ? resolveMetadata(typename) : {};
155
+ return {
156
+ id: obj.id,
157
+ type: 'search-result',
158
+ data: obj,
159
+ properties: {
160
+ label: Obj.getLabel(obj) ?? obj.id,
161
+ icon: metadata.icon ?? 'ph--placeholder--regular',
162
+ iconHue: metadata.iconHue,
163
+ },
164
+ } as Node.Node;
165
+ });
166
+ }, [isQueryEmpty, filteredChildren, results, resolveMetadata]);
167
+
168
+ return { items, handleSearch };
169
+ };
@@ -2,6 +2,6 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- export * from './Searchbar';
6
5
  export * from './SearchMain';
7
6
  export * from './SearchDialog';
7
+ export * from './SpaceMain';
@@ -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,7 +5,8 @@
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';
8
+ import { Filter, Obj } from '@dxos/echo';
9
+ import { TestSchema } from '@dxos/echo/testing';
9
10
  import { faker } from '@dxos/random';
10
11
 
11
12
  import { filterObjectsSync } from './sync';
@@ -15,7 +16,8 @@ faker.seed(1);
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();
@@ -25,10 +27,10 @@ describe('Search', () => {
25
27
  Array.from({ length: 20 }).map((_, i) => {
26
28
  const content =
27
29
  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 }));
30
+ return space.db.add(Obj.make(TestSchema.Expando, { title: faker.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
@@ -5,8 +5,8 @@
5
5
  import type * as Schema from 'effect/Schema';
6
6
  import * as SchemaAST from 'effect/SchemaAST';
7
7
 
8
- import { Obj } from '@dxos/echo';
9
- import { DataType } from '@dxos/schema';
8
+ import { type Entity, Obj } from '@dxos/echo';
9
+ import { Text } from '@dxos/schema';
10
10
 
11
11
  import { type SearchResult } from '../types';
12
12
 
@@ -39,39 +39,34 @@ const getIcon = (schema: Schema.Schema.AnyNoContext | undefined): string | undef
39
39
  export type TextFields = Record<string, string>;
40
40
 
41
41
  // TODO(thure): This returns `SearchResult[]` which is not just a narrower set of objects as the name implies.
42
- 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>[] => {
43
43
  if (!match) {
44
44
  return [];
45
45
  }
46
46
 
47
- return objects.reduce<SearchResult[]>((results, object) => {
47
+ return objects.reduce<SearchResult<T>[]>((results, object) => {
48
48
  // TODO(burdon): Hack to ignore Text objects.
49
- if (object instanceof DataType.Text) {
49
+ if (object instanceof Text.Text) {
50
50
  return results;
51
51
  }
52
52
 
53
53
  const fields = mapObjectToTextFields(object);
54
54
  Object.entries(fields).some(([, value]) => {
55
- const result = value.match(match);
56
- if (result) {
57
- // TODO(burdon): Use schema.
58
- const label = getStringProperty(object, ['label', 'name', 'title']);
59
-
60
- results.push({
61
- id: object.id,
62
- type: getIcon(Obj.getSchema(object)),
63
- label,
64
- match,
65
- // TODO(burdon): Truncate.
66
- // TODO(burdon): Issue with sketch documents.
67
- snippet: value !== label ? value : (fields.content ?? fields.description ?? undefined),
68
- object,
69
- });
70
-
71
- return true;
72
- }
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
+ });
73
68
 
74
- return false;
69
+ return true;
75
70
  });
76
71
 
77
72
  return results;
@@ -95,13 +90,13 @@ export const getStringProperty = (object: any, keys: string[]): string | undefin
95
90
  };
96
91
 
97
92
  // TODO(burdon): Filter system fields.
98
- const getKeys = (object: Record<string, unknown>): string[] => {
93
+ const getKeys = <T extends Entity.Unknown>(object: T): string[] => {
99
94
  try {
100
95
  const obj = JSON.parse(JSON.stringify(object));
101
96
  return Object.keys(obj).filter(
102
97
  (key) => key !== 'id' && key !== 'identityKey' && key[0] !== '_' && key[0] !== '@',
103
98
  ) as string[];
104
- } catch (err) {
99
+ } catch {
105
100
  // TODO(burdon): Error with TLDraw sketch.
106
101
  // Uncaught Error: Type with the name content has already been defined with a different constructor
107
102
  return [];
@@ -109,13 +104,13 @@ const getKeys = (object: Record<string, unknown>): string[] => {
109
104
  };
110
105
 
111
106
  // TODO(burdon): Use ECHO schema.
112
- export const mapObjectToTextFields = <T extends Record<string, unknown>>(object: T): TextFields => {
107
+ export const mapObjectToTextFields = <T extends Entity.Unknown>(object: T): TextFields => {
113
108
  return getKeys(object).reduce<TextFields>((fields, key) => {
114
- const value = object[key] as any;
115
- if (typeof value === 'string' || value instanceof DataType.Text) {
109
+ const value = (object as any)[key];
110
+ if (typeof value === 'string' || value instanceof Text.Text) {
116
111
  try {
117
112
  fields[key] = String(value);
118
- } catch (err) {
113
+ } catch {
119
114
  // TODO(burdon): Exception when parsing sketch document.
120
115
  }
121
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-searchlist';
8
10
 
9
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-searchlist';
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,