@dxos/plugin-search 0.6.8-main.046e6cf

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 (67) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +4 -0
  3. package/dist/lib/browser/chunk-56ITWEGS.mjs +23 -0
  4. package/dist/lib/browser/chunk-56ITWEGS.mjs.map +7 -0
  5. package/dist/lib/browser/index.mjs +616 -0
  6. package/dist/lib/browser/index.mjs.map +7 -0
  7. package/dist/lib/browser/meta.json +1 -0
  8. package/dist/lib/browser/meta.mjs +11 -0
  9. package/dist/lib/browser/meta.mjs.map +7 -0
  10. package/dist/types/src/SearchPlugin.d.ts +4 -0
  11. package/dist/types/src/SearchPlugin.d.ts.map +1 -0
  12. package/dist/types/src/components/Search.stories.d.ts +25 -0
  13. package/dist/types/src/components/Search.stories.d.ts.map +1 -0
  14. package/dist/types/src/components/SearchDialog.d.ts +10 -0
  15. package/dist/types/src/components/SearchDialog.d.ts.map +1 -0
  16. package/dist/types/src/components/SearchMain.d.ts +6 -0
  17. package/dist/types/src/components/SearchMain.d.ts.map +1 -0
  18. package/dist/types/src/components/SearchResults/SearchResults.d.ts +19 -0
  19. package/dist/types/src/components/SearchResults/SearchResults.d.ts.map +1 -0
  20. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts +20 -0
  21. package/dist/types/src/components/SearchResults/SearchResults.stories.d.ts.map +1 -0
  22. package/dist/types/src/components/SearchResults/index.d.ts +2 -0
  23. package/dist/types/src/components/SearchResults/index.d.ts.map +1 -0
  24. package/dist/types/src/components/Searchbar/Searchbar.d.ts +13 -0
  25. package/dist/types/src/components/Searchbar/Searchbar.d.ts.map +1 -0
  26. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts +18 -0
  27. package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts.map +1 -0
  28. package/dist/types/src/components/Searchbar/index.d.ts +2 -0
  29. package/dist/types/src/components/Searchbar/index.d.ts.map +1 -0
  30. package/dist/types/src/components/index.d.ts +4 -0
  31. package/dist/types/src/components/index.d.ts.map +1 -0
  32. package/dist/types/src/context.d.ts +12 -0
  33. package/dist/types/src/context.d.ts.map +1 -0
  34. package/dist/types/src/index.d.ts +6 -0
  35. package/dist/types/src/index.d.ts.map +1 -0
  36. package/dist/types/src/meta.d.ts +16 -0
  37. package/dist/types/src/meta.d.ts.map +1 -0
  38. package/dist/types/src/search-sync.d.ts +13 -0
  39. package/dist/types/src/search-sync.d.ts.map +1 -0
  40. package/dist/types/src/search.d.ts +3 -0
  41. package/dist/types/src/search.d.ts.map +1 -0
  42. package/dist/types/src/search.test.d.ts +2 -0
  43. package/dist/types/src/search.test.d.ts.map +1 -0
  44. package/dist/types/src/translations.d.ts +15 -0
  45. package/dist/types/src/translations.d.ts.map +1 -0
  46. package/dist/types/src/types.d.ts +6 -0
  47. package/dist/types/src/types.d.ts.map +1 -0
  48. package/package.json +66 -0
  49. package/src/SearchPlugin.tsx +122 -0
  50. package/src/components/Search.stories.tsx +63 -0
  51. package/src/components/SearchDialog.tsx +141 -0
  52. package/src/components/SearchMain.tsx +48 -0
  53. package/src/components/SearchResults/SearchResults.stories.tsx +57 -0
  54. package/src/components/SearchResults/SearchResults.tsx +124 -0
  55. package/src/components/SearchResults/index.ts +5 -0
  56. package/src/components/Searchbar/Searchbar.stories.tsx +34 -0
  57. package/src/components/Searchbar/Searchbar.tsx +57 -0
  58. package/src/components/Searchbar/index.ts +5 -0
  59. package/src/components/index.ts +7 -0
  60. package/src/context.tsx +41 -0
  61. package/src/index.ts +12 -0
  62. package/src/meta.tsx +20 -0
  63. package/src/search-sync.ts +130 -0
  64. package/src/search.test.ts +36 -0
  65. package/src/search.ts +61 -0
  66. package/src/translations.ts +21 -0
  67. package/src/types.tsx +25 -0
@@ -0,0 +1,122 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type IconProps, MagnifyingGlass } from '@phosphor-icons/react';
6
+ import React from 'react';
7
+
8
+ import {
9
+ type PluginDefinition,
10
+ type LocationProvides,
11
+ type GraphProvides,
12
+ type Plugin,
13
+ resolvePlugin,
14
+ parseIntentPlugin,
15
+ parseGraphPlugin,
16
+ parseNavigationPlugin,
17
+ LayoutAction,
18
+ firstIdInPart,
19
+ } from '@dxos/app-framework';
20
+ import { createExtension, type Node } from '@dxos/plugin-graph';
21
+ import { getActiveSpace } from '@dxos/plugin-space';
22
+
23
+ import { SearchDialog, SearchMain } from './components';
24
+ import { SearchContextProvider } from './context';
25
+ import meta, { SEARCH_PLUGIN, SEARCH_RESULT } from './meta';
26
+ import type { SearchResult } from './search-sync';
27
+ import translations from './translations';
28
+ import { SearchAction, type SearchPluginProvides } from './types';
29
+
30
+ export const SearchPlugin = (): PluginDefinition<SearchPluginProvides> => {
31
+ let navigationPlugin: Plugin<LocationProvides> | undefined;
32
+ let graphPlugin: Plugin<GraphProvides> | undefined;
33
+
34
+ return {
35
+ meta,
36
+ ready: async (plugins) => {
37
+ navigationPlugin = resolvePlugin(plugins, parseNavigationPlugin);
38
+ graphPlugin = resolvePlugin(plugins, parseGraphPlugin);
39
+ },
40
+ provides: {
41
+ translations,
42
+ metadata: {
43
+ records: {
44
+ [SEARCH_RESULT]: {
45
+ parse: (item: SearchResult, type: string) => {
46
+ switch (type) {
47
+ case 'node':
48
+ return { id: item.id, label: item.label, data: item.object };
49
+ case 'object':
50
+ return item.object;
51
+ case 'view-object':
52
+ return item;
53
+ }
54
+ },
55
+ },
56
+ },
57
+ },
58
+ graph: {
59
+ builder: (plugins) => {
60
+ const intentPlugin = resolvePlugin(plugins, parseIntentPlugin);
61
+ return createExtension({
62
+ id: SEARCH_PLUGIN,
63
+ filter: (node): node is Node<null> => node.id === 'root',
64
+ actions: () => [
65
+ {
66
+ id: SearchAction.SEARCH,
67
+ data: async () => {
68
+ await intentPlugin?.provides.intent.dispatch({
69
+ plugin: SEARCH_PLUGIN,
70
+ action: SearchAction.SEARCH,
71
+ });
72
+ },
73
+ properties: {
74
+ label: ['search action label', { ns: SEARCH_PLUGIN }],
75
+ icon: (props: IconProps) => <MagnifyingGlass {...props} />,
76
+ iconSymbol: 'ph--magnifying-glass--regular',
77
+ keyBinding: {
78
+ macos: 'shift+meta+f',
79
+ windows: 'shift+alt+f',
80
+ },
81
+ testId: 'searchPlugin.search',
82
+ },
83
+ },
84
+ ],
85
+ });
86
+ },
87
+ },
88
+ context: ({ children }) => <SearchContextProvider>{children}</SearchContextProvider>,
89
+ surface: {
90
+ component: ({ data, role }) => {
91
+ const location = navigationPlugin?.provides.location;
92
+ const graph = graphPlugin?.provides.graph;
93
+ const space = graph && location ? getActiveSpace(graph, firstIdInPart(location.active, 'main')) : undefined;
94
+
95
+ switch (role) {
96
+ case 'dialog':
97
+ return data.component === `${SEARCH_PLUGIN}/Dialog` ? (
98
+ <SearchDialog subject={data.subject as any} />
99
+ ) : null;
100
+ case 'search-input':
101
+ return space ? <SearchMain space={space} /> : null;
102
+ }
103
+
104
+ return null;
105
+ },
106
+ },
107
+ intent: {
108
+ resolver: (intent, plugins) => {
109
+ switch (intent.action) {
110
+ case SearchAction.SEARCH: {
111
+ const intentPlugin = resolvePlugin(plugins, parseIntentPlugin);
112
+ return intentPlugin?.provides.intent.dispatch({
113
+ action: LayoutAction.SET_LAYOUT,
114
+ data: { element: 'complementary', state: true },
115
+ });
116
+ }
117
+ }
118
+ },
119
+ },
120
+ },
121
+ };
122
+ };
@@ -0,0 +1,63 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import { type Decorator, type StoryFn } from '@storybook/react';
8
+ import React, { type FC, useState } from 'react';
9
+
10
+ import { faker } from '@dxos/random';
11
+ import { DensityProvider } from '@dxos/react-ui';
12
+ import { withFullscreen, withTheme } from '@dxos/storybook-utils';
13
+
14
+ import { SearchResults } from './SearchResults';
15
+ import { Searchbar } from './Searchbar';
16
+ import { SearchContextProvider, useGlobalSearch, useGlobalSearchResults } from '../context';
17
+
18
+ faker.seed(1);
19
+
20
+ const Story: FC<{ objects: any[] }> = ({ objects }) => {
21
+ const [selected, setSelected] = useState<string>();
22
+ const { setMatch } = useGlobalSearch();
23
+ const filteredItems = useGlobalSearchResults(objects);
24
+
25
+ return (
26
+ <DensityProvider density='fine'>
27
+ <div className='flex grow justify-center overflow-hidden'>
28
+ <div className='flex flex-col w-[300px] m-4 overflow-hidden'>
29
+ <Searchbar variant='subdued' placeholder='Enter regular expression...' onChange={setMatch} />
30
+ <SearchResults items={filteredItems} selected={selected} onSelect={setSelected} />
31
+ </div>
32
+ </div>
33
+ </DensityProvider>
34
+ );
35
+ };
36
+
37
+ const SearchContextDecorator = (): Decorator => {
38
+ return (Story: StoryFn) => (
39
+ <SearchContextProvider>
40
+ <Story />
41
+ </SearchContextProvider>
42
+ );
43
+ };
44
+
45
+ export default {
46
+ title: 'plugin-search/Search',
47
+ component: Searchbar,
48
+ render: Story,
49
+ decorators: [withTheme, withFullscreen(), SearchContextDecorator()],
50
+ parameters: {
51
+ layout: 'fullscreen',
52
+ },
53
+ };
54
+
55
+ export const Default = {
56
+ args: {
57
+ objects: Array.from({ length: 8 }).map(() => ({
58
+ id: faker.string.uuid(),
59
+ label: faker.lorem.sentence(4).replace(/\./g, ''),
60
+ content: faker.lorem.sentences(3),
61
+ })),
62
+ },
63
+ };
@@ -0,0 +1,141 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { Placeholder } from '@phosphor-icons/react';
6
+ import React, { forwardRef, useCallback, useState } from 'react';
7
+
8
+ import {
9
+ useResolvePlugin,
10
+ parseNavigationPlugin,
11
+ parseGraphPlugin,
12
+ NavigationAction,
13
+ LayoutAction,
14
+ type LayoutCoordinate,
15
+ isLayoutParts,
16
+ useIntentDispatcher,
17
+ } from '@dxos/app-framework';
18
+ import { type Node } from '@dxos/plugin-graph';
19
+ import { useClient } from '@dxos/react-client';
20
+ import { fullyQualifiedId, useQuery } from '@dxos/react-client/echo';
21
+ import { Button, Dialog, toLocalizedString, useTranslation } from '@dxos/react-ui';
22
+ import { SearchList, type SearchListItemProps } from '@dxos/react-ui-searchlist';
23
+ import { descriptionText, mx } from '@dxos/react-ui-theme';
24
+
25
+ import { SEARCH_PLUGIN } from '../meta';
26
+ import { useSearchResults } from '../search';
27
+
28
+ type SearchListResultProps = {
29
+ node: Node;
30
+ } & Pick<SearchListItemProps, 'onSelect'>;
31
+
32
+ const SearchListResult = forwardRef<HTMLDivElement, SearchListResultProps>(({ node, onSelect }, forwardedRef) => {
33
+ const { t } = useTranslation(SEARCH_PLUGIN);
34
+ const Icon = node?.properties.icon ?? Placeholder;
35
+ const label = toLocalizedString(node?.properties.label ?? 'never', t);
36
+ return (
37
+ <SearchList.Item
38
+ value={label}
39
+ key={node!.id}
40
+ classNames='flex gap-2 items-center pli-2'
41
+ onSelect={() => onSelect?.(node!.id)}
42
+ ref={forwardedRef}
43
+ >
44
+ <Icon />
45
+ <span className='is-0 grow truncate'>{label}</span>
46
+ </SearchList.Item>
47
+ );
48
+ });
49
+
50
+ export const SearchDialog = ({
51
+ subject,
52
+ }: {
53
+ subject: { action: NavigationAction; layoutCoordinate: LayoutCoordinate; position: 'add-after' | 'add-before' };
54
+ }) => {
55
+ const { t } = useTranslation(SEARCH_PLUGIN);
56
+ const graphPlugin = useResolvePlugin(parseGraphPlugin);
57
+ const graph = graphPlugin?.provides.graph;
58
+ const navigationPlugin = useResolvePlugin(parseNavigationPlugin);
59
+ const providedClosed = navigationPlugin?.provides.location.closed ?? [];
60
+ const closed = (Array.isArray(providedClosed) ? providedClosed : [providedClosed])
61
+ .map((id) => graph?.findNode(id))
62
+ .filter(Boolean);
63
+ const active = navigationPlugin?.provides.location.active;
64
+ const [queryString, setQueryString] = useState('');
65
+ const client = useClient();
66
+ const dangerouslyLoadAllObjects = useQuery(client.spaces);
67
+ const [pending, results] = useSearchResults(queryString, dangerouslyLoadAllObjects);
68
+ const resultObjects = Array.from(results.keys());
69
+ const dispatch = useIntentDispatcher();
70
+
71
+ const handleSelect = useCallback(
72
+ (nodeId: string) => {
73
+ if (active && isLayoutParts(active)) {
74
+ // If node is already present in the active parts, scroll to it and close the dialog.
75
+ const index = active?.main?.findIndex((entry) => entry.id === nodeId);
76
+ if (index !== -1) {
77
+ return dispatch([
78
+ { action: LayoutAction.SET_LAYOUT, data: { element: 'dialog', state: false } },
79
+ { action: LayoutAction.SCROLL_INTO_VIEW, data: { id: nodeId } },
80
+ ]);
81
+ }
82
+
83
+ return dispatch([
84
+ {
85
+ action: NavigationAction.ADD_TO_ACTIVE,
86
+ data: {
87
+ part: subject.layoutCoordinate.part,
88
+ id: nodeId,
89
+ pivotId: subject.layoutCoordinate.entryId,
90
+ positioning: 'end',
91
+ },
92
+ },
93
+ { action: LayoutAction.SET_LAYOUT, data: { element: 'dialog', state: false } },
94
+ { action: LayoutAction.SCROLL_INTO_VIEW, data: { id: nodeId } },
95
+ ]);
96
+ }
97
+ },
98
+ [subject, dispatch, active],
99
+ );
100
+
101
+ return (
102
+ <Dialog.Content classNames={['md:max-is-[24rem] overflow-hidden mbs-12']}>
103
+ <Dialog.Title>{t('search dialog title')}</Dialog.Title>
104
+ <SearchList.Root label={t('search placeholder')} classNames='flex flex-col grow overflow-hidden my-2'>
105
+ <SearchList.Input
106
+ value={queryString}
107
+ onValueChange={(nextValue) => {
108
+ setQueryString(nextValue);
109
+ }}
110
+ placeholder={t('search placeholder')}
111
+ classNames='px-1 my-2'
112
+ />
113
+ <SearchList.Content classNames='max-bs-[24rem] overflow-auto'>
114
+ {queryString.length > 0 ? (
115
+ resultObjects.length > 0 ? (
116
+ resultObjects
117
+ .map((object) => graph?.findNode(fullyQualifiedId(object)))
118
+ .filter(Boolean)
119
+ .map((node) => <SearchListResult key={node!.id} node={node!} onSelect={handleSelect} />)
120
+ ) : (
121
+ <p className='pli-1'>{t(pending ? 'pending results message' : 'empty results message')}</p>
122
+ )
123
+ ) : (
124
+ <>
125
+ {closed.length > 0 && <h2 className={mx('mlb-1', descriptionText)}>{t('recently closed heading')}</h2>}
126
+ {closed.map((node) => (
127
+ <SearchListResult key={node!.id} node={node!} onSelect={handleSelect} />
128
+ ))}
129
+ </>
130
+ )}
131
+ </SearchList.Content>
132
+ </SearchList.Root>
133
+
134
+ <Dialog.Close asChild>
135
+ <Button variant='primary' classNames='mbs-2'>
136
+ {t('close label', { ns: 'os' })}
137
+ </Button>
138
+ </Dialog.Close>
139
+ </Dialog.Content>
140
+ );
141
+ };
@@ -0,0 +1,48 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import React, { type FC, useState } from 'react';
6
+
7
+ import { useClient } from '@dxos/react-client';
8
+ import { useQuery, type Space } from '@dxos/react-client/echo';
9
+ import { useTranslation } from '@dxos/react-ui';
10
+ import { groupSurface, mx } from '@dxos/react-ui-theme';
11
+
12
+ import { SearchResults } from './SearchResults';
13
+ import { Searchbar } from './Searchbar';
14
+ import { useGlobalSearch, useGlobalSearchResults } from '../context';
15
+ import { SEARCH_PLUGIN } from '../meta';
16
+
17
+ export const SearchMain: FC<{ space: Space }> = ({ space }) => {
18
+ const { t } = useTranslation(SEARCH_PLUGIN);
19
+ const client = useClient();
20
+ const { setMatch } = useGlobalSearch();
21
+ // TODO(burdon): UX to select all spaces.
22
+ const allSpaces = false;
23
+
24
+ // TODO(burdon): Returns ALL objects (e.g., incl. Text objects that are fields of parent objects).
25
+ const objects = useQuery(allSpaces ? client.spaces : space);
26
+ const results = useGlobalSearchResults(objects);
27
+
28
+ const [selected, setSelected] = useState<string>();
29
+
30
+ // TODO(burdon): Activate and/or filter current main (set context).
31
+ const handleSelect = (id: string) => {
32
+ setSelected((selected) => (selected === id ? undefined : id));
33
+ };
34
+
35
+ return (
36
+ <div className='flex flex-col grow h-full'>
37
+ <Searchbar placeholder={t('search placeholder')} onChange={setMatch} />
38
+ {results.length > 0 && (
39
+ <div className='absolute top-[--topbar-size] bottom-0 z-[1]'>
40
+ {/* TODO(burdon): Change to Portal? */}
41
+ <div className={mx('flex flex-col h-full overflow-hidden', groupSurface)}>
42
+ <SearchResults items={results} selected={selected} onSelect={handleSelect} />
43
+ </div>
44
+ </div>
45
+ )}
46
+ </div>
47
+ );
48
+ };
@@ -0,0 +1,57 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import React, { type FC, useState } from 'react';
8
+
9
+ import { faker } from '@dxos/random';
10
+ import { DensityProvider } from '@dxos/react-ui';
11
+ import { withFullscreen, withTheme } from '@dxos/storybook-utils';
12
+
13
+ import { SearchResults, type SearchResultsProps } from './SearchResults';
14
+ import { filterObjectsSync } from '../../search-sync';
15
+
16
+ faker.seed(1);
17
+
18
+ const Story: FC<SearchResultsProps> = (args) => {
19
+ const [selected, setSelected] = useState<string>();
20
+
21
+ return (
22
+ <DensityProvider density='fine'>
23
+ <div className='flex grow justify-center overflow-hidden'>
24
+ <div className='flex w-[300px] m-4 overflow-hidden'>
25
+ <SearchResults {...args} selected={selected} onSelect={setSelected} />
26
+ </div>
27
+ </div>
28
+ </DensityProvider>
29
+ );
30
+ };
31
+
32
+ export default {
33
+ title: 'plugin-search/SearchResults',
34
+ component: SearchResults,
35
+ render: Story,
36
+ decorators: [withTheme, withFullscreen()],
37
+ parameters: {
38
+ layout: 'fullscreen',
39
+ },
40
+ };
41
+
42
+ const word = 'hello';
43
+
44
+ const objects = Array.from({ length: 100 }).map((_, i) => ({
45
+ id: faker.string.uuid(),
46
+ name: faker.lorem.sentence(4),
47
+ content: faker.lorem.sentence() + ` ${word} ` + faker.lorem.sentence(),
48
+ }));
49
+
50
+ const match = new RegExp(word, 'i');
51
+
52
+ export const Default = {
53
+ args: {
54
+ items: filterObjectsSync(objects, match),
55
+ match,
56
+ },
57
+ };
@@ -0,0 +1,124 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { type Icon, Buildings, Folders, User } from '@phosphor-icons/react';
6
+ import React, { type FC, forwardRef } from 'react';
7
+
8
+ import { ScrollArea } from '@dxos/react-ui';
9
+ import { Card } from '@dxos/react-ui-card';
10
+ import { type MosaicTileComponent, Mosaic } from '@dxos/react-ui-mosaic';
11
+ import { ghostHover, mx } from '@dxos/react-ui-theme';
12
+
13
+ import { SEARCH_RESULT } from '../../meta';
14
+ import type { SearchResult } from '../../search-sync';
15
+
16
+ // TODO(burdon): Factor out.
17
+ const styles = {
18
+ selected: '!bg-primary-100 !dark:bg-primary-900',
19
+ };
20
+
21
+ // TODO(burdon): Registry defined by plugins?
22
+ const getIcon = (type: string): Icon | undefined => {
23
+ const iconMap: Record<string, Icon> = {
24
+ user: User,
25
+ organization: Buildings,
26
+ project: Folders,
27
+ };
28
+
29
+ return iconMap[type];
30
+ };
31
+
32
+ export const Snippet: FC<{ text: string; match?: RegExp }> = ({ text, match }) => {
33
+ let content = <>{text}</>;
34
+ if (match) {
35
+ const result = text.match(match);
36
+ if (result?.index !== undefined) {
37
+ // Break near to match.
38
+ const maxOffset = 16;
39
+ let before = text.slice(0, result.index);
40
+ if (before.length > maxOffset) {
41
+ const idx = before.indexOf(' ', result.index - maxOffset);
42
+ before = '… ' + before.slice(idx);
43
+ }
44
+
45
+ const after = text.slice(result.index + result[0].length);
46
+ content = (
47
+ <>
48
+ {before}
49
+ <span className='text-blue-500'>{result[0]}</span>
50
+ {after}
51
+ </>
52
+ );
53
+ }
54
+ }
55
+
56
+ return <span className='text-xs mb-1 line-clamp-3 text-neutral-300'>{content}</span>;
57
+ };
58
+
59
+ export type SearchItemProps = SearchResult & { selected: boolean } & Pick<SearchResultsProps, 'onSelect'>;
60
+
61
+ export const SearchItem: MosaicTileComponent<SearchItemProps> = forwardRef(
62
+ ({ draggableStyle, draggableProps, item, debug = false }, forwardRef) => {
63
+ const { id, type, label, snippet, match, selected, onSelect } = item;
64
+ const Icon = type ? getIcon(type) : undefined;
65
+
66
+ return (
67
+ <Card.Root
68
+ ref={forwardRef}
69
+ style={draggableStyle}
70
+ classNames={mx('mx-2 mt-2 cursor-pointer', ghostHover, selected && styles.selected)}
71
+ onClick={() => onSelect?.(id)}
72
+ >
73
+ <Card.Header>
74
+ <Card.DragHandle {...draggableProps} />
75
+ <Card.Title title={label ?? 'Untitled'} />
76
+ {Icon && <Card.Endcap Icon={Icon} />}
77
+ </Card.Header>
78
+ {snippet && (
79
+ <Card.Body gutter>
80
+ <Snippet text={snippet} match={match} />
81
+ </Card.Body>
82
+ )}
83
+ {debug && (
84
+ <Card.Body gutter>
85
+ <pre className='text-xs whitespace-pre-line'>{JSON.stringify(item)}</pre>
86
+ </Card.Body>
87
+ )}
88
+ </Card.Root>
89
+ );
90
+ },
91
+ );
92
+
93
+ export type SearchResultsProps = {
94
+ match?: RegExp;
95
+ items: SearchResult[];
96
+ selected?: string;
97
+ onSelect?: (id: string) => void;
98
+ };
99
+
100
+ // TODO(burdon): Key cursor up/down.
101
+ export const SearchResults = ({ items, selected, onSelect }: SearchResultsProps) => {
102
+ const path = 'search';
103
+ return (
104
+ <ScrollArea.Root classNames='grow'>
105
+ <ScrollArea.Viewport>
106
+ <Mosaic.Container id={path} Component={SearchItem}>
107
+ {items.map((item) => (
108
+ <Mosaic.DraggableTile
109
+ key={item.id}
110
+ path={path}
111
+ type={SEARCH_RESULT}
112
+ item={{ ...item, selected: selected === item.id, onSelect }}
113
+ Component={SearchItem}
114
+ />
115
+ ))}
116
+ </Mosaic.Container>
117
+ </ScrollArea.Viewport>
118
+ <ScrollArea.Scrollbar orientation='vertical'>
119
+ <ScrollArea.Thumb />
120
+ </ScrollArea.Scrollbar>
121
+ <ScrollArea.Corner />
122
+ </ScrollArea.Root>
123
+ );
124
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './SearchResults';
@@ -0,0 +1,34 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import '@dxosTheme';
6
+
7
+ import React, { type FC } from 'react';
8
+
9
+ import { DensityProvider } from '@dxos/react-ui';
10
+
11
+ import { Searchbar, type SearchbarProps } from './Searchbar';
12
+
13
+ const Story: FC<SearchbarProps> = (args) => {
14
+ return (
15
+ <DensityProvider density='coarse'>
16
+ <Searchbar {...args} />
17
+ </DensityProvider>
18
+ );
19
+ };
20
+
21
+ export default {
22
+ title: 'plugin-search/Searchbar',
23
+ component: Searchbar,
24
+ render: Story,
25
+ parameters: {
26
+ layout: 'centered',
27
+ },
28
+ };
29
+
30
+ export const Default = {
31
+ args: {
32
+ placeholder: 'Search...',
33
+ },
34
+ };
@@ -0,0 +1,57 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ import { MagnifyingGlass } from '@phosphor-icons/react';
6
+ import React, { useEffect, useRef, useState } from 'react';
7
+
8
+ import { Input, type TextInputProps } from '@dxos/react-ui';
9
+ import { getSize, mx } from '@dxos/react-ui-theme';
10
+
11
+ export type SearchbarProps = Pick<TextInputProps, 'variant' | 'placeholder'> & {
12
+ classes?: {
13
+ root?: string;
14
+ input?: string;
15
+ };
16
+ value?: string;
17
+ onChange?: (text?: string) => void;
18
+ delay?: number;
19
+ };
20
+
21
+ export const Searchbar = ({ classes, variant, placeholder, value, onChange }: SearchbarProps) => {
22
+ const inputRef = useRef<HTMLInputElement>(null);
23
+ const [text, setText] = useState(value);
24
+ useEffect(() => {
25
+ setText(value);
26
+ }, [value]);
27
+ const handleChange = (text?: string) => {
28
+ setText(text);
29
+ onChange?.(text);
30
+ };
31
+
32
+ const handleReset = () => {
33
+ handleChange(undefined);
34
+ inputRef.current?.focus();
35
+ };
36
+
37
+ return (
38
+ <div className='flex w-full items-center pli-1 pbs-1'>
39
+ <Input.Root>
40
+ <Input.TextInput
41
+ ref={inputRef}
42
+ placeholder={placeholder}
43
+ variant={variant}
44
+ value={text ?? ''}
45
+ classNames={mx('pl-3 pr-10', classes?.input)}
46
+ onChange={({ target }) => handleChange(target.value)}
47
+ onKeyDown={({ key }) => key === 'Escape' && handleReset()}
48
+ />
49
+
50
+ {/* TODO(burdon): Margin should be density specific. */}
51
+ <div role='button' className='-ml-7 p-0 cursor-pointer' onClick={handleReset}>
52
+ <MagnifyingGlass className={getSize(5)} />
53
+ </div>
54
+ </Input.Root>
55
+ </div>
56
+ );
57
+ };
@@ -0,0 +1,5 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './Searchbar';
@@ -0,0 +1,7 @@
1
+ //
2
+ // Copyright 2023 DXOS.org
3
+ //
4
+
5
+ export * from './Searchbar';
6
+ export * from './SearchMain';
7
+ export * from './SearchDialog';