moi-computer 0.13.0 → 0.13.1-next.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.
Files changed (51) hide show
  1. package/client/api/query-client.ts +5 -0
  2. package/client/app/app-router.tsx +1 -1
  3. package/client/app/routes/workspace-route.tsx +2 -0
  4. package/client/features/account/account-provider.tsx +28 -77
  5. package/client/features/account/account-test-utils.tsx +95 -93
  6. package/client/features/account/api.ts +32 -109
  7. package/client/features/account/profile-actions.ts +31 -0
  8. package/client/features/app-settings/settings-page.tsx +0 -4
  9. package/client/features/collab/client.ts +38 -13
  10. package/client/features/collab/hooks.tsx +9 -8
  11. package/client/features/collab/host-state.ts +45 -85
  12. package/client/features/collab/testing/fake-engine.ts +13 -1
  13. package/client/features/collab/users.ts +0 -12
  14. package/client/features/dev/cloud-avatar-upload.tsx +58 -0
  15. package/client/features/dev/cloud-page.tsx +363 -0
  16. package/client/features/dev/dev-index-page.tsx +6 -0
  17. package/client/features/dev/dev-routes.tsx +2 -0
  18. package/client/features/identity/host-api.ts +34 -0
  19. package/client/features/identity/store.ts +32 -0
  20. package/client/features/identity/types.ts +41 -0
  21. package/client/features/identity/use-identity.ts +6 -0
  22. package/client/features/workspace-members/api.ts +20 -0
  23. package/client/features/workspace-members/hooks.ts +127 -0
  24. package/client/features/workspace-members/keys.ts +6 -0
  25. package/client/features/workspace-members/store.ts +123 -0
  26. package/client/features/workspace-members/types.ts +53 -0
  27. package/client/index.html +1 -1
  28. package/client/index.tsx +5 -9
  29. package/client/main.tsx +2 -2
  30. package/dist/{chunk-cqt3s31j.js → chunk-00s35r33.js} +68 -68
  31. package/dist/{chunk-bncc7heg.js → chunk-7rz6g7g7.js} +1 -1
  32. package/dist/chunk-af2jyddf.js +2 -0
  33. package/dist/chunk-cekekc3f.css +1 -0
  34. package/dist/chunk-ep5vh16c.js +75 -0
  35. package/dist/{chunk-2y59vrxg.js → chunk-kdgk4dac.js} +1 -1
  36. package/dist/chunk-s5s1vzdw.js +2 -0
  37. package/dist/{chunk-58dyt9bk.js → chunk-svxhxfeq.js} +2 -2
  38. package/dist/chunk-t0r0bds1.js +1 -0
  39. package/dist/chunk-vp2qzc0d.js +1 -0
  40. package/dist/{chunk-q37knr3m.js → chunk-wz5qv1bn.js} +2 -2
  41. package/dist/index.html +2 -2
  42. package/package.json +5 -4
  43. package/server/app-config.ts +3 -1
  44. package/client/features/account/identity.ts +0 -23
  45. package/client/features/app-settings/users-settings.tsx +0 -195
  46. package/dist/chunk-dcmks9sm.js +0 -75
  47. package/dist/chunk-md45evr2.css +0 -1
  48. package/dist/chunk-n0nab26d.js +0 -1
  49. package/dist/chunk-nmzhgyp9.js +0 -2
  50. package/dist/chunk-s90m4qkz.js +0 -1
  51. package/dist/chunk-wjvd4byz.js +0 -2
@@ -0,0 +1,123 @@
1
+ import { memberKeys } from './keys'
2
+ import { queryClient } from '@/client/api/query-client'
3
+ import { getIdentity, getAccountGeneration } from '../identity/store'
4
+ import type { Identity } from '../identity/types'
5
+ import { collabIdentityId, identityKey } from '../identity/types'
6
+ import { normalizeUserProfile } from '../collab/host-state'
7
+ import type { UserProfile } from '@/lib/collab/types'
8
+ import { parseWorkspaceMembers, validWorkspaceId } from './types'
9
+ import type { WorkspaceMembers, WorkspaceMembersSnapshot } from './types'
10
+
11
+ // Host-published memberships remain authoritative until replaced or cleared,
12
+ // including for workspaces that have no mounted React observer yet.
13
+ queryClient.setQueryDefaults(memberKeys.all, { gcTime: Infinity })
14
+
15
+ // Revisions track membership writes so older cloud snapshots cannot replace them.
16
+ let membersRevision = 0
17
+ let membersLoadedForAccountGeneration: number | undefined
18
+ const collectionListeners = new Set<() => void>()
19
+ const ownerSnapshots = new WeakMap<Identity, WorkspaceMembers>()
20
+ queryClient.getQueryCache().subscribe(event => {
21
+ if (event.query.queryKey[0] !== memberKeys.all[0]) return
22
+ if (event.type === 'removed' || (event.type === 'updated' && event.action.type === 'success')) {
23
+ membersRevision++
24
+ }
25
+ })
26
+ export const getWorkspaceMembersRevision = () => membersRevision
27
+ const directoryCache = new WeakMap<WorkspaceMembers, readonly UserProfile[]>()
28
+ // Cloud routing currently opens the owner's machine. No sharing record means
29
+ // owner-only; Moi's own workspace registry decides whether that workspace exists.
30
+ export function ownerOnlyWorkspaceMembers(): WorkspaceMembers | undefined {
31
+ const owner = getIdentity()
32
+ if (
33
+ membersLoadedForAccountGeneration !== getAccountGeneration() ||
34
+ owner?.type !== 'user' ||
35
+ owner.isAnonymous
36
+ )
37
+ return undefined
38
+ let snapshot = ownerSnapshots.get(owner)
39
+ if (!snapshot) {
40
+ snapshot = { members: [owner], invitations: [] }
41
+ ownerSnapshots.set(owner, snapshot)
42
+ }
43
+ return snapshot
44
+ }
45
+ export function setWorkspaceMembers(workspaceId: string, value: WorkspaceMembers): void {
46
+ const identity = getIdentity()
47
+ const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
48
+ validWorkspaceId(workspaceId)
49
+ const data = parseWorkspaceMembers(value)
50
+ // A push supersedes any older in-flight GET for this snapshot.
51
+ void queryClient.cancelQueries({ queryKey, exact: true })
52
+ queryClient.setQueryData(queryKey, data)
53
+ }
54
+ export function setAllWorkspaceMembers(value: WorkspaceMembersSnapshot[]): void {
55
+ if (!Array.isArray(value)) throw new Error('Invalid workspace members')
56
+ const snapshots = value.map(workspace => ({
57
+ id: validWorkspaceId(workspace.id),
58
+ data: parseWorkspaceMembers(workspace)
59
+ }))
60
+ const ids = new Set(snapshots.map(workspace => workspace.id))
61
+ if (ids.size !== snapshots.length) throw new Error('Duplicate workspace id')
62
+ membersLoadedForAccountGeneration = getAccountGeneration()
63
+ const identity = getIdentity()
64
+ const accountId = identity ? identityKey(identity) : undefined
65
+ const prefix = memberKeys.account(accountId)
66
+ void queryClient.cancelQueries({ queryKey: prefix })
67
+ for (const query of queryClient.getQueryCache().findAll({ queryKey: prefix })) {
68
+ if (!ids.has(query.queryKey[2] as string)) forgetWorkspaceMembers(query.queryKey)
69
+ }
70
+ for (const workspace of snapshots)
71
+ queryClient.setQueryData(memberKeys.workspace(accountId, workspace.id), workspace.data)
72
+ membersRevision++
73
+ collectionListeners.forEach(listener => listener())
74
+ }
75
+ export function removeWorkspaceMembers(workspaceId: string) {
76
+ const identity = getIdentity()
77
+ const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
78
+ validWorkspaceId(workspaceId)
79
+ void queryClient.cancelQueries({ queryKey, exact: true })
80
+ forgetWorkspaceMembers(queryKey)
81
+ }
82
+ export function clearWorkspaceMembers() {
83
+ void queryClient.cancelQueries({ queryKey: memberKeys.all })
84
+ for (const query of queryClient.getQueryCache().findAll({ queryKey: memberKeys.all }))
85
+ forgetWorkspaceMembers(query.queryKey)
86
+ }
87
+ function forgetWorkspaceMembers(queryKey: readonly unknown[]) {
88
+ // Removing an observed query leaves its current UI snapshot intact. Publish
89
+ // the replacement so mounted views and the presence engine update together.
90
+ queryClient.setQueryData<WorkspaceMembers>(
91
+ queryKey,
92
+ ownerOnlyWorkspaceMembers() ?? { members: [], invitations: [] }
93
+ )
94
+ }
95
+ export function getWorkspaceDirectory(workspaceId: string): readonly UserProfile[] | undefined {
96
+ const identity = getIdentity()
97
+ const queryKey = memberKeys.workspace(identity ? identityKey(identity) : undefined, workspaceId)
98
+ const data = queryClient.getQueryData<WorkspaceMembers>(queryKey) ?? ownerOnlyWorkspaceMembers()
99
+ if (!data) return undefined
100
+ let profiles = directoryCache.get(data)
101
+ if (!profiles) {
102
+ profiles = data.members.map(member =>
103
+ normalizeUserProfile({
104
+ id: collabIdentityId(member),
105
+ name: member.name || undefined,
106
+ avatar: member.avatar,
107
+ email: member.type === 'user' ? member.email || undefined : undefined
108
+ })
109
+ )
110
+ directoryCache.set(data, profiles)
111
+ }
112
+ return profiles
113
+ }
114
+ export function subscribeWorkspaceDirectory(listener: () => void) {
115
+ collectionListeners.add(listener)
116
+ const unsubscribe = queryClient.getQueryCache().subscribe(event => {
117
+ if (event.query.queryKey[0] === memberKeys.all[0]) listener()
118
+ })
119
+ return () => {
120
+ collectionListeners.delete(listener)
121
+ unsubscribe()
122
+ }
123
+ }
@@ -0,0 +1,53 @@
1
+ import { isRecord } from '@/lib/collab/protocol'
2
+ import { identityKey, parseIdentity } from '../identity/types'
3
+ import type { Identity } from '../identity/types'
4
+
5
+ export type WorkspaceMember = Identity & { membershipId?: string }
6
+ export type WorkspaceInvitation = {
7
+ id: string
8
+ email: string
9
+ expiresAt: string
10
+ pending?: boolean
11
+ }
12
+ export type WorkspaceMembers = { members: WorkspaceMember[]; invitations: WorkspaceInvitation[] }
13
+ export type WorkspaceMembersSnapshot = WorkspaceMembers & { id: string }
14
+ export function parseWorkspaceMembers(value: unknown): WorkspaceMembers {
15
+ if (!isRecord(value) || !Array.isArray(value.members) || !Array.isArray(value.invitations))
16
+ throw new Error('Invalid workspace members')
17
+ const members = value.members.map(member => {
18
+ const identity = parseIdentity(member)
19
+ if (isRecord(member) && member.membershipId !== undefined) {
20
+ if (
21
+ typeof member.membershipId !== 'string' ||
22
+ !member.membershipId ||
23
+ member.membershipId.length > 240
24
+ )
25
+ throw new Error('Invalid membership id')
26
+ return { ...identity, membershipId: member.membershipId }
27
+ }
28
+ return identity
29
+ })
30
+ const invitations = value.invitations.map(invitation => {
31
+ if (
32
+ !isRecord(invitation) ||
33
+ typeof invitation.id !== 'string' ||
34
+ !invitation.id ||
35
+ typeof invitation.email !== 'string' ||
36
+ !invitation.email ||
37
+ typeof invitation.expiresAt !== 'string' ||
38
+ !Number.isFinite(Date.parse(invitation.expiresAt))
39
+ )
40
+ throw new Error('Invalid invitation')
41
+ return { id: invitation.id, email: invitation.email, expiresAt: invitation.expiresAt }
42
+ })
43
+ if (
44
+ new Set(members.map(identityKey)).size !== members.length ||
45
+ new Set(invitations.map(invitation => invitation.id)).size !== invitations.length
46
+ )
47
+ throw new Error('Duplicate workspace member or invitation')
48
+ return { members, invitations }
49
+ }
50
+ export function validWorkspaceId(id: string): string {
51
+ if (!/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(id)) throw new Error('Invalid workspace id')
52
+ return id
53
+ }
package/client/index.html CHANGED
@@ -59,7 +59,7 @@
59
59
  <body>
60
60
  <div id="root"></div>
61
61
  <script type="module" src="./index.tsx"></script>
62
- <script type="module">
62
+ <script id="moi-start" type="module">
63
63
  const [react, jsxRuntime, jsxDevRuntime, reactDom, reactDomClient] = await Promise.all([
64
64
  import('react'),
65
65
  import('react/jsx-runtime'),
package/client/index.tsx CHANGED
@@ -4,11 +4,9 @@ import './structured-clone-shim'
4
4
  import 'blobatar/motion.css'
5
5
  import './index.css'
6
6
 
7
- import {
8
- initializeLocalUser,
9
- installHostApi,
10
- subscribeLocalProfileStorage
11
- } from './features/collab/host-state'
7
+ import { initializeLocalUser, subscribeLocalProfileStorage } from './features/collab/host-state'
8
+
9
+ import { installIdentityHostApi } from './features/identity/host-api'
12
10
 
13
11
  import { startHmrWatchdog } from './runtime/hmr-watchdog'
14
12
 
@@ -27,10 +25,8 @@ if (import.meta.hot) {
27
25
  startHmrWatchdog()
28
26
  }
29
27
 
30
- // Installed while this module evaluates, not inside `init`: an outer host's
31
- // script runs right after this bundle and calls `window.moi.collab.publishUsers`
32
- // straight away, before the async startup below has finished.
33
- installHostApi()
28
+ // A host can call this API after the entry chunk and before #moi-start mounts React.
29
+ installIdentityHostApi()
34
30
  subscribeLocalProfileStorage()
35
31
 
36
32
  export async function init(el: HTMLElement) {
package/client/main.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
1
+ import { QueryClientProvider } from '@tanstack/react-query'
2
2
  import { createRoot } from 'react-dom/client'
3
3
  import { Router } from 'wouter'
4
4
 
@@ -10,7 +10,7 @@ import { AccountProvider } from '@/client/features/account/account-provider'
10
10
  import { AppProvider } from './app/app-provider'
11
11
  import { AppRouter } from './app/app-router'
12
12
 
13
- const queryClient = new QueryClient()
13
+ import { queryClient } from './api/query-client'
14
14
 
15
15
  // Open the single app-wide chat WebSocket once and hand it the query client so
16
16
  // live frames fold into the RQ transcript cache. Lives for the page's lifetime.