@dxos/react-ui-attention 0.8.4-main.c85a9c8dae → 0.8.4-main.d05673bc65

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.c85a9c8dae",
3
+ "version": "0.8.4-main.d05673bc65",
4
4
  "description": "Signifier components and providers for attention system.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -57,11 +57,11 @@
57
57
  "@radix-ui/react-slot": "1.1.2",
58
58
  "@radix-ui/react-use-controllable-state": "1.1.0",
59
59
  "effect": "3.19.16",
60
- "@dxos/echo": "0.8.4-main.c85a9c8dae",
61
- "@dxos/invariant": "0.8.4-main.c85a9c8dae",
62
- "@dxos/log": "0.8.4-main.c85a9c8dae",
63
- "@dxos/util": "0.8.4-main.c85a9c8dae",
64
- "@dxos/react-hooks": "0.8.4-main.c85a9c8dae"
60
+ "@dxos/invariant": "0.8.4-main.d05673bc65",
61
+ "@dxos/echo": "0.8.4-main.d05673bc65",
62
+ "@dxos/log": "0.8.4-main.d05673bc65",
63
+ "@dxos/react-hooks": "0.8.4-main.d05673bc65",
64
+ "@dxos/util": "0.8.4-main.d05673bc65"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@types/react": "~19.2.7",
@@ -69,17 +69,17 @@
69
69
  "react": "~19.2.3",
70
70
  "react-dom": "~19.2.3",
71
71
  "vite": "^7.1.11",
72
- "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
73
- "@dxos/storybook-utils": "0.8.4-main.c85a9c8dae",
74
- "@dxos/random": "0.8.4-main.c85a9c8dae",
75
- "@dxos/ui-theme": "0.8.4-main.c85a9c8dae"
72
+ "@dxos/random": "0.8.4-main.d05673bc65",
73
+ "@dxos/react-ui": "0.8.4-main.d05673bc65",
74
+ "@dxos/storybook-utils": "0.8.4-main.d05673bc65",
75
+ "@dxos/ui-theme": "0.8.4-main.d05673bc65"
76
76
  },
77
77
  "peerDependencies": {
78
78
  "effect": "3.19.16",
79
79
  "react": "~19.2.3",
80
80
  "react-dom": "~19.2.3",
81
- "@dxos/react-ui": "0.8.4-main.c85a9c8dae",
82
- "@dxos/ui-theme": "0.8.4-main.c85a9c8dae"
81
+ "@dxos/react-ui": "0.8.4-main.d05673bc65",
82
+ "@dxos/ui-theme": "0.8.4-main.d05673bc65"
83
83
  },
84
84
  "publishConfig": {
85
85
  "access": "public"
@@ -5,48 +5,177 @@
5
5
  import { Registry } from '@effect-atom/atom-react';
6
6
  import { describe, expect, test } from 'vitest';
7
7
 
8
- import { AttentionManager } from './attention';
8
+ import { AttentionManager, expandAttendableId, getSegmentId, getParentId, isSeparatorPrefixed } from './attention';
9
9
 
10
10
  describe('AttentionManager', () => {
11
- test('takes an initial attendable', () => {
11
+ test('takes an initial attended id', ({ expect }) => {
12
12
  const registry = Registry.make();
13
- const attention = new AttentionManager(registry, ['a', 'b', 'c']);
14
- expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
15
- expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
16
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
17
- expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
18
- expect(attention.get(['c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
19
- expect(attention.get(['a', 'b', 'c'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
20
- expect(attention.get(['b'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
13
+ const attention = new AttentionManager(registry, ['root/a/b']);
14
+
15
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b']);
16
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
17
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
18
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
19
+ expect(attention.get('unrelated')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
21
20
  });
22
21
 
23
- test('current returns last updated key', () => {
22
+ test('current returns last updated ids', ({ expect }) => {
24
23
  const registry = Registry.make();
25
24
  const attention = new AttentionManager(registry);
26
25
  expect(attention.getCurrent()).to.deep.equal([]);
27
26
 
28
- attention.update(['a', 'b', 'c']);
29
- expect(attention.getCurrent()).to.deep.equal(['a', 'b', 'c']);
27
+ attention.update(['root/a/b/c']);
28
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b/c']);
30
29
  });
31
30
 
32
- test('keys returns all stored attendables', () => {
31
+ test('keys returns all tracked qualified ids', ({ expect }) => {
33
32
  const registry = Registry.make();
34
33
  const attention = new AttentionManager(registry);
35
34
  expect(attention.keys()).to.deep.equal([]);
36
35
 
37
- attention.update(['a']);
38
- expect(attention.keys()).to.deep.equal([['a']]);
36
+ attention.update(['root/a']);
37
+ expect(attention.keys()).to.include('root');
38
+ expect(attention.keys()).to.include('root/a');
39
+ });
40
+
41
+ test('get returns attention state for a given qualified id', ({ expect }) => {
42
+ const registry = Registry.make();
43
+ const attention = new AttentionManager(registry);
44
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
45
+
46
+ attention.update(['root/a']);
47
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
48
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
49
+ });
50
+
51
+ test('clears previous attention on update', ({ expect }) => {
52
+ const registry = Registry.make();
53
+ const attention = new AttentionManager(registry);
54
+
55
+ attention.update(['root/a/b']);
56
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
57
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
58
+
59
+ attention.update(['root/x/y']);
60
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
61
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
62
+ expect(attention.get('root/x/y')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
63
+ expect(attention.get('root/x')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
64
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
65
+ });
66
+
67
+ test('separator-prefixed segment marks parent with isRelated and isAncestor', ({ expect }) => {
68
+ const registry = Registry.make();
69
+ const attention = new AttentionManager(registry);
70
+
71
+ attention.update(['root/space/obj/~settings']);
72
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
73
+ hasAttention: true,
74
+ isAncestor: false,
75
+ isRelated: false,
76
+ });
77
+ expect(attention.get('root/space/obj')).to.deep.equal({
78
+ hasAttention: false,
79
+ isAncestor: true,
80
+ isRelated: true,
81
+ });
82
+ expect(attention.get('root/space')).to.deep.equal({
83
+ hasAttention: false,
84
+ isAncestor: true,
85
+ isRelated: false,
86
+ });
87
+ expect(attention.get('root')).to.deep.equal({
88
+ hasAttention: false,
89
+ isAncestor: true,
90
+ isRelated: false,
91
+ });
92
+ });
93
+
94
+ test('switching from separator-prefixed to non-prefixed clears parent isRelated', ({ expect }) => {
95
+ const registry = Registry.make();
96
+ const attention = new AttentionManager(registry);
97
+
98
+ attention.update(['root/space/obj/~settings']);
99
+ expect(attention.get('root/space/obj')).to.deep.equal({
100
+ hasAttention: false,
101
+ isAncestor: true,
102
+ isRelated: true,
103
+ });
39
104
 
40
- attention.update(['a', 'b']);
41
- expect(attention.keys()).to.deep.equal([['a'], ['b'], ['a', 'b']]);
105
+ attention.update(['root/space/obj']);
106
+ expect(attention.get('root/space/obj')).to.deep.equal({
107
+ hasAttention: true,
108
+ isAncestor: false,
109
+ isRelated: false,
110
+ });
111
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
112
+ hasAttention: false,
113
+ isAncestor: false,
114
+ isRelated: false,
115
+ });
42
116
  });
43
117
 
44
- test('get returns attention object for a given key', () => {
118
+ test('keys sharing the same segment id are marked isRelated', ({ expect }) => {
45
119
  const registry = Registry.make();
46
120
  const attention = new AttentionManager(registry);
47
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
48
121
 
49
- attention.update(['a']);
50
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
122
+ // Pre-register the alternate path so the manager knows about it.
123
+ attention.get('root/z/y/c');
124
+
125
+ attention.update(['root/a/b/c']);
126
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
127
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
128
+ });
129
+
130
+ test('related keys are cleared when attention moves to a different segment id', ({ expect }) => {
131
+ const registry = Registry.make();
132
+ const attention = new AttentionManager(registry);
133
+
134
+ attention.get('root/z/y/c');
135
+ attention.update(['root/a/b/c']);
136
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
137
+
138
+ attention.update(['root/x/y/d']);
139
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
140
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
141
+ });
142
+ });
143
+
144
+ describe('expandAttendableId', () => {
145
+ test('produces progressive prefixes', ({ expect }) => {
146
+ expect(expandAttendableId('root/space/types/doc')).to.deep.equal([
147
+ 'root',
148
+ 'root/space',
149
+ 'root/space/types',
150
+ 'root/space/types/doc',
151
+ ]);
152
+ });
153
+
154
+ test('single segment returns itself', ({ expect }) => {
155
+ expect(expandAttendableId('root')).to.deep.equal(['root']);
156
+ });
157
+ });
158
+
159
+ describe('isSeparatorPrefixed', () => {
160
+ test('detects separator prefix on last segment', ({ expect }) => {
161
+ expect(isSeparatorPrefixed('root/space/obj/~settings')).toBe(true);
162
+ expect(isSeparatorPrefixed('root/space/obj')).toBe(false);
163
+ expect(isSeparatorPrefixed('~standalone')).toBe(true);
164
+ });
165
+ });
166
+
167
+ describe('getParentId', () => {
168
+ test('returns parent qualified id', ({ expect }) => {
169
+ expect(getParentId('root/space/obj/~settings')).toBe('root/space/obj');
170
+ expect(getParentId('root/space')).toBe('root');
171
+ expect(getParentId('root')).toBeUndefined();
172
+ });
173
+ });
174
+
175
+ describe('getSegmentId', () => {
176
+ test('returns the last path segment', ({ expect }) => {
177
+ expect(getSegmentId('root/space/obj')).toBe('obj');
178
+ expect(getSegmentId('root')).toBe('root');
179
+ expect(getSegmentId('root/a/~settings')).toBe('~settings');
51
180
  });
52
181
  });
package/src/attention.ts CHANGED
@@ -4,48 +4,42 @@
4
4
 
5
5
  import { Atom, type Registry } from '@effect-atom/atom-react';
6
6
 
7
- import { ComplexMap } from '@dxos/util';
8
-
9
7
  import { ATTENDABLE_PATH_SEPARATOR, type Attention } from './types';
10
8
 
11
- // TODO(wittjosiah): Use mosaic path utility?
12
- const stringKey = (key: string[]) => key.join(',');
13
-
14
9
  /**
15
10
  * Manages attention state for an application.
11
+ * Attention keys are slash-qualified graph IDs; ancestry is derived from progressive prefixes.
16
12
  */
17
13
  export class AttentionManager {
18
- // Each attention path is associated with an attention atom.
19
- // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
20
- private readonly _map = new ComplexMap<string[], Atom.Writable<Attention>>(stringKey);
14
+ private readonly _map = new Map<string, Atom.Writable<Attention>>();
21
15
  private readonly _currentAtom: Atom.Writable<string[]>;
22
16
 
23
17
  constructor(
24
18
  private readonly _registry: Registry.Registry,
25
19
  initial: string[] = [],
26
20
  ) {
27
- this._currentAtom = Atom.make<string[]>([]);
21
+ this._currentAtom = Atom.make<string[]>([]).pipe(Atom.keepAlive);
28
22
  if (initial.length > 0) {
29
23
  this.update(initial);
30
24
  }
31
25
  }
32
26
 
33
27
  /**
34
- * Atom for the currently attended element path.
28
+ * Atom for the currently attended element IDs.
35
29
  */
36
30
  get current(): Atom.Atom<string[]> {
37
31
  return this._currentAtom;
38
32
  }
39
33
 
40
34
  /**
41
- * Gets the currently attended element path.
35
+ * Gets the currently attended element IDs.
42
36
  */
43
37
  getCurrent(): readonly string[] {
44
38
  return this._registry.get(this._currentAtom);
45
39
  }
46
40
 
47
41
  /**
48
- * Subscribe to changes in the current attention path.
42
+ * Subscribe to changes in the current attention IDs.
49
43
  */
50
44
  subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
51
45
  this._registry.get(this._currentAtom);
@@ -55,79 +49,104 @@ export class AttentionManager {
55
49
  }
56
50
 
57
51
  /**
58
- * All attention paths.
52
+ * All tracked qualified IDs.
59
53
  */
60
- keys(): string[][] {
54
+ keys(): string[] {
61
55
  return Array.from(this._map.keys());
62
56
  }
63
57
 
64
58
  /**
65
- * Get the attention state for a given path.
59
+ * Get the attention state for a given qualified ID.
66
60
  */
67
- get(key: string[]): Attention {
68
- const atom = this._getAtom(key);
61
+ get(id: string): Attention {
62
+ const atom = this._getAtom(id);
69
63
  return this._registry.get(atom);
70
64
  }
71
65
 
72
66
  /**
73
- * Subscribe to changes in the attention state for a given path.
67
+ * Subscribe to changes in the attention state for a given qualified ID.
74
68
  */
75
- subscribe(key: string[], cb: (attention: Attention) => void): () => void {
76
- const atom = this._getAtom(key);
69
+ subscribe(id: string, cb: (attention: Attention) => void): () => void {
70
+ const atom = this._getAtom(id);
77
71
  this._registry.get(atom);
78
72
  return this._registry.subscribe(atom, () => {
79
73
  cb(this._registry.get(atom));
80
74
  });
81
75
  }
82
76
 
83
- private _getAtom(key: string[]): Atom.Writable<Attention> {
84
- const existing = this._map.get(key);
77
+ private _getAtom(id: string): Atom.Writable<Attention> {
78
+ const existing = this._map.get(id);
85
79
  if (existing) {
86
80
  return existing;
87
81
  }
88
82
 
89
- const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false });
90
- this._map.set(key, newAtom);
83
+ const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false }).pipe(
84
+ Atom.keepAlive,
85
+ );
86
+ this._map.set(id, newAtom);
91
87
  return newAtom;
92
88
  }
93
89
 
94
90
  /**
95
91
  * Update the currently attended element.
92
+ * Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
93
+ * Ancestry is derived from the progressive prefixes of the primary ID.
94
+ * Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
95
+ * attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID has a
96
+ * separator-prefixed last segment (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
96
97
  *
97
98
  * @internal
98
99
  */
99
- update(nextKey: string[]): void {
100
- const currentKey = this.getCurrent();
101
- const currentRelatedTo = currentKey[0];
102
- const nextRelatedTo = nextKey[0];
100
+ update(nextIds: string[]): void {
101
+ const primaryId = nextIds[0];
102
+ if (!primaryId) {
103
+ return;
104
+ }
103
105
 
104
- // Ensure related attention object is initialized.
105
- this._getAtom([nextRelatedTo]);
106
+ const currentIds = this.getCurrent();
107
+ const prevPrimaryId = currentIds[0];
108
+ // Clear previous attention state: ancestors, primary, and related keys.
109
+ if (prevPrimaryId) {
110
+ const prevPrefixes = expandAttendableId(prevPrimaryId);
111
+ for (const prefix of prevPrefixes) {
112
+ this._set(prefix, {});
113
+ }
114
+ const prevSegmentId = getSegmentId(prevPrimaryId);
115
+ for (const key of this.keys()) {
116
+ if (getSegmentId(key) === prevSegmentId) {
117
+ this._set(key, {});
118
+ }
119
+ }
120
+ }
106
121
 
107
- const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
108
- const nextRelatedKeys = this.keys().filter(
109
- (key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
110
- );
111
- const prevRelatedKeys = currentRelatedKeys.filter(
112
- (curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
113
- );
122
+ this._registry.set(this._currentAtom, nextIds);
114
123
 
115
- const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
116
- const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
117
- const prevAncestorKeys = currentAncestorKeys.filter(
118
- (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
119
- );
124
+ // Set ancestors and primary.
125
+ const prefixes = expandAttendableId(primaryId);
126
+ const prefixSet = new Set(prefixes);
127
+ const separatedParent = isSeparatorPrefixed(primaryId) ? getParentId(primaryId) : undefined;
128
+
129
+ for (const prefix of prefixes) {
130
+ if (prefix === primaryId) {
131
+ this._set(prefix, { hasAttention: true });
132
+ } else if (prefix === separatedParent) {
133
+ this._set(prefix, { isAncestor: true, isRelated: true });
134
+ } else {
135
+ this._set(prefix, { isAncestor: true });
136
+ }
137
+ }
120
138
 
121
- this._registry.set(this._currentAtom, nextKey);
122
- prevRelatedKeys.forEach((key) => this._set(key, {}));
123
- prevAncestorKeys.forEach((key) => this._set(key, {}));
124
- nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
125
- nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
126
- this._set(nextKey, { hasAttention: true });
139
+ // Set related keys: any tracked key sharing the same segment ID.
140
+ const segmentId = getSegmentId(primaryId);
141
+ for (const key of this.keys()) {
142
+ if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
143
+ this._set(key, { isRelated: true });
144
+ }
145
+ }
127
146
  }
128
147
 
129
- private _set(key: string[], attention: Partial<Attention>): void {
130
- const atom = this._getAtom(key);
148
+ private _set(id: string, attention: Partial<Attention>): void {
149
+ const atom = this._getAtom(id);
131
150
  this._registry.set(atom, {
132
151
  hasAttention: attention.hasAttention ?? false,
133
152
  isAncestor: attention.isAncestor ?? false,
@@ -137,7 +156,8 @@ export class AttentionManager {
137
156
  }
138
157
 
139
158
  /**
140
- * Accumulates all attendable id's between the element provided and the root, inclusive.
159
+ * Accumulates all attendable IDs between the element provided and the root, inclusive.
160
+ * Each `data-attendable-id` value is treated as a single qualified ID (no splitting).
141
161
  */
142
162
  export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
143
163
  // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
@@ -153,8 +173,7 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
153
173
  return getAttendables(selector, trigger, acc);
154
174
  }
155
175
  } else {
156
- acc.push(...attendableId.split(ATTENDABLE_PATH_SEPARATOR));
157
- // TODO: Attempt tail recursion.
176
+ acc.push(attendableId);
158
177
  return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
159
178
  }
160
179
  }
@@ -165,3 +184,39 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
165
184
  export type AttendableId = { attendableId?: string };
166
185
 
167
186
  export type Related = { related?: boolean };
187
+
188
+ /**
189
+ * Decompose a qualified graph ID into progressive prefixes for ancestry tracking.
190
+ * e.g. `root/a/b/c` yields `['root', 'root/a', 'root/a/b', 'root/a/b/c']`.
191
+ */
192
+ export const expandAttendableId = (qualifiedId: string): string[] => {
193
+ const segments = qualifiedId.split('/');
194
+ return segments.map((_, index) => segments.slice(0, index + 1).join('/'));
195
+ };
196
+
197
+ /**
198
+ * Whether the last segment of a qualified ID starts with the attendable path separator.
199
+ */
200
+ export const isSeparatorPrefixed = (qualifiedId: string): boolean => {
201
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
202
+ return lastSegment.startsWith(ATTENDABLE_PATH_SEPARATOR);
203
+ };
204
+
205
+ /**
206
+ * Get the parent qualified ID (everything before the last `/` segment).
207
+ */
208
+ export const getParentId = (qualifiedId: string | undefined): string | undefined => {
209
+ if (!qualifiedId) {
210
+ return undefined;
211
+ }
212
+
213
+ const lastSlash = qualifiedId.lastIndexOf('/');
214
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
215
+ };
216
+
217
+ /**
218
+ * Extract the segment ID (last segment) of a qualified ID.
219
+ */
220
+ export const getSegmentId = (qualifiedId: string): string => {
221
+ return qualifiedId.split('/').pop() ?? qualifiedId;
222
+ };
@@ -10,7 +10,7 @@ import { mx } from '@dxos/ui-theme';
10
10
 
11
11
  const attentionGlyphStyles = mx(
12
12
  'inline-block rounded-xs w-3 h-3 bg-transparent text-accent-text transition-colors',
13
- '[[data-contains-attended=true]_&]:bg-attention-surface-surface',
13
+ '[[data-contains-attended=true]_&]:bg-attention-contains',
14
14
  '[[data-attention=true]_&]:bg-accent-surface',
15
15
  '[[data-attention=true]_&]:text-accent-surface-text',
16
16
  '[[aria-current][data-attention=true]_&]:bg-accent-surface',
@@ -23,7 +23,7 @@ import { type ThemedClassName } from '@dxos/react-ui';
23
23
  import { mx } from '@dxos/ui-theme';
24
24
 
25
25
  import { AttentionManager, getAttendables } from '../attention';
26
- import { ATTENDABLE_PATH_SEPARATOR, type Attention } from '../types';
26
+ import { type Attention } from '../types';
27
27
 
28
28
  const ATTENTION_NAME = 'Attention';
29
29
  const ATTENDABLE_ATTRIBUTE = 'data-attendable-id';
@@ -31,41 +31,34 @@ const ATTENTION_SOURCE_ATTRIBUTE = 'data-w-attention-source';
31
31
 
32
32
  type AttentionContextValue = {
33
33
  attention: AttentionManager;
34
- path: string[];
35
34
  };
36
35
 
37
36
  const [AttentionContextProvider, useAttentionContext] = createContext<AttentionContextValue>(ATTENTION_NAME, {
38
37
  attention: undefined as unknown as AttentionManager,
39
- path: [],
40
38
  });
41
39
 
42
40
  const UNKNOWN_ATTENDABLE = { hasAttention: false, isAncestor: false, isRelated: false } as Attention;
43
41
 
42
+ /**
43
+ * Subscribe to the attention state for a qualified graph ID.
44
+ */
44
45
  const useAttention = (attendableId?: string): Attention => {
45
- const { attention, path } = useAttentionContext(ATTENTION_NAME);
46
+ const { attention } = useAttentionContext(ATTENTION_NAME);
46
47
  const [state, setState] = useState<Attention>(UNKNOWN_ATTENDABLE);
47
48
 
48
- const key = useMemo(() => {
49
- if (!attendableId) {
50
- return undefined;
51
- }
52
- return [...attendableId.split(ATTENDABLE_PATH_SEPARATOR), ...path];
53
- }, [attendableId, path]);
54
-
55
49
  useEffect(() => {
56
- if (!key || !attention) {
50
+ if (!attendableId || !attention) {
57
51
  setState(UNKNOWN_ATTENDABLE);
58
52
  return;
59
53
  }
60
54
 
61
- // Set initial state.
62
- setState(attention.get(key));
55
+ const currentState = attention.get(attendableId);
56
+ setState(currentState);
63
57
 
64
- // Subscribe to changes.
65
- return attention.subscribe(key, (newState) => {
58
+ return attention.subscribe(attendableId, (newState) => {
66
59
  setState(newState);
67
60
  });
68
- }, [attention, key]);
61
+ }, [attention, attendableId]);
69
62
 
70
63
  return state;
71
64
  };
@@ -79,10 +72,8 @@ const useAttended = () => {
79
72
  return;
80
73
  }
81
74
 
82
- // Set initial state.
83
75
  setCurrent(attention.getCurrent());
84
76
 
85
- // Subscribe to changes.
86
77
  return attention.subscribeCurrent((newCurrent) => {
87
78
  setCurrent(newCurrent);
88
79
  });
@@ -93,7 +84,6 @@ const useAttended = () => {
93
84
 
94
85
  /**
95
86
  * Computes HTML element attributes to apply so the attention system can detect changes.
96
- * @param attendableId
97
87
  */
98
88
  const useAttentionAttributes = (attendableId?: string) => {
99
89
  const { hasAttention } = useAttention(attendableId);
@@ -107,11 +97,6 @@ const useAttentionAttributes = (attendableId?: string) => {
107
97
  }, [attendableId, hasAttention]);
108
98
  };
109
99
 
110
- const useAttentionPath = () => {
111
- const { path } = useAttentionContext(ATTENTION_NAME);
112
- return path;
113
- };
114
-
115
100
  const RootAttentionProvider = ({
116
101
  children,
117
102
  attention: propsAttention,
@@ -147,7 +132,7 @@ const RootAttentionProvider = ({
147
132
  };
148
133
 
149
134
  return (
150
- <AttentionContextProvider attention={attention} path={[]}>
135
+ <AttentionContextProvider attention={attention}>
151
136
  <div role='none' className='contents' onFocusCapture={handleFocus}>
152
137
  {children}
153
138
  </div>
@@ -155,25 +140,15 @@ const RootAttentionProvider = ({
155
140
  );
156
141
  };
157
142
 
158
- const AttentionProvider = ({ id, children }: PropsWithChildren<{ id: string }>) => {
159
- const { attention, path } = useAttentionContext(ATTENTION_NAME);
160
- const nextPath = useMemo(() => [id, ...path], [id, path]);
161
-
162
- return (
163
- <AttentionContextProvider attention={attention} path={nextPath}>
164
- {children}
165
- </AttentionContextProvider>
166
- );
167
- };
168
-
169
143
  export type AttendableContainerProps = ThemedClassName<
170
144
  ComponentPropsWithRef<'div'> & { id: string; asChild?: boolean }
171
145
  >;
172
146
 
173
147
  /**
174
- * Note that DeckPlugin and StackPlugin both handle attention on their own, and when rendering content in those cases it
175
- * is not necessary to also render an `AttendableContainer`. This component is primarily for Storybook stories and other
176
- * testing scenarios, or the rare cases where an attendable entity is rendered outside of either of those plugins.
148
+ * Note that DeckPlugin and StackPlugin both handle attention on their own,
149
+ * and when rendering content in those cases it is not necessary to also render an `AttendableContainer`.
150
+ * This component is primarily for Storybook stories and other testing scenarios,
151
+ * or the rare cases where an attendable entity is rendered outside of either of those plugins.
177
152
  */
178
153
  const AttendableContainer = forwardRef<HTMLDivElement, AttendableContainerProps>(
179
154
  ({ id, classNames, children, asChild, ...props }, forwardedRef) => {
@@ -182,8 +157,8 @@ const AttendableContainer = forwardRef<HTMLDivElement, AttendableContainerProps>
182
157
  return (
183
158
  <Comp
184
159
  role='none'
185
- {...attentionAttrs}
186
160
  {...props}
161
+ {...attentionAttrs}
187
162
  className={mx('dx-attention-surface', props.tabIndex === 0 && 'dx-focus-ring-inset-over-all', classNames)}
188
163
  ref={forwardedRef}
189
164
  >
@@ -195,13 +170,11 @@ const AttendableContainer = forwardRef<HTMLDivElement, AttendableContainerProps>
195
170
 
196
171
  export {
197
172
  RootAttentionProvider,
198
- AttentionProvider,
199
173
  AttendableContainer,
200
174
  useAttentionContext,
201
175
  useAttention,
202
176
  useAttended,
203
177
  useAttentionAttributes,
204
- useAttentionPath,
205
178
  ATTENTION_NAME,
206
179
  ATTENDABLE_ATTRIBUTE,
207
180
  ATTENTION_SOURCE_ATTRIBUTE,