moi-computer 0.12.1-next.2 → 0.12.1-next.3
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/client/app/app-router.tsx +2 -0
- package/client/app/routes/settings-route.tsx +10 -0
- package/client/app/shell/sidebar-layout.tsx +34 -53
- package/client/features/account/account-menu.tsx +96 -0
- package/client/features/account/account-provider.tsx +142 -0
- package/client/features/account/account-test-utils.tsx +293 -0
- package/client/features/account/api.ts +142 -0
- package/client/features/account/identity.ts +23 -0
- package/client/features/collab/applet-api.ts +15 -14
- package/client/features/collab/components/cursors.tsx +61 -24
- package/client/features/collab/components/presence-avatars.tsx +25 -12
- package/client/features/collab/components/presence-frame.tsx +20 -24
- package/client/features/collab/components/presence-group.tsx +15 -7
- package/client/features/collab/components/user-avatar-group.tsx +21 -8
- package/client/features/collab/components/user-avatar.tsx +38 -7
- package/client/features/collab/components/user.tsx +31 -12
- package/client/features/collab/users.ts +0 -5
- package/client/features/collab/workspace-users-menu.tsx +3 -3
- package/client/features/dev/collab/collab-playground.tsx +7 -7
- package/client/features/dev/collab/local-user-settings.tsx +7 -53
- package/client/features/settings/connectors-settings.tsx +3 -3
- package/client/features/settings/environment-settings.tsx +3 -3
- package/client/features/settings/general-settings.tsx +1 -1
- package/client/features/settings/profile-settings.tsx +95 -0
- package/client/features/settings/settings-page.tsx +44 -0
- package/client/features/settings/settings-section.tsx +26 -0
- package/client/features/settings/users-settings.tsx +195 -0
- package/client/features/update/update-control.tsx +1 -1
- package/client/features/workspace/add/create-workspace-dialog.tsx +5 -2
- package/client/lib/user-display-name.ts +6 -0
- package/client/main.tsx +4 -1
- package/dist/{chunk-vt6zybej.js → chunk-20ynagd4.js} +2 -2
- package/dist/chunk-42tde9a6.js +1 -0
- package/dist/{chunk-hmka9rt5.js → chunk-9w543s0r.js} +2 -2
- package/dist/{chunk-y8nbwp21.js → chunk-a5372n61.js} +2 -2
- package/dist/chunk-aja9y1vn.js +68 -0
- package/dist/chunk-fvcvrh3d.css +1 -0
- package/dist/{chunk-ysfdcew7.js → chunk-gtfjw3nx.js} +1 -1
- package/dist/{chunk-hvh9f5h0.js → chunk-j39y2aky.js} +1 -1
- package/dist/chunk-pja30km3.js +1 -0
- package/dist/{chunk-d7cbm65e.js → chunk-rm894bc2.js} +1 -1
- package/dist/chunk-tevvtfbk.js +10 -0
- package/dist/{chunk-am4agx6v.js → chunk-tgt9zd0r.js} +1 -1
- package/dist/chunk-tv5k5z00.js +1 -0
- package/dist/chunk-zq2ae5dx.js +2 -0
- package/dist/index.html +1 -1
- package/lib/types.ts +1 -1
- package/package.json +1 -1
- package/server/api/index.ts +2 -6
- package/server/app-config.ts +7 -0
- package/server/web.ts +1 -0
- package/server/workspaces/init.ts +0 -10
- package/server/workspaces/registry.ts +31 -9
- package/dist/chunk-00rh9df4.js +0 -2
- package/dist/chunk-4nh7y1hq.css +0 -1
- package/dist/chunk-hbpdv7vd.js +0 -1
- package/dist/chunk-mjjregkn.js +0 -10
- package/dist/chunk-rt30jbcm.js +0 -1
- package/dist/chunk-wbkwf6cc.js +0 -68
- package/dist/chunk-xjyfyz64.js +0 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { queryOptions } from '@tanstack/react-query'
|
|
2
|
+
import { jsonRequest } from '@/client/api/http'
|
|
3
|
+
import { isRecord, isUserProfile } from '@/lib/collab/protocol'
|
|
4
|
+
|
|
5
|
+
export type CloudSession = {
|
|
6
|
+
user: { id: string; email: string; name: string; isAnonymous: boolean }
|
|
7
|
+
} | null
|
|
8
|
+
export type TeamMember = {
|
|
9
|
+
id: string
|
|
10
|
+
userId: string
|
|
11
|
+
email: string
|
|
12
|
+
name: string
|
|
13
|
+
role: string
|
|
14
|
+
me: boolean
|
|
15
|
+
}
|
|
16
|
+
export type TeamInvitation = { id: string; email: string; role: string; expiresAt: string }
|
|
17
|
+
export type CloudTeam = {
|
|
18
|
+
id: string
|
|
19
|
+
name: string
|
|
20
|
+
role: string
|
|
21
|
+
members: TeamMember[]
|
|
22
|
+
invitations: TeamInvitation[]
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const accountKeys = {
|
|
26
|
+
all: ['account'] as const,
|
|
27
|
+
session: ['account', 'session'] as const,
|
|
28
|
+
team: ['account', 'team'] as const
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export class CloudApiError extends Error {
|
|
32
|
+
constructor(
|
|
33
|
+
message: string,
|
|
34
|
+
readonly status: number
|
|
35
|
+
) {
|
|
36
|
+
super(message)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export async function cloudRequest(path: string, init?: RequestInit): Promise<Response> {
|
|
41
|
+
const response = await fetch(`/cloud/api/auth/${path}`, { ...init, credentials: 'same-origin' })
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const body: unknown = await response.json().catch(() => undefined)
|
|
44
|
+
const message = isRecord(body)
|
|
45
|
+
? [body.error, body.message].find(
|
|
46
|
+
(value): value is string => typeof value === 'string' && value.trim().length > 0
|
|
47
|
+
)
|
|
48
|
+
: undefined
|
|
49
|
+
throw new CloudApiError(
|
|
50
|
+
message ?? 'Couldn’t complete this request. Try again.',
|
|
51
|
+
response.status
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
return response
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function validProfile(id: unknown, name: unknown, email: unknown): boolean {
|
|
58
|
+
return (
|
|
59
|
+
typeof id === 'string' &&
|
|
60
|
+
id === id.trim() &&
|
|
61
|
+
typeof name === 'string' &&
|
|
62
|
+
typeof email === 'string' &&
|
|
63
|
+
isUserProfile({ id, name: name.trim() || undefined, email, color: 'blue' })
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function parseSession(value: unknown): CloudSession {
|
|
68
|
+
if (value === null) return null
|
|
69
|
+
if (
|
|
70
|
+
!isRecord(value) ||
|
|
71
|
+
!isRecord(value.user) ||
|
|
72
|
+
!validProfile(value.user.id, value.user.name, value.user.email) ||
|
|
73
|
+
typeof value.user.isAnonymous !== 'boolean'
|
|
74
|
+
)
|
|
75
|
+
throw new Error('Invalid account response')
|
|
76
|
+
return value as NonNullable<CloudSession>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function parseTeam(value: unknown): CloudTeam {
|
|
80
|
+
if (
|
|
81
|
+
!isRecord(value) ||
|
|
82
|
+
typeof value.id !== 'string' ||
|
|
83
|
+
typeof value.name !== 'string' ||
|
|
84
|
+
typeof value.role !== 'string' ||
|
|
85
|
+
!Array.isArray(value.members) ||
|
|
86
|
+
!Array.isArray(value.invitations) ||
|
|
87
|
+
!value.members.every(
|
|
88
|
+
member =>
|
|
89
|
+
isRecord(member) &&
|
|
90
|
+
typeof member.id === 'string' &&
|
|
91
|
+
validProfile(member.userId, member.name, member.email) &&
|
|
92
|
+
typeof member.role === 'string' &&
|
|
93
|
+
typeof member.me === 'boolean'
|
|
94
|
+
) ||
|
|
95
|
+
!value.invitations.every(
|
|
96
|
+
invitation =>
|
|
97
|
+
isRecord(invitation) &&
|
|
98
|
+
validProfile(invitation.id, '', invitation.email) &&
|
|
99
|
+
typeof invitation.role === 'string' &&
|
|
100
|
+
typeof invitation.expiresAt === 'string' &&
|
|
101
|
+
Number.isFinite(Date.parse(invitation.expiresAt))
|
|
102
|
+
)
|
|
103
|
+
)
|
|
104
|
+
throw new Error('Invalid users response')
|
|
105
|
+
const team = value as CloudTeam
|
|
106
|
+
if (
|
|
107
|
+
new Set(team.members.map(member => member.userId)).size !== team.members.length ||
|
|
108
|
+
new Set(team.members.map(member => member.id)).size !== team.members.length ||
|
|
109
|
+
new Set(team.invitations.map(invitation => invitation.id)).size !== team.invitations.length
|
|
110
|
+
)
|
|
111
|
+
throw new Error('Invalid users response')
|
|
112
|
+
return team
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const sessionQuery = queryOptions({
|
|
116
|
+
queryKey: accountKeys.session,
|
|
117
|
+
queryFn: async ({ signal }) => {
|
|
118
|
+
const response = await cloudRequest('get-session', { signal })
|
|
119
|
+
return parseSession(await response.json())
|
|
120
|
+
},
|
|
121
|
+
retry: false,
|
|
122
|
+
staleTime: 30_000
|
|
123
|
+
})
|
|
124
|
+
export const teamQuery = (userId?: string) =>
|
|
125
|
+
queryOptions({
|
|
126
|
+
queryKey: [...accountKeys.team, userId],
|
|
127
|
+
queryFn: async ({ signal }) => {
|
|
128
|
+
const response = await cloudRequest('organization', { signal })
|
|
129
|
+
return parseTeam(await response.json())
|
|
130
|
+
},
|
|
131
|
+
retry: false,
|
|
132
|
+
staleTime: 30_000
|
|
133
|
+
})
|
|
134
|
+
export const updateCloudName = (name: string) =>
|
|
135
|
+
cloudRequest('profile', jsonRequest('POST', { name }))
|
|
136
|
+
export const signOutCloud = () => cloudRequest('sign-out', jsonRequest('POST'))
|
|
137
|
+
export const inviteMember = (email: string) =>
|
|
138
|
+
cloudRequest('invite', jsonRequest('POST', { email }))
|
|
139
|
+
export const cancelInvitation = (id: string) =>
|
|
140
|
+
cloudRequest(`invitation/${encodeURIComponent(id)}/cancel`, jsonRequest('POST'))
|
|
141
|
+
export const removeMember = (memberId: string) =>
|
|
142
|
+
cloudRequest('members/remove', jsonRequest('POST', { memberId }))
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { getCurrentUser, getDirectory, publishUsers } from '@/client/features/collab/host-state'
|
|
2
|
+
import type { CloudSession, CloudTeam } from './api'
|
|
3
|
+
|
|
4
|
+
// The session identifies the viewer. Keep the injected directory until the team
|
|
5
|
+
// request resolves, and preserve known avatar and color information by user id.
|
|
6
|
+
export function syncCloudIdentity(session: CloudSession, team?: CloudTeam): void {
|
|
7
|
+
if (!session || session.user.isAnonymous) {
|
|
8
|
+
publishUsers({ users: [] })
|
|
9
|
+
return
|
|
10
|
+
}
|
|
11
|
+
const { id, name, email } = session.user
|
|
12
|
+
const sameAccount = getCurrentUser()?.id === id
|
|
13
|
+
const known = new Map((getDirectory() ?? []).map(user => [user.id, user]))
|
|
14
|
+
const users =
|
|
15
|
+
team?.members.map(user => ({
|
|
16
|
+
...known.get(user.userId),
|
|
17
|
+
id: user.userId,
|
|
18
|
+
name: user.name,
|
|
19
|
+
email: user.email
|
|
20
|
+
})) ?? (sameAccount ? [...known.values()] : [])
|
|
21
|
+
const profile = { ...known.get(id), id, name, email }
|
|
22
|
+
publishUsers({ me: id, users: [...users.filter(user => user.id !== id), profile] })
|
|
23
|
+
}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import type * as CollabApi from 'moi/collab'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { UserAvatarGroupContainer } from './components/user-avatar-group'
|
|
3
|
+
import { UserContainer } from './components/user'
|
|
4
|
+
import { UserAvatarContainer } from './components/user-avatar'
|
|
5
|
+
import { CursorsContainer } from './components/cursors'
|
|
6
|
+
import { PresenceFrameContainer } from './components/presence-frame'
|
|
7
|
+
import { PresenceGroupContainer } from './components/presence-group'
|
|
8
|
+
import { PresenceAvatarsContainer } from './components/presence-avatars'
|
|
9
9
|
import {
|
|
10
10
|
useMe,
|
|
11
11
|
usePeers,
|
|
@@ -17,14 +17,15 @@ import {
|
|
|
17
17
|
|
|
18
18
|
export type AppletCollabApi = typeof CollabApi
|
|
19
19
|
|
|
20
|
+
// SDK components resolve collaboration data; host UI can import the plain components.
|
|
20
21
|
export const appletCollabApi = {
|
|
21
|
-
Cursors,
|
|
22
|
-
User,
|
|
23
|
-
UserAvatar,
|
|
24
|
-
UserAvatarGroup,
|
|
25
|
-
PresenceFrame,
|
|
26
|
-
PresenceAvatars,
|
|
27
|
-
PresenceGroup,
|
|
22
|
+
Cursors: CursorsContainer,
|
|
23
|
+
User: UserContainer,
|
|
24
|
+
UserAvatar: UserAvatarContainer,
|
|
25
|
+
UserAvatarGroup: UserAvatarGroupContainer,
|
|
26
|
+
PresenceFrame: PresenceFrameContainer,
|
|
27
|
+
PresenceAvatars: PresenceAvatarsContainer,
|
|
28
|
+
PresenceGroup: PresenceGroupContainer,
|
|
28
29
|
useMe,
|
|
29
30
|
usePeers,
|
|
30
31
|
useUser,
|
|
@@ -1,17 +1,24 @@
|
|
|
1
|
-
import type { CursorsProps } from 'moi/collab'
|
|
1
|
+
import type { CursorsProps as CursorsContainerProps } from 'moi/collab'
|
|
2
|
+
import type { UserProfile } from '@/lib/collab/types'
|
|
2
3
|
import type { JsonValue } from 'moi'
|
|
3
4
|
import { useLayoutEffect, useRef } from 'react'
|
|
4
|
-
import type { PointerEvent, Ref, RefObject } from 'react'
|
|
5
|
+
import type { PointerEvent, ReactNode, Ref, RefObject } from 'react'
|
|
5
6
|
import { IconPointer2 } from '@tabler/icons-react'
|
|
6
7
|
|
|
7
8
|
import { cn } from '@/client/lib/cn'
|
|
8
9
|
import { Badge } from '@/ui-components/badge'
|
|
9
10
|
|
|
10
|
-
import { presenceChannels, usePresenceChannel, usePresencePublisher,
|
|
11
|
-
import { userDisplayName } from '
|
|
11
|
+
import { presenceChannels, usePresenceChannel, usePresencePublisher, useUsers } from '../hooks'
|
|
12
|
+
import { userDisplayName } from '@/client/lib/user-display-name'
|
|
12
13
|
import { hasPresence } from './presence-helpers'
|
|
13
14
|
|
|
14
|
-
type PointerPosition = {
|
|
15
|
+
export type PointerPosition = {
|
|
16
|
+
x: number
|
|
17
|
+
y: number
|
|
18
|
+
target?: string
|
|
19
|
+
targetX?: number
|
|
20
|
+
targetY?: number
|
|
21
|
+
}
|
|
15
22
|
function pointerPosition(value: JsonValue): PointerPosition | null {
|
|
16
23
|
if (!value || typeof value !== 'object' || Array.isArray(value)) return null
|
|
17
24
|
if (typeof value.x !== 'number' || typeof value.y !== 'number') return null
|
|
@@ -29,7 +36,46 @@ function pointerPosition(value: JsonValue): PointerPosition | null {
|
|
|
29
36
|
}
|
|
30
37
|
}
|
|
31
38
|
|
|
32
|
-
export function
|
|
39
|
+
export function CursorsContainer({ id = 'default', ...props }: CursorsContainerProps) {
|
|
40
|
+
const channel = presenceChannels.cursor(id)
|
|
41
|
+
const cursors = usePresenceChannel<JsonValue>(channel)
|
|
42
|
+
const publish = usePresencePublisher<JsonValue>(channel, null, hasPresence)
|
|
43
|
+
const byConnection = new Map<string, { id: string; point: PointerPosition }>()
|
|
44
|
+
for (const { connectionId, userId, value } of cursors) {
|
|
45
|
+
const point = pointerPosition(value)
|
|
46
|
+
if (point) byConnection.set(connectionId, { id: userId, point })
|
|
47
|
+
}
|
|
48
|
+
const entries = [...byConnection]
|
|
49
|
+
const profiles = useUsers(entries.map(([, cursor]) => cursor.id))
|
|
50
|
+
return (
|
|
51
|
+
<Cursors
|
|
52
|
+
{...props}
|
|
53
|
+
id={id}
|
|
54
|
+
onPositionChange={publish}
|
|
55
|
+
cursors={entries.map(([connectionId, { point }], index) => ({
|
|
56
|
+
connectionId,
|
|
57
|
+
point,
|
|
58
|
+
profile: profiles[index]
|
|
59
|
+
}))}
|
|
60
|
+
/>
|
|
61
|
+
)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export type CursorsProps = {
|
|
65
|
+
id?: string
|
|
66
|
+
children: ReactNode
|
|
67
|
+
className?: string
|
|
68
|
+
cursors: readonly { connectionId: string; point: PointerPosition; profile?: UserProfile }[]
|
|
69
|
+
onPositionChange?: (point: PointerPosition | null) => void
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function Cursors({
|
|
73
|
+
id = 'default',
|
|
74
|
+
children,
|
|
75
|
+
className,
|
|
76
|
+
cursors,
|
|
77
|
+
onPositionChange
|
|
78
|
+
}: CursorsProps) {
|
|
33
79
|
const root = useRef<HTMLDivElement>(null)
|
|
34
80
|
const layer = useRef<HTMLDivElement>(null)
|
|
35
81
|
// Keep the overlay in the visible viewport when the cursor area itself scrolls.
|
|
@@ -44,14 +90,6 @@ export function Cursors({ id = 'default', children, className }: CursorsProps) {
|
|
|
44
90
|
element.addEventListener('scroll', position)
|
|
45
91
|
return () => element.removeEventListener('scroll', position)
|
|
46
92
|
}, [])
|
|
47
|
-
const channel = presenceChannels.cursor(id)
|
|
48
|
-
const cursors = usePresenceChannel<JsonValue>(channel)
|
|
49
|
-
const publish = usePresencePublisher<JsonValue>(channel, null, hasPresence)
|
|
50
|
-
const byConnection = new Map<string, { id: string; point: PointerPosition }>()
|
|
51
|
-
for (const { connectionId, userId, value } of cursors) {
|
|
52
|
-
const point = pointerPosition(value)
|
|
53
|
-
if (point) byConnection.set(connectionId, { id: userId, point })
|
|
54
|
-
}
|
|
55
93
|
const move = (event: PointerEvent<HTMLDivElement>) => {
|
|
56
94
|
if (event.pointerType === 'touch') return
|
|
57
95
|
const element = root.current
|
|
@@ -68,7 +106,7 @@ export function Cursors({ id = 'default', children, className }: CursorsProps) {
|
|
|
68
106
|
: null
|
|
69
107
|
const anchor = target && element.contains(target) ? target : null
|
|
70
108
|
const rect = anchor?.getBoundingClientRect()
|
|
71
|
-
|
|
109
|
+
onPositionChange?.({
|
|
72
110
|
x: event.clientX - bounds.left - element.clientLeft + element.scrollLeft,
|
|
73
111
|
y: event.clientY - bounds.top - element.clientTop + element.scrollTop,
|
|
74
112
|
...(anchor && rect
|
|
@@ -92,7 +130,7 @@ export function Cursors({ id = 'default', children, className }: CursorsProps) {
|
|
|
92
130
|
event.target instanceof Element &&
|
|
93
131
|
event.target.closest('[data-collab-cursors]') === event.currentTarget
|
|
94
132
|
)
|
|
95
|
-
|
|
133
|
+
onPositionChange?.(null)
|
|
96
134
|
}}
|
|
97
135
|
>
|
|
98
136
|
{children}
|
|
@@ -101,8 +139,8 @@ export function Cursors({ id = 'default', children, className }: CursorsProps) {
|
|
|
101
139
|
className="pointer-events-none absolute inset-0 overflow-hidden"
|
|
102
140
|
aria-hidden="true"
|
|
103
141
|
>
|
|
104
|
-
{
|
|
105
|
-
<RemoteCursor key={connectionId} root={root} point={point}
|
|
142
|
+
{cursors.map(({ connectionId, point, profile }) => (
|
|
143
|
+
<RemoteCursor key={connectionId} root={root} point={point} profile={profile} />
|
|
106
144
|
))}
|
|
107
145
|
</div>
|
|
108
146
|
</div>
|
|
@@ -112,11 +150,11 @@ export function Cursors({ id = 'default', children, className }: CursorsProps) {
|
|
|
112
150
|
type RemoteCursorProps = {
|
|
113
151
|
root: RefObject<HTMLDivElement | null>
|
|
114
152
|
point: PointerPosition
|
|
115
|
-
|
|
153
|
+
profile?: UserProfile
|
|
116
154
|
}
|
|
117
155
|
// Semantic anchors follow their own data through scrolling and reordering.
|
|
118
156
|
// A cursor on a different record's modal has no matching anchor and stays hidden.
|
|
119
|
-
function RemoteCursor({ root, point,
|
|
157
|
+
function RemoteCursor({ root, point, profile }: RemoteCursorProps) {
|
|
120
158
|
const marker = useRef<HTMLSpanElement>(null)
|
|
121
159
|
useLayoutEffect(() => {
|
|
122
160
|
const element = root.current
|
|
@@ -166,15 +204,14 @@ function RemoteCursor({ root, point, id }: RemoteCursorProps) {
|
|
|
166
204
|
window.removeEventListener('resize', reposition)
|
|
167
205
|
}
|
|
168
206
|
}, [root, point])
|
|
169
|
-
return <Cursor ref={marker}
|
|
207
|
+
return <Cursor ref={marker} profile={profile} />
|
|
170
208
|
}
|
|
171
209
|
|
|
172
210
|
type CursorProps = {
|
|
173
|
-
|
|
211
|
+
profile?: UserProfile
|
|
174
212
|
ref?: Ref<HTMLSpanElement>
|
|
175
213
|
}
|
|
176
|
-
function Cursor({
|
|
177
|
-
const user = useUser(id)
|
|
214
|
+
function Cursor({ profile: user, ref }: CursorProps) {
|
|
178
215
|
return (
|
|
179
216
|
<span
|
|
180
217
|
ref={ref}
|
|
@@ -1,47 +1,56 @@
|
|
|
1
|
-
import type { PresenceAvatarsProps } from 'moi/collab'
|
|
1
|
+
import type { PresenceAvatarsProps as PresenceAvatarsContainerProps } from 'moi/collab'
|
|
2
|
+
import type { UserProfile } from '@/lib/collab/types'
|
|
2
3
|
import { useContext } from 'react'
|
|
3
4
|
import type { HTMLAttributes, ReactNode, Ref } from 'react'
|
|
4
5
|
import { motion } from 'motion/react'
|
|
5
6
|
import { cn } from '@/client/lib/cn'
|
|
6
7
|
import { PresenceGroupContext, usePresenceTarget, useTargetPresence } from './presence-helpers'
|
|
8
|
+
import { useUsers } from '../hooks'
|
|
7
9
|
import { UserAvatar } from './user-avatar'
|
|
8
10
|
|
|
9
|
-
export function
|
|
11
|
+
export function PresenceAvatarsContainer({
|
|
12
|
+
id,
|
|
13
|
+
present,
|
|
14
|
+
align,
|
|
15
|
+
children,
|
|
16
|
+
className
|
|
17
|
+
}: PresenceAvatarsContainerProps) {
|
|
10
18
|
const target = usePresenceTarget(id)
|
|
11
19
|
const presence = useTargetPresence(target, present)
|
|
20
|
+
const profiles = useUsers(presence.users.map(user => user.id))
|
|
12
21
|
const grouped = useContext(PresenceGroupContext).length > 0
|
|
13
22
|
return (
|
|
14
|
-
<
|
|
23
|
+
<PresenceAvatars
|
|
15
24
|
{...presence.props}
|
|
16
|
-
users={presence.users}
|
|
25
|
+
users={presence.users.map((user, index) => ({ ...user, profile: profiles[index] }))}
|
|
17
26
|
// Controlled presence can occupy several targets at once; only focus moves between them.
|
|
18
27
|
animate={grouped && present === undefined}
|
|
19
28
|
align={align}
|
|
20
29
|
className={className}
|
|
21
30
|
>
|
|
22
31
|
{children}
|
|
23
|
-
</
|
|
32
|
+
</PresenceAvatars>
|
|
24
33
|
)
|
|
25
34
|
}
|
|
26
35
|
|
|
27
|
-
type
|
|
36
|
+
export type PresenceAvatarsProps = HTMLAttributes<HTMLDivElement> & {
|
|
28
37
|
ref?: Ref<HTMLDivElement>
|
|
29
|
-
users: readonly { id: string; connectionId: string }[]
|
|
38
|
+
users: readonly { id: string; connectionId: string; profile?: UserProfile }[]
|
|
30
39
|
children: ReactNode
|
|
31
40
|
animate?: boolean
|
|
32
|
-
align?:
|
|
41
|
+
align?: PresenceAvatarsContainerProps['align']
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
// A target owns its own gutter. Optional group-scoped layout IDs let the same
|
|
36
45
|
// user's face glide between targets without searching or guessing DOM order.
|
|
37
|
-
function
|
|
46
|
+
export function PresenceAvatars({
|
|
38
47
|
users,
|
|
39
48
|
children,
|
|
40
49
|
animate = false,
|
|
41
50
|
align = 'center',
|
|
42
51
|
className,
|
|
43
52
|
...rest
|
|
44
|
-
}:
|
|
53
|
+
}: PresenceAvatarsProps) {
|
|
45
54
|
return (
|
|
46
55
|
<div className={cn('relative pl-8', className)} {...rest}>
|
|
47
56
|
{children}
|
|
@@ -50,9 +59,13 @@ function PresenceAvatarsPrimitive({
|
|
|
50
59
|
className="pointer-events-none absolute inset-y-0 left-0 flex items-center -space-x-3 data-[align=end]:items-end data-[align=start]:items-start"
|
|
51
60
|
aria-hidden="true"
|
|
52
61
|
>
|
|
53
|
-
{users.map(({ id, connectionId }) => (
|
|
62
|
+
{users.map(({ id, connectionId, profile }) => (
|
|
54
63
|
<motion.span key={id} layoutId={animate ? `presence:${connectionId}` : undefined}>
|
|
55
|
-
<UserAvatar
|
|
64
|
+
<UserAvatar
|
|
65
|
+
profile={profile}
|
|
66
|
+
size="sm"
|
|
67
|
+
className="animate-in duration-200 zoom-in-75 fade-in"
|
|
68
|
+
/>
|
|
56
69
|
</motion.span>
|
|
57
70
|
))}
|
|
58
71
|
</div>
|
|
@@ -1,51 +1,47 @@
|
|
|
1
|
-
import type { PresenceFrameProps } from 'moi/collab'
|
|
1
|
+
import type { PresenceFrameProps as PresenceFrameContainerProps } from 'moi/collab'
|
|
2
|
+
import type { UserProfile } from '@/lib/collab/types'
|
|
2
3
|
import { useLayoutEffect, useRef } from 'react'
|
|
3
4
|
import type { HTMLAttributes, ReactNode, Ref } from 'react'
|
|
4
5
|
import { cn } from '@/client/lib/cn'
|
|
5
6
|
import type { UserColor } from '@/lib/collab/colors'
|
|
6
7
|
import { Badge } from '@/ui-components/badge'
|
|
7
8
|
import { useUsers } from '../hooks'
|
|
8
|
-
import { userDisplayName } from '
|
|
9
|
+
import { userDisplayName } from '@/client/lib/user-display-name'
|
|
9
10
|
import { usePresenceTarget, useTargetPresence } from './presence-helpers'
|
|
10
11
|
|
|
11
|
-
export function
|
|
12
|
+
export function PresenceFrameContainer({
|
|
13
|
+
id,
|
|
14
|
+
present,
|
|
15
|
+
align,
|
|
16
|
+
children,
|
|
17
|
+
className
|
|
18
|
+
}: PresenceFrameContainerProps) {
|
|
12
19
|
const target = usePresenceTarget(id)
|
|
13
20
|
const presence = useTargetPresence(target, present)
|
|
21
|
+
const users = useUsers(presence.users.map(user => user.id)).filter(user => user !== undefined)
|
|
14
22
|
return (
|
|
15
|
-
<
|
|
16
|
-
{...presence.props}
|
|
17
|
-
ids={presence.users.map(user => user.id)}
|
|
18
|
-
align={align}
|
|
19
|
-
className={className}
|
|
20
|
-
>
|
|
23
|
+
<PresenceFrame {...presence.props} users={users} align={align} className={className}>
|
|
21
24
|
{children}
|
|
22
|
-
</
|
|
25
|
+
</PresenceFrame>
|
|
23
26
|
)
|
|
24
27
|
}
|
|
25
28
|
|
|
26
|
-
type
|
|
29
|
+
export type PresenceFrameProps = HTMLAttributes<HTMLDivElement> & {
|
|
27
30
|
ref?: Ref<HTMLDivElement>
|
|
28
31
|
// Everyone at this element; the first user's color draws the frame.
|
|
29
|
-
|
|
32
|
+
users: readonly UserProfile[]
|
|
30
33
|
children: ReactNode
|
|
31
|
-
align?:
|
|
34
|
+
align?: PresenceFrameContainerProps['align']
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
// One visible element gets a frame matching its shape; other content gets one shared frame.
|
|
35
|
-
export function
|
|
36
|
-
ids,
|
|
37
|
-
align,
|
|
38
|
-
children,
|
|
39
|
-
className,
|
|
40
|
-
...rest
|
|
41
|
-
}: PresenceOutlineProps) {
|
|
42
|
-
const resolved = useUsers(ids).filter(user => user !== undefined)
|
|
38
|
+
export function PresenceFrame({ users, align, children, className, ...rest }: PresenceFrameProps) {
|
|
43
39
|
return (
|
|
44
40
|
<div className={cn('relative', className)} {...rest}>
|
|
45
41
|
{/* Keep the overlay first so showing it never changes the content's spacing. */}
|
|
46
42
|
<FrameOutline
|
|
47
|
-
names={
|
|
48
|
-
color={
|
|
43
|
+
names={users.map(userDisplayName).join(', ')}
|
|
44
|
+
color={users[0]?.color}
|
|
49
45
|
align={align}
|
|
50
46
|
/>
|
|
51
47
|
{children}
|
|
@@ -66,7 +62,7 @@ const CORNERS = [
|
|
|
66
62
|
type FrameOutlineProps = {
|
|
67
63
|
names: string
|
|
68
64
|
color: UserColor | undefined
|
|
69
|
-
align?:
|
|
65
|
+
align?: PresenceFrameContainerProps['align']
|
|
70
66
|
}
|
|
71
67
|
|
|
72
68
|
function FrameOutline({ names, color, align = 'end' }: FrameOutlineProps) {
|
|
@@ -1,23 +1,31 @@
|
|
|
1
|
-
import type { PresenceGroupProps } from 'moi/collab'
|
|
1
|
+
import type { PresenceGroupProps as PresenceGroupContainerProps } from 'moi/collab'
|
|
2
|
+
import type { ReactNode } from 'react'
|
|
2
3
|
import { useContext, useId, useMemo } from 'react'
|
|
3
4
|
import { LayoutGroup, MotionConfig } from 'motion/react'
|
|
4
5
|
import { presenceTarget } from '../presence-target'
|
|
5
6
|
import { PresenceGroupContext } from './presence-helpers'
|
|
6
7
|
|
|
7
|
-
export function
|
|
8
|
+
export function PresenceGroupContainer({ id, children }: PresenceGroupContainerProps) {
|
|
8
9
|
const parent = useContext(PresenceGroupContext)
|
|
9
10
|
const scope = useMemo(() => {
|
|
10
11
|
presenceTarget(id)
|
|
11
12
|
return [...parent, id]
|
|
12
13
|
}, [parent, id])
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
return (
|
|
15
|
+
<PresenceGroup>
|
|
16
|
+
<PresenceGroupContext value={scope}>{children}</PresenceGroupContext>
|
|
17
|
+
</PresenceGroup>
|
|
18
|
+
)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type PresenceGroupProps = { children: ReactNode }
|
|
22
|
+
|
|
23
|
+
export function PresenceGroup({ children }: PresenceGroupProps) {
|
|
24
|
+
// Keep avatar animations inside this particular rendered group.
|
|
15
25
|
const animationId = useId()
|
|
16
26
|
return (
|
|
17
27
|
<MotionConfig reducedMotion="user">
|
|
18
|
-
<LayoutGroup id={animationId}>
|
|
19
|
-
<PresenceGroupContext value={scope}>{children}</PresenceGroupContext>
|
|
20
|
-
</LayoutGroup>
|
|
28
|
+
<LayoutGroup id={animationId}>{children}</LayoutGroup>
|
|
21
29
|
</MotionConfig>
|
|
22
30
|
)
|
|
23
31
|
}
|
|
@@ -1,16 +1,29 @@
|
|
|
1
|
-
import type { UserAvatarGroupProps } from 'moi/collab'
|
|
1
|
+
import type { UserAvatarGroupProps as UserAvatarGroupContainerProps } from 'moi/collab'
|
|
2
|
+
import type { UserProfile } from '@/lib/collab/types'
|
|
2
3
|
import { AvatarGroup as AvatarGroupPrimitive, AvatarGroupCount } from '@/ui-components/avatar'
|
|
3
4
|
import { UserAvatar } from './user-avatar'
|
|
5
|
+
import type { UserAvatarProps } from './user-avatar'
|
|
6
|
+
import { useUsers } from '../hooks'
|
|
4
7
|
|
|
5
|
-
export function
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
export function UserAvatarGroupContainer({ ids, ...props }: UserAvatarGroupContainerProps) {
|
|
9
|
+
const users = useUsers([...new Set(ids)])
|
|
10
|
+
return <UserAvatarGroup users={users} {...props} />
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type UserAvatarGroupProps = {
|
|
14
|
+
users: readonly (UserProfile | undefined)[]
|
|
15
|
+
max?: number
|
|
16
|
+
size?: UserAvatarProps['size']
|
|
17
|
+
className?: string
|
|
18
|
+
}
|
|
9
19
|
|
|
20
|
+
export function UserAvatarGroup({ users, max = 3, size = 'sm', className }: UserAvatarGroupProps) {
|
|
21
|
+
const shown = users.slice(0, max)
|
|
22
|
+
const hidden = users.length - shown.length
|
|
10
23
|
return (
|
|
11
|
-
<AvatarGroupPrimitive className={className} aria-label={`${
|
|
12
|
-
{shown.map(
|
|
13
|
-
<UserAvatar key={id}
|
|
24
|
+
<AvatarGroupPrimitive className={className} aria-label={`${users.length} users`}>
|
|
25
|
+
{shown.map((profile, index) => (
|
|
26
|
+
<UserAvatar key={profile?.id ?? index} profile={profile} size={size} />
|
|
14
27
|
))}
|
|
15
28
|
{hidden > 0 && (
|
|
16
29
|
<AvatarGroupCount size={size} aria-label={`${hidden} more`}>
|