@dxos/react-ui-attention 0.8.4-main.ead640a → 0.8.4-main.f466a3d56e

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 (74) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/chunk-D2BLVJ6Z.mjs +421 -0
  3. package/dist/lib/browser/chunk-D2BLVJ6Z.mjs.map +7 -0
  4. package/dist/lib/browser/chunk-I4VO2RQM.mjs +255 -0
  5. package/dist/lib/browser/chunk-I4VO2RQM.mjs.map +7 -0
  6. package/dist/lib/browser/index.mjs +89 -23
  7. package/dist/lib/browser/index.mjs.map +4 -4
  8. package/dist/lib/browser/meta.json +1 -1
  9. package/dist/lib/browser/testing/index.mjs +22 -5
  10. package/dist/lib/browser/testing/index.mjs.map +3 -3
  11. package/dist/lib/browser/types/index.mjs +29 -0
  12. package/dist/lib/browser/types/index.mjs.map +7 -0
  13. package/dist/lib/node-esm/chunk-6INLBC62.mjs +423 -0
  14. package/dist/lib/node-esm/chunk-6INLBC62.mjs.map +7 -0
  15. package/dist/lib/node-esm/chunk-ZTUVSH4P.mjs +256 -0
  16. package/dist/lib/node-esm/chunk-ZTUVSH4P.mjs.map +7 -0
  17. package/dist/lib/node-esm/index.mjs +89 -23
  18. package/dist/lib/node-esm/index.mjs.map +4 -4
  19. package/dist/lib/node-esm/meta.json +1 -1
  20. package/dist/lib/node-esm/testing/index.mjs +22 -5
  21. package/dist/lib/node-esm/testing/index.mjs.map +3 -3
  22. package/dist/lib/node-esm/types/index.mjs +30 -0
  23. package/dist/lib/node-esm/types/index.mjs.map +7 -0
  24. package/dist/types/src/attention.d.ts +60 -20
  25. package/dist/types/src/attention.d.ts.map +1 -1
  26. package/dist/types/src/components/AttentionGlyph/AttentionGlyph.d.ts.map +1 -0
  27. package/dist/types/src/components/AttentionGlyph/AttentionGlyph.stories.d.ts.map +1 -0
  28. package/dist/types/src/components/AttentionGlyph/index.d.ts +2 -0
  29. package/dist/types/src/components/AttentionGlyph/index.d.ts.map +1 -0
  30. package/dist/types/src/components/AttentionProvider/AttentionProvider.d.ts +34 -0
  31. package/dist/types/src/components/AttentionProvider/AttentionProvider.d.ts.map +1 -0
  32. package/dist/types/src/components/AttentionProvider/AttentionProvider.stories.d.ts.map +1 -0
  33. package/dist/types/src/components/AttentionProvider/index.d.ts +2 -0
  34. package/dist/types/src/components/AttentionProvider/index.d.ts.map +1 -0
  35. package/dist/types/src/components/{SelectionProvider.d.ts → SelectionProvider/SelectionProvider.d.ts} +7 -6
  36. package/dist/types/src/components/SelectionProvider/SelectionProvider.d.ts.map +1 -0
  37. package/dist/types/src/components/SelectionProvider/index.d.ts +2 -0
  38. package/dist/types/src/components/SelectionProvider/index.d.ts.map +1 -0
  39. package/dist/types/src/selection.d.ts +23 -3
  40. package/dist/types/src/selection.d.ts.map +1 -1
  41. package/dist/types/src/testing/decorators/withAttention.d.ts +5 -1
  42. package/dist/types/src/testing/decorators/withAttention.d.ts.map +1 -1
  43. package/dist/types/src/types/index.d.ts +3 -0
  44. package/dist/types/src/types/index.d.ts.map +1 -0
  45. package/dist/types/tsconfig.tsbuildinfo +1 -1
  46. package/package.json +34 -32
  47. package/src/attention.test.ts +188 -27
  48. package/src/attention.ts +179 -75
  49. package/src/components/{AttentionGlyph.stories.tsx → AttentionGlyph/AttentionGlyph.stories.tsx} +2 -2
  50. package/src/components/{AttentionGlyph.tsx → AttentionGlyph/AttentionGlyph.tsx} +13 -6
  51. package/src/components/AttentionGlyph/index.ts +5 -0
  52. package/src/components/{AttentionProvider.stories.tsx → AttentionProvider/AttentionProvider.stories.tsx} +3 -4
  53. package/src/components/AttentionProvider/AttentionProvider.tsx +176 -0
  54. package/src/components/AttentionProvider/index.ts +5 -0
  55. package/src/components/SelectionProvider/SelectionProvider.tsx +174 -0
  56. package/src/components/SelectionProvider/index.ts +5 -0
  57. package/src/selection.ts +121 -79
  58. package/src/testing/decorators/withAttention.ts +22 -4
  59. package/src/types/index.ts +9 -0
  60. package/dist/lib/browser/chunk-HELLA3BH.mjs +0 -675
  61. package/dist/lib/browser/chunk-HELLA3BH.mjs.map +0 -7
  62. package/dist/lib/node-esm/chunk-FBZV7QG7.mjs +0 -677
  63. package/dist/lib/node-esm/chunk-FBZV7QG7.mjs.map +0 -7
  64. package/dist/types/src/components/AttentionGlyph.d.ts.map +0 -1
  65. package/dist/types/src/components/AttentionGlyph.stories.d.ts.map +0 -1
  66. package/dist/types/src/components/AttentionProvider.d.ts +0 -38
  67. package/dist/types/src/components/AttentionProvider.d.ts.map +0 -1
  68. package/dist/types/src/components/AttentionProvider.stories.d.ts.map +0 -1
  69. package/dist/types/src/components/SelectionProvider.d.ts.map +0 -1
  70. package/src/components/AttentionProvider.tsx +0 -201
  71. package/src/components/SelectionProvider.tsx +0 -120
  72. /package/dist/types/src/components/{AttentionGlyph.d.ts → AttentionGlyph/AttentionGlyph.d.ts} +0 -0
  73. /package/dist/types/src/components/{AttentionGlyph.stories.d.ts → AttentionGlyph/AttentionGlyph.stories.d.ts} +0 -0
  74. /package/dist/types/src/components/{AttentionProvider.stories.d.ts → AttentionProvider/AttentionProvider.stories.d.ts} +0 -0
package/package.json CHANGED
@@ -1,12 +1,16 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-attention",
3
- "version": "0.8.4-main.ead640a",
3
+ "version": "0.8.4-main.f466a3d56e",
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",
7
- "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/dxos/dxos"
10
+ },
11
+ "license": "FSL-1.1-Apache-2.0",
8
12
  "author": "DXOS.org",
9
- "sideEffects": true,
13
+ "sideEffects": false,
10
14
  "type": "module",
11
15
  "exports": {
12
16
  ".": {
@@ -20,23 +24,21 @@
20
24
  "types": "./dist/types/src/testing/index.d.ts",
21
25
  "browser": "./dist/lib/browser/testing/index.mjs",
22
26
  "node": "./dist/lib/node-esm/testing/index.mjs"
27
+ },
28
+ "./types": {
29
+ "source": "./src/types/index.ts",
30
+ "types": "./dist/types/src/types/index.d.ts",
31
+ "browser": "./dist/lib/browser/types/index.mjs",
32
+ "node": "./dist/lib/node-esm/types/index.mjs"
23
33
  }
24
34
  },
25
35
  "types": "dist/types/src/index.d.ts",
26
- "typesVersions": {
27
- "*": {
28
- "testing": [
29
- "dist/types/src/testing/index.d.ts"
30
- ]
31
- }
32
- },
33
36
  "files": [
34
37
  "dist",
35
38
  "src"
36
39
  ],
37
40
  "dependencies": {
38
- "@preact-signals/safe-react": "^0.9.0",
39
- "@preact/signals-core": "^1.12.1",
41
+ "@effect-atom/atom-react": "^0.5.0",
40
42
  "@radix-ui/primitive": "1.1.1",
41
43
  "@radix-ui/react-compose-refs": "1.1.1",
42
44
  "@radix-ui/react-context": "1.1.1",
@@ -44,30 +46,30 @@
44
46
  "@radix-ui/react-primitive": "2.0.2",
45
47
  "@radix-ui/react-slot": "1.1.2",
46
48
  "@radix-ui/react-use-controllable-state": "1.1.0",
47
- "effect": "3.18.3",
48
- "@dxos/echo": "0.8.4-main.ead640a",
49
- "@dxos/invariant": "0.8.4-main.ead640a",
50
- "@dxos/live-object": "0.8.4-main.ead640a",
51
- "@dxos/log": "0.8.4-main.ead640a",
52
- "@dxos/util": "0.8.4-main.ead640a"
49
+ "effect": "3.20.0",
50
+ "@dxos/echo": "0.8.4-main.f466a3d56e",
51
+ "@dxos/invariant": "0.8.4-main.f466a3d56e",
52
+ "@dxos/react-hooks": "0.8.4-main.f466a3d56e",
53
+ "@dxos/log": "0.8.4-main.f466a3d56e",
54
+ "@dxos/util": "0.8.4-main.f466a3d56e"
53
55
  },
54
56
  "devDependencies": {
55
- "@types/react": "~19.2.2",
56
- "@types/react-dom": "~19.2.1",
57
- "react": "~19.2.0",
58
- "react-dom": "~19.2.0",
59
- "vite": "7.1.9",
60
- "@dxos/react-ui-theme": "0.8.4-main.ead640a",
61
- "@dxos/random": "0.8.4-main.ead640a",
62
- "@dxos/react-ui": "0.8.4-main.ead640a",
63
- "@dxos/storybook-utils": "0.8.4-main.ead640a"
57
+ "@types/react": "~19.2.7",
58
+ "@types/react-dom": "~19.2.3",
59
+ "react": "~19.2.3",
60
+ "react-dom": "~19.2.3",
61
+ "vite": "^8.0.13",
62
+ "@dxos/random": "0.8.4-main.f466a3d56e",
63
+ "@dxos/storybook-utils": "0.8.4-main.f466a3d56e",
64
+ "@dxos/ui-theme": "0.8.4-main.f466a3d56e",
65
+ "@dxos/react-ui": "0.8.4-main.f466a3d56e"
64
66
  },
65
67
  "peerDependencies": {
66
- "effect": "^3.13.3",
67
- "react": "^19.0.0",
68
- "react-dom": "^19.0.0",
69
- "@dxos/react-ui": "0.8.4-main.ead640a",
70
- "@dxos/react-ui-theme": "0.8.4-main.ead640a"
68
+ "effect": "3.20.0",
69
+ "react": "~19.2.3",
70
+ "react-dom": "~19.2.3",
71
+ "@dxos/react-ui": "0.8.4-main.f466a3d56e",
72
+ "@dxos/ui-theme": "0.8.4-main.f466a3d56e"
71
73
  },
72
74
  "publishConfig": {
73
75
  "access": "public"
@@ -2,46 +2,207 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { describe, expect, test } from 'vitest';
5
+ import { Registry } from '@effect-atom/atom-react';
6
+ import { describe, test } from 'vitest';
6
7
 
7
- import { AttentionManager } from './attention';
8
+ import {
9
+ AttentionManager,
10
+ expandAttendableId,
11
+ getLinkedVariant,
12
+ getParentId,
13
+ getSegmentId,
14
+ isLinkedSegment,
15
+ linkedSegment,
16
+ } from './attention';
8
17
 
9
18
  describe('AttentionManager', () => {
10
- test('takes an initial attendable', () => {
11
- const attention = new AttentionManager(['a', 'b', 'c']);
12
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
13
- expect(attention.keys()).to.deep.equal([['a'], ['b', 'c'], ['c'], ['a', 'b', 'c']]);
14
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
15
- expect(attention.get(['b', 'c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
16
- expect(attention.get(['c'])).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
17
- expect(attention.get(['a', 'b', 'c'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
18
- expect(attention.get(['b'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
19
+ test('takes an initial attended id', ({ expect }) => {
20
+ const registry = Registry.make();
21
+ const attention = new AttentionManager(registry, ['root/a/b']);
22
+
23
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b']);
24
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
25
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
26
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
27
+ expect(attention.get('unrelated')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
19
28
  });
20
29
 
21
- test('current returns last updated key', () => {
22
- const attention = new AttentionManager();
23
- expect(attention.current).to.deep.equal([]);
30
+ test('current returns last updated ids', ({ expect }) => {
31
+ const registry = Registry.make();
32
+ const attention = new AttentionManager(registry);
33
+ expect(attention.getCurrent()).to.deep.equal([]);
24
34
 
25
- attention.update(['a', 'b', 'c']);
26
- expect(attention.current).to.deep.equal(['a', 'b', 'c']);
35
+ attention.update(['root/a/b/c']);
36
+ expect(attention.getCurrent()).to.deep.equal(['root/a/b/c']);
27
37
  });
28
38
 
29
- test('keys returns all stored attendables', () => {
30
- const attention = new AttentionManager();
39
+ test('keys returns all tracked qualified ids', ({ expect }) => {
40
+ const registry = Registry.make();
41
+ const attention = new AttentionManager(registry);
31
42
  expect(attention.keys()).to.deep.equal([]);
32
43
 
33
- attention.update(['a']);
34
- expect(attention.keys()).to.deep.equal([['a']]);
44
+ attention.update(['root/a']);
45
+ expect(attention.keys()).to.include('root');
46
+ expect(attention.keys()).to.include('root/a');
47
+ });
48
+
49
+ test('get returns attention state for a given qualified id', ({ expect }) => {
50
+ const registry = Registry.make();
51
+ const attention = new AttentionManager(registry);
52
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
35
53
 
36
- attention.update(['a', 'b']);
37
- expect(attention.keys()).to.deep.equal([['a'], ['b'], ['a', 'b']]);
54
+ attention.update(['root/a']);
55
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
56
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
38
57
  });
39
58
 
40
- test('get returns attention object for a given key', () => {
41
- const attention = new AttentionManager();
42
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
59
+ test('clears previous attention on update', ({ expect }) => {
60
+ const registry = Registry.make();
61
+ const attention = new AttentionManager(registry);
62
+
63
+ attention.update(['root/a/b']);
64
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
65
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
66
+
67
+ attention.update(['root/x/y']);
68
+ expect(attention.get('root/a/b')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
69
+ expect(attention.get('root/a')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
70
+ expect(attention.get('root/x/y')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
71
+ expect(attention.get('root/x')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
72
+ expect(attention.get('root')).to.deep.equal({ hasAttention: false, isAncestor: true, isRelated: false });
73
+ });
74
+
75
+ test('linked segment marks parent with isRelated and isAncestor', ({ expect }) => {
76
+ const registry = Registry.make();
77
+ const attention = new AttentionManager(registry);
78
+
79
+ attention.update(['root/space/obj/~settings']);
80
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
81
+ hasAttention: true,
82
+ isAncestor: false,
83
+ isRelated: false,
84
+ });
85
+ expect(attention.get('root/space/obj')).to.deep.equal({
86
+ hasAttention: false,
87
+ isAncestor: true,
88
+ isRelated: true,
89
+ });
90
+ expect(attention.get('root/space')).to.deep.equal({
91
+ hasAttention: false,
92
+ isAncestor: true,
93
+ isRelated: false,
94
+ });
95
+ expect(attention.get('root')).to.deep.equal({
96
+ hasAttention: false,
97
+ isAncestor: true,
98
+ isRelated: false,
99
+ });
100
+ });
101
+
102
+ test('switching from linked segment to non-linked clears parent isRelated', ({ expect }) => {
103
+ const registry = Registry.make();
104
+ const attention = new AttentionManager(registry);
105
+
106
+ attention.update(['root/space/obj/~settings']);
107
+ expect(attention.get('root/space/obj')).to.deep.equal({
108
+ hasAttention: false,
109
+ isAncestor: true,
110
+ isRelated: true,
111
+ });
112
+
113
+ attention.update(['root/space/obj']);
114
+ expect(attention.get('root/space/obj')).to.deep.equal({
115
+ hasAttention: true,
116
+ isAncestor: false,
117
+ isRelated: false,
118
+ });
119
+ expect(attention.get('root/space/obj/~settings')).to.deep.equal({
120
+ hasAttention: false,
121
+ isAncestor: false,
122
+ isRelated: false,
123
+ });
124
+ });
125
+
126
+ test('keys sharing the same segment id are marked isRelated', ({ expect }) => {
127
+ const registry = Registry.make();
128
+ const attention = new AttentionManager(registry);
129
+
130
+ // Pre-register the alternate path so the manager knows about it.
131
+ attention.get('root/z/y/c');
132
+
133
+ attention.update(['root/a/b/c']);
134
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
135
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
136
+ });
137
+
138
+ test('related keys are cleared when attention moves to a different segment id', ({ expect }) => {
139
+ const registry = Registry.make();
140
+ const attention = new AttentionManager(registry);
141
+
142
+ attention.get('root/z/y/c');
143
+ attention.update(['root/a/b/c']);
144
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: true });
145
+
146
+ attention.update(['root/x/y/d']);
147
+ expect(attention.get('root/z/y/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
148
+ expect(attention.get('root/a/b/c')).to.deep.equal({ hasAttention: false, isAncestor: false, isRelated: false });
149
+ });
150
+ });
151
+
152
+ describe('expandAttendableId', () => {
153
+ test('produces progressive prefixes', ({ expect }) => {
154
+ expect(expandAttendableId('root/space/types/doc')).to.deep.equal([
155
+ 'root',
156
+ 'root/space',
157
+ 'root/space/types',
158
+ 'root/space/types/doc',
159
+ ]);
160
+ });
161
+
162
+ test('single segment returns itself', ({ expect }) => {
163
+ expect(expandAttendableId('root')).to.deep.equal(['root']);
164
+ });
165
+ });
166
+
167
+ describe('isLinkedSegment', () => {
168
+ test('detects linked prefix on last segment', ({ expect }) => {
169
+ expect(isLinkedSegment('root/space/obj/~settings')).toBe(true);
170
+ expect(isLinkedSegment('root/space/obj')).toBe(false);
171
+ expect(isLinkedSegment('~standalone')).toBe(true);
172
+ });
173
+ });
174
+
175
+ describe('linkedSegment', () => {
176
+ test('builds a linked segment', ({ expect }) => {
177
+ expect(linkedSegment('settings')).toBe('~settings');
178
+ expect(linkedSegment('comments')).toBe('~comments');
179
+ });
180
+ });
181
+
182
+ describe('getLinkedVariant', () => {
183
+ test('extracts variant from linked last segment', ({ expect }) => {
184
+ expect(getLinkedVariant('root/space/obj/~settings')).toBe('settings');
185
+ expect(getLinkedVariant('root/a/~comments')).toBe('comments');
186
+ });
187
+
188
+ test('returns segment as-is when not linked', ({ expect }) => {
189
+ expect(getLinkedVariant('root/space/obj')).toBe('obj');
190
+ expect(getLinkedVariant('root')).toBe('root');
191
+ });
192
+ });
193
+
194
+ describe('getParentId', () => {
195
+ test('returns parent qualified id', ({ expect }) => {
196
+ expect(getParentId('root/space/obj/~settings')).toBe('root/space/obj');
197
+ expect(getParentId('root/space')).toBe('root');
198
+ expect(getParentId('root')).toBeUndefined();
199
+ });
200
+ });
43
201
 
44
- attention.update(['a']);
45
- expect(attention.get(['a'])).to.deep.equal({ hasAttention: true, isAncestor: false, isRelated: false });
202
+ describe('getSegmentId', () => {
203
+ test('returns the last path segment', ({ expect }) => {
204
+ expect(getSegmentId('root/space/obj')).toBe('obj');
205
+ expect(getSegmentId('root')).toBe('root');
206
+ expect(getSegmentId('root/a/~settings')).toBe('~settings');
46
207
  });
47
208
  });
package/src/attention.ts CHANGED
@@ -2,120 +2,166 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import { untracked } from '@preact/signals-core';
6
- import * as Schema from 'effect/Schema';
5
+ import { Atom, type Registry } from '@effect-atom/atom-react';
7
6
 
8
- import { live } from '@dxos/echo/internal';
9
- import { type Live } from '@dxos/live-object';
10
- import { ComplexMap } from '@dxos/util';
11
-
12
- // NOTE: Chosen from RFC 1738’s `safe` characters: http://www.faqs.org/rfcs/rfc1738.html
13
- export const ATTENDABLE_PATH_SEPARATOR = '~';
14
-
15
- export const AttentionSchema = Schema.mutable(
16
- Schema.Struct({
17
- hasAttention: Schema.Boolean,
18
- isAncestor: Schema.Boolean,
19
- isRelated: Schema.Boolean,
20
- }),
21
- );
22
-
23
- export type Attention = Schema.Schema.Type<typeof AttentionSchema>;
24
-
25
- // TODO(wittjosiah): Use mosaic path utility?
26
- const stringKey = (key: string[]) => key.join(',');
7
+ export type Attention = {
8
+ hasAttention: boolean;
9
+ isAncestor: boolean;
10
+ isRelated: boolean;
11
+ };
27
12
 
28
13
  /**
29
14
  * Manages attention state for an application.
15
+ * Attention keys are slash-qualified graph IDs; ancestry is derived from progressive prefixes.
30
16
  */
31
- // TODO(wittjosiah): Write unit tests.
32
17
  export class AttentionManager {
33
- // Each attention path is associated with an attention object.
34
- // The lookup is not a reactive object to ensure that attention for each path is subscribable independently.
35
- private readonly _map = new ComplexMap<string[], Live<Attention>>(stringKey);
36
- private readonly _state = live<{ current: string[] }>({ current: [] });
37
-
38
- constructor(initial: string[] = []) {
18
+ private readonly _map = new Map<string, Atom.Writable<Attention>>();
19
+ private readonly _currentAtom: Atom.Writable<string[]>;
20
+
21
+ constructor(
22
+ private readonly _registry: Registry.Registry,
23
+ initial: string[] = [],
24
+ ) {
25
+ this._currentAtom = Atom.make<string[]>([]).pipe(Atom.keepAlive);
39
26
  if (initial.length > 0) {
40
27
  this.update(initial);
41
28
  }
42
29
  }
43
30
 
44
31
  /**
45
- * Currently attended element.
46
- *
47
- * @reactive
32
+ * Atom for the currently attended element IDs.
33
+ */
34
+ get current(): Atom.Atom<string[]> {
35
+ return this._currentAtom;
36
+ }
37
+
38
+ /**
39
+ * Gets the currently attended element IDs.
40
+ */
41
+ getCurrent(): readonly string[] {
42
+ return this._registry.get(this._currentAtom);
43
+ }
44
+
45
+ /**
46
+ * Subscribe to changes in the current attention IDs.
48
47
  */
49
- get current(): Live<readonly string[]> {
50
- return this._state.current;
48
+ subscribeCurrent(cb: (current: readonly string[]) => void): () => void {
49
+ this._registry.get(this._currentAtom);
50
+ return this._registry.subscribe(this._currentAtom, () => {
51
+ cb(this._registry.get(this._currentAtom));
52
+ });
51
53
  }
52
54
 
53
55
  /**
54
- * All attention paths.
56
+ * All tracked qualified IDs.
55
57
  */
56
- keys(): string[][] {
58
+ keys(): string[] {
57
59
  return Array.from(this._map.keys());
58
60
  }
59
61
 
60
62
  /**
61
- * Get the attention state for a given path.
63
+ * Get the attention state for a given qualified ID.
64
+ */
65
+ get(id: string): Attention {
66
+ const atom = this._getAtom(id);
67
+ return this._registry.get(atom);
68
+ }
69
+
70
+ /**
71
+ * Subscribe to changes in the attention state for a given qualified ID.
62
72
  */
63
- get(key: string[]): Live<Attention> {
64
- const object = this._map.get(key);
65
- if (object) {
66
- return object;
73
+ subscribe(id: string, cb: (attention: Attention) => void): () => void {
74
+ const atom = this._getAtom(id);
75
+ this._registry.get(atom);
76
+ return this._registry.subscribe(atom, () => {
77
+ cb(this._registry.get(atom));
78
+ });
79
+ }
80
+
81
+ private _getAtom(id: string): Atom.Writable<Attention> {
82
+ const existing = this._map.get(id);
83
+ if (existing) {
84
+ return existing;
67
85
  }
68
86
 
69
- const newObject = live(AttentionSchema, { hasAttention: false, isAncestor: false, isRelated: false });
70
- this._map.set(key, newObject);
71
- return newObject;
87
+ const newAtom = Atom.make<Attention>({ hasAttention: false, isAncestor: false, isRelated: false }).pipe(
88
+ Atom.keepAlive,
89
+ );
90
+ this._map.set(id, newAtom);
91
+ return newAtom;
72
92
  }
73
93
 
74
94
  /**
75
95
  * Update the currently attended element.
96
+ * Takes the array of qualified IDs collected from the DOM; the first element is the primary attended item.
97
+ * Ancestry is derived from the progressive prefixes of the primary ID.
98
+ * Relatedness is derived from the segment ID: any tracked key whose last `/` segment matches the
99
+ * attended ID's segment ID is marked `isRelated`. Additionally, if the primary ID is a linked segment
100
+ * (starts with `~`), its immediate parent gets `isRelated` alongside `isAncestor`.
76
101
  *
77
102
  * @internal
78
103
  */
79
- update(nextKey: string[]): void {
80
- const currentKey = untracked(() => this.current);
81
- const currentRelatedTo = currentKey[0];
82
- const nextRelatedTo = nextKey[0];
104
+ update(nextIds: string[]): void {
105
+ const primaryId = nextIds[0];
106
+ if (!primaryId) {
107
+ return;
108
+ }
83
109
 
84
- // Ensure related attention object is initialized.
85
- this.get([nextRelatedTo]);
110
+ const currentIds = this.getCurrent();
111
+ const prevPrimaryId = currentIds[0];
112
+ // Clear previous attention state: ancestors, primary, and related keys.
113
+ if (prevPrimaryId) {
114
+ const prevPrefixes = expandAttendableId(prevPrimaryId);
115
+ for (const prefix of prevPrefixes) {
116
+ this._set(prefix, {});
117
+ }
118
+ const prevSegmentId = getSegmentId(prevPrimaryId);
119
+ for (const key of this.keys()) {
120
+ if (getSegmentId(key) === prevSegmentId) {
121
+ this._set(key, {});
122
+ }
123
+ }
124
+ }
86
125
 
87
- const currentRelatedKeys = this.keys().filter((key) => key[0] === currentRelatedTo);
88
- const nextRelatedKeys = this.keys().filter(
89
- (key) => key[0] === nextRelatedTo && stringKey(key) !== stringKey(nextKey),
90
- );
91
- const prevRelatedKeys = currentRelatedKeys.filter(
92
- (curr) => !nextRelatedKeys.find((next) => stringKey(curr) === stringKey(next)),
93
- );
126
+ this._registry.set(this._currentAtom, nextIds);
94
127
 
95
- const currentAncestorKeys = currentKey.slice(1).map((_, i) => currentKey.slice(i + 1, currentKey.length));
96
- const nextAncestorKeys = nextKey.slice(1).map((_, i) => nextKey.slice(i + 1, nextKey.length));
97
- const prevAncestorKeys = currentAncestorKeys.filter(
98
- (curr) => !nextAncestorKeys.find((next) => stringKey(curr) === stringKey(next)),
99
- );
128
+ // Set ancestors and primary.
129
+ const prefixes = expandAttendableId(primaryId);
130
+ const prefixSet = new Set(prefixes);
131
+ const linkedParent = isLinkedSegment(primaryId) ? getParentId(primaryId) : undefined;
100
132
 
101
- this._state.current = nextKey;
102
- prevRelatedKeys.forEach((key) => this._set(key, {}));
103
- prevAncestorKeys.forEach((key) => this._set(key, {}));
104
- nextRelatedKeys.forEach((key) => this._set(key, { isRelated: true }));
105
- nextAncestorKeys.forEach((key) => this._set(key, { isAncestor: true }));
106
- this._set(nextKey, { hasAttention: true });
133
+ for (const prefix of prefixes) {
134
+ if (prefix === primaryId) {
135
+ this._set(prefix, { hasAttention: true });
136
+ } else if (prefix === linkedParent) {
137
+ this._set(prefix, { isAncestor: true, isRelated: true });
138
+ } else {
139
+ this._set(prefix, { isAncestor: true });
140
+ }
141
+ }
142
+
143
+ // Set related keys: any tracked key sharing the same segment ID.
144
+ const segmentId = getSegmentId(primaryId);
145
+ for (const key of this.keys()) {
146
+ if (!prefixSet.has(key) && getSegmentId(key) === segmentId) {
147
+ this._set(key, { isRelated: true });
148
+ }
149
+ }
107
150
  }
108
151
 
109
- private _set(key: string[], attention: Partial<Attention>): void {
110
- const object = this.get(key);
111
- object.hasAttention = attention.hasAttention ?? false;
112
- object.isAncestor = attention.isAncestor ?? false;
113
- object.isRelated = attention.isRelated ?? false;
152
+ private _set(id: string, attention: Partial<Attention>): void {
153
+ const atom = this._getAtom(id);
154
+ this._registry.set(atom, {
155
+ hasAttention: attention.hasAttention ?? false,
156
+ isAncestor: attention.isAncestor ?? false,
157
+ isRelated: attention.isRelated ?? false,
158
+ });
114
159
  }
115
160
  }
116
161
 
117
162
  /**
118
- * Accumulates all attendable id’s between the element provided and the root, inclusive.
163
+ * Accumulates all attendable IDs between the element provided and the root, inclusive.
164
+ * Each `data-attendable-id` value is treated as a single qualified ID (no splitting).
119
165
  */
120
166
  export const getAttendables = (selector: string, cursor: Element, acc: string[] = []): string[] => {
121
167
  // Find the closest element with `data-attendable-id`, if any; start from cursor and move up the DOM tree.
@@ -131,8 +177,7 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
131
177
  return getAttendables(selector, trigger, acc);
132
178
  }
133
179
  } else {
134
- acc.push(...attendableId.split(ATTENDABLE_PATH_SEPARATOR));
135
- // TODO: Attempt tail recursion.
180
+ acc.push(attendableId);
136
181
  return !closestAttendable.parentElement ? acc : getAttendables(selector, closestAttendable.parentElement, acc);
137
182
  }
138
183
  }
@@ -143,3 +188,62 @@ export const getAttendables = (selector: string, cursor: Element, acc: string[]
143
188
  export type AttendableId = { attendableId?: string };
144
189
 
145
190
  export type Related = { related?: boolean };
191
+
192
+ /**
193
+ * Prefix for linked segments. A linked segment's attention state is shared with its parent:
194
+ * when a linked node has attention, the parent is marked as both ancestor and related.
195
+ */
196
+ const LINKED_PREFIX = '~';
197
+
198
+ /**
199
+ * Decompose a qualified graph ID into progressive prefixes for ancestry tracking.
200
+ * e.g. `root/a/b/c` yields `['root', 'root/a', 'root/a/b', 'root/a/b/c']`.
201
+ */
202
+ export const expandAttendableId = (qualifiedId: string): string[] => {
203
+ const segments = qualifiedId.split('/');
204
+ return segments.map((_, index) => segments.slice(0, index + 1).join('/'));
205
+ };
206
+
207
+ /**
208
+ * Whether the last segment of a qualified ID is a linked segment (starts with `~`).
209
+ * Linked segments share their attention state with the parent node.
210
+ */
211
+ export const isLinkedSegment = (qualifiedId: string): boolean => {
212
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
213
+ return lastSegment.startsWith(LINKED_PREFIX);
214
+ };
215
+
216
+ /**
217
+ * Build a linked segment ID from a variant name (e.g., `'settings'` -> `'~settings'`).
218
+ * Nodes identified by linked segments share their attention state with the parent node.
219
+ */
220
+ export const linkedSegment = (variant: string): string => `${LINKED_PREFIX}${variant}`;
221
+
222
+ /**
223
+ * Extract the variant name from the last segment of a qualified ID, stripping the linked prefix.
224
+ * e.g. `'root/space/obj/~settings'` -> `'settings'`.
225
+ * If the segment is not linked (no `~` prefix), returns the segment as-is.
226
+ */
227
+ export const getLinkedVariant = (qualifiedId: string): string => {
228
+ const lastSegment = qualifiedId.split('/').pop() ?? '';
229
+ return lastSegment.startsWith(LINKED_PREFIX) ? lastSegment.slice(LINKED_PREFIX.length) : lastSegment;
230
+ };
231
+
232
+ /**
233
+ * Get the parent qualified ID (everything before the last `/` segment).
234
+ */
235
+ export const getParentId = (qualifiedId: string | undefined): string | undefined => {
236
+ if (!qualifiedId) {
237
+ return undefined;
238
+ }
239
+
240
+ const lastSlash = qualifiedId.lastIndexOf('/');
241
+ return lastSlash > 0 ? qualifiedId.slice(0, lastSlash) : undefined;
242
+ };
243
+
244
+ /**
245
+ * Extract the segment ID (last segment) of a qualified ID.
246
+ */
247
+ export const getSegmentId = (qualifiedId: string): string => {
248
+ return qualifiedId.split('/').pop() ?? qualifiedId;
249
+ };
@@ -13,7 +13,7 @@ import { AttentionGlyph, type AttentionGlyphProps } from './AttentionGlyph';
13
13
 
14
14
  const DefaultStory = (props: AttentionGlyphProps) => {
15
15
  return (
16
- <ul className='flex gap-2 mbe-2'>
16
+ <ul className='flex gap-2 mb-2'>
17
17
  <li>
18
18
  <AttentionGlyph presence='none' {...props} />
19
19
  </li>
@@ -31,7 +31,7 @@ const meta = {
31
31
  title: 'ui/react-ui-attention/AttentionGlyph',
32
32
  component: AttentionGlyph as any,
33
33
  render: DefaultStory,
34
- decorators: [withTheme],
34
+ decorators: [withTheme()],
35
35
  } satisfies Meta<typeof DefaultStory>;
36
36
 
37
37
  export default meta;