@towns-protocol/react-sdk
Version:
React Hooks for Towns Protocol SDK
29 lines (27 loc) • 771 B
text/typescript
import {
type Channel,
type Dm,
type Gdm,
Space,
type SyncAgent,
isChannelStreamId,
isDMChannelStreamId,
isGDMChannelStreamId,
isSpaceStreamId,
spaceIdFromChannelId,
} from '@towns-protocol/sdk'
export const getRoom = (sync: SyncAgent, streamId: string): Gdm | Channel | Dm | Space => {
if (isChannelStreamId(streamId)) {
return sync.spaces.getSpace(spaceIdFromChannelId(streamId)).getChannel(streamId)
}
if (isGDMChannelStreamId(streamId)) {
return sync.gdms.getGdm(streamId)
}
if (isDMChannelStreamId(streamId)) {
return sync.dms.getDm(streamId)
}
if (isSpaceStreamId(streamId)) {
return sync.spaces.getSpace(streamId)
}
throw new Error('Invalid room type')
}