Skip to content

Commit

Permalink
react-sdk: useJoinSpace (#599)
Browse files Browse the repository at this point in the history
This updates the `useJoinSpace` action to have the signature of `join :
(spaceId, signer, options) => void`, instead of passing the `spaceId` as
a hook parameter
  • Loading branch information
miguel-nascimento authored Aug 5, 2024
1 parent 761fe1c commit ba85af9
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 21 deletions.
22 changes: 12 additions & 10 deletions packages/playground/src/components/blocks/channels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ export const ChannelsBlock = ({ changeChannel }: ChannelsBlockProps) => {
return (
<Block title={`Channels in ${space.metadata?.name || 'Unnamed Space'}`}>
<CreateChannel variant="secondary" spaceId={spaceId} onChannelCreated={changeChannel} />
<div className="flex flex-col gap-1">
<div className="flex flex-col gap-2">
<span className="text-xs">Select a channel to start messaging</span>
{space.channelIds.map((channelId) => (
<ChannelInfo
key={`${spaceId}-${channelId}`}
spaceId={space.id}
channelId={channelId}
changeChannel={changeChannel}
/>
))}
<div className="flex max-h-64 flex-col gap-1 overflow-y-auto">
{space.channelIds.map((channelId) => (
<ChannelInfo
key={`${spaceId}-${channelId}`}
spaceId={space.id}
channelId={channelId}
changeChannel={changeChannel}
/>
))}
</div>
</div>
{space.channelIds.length === 0 && (
<p className="pt-4 text-center text-sm text-secondary-foreground">
Expand Down Expand Up @@ -94,7 +96,7 @@ export const CreateChannel = (
<Block {...rest}>
<Form {...form}>
<form
className="space-y-8"
className="space-y-3"
onSubmit={form.handleSubmit(async ({ channelName }) => {
if (!signer) {
return
Expand Down
61 changes: 56 additions & 5 deletions packages/playground/src/components/blocks/spaces.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCreateSpace, useSpace, useUserSpaces } from '@river-build/react-sdk'
import { useCreateSpace, useJoinSpace, useSpace, useUserSpaces } from '@river-build/react-sdk'
import { zodResolver } from '@hookform/resolvers/zod'
import { useForm } from 'react-hook-form'
import { z } from 'zod'
Expand All @@ -21,7 +21,7 @@ type SpacesBlockProps = {
changeSpace: (spaceId: string) => void
}

const formSchema = z.object({
const createSpaceFormSchema = z.object({
spaceName: z.string().min(1, { message: 'Space name is required' }),
})

Expand All @@ -30,6 +30,7 @@ export const SpacesBlock = ({ changeSpace }: SpacesBlockProps) => {
return (
<Block title="Spaces">
<CreateSpace variant="secondary" onCreateSpace={changeSpace} />
<JoinSpace variant="secondary" onJoinSpace={changeSpace} />
<span className="text-xs">Select a space to start messaging</span>
<div className="flex flex-col gap-1">
{spaceIds.map((spaceId) => (
Expand Down Expand Up @@ -64,21 +65,71 @@ const SpaceInfo = ({
)
}

const joinSpaceFormSchema = z.object({
spaceId: z.string().min(1, { message: 'Space Id is required' }),
})
export const JoinSpace = (props: { onJoinSpace: (spaceId: string) => void } & BlockProps) => {
const { onJoinSpace, ...rest } = props
const { joinSpace, isPending } = useJoinSpace()
const signer = useEthersSigner()

const form = useForm<z.infer<typeof joinSpaceFormSchema>>({
resolver: zodResolver(joinSpaceFormSchema),
defaultValues: { spaceId: '' },
})

return (
<Block {...rest}>
<Form {...form}>
<form
className="space-y-4"
onSubmit={form.handleSubmit(async ({ spaceId }) => {
if (!signer) {
return
}
joinSpace(spaceId, signer).then(() => {
onJoinSpace(spaceId)
})
})}
>
<FormField
control={form.control}
name="spaceId"
render={({ field }) => (
<FormItem>
<FormLabel>Join Space</FormLabel>
<FormControl>
<Input placeholder="spaceId" {...field} />
</FormControl>
<FormDescription>
The spaceId of the space you want to join.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit"> {isPending ? 'Joining...' : 'Join'}</Button>
</form>
</Form>
</Block>
)
}

export const CreateSpace = (props: { onCreateSpace: (spaceId: string) => void } & BlockProps) => {
const { onCreateSpace, ...rest } = props
const { createSpace, isPending } = useCreateSpace()
const signer = useEthersSigner()

const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
const form = useForm<z.infer<typeof createSpaceFormSchema>>({
resolver: zodResolver(createSpaceFormSchema),
defaultValues: { spaceName: '' },
})

return (
<Block {...rest}>
<Form {...form}>
<form
className="space-y-8"
className="space-y-3"
onSubmit={form.handleSubmit(async ({ spaceName }) => {
if (!signer) {
return
Expand Down
2 changes: 2 additions & 0 deletions packages/playground/src/components/ui/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const Input = React.forwardRef<HTMLInputElement, InputProps>(
return (
<input
type={type}
spellCheck={false}
autoComplete="off"
className={cn(
'flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1',
'text-sm shadow-sm transition-colors placeholder:text-muted-foreground',
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/src/routes/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const FormExample = () => {
}
return (
<Form {...form}>
<form className="space-y-8" onSubmit={form.handleSubmit(onSubmit)}>
<form className="space-y-3" onSubmit={form.handleSubmit(onSubmit)}>
<FormField
control={form.control}
name="username"
Expand Down
1 change: 1 addition & 0 deletions packages/react-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './connectRiver'
export * from './useChannel'
export * from './useCreateChannel'
export * from './useCreateSpace'
export * from './useJoinSpace'
export * from './useObservable'
export * from './useRiver'
export * from './useRiverAuthStatus'
Expand Down
9 changes: 4 additions & 5 deletions packages/react-sdk/src/useJoinSpace.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
'use client'

import type { Space } from '@river-build/sdk'
import type { Spaces } from '@river-build/sdk'
import { type ActionConfig, useAction } from './internals/useAction'
import { useSyncAgent } from './useSyncAgent'

export const useJoinSpace = (spaceId: string, config: ActionConfig<Space['join']> = {}) => {
export const useJoinSpace = (config: ActionConfig<Spaces['joinSpace']> = {}) => {
const sync = useSyncAgent()
const space = sync.spaces.getSpace(spaceId)
const { action: join, ...rest } = useAction(space, 'join', config)
const { action: joinSpace, ...rest } = useAction(sync.spaces, 'joinSpace', config)

return {
join,
joinSpace,
...rest,
}
}
5 changes: 5 additions & 0 deletions packages/sdk/src/sync-agent/spaces/spaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ export class Spaces extends PersistedObservable<SpacesModel> {
})
return { spaceId, defaultChannelId }
}

async joinSpace(spaceId: string, ...args: Parameters<Space['join']>) {
const space = this.getSpace(spaceId)
return space.join(...args)
}
}

0 comments on commit ba85af9

Please sign in to comment.