2023-06-20 13:31:26 +00:00
|
|
|
import { useValue } from '@tldraw/state'
|
2023-05-25 09:54:29 +00:00
|
|
|
import { TLInstancePresence } from '@tldraw/tlschema'
|
2023-06-02 15:21:45 +00:00
|
|
|
import { useEditor } from './useEditor'
|
2023-05-03 13:48:46 +00:00
|
|
|
|
|
|
|
// TODO: maybe move this to a computed property on the App class?
|
|
|
|
/**
|
|
|
|
* @returns The list of peer UserIDs
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-05-25 09:54:29 +00:00
|
|
|
export function usePresence(userId: string): TLInstancePresence | null {
|
2023-06-02 15:21:45 +00:00
|
|
|
const editor = useEditor()
|
2023-05-03 13:48:46 +00:00
|
|
|
|
|
|
|
const latestPresence = useValue(
|
|
|
|
`latestPresence:${userId}`,
|
|
|
|
() => {
|
2024-04-19 13:56:55 +00:00
|
|
|
return editor.getCollaborators().find((c) => c.userId === userId)
|
2023-05-03 13:48:46 +00:00
|
|
|
},
|
2024-04-19 13:56:55 +00:00
|
|
|
[editor]
|
2023-05-03 13:48:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return latestPresence ?? null
|
|
|
|
}
|