@dxos/react-ui-attention 0.7.5-staging.b81e783 → 0.8.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.
@@ -3,8 +3,7 @@
3
3
  //
4
4
 
5
5
  import { createContext } from '@radix-ui/react-context';
6
- import { useCallback, type PropsWithChildren } from 'react';
7
- import React from 'react';
6
+ import React, { useCallback, useMemo, type PropsWithChildren } from 'react';
8
7
 
9
8
  import { useDefaultValue } from '@dxos/react-ui';
10
9
 
@@ -20,6 +19,11 @@ const [SelectionContextProvider, useSelectionContext] = createContext<SelectionC
20
19
  selection: new SelectionManager(),
21
20
  });
22
21
 
22
+ /**
23
+ * Manages selection state across the app for multiple contexts.
24
+ */
25
+ // TODO(burdon): Review/remove this.
26
+ // TODO(burdon): When is the selection removed?
23
27
  export const SelectionProvider = ({
24
28
  children,
25
29
  selection: propsSelection,
@@ -28,40 +32,44 @@ export const SelectionProvider = ({
28
32
  return <SelectionContextProvider selection={selection}>{children}</SelectionContextProvider>;
29
33
  };
30
34
 
31
- export const useSelectedItems = (contextId: string): Set<string> => {
35
+ const emptySet = new Set<string>();
36
+
37
+ // TODO(burdon): Return array?
38
+ export const useSelectedItems = (contextId?: string): Set<string> => {
32
39
  const { selection } = useSelectionContext(SELECTION_NAME);
33
- return selection.getSelection(contextId);
40
+ return contextId ? selection.getSelection(contextId) : emptySet;
34
41
  };
35
42
 
36
- export const useSelectionActions = (contextId?: string) => {
43
+ /**
44
+ * Provides functions to manage the selection state for multiple contexts.
45
+ */
46
+ export const useSelectionActions = (contextIds: string[]) => {
47
+ const stableContextIds = useMemo(() => contextIds, [JSON.stringify(contextIds)]);
37
48
  const { selection } = useSelectionContext(SELECTION_NAME);
38
49
 
39
50
  const select = useCallback(
40
51
  (ids: string[]) => {
41
- if (!contextId) {
42
- return new Set();
52
+ for (const contextId of stableContextIds) {
53
+ selection.updateSelection(contextId, ids);
43
54
  }
44
- selection.updateSelection(contextId, ids);
45
55
  },
46
- [selection, contextId],
56
+ [selection, stableContextIds],
47
57
  );
48
58
 
49
59
  const toggle = useCallback(
50
60
  (id: string) => {
51
- if (!contextId) {
52
- return new Set();
61
+ for (const contextId of stableContextIds) {
62
+ selection.toggleSelection(contextId, id);
53
63
  }
54
- selection.toggleSelection(contextId, id);
55
64
  },
56
- [selection, contextId],
65
+ [selection, stableContextIds],
57
66
  );
58
67
 
59
68
  const clear = useCallback(() => {
60
- if (!contextId) {
61
- return;
69
+ for (const contextId of stableContextIds) {
70
+ selection.clearSelection(contextId);
62
71
  }
63
- selection.clearSelection(contextId);
64
- }, [selection, contextId]);
72
+ }, [selection, stableContextIds]);
65
73
 
66
74
  return { select, toggle, clear };
67
75
  };
package/src/selection.ts CHANGED
@@ -10,6 +10,7 @@ import { create, type ReactiveObject } from '@dxos/live-object';
10
10
  * Manages selection state for different contexts.
11
11
  * Each context maintains its own list of selected IDs.
12
12
  */
13
+ // TODO(burdon): Review/remove this abstraction. Selection should be managed by specific components.
13
14
  export class SelectionManager {
14
15
  private readonly _state = create<{ selections: Record<string, Set<string>> }>({ selections: {} });
15
16