// @ts-nocheck import { zodResolver } from '@hookform/resolvers/zod' import { IS_PLATFORM, useParams } from 'common' import { ChevronDown } from 'lucide-react' import { Dispatch, SetStateAction, useState } from 'react' import { useForm } from 'react-hook-form' import { Button, Form, FormControl, FormDescription, FormField, FormItem, FormLabel, Input, Popover, PopoverContent, PopoverTrigger, Switch, } from 'ui' import * as z from 'zod' import { RealtimeConfig } from './useRealtimeMessages' import { DocsButton } from '@/components/ui/DocsButton' import { ShortcutTooltip } from '@/components/ui/ShortcutTooltip' import { getTemporaryAPIKey } from '@/data/api-keys/temp-api-keys-query' import { useSendEventMutation } from '@/data/telemetry/send-event-mutation' import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization' import { DOCS_URL } from '@/lib/constants' import { SHORTCUT_IDS } from '@/state/shortcuts/registry' type ControlledOpenProps = | { open: boolean; onOpenChange: (open: boolean) => void } | { open?: undefined; onOpenChange?: undefined } type ChooseChannelPopoverProps = { config: RealtimeConfig onChangeConfig: Dispatch> } & ControlledOpenProps const FormSchema = z.object({ channel: z.string(), isPrivate: z.boolean() }) export const ChooseChannelPopover = ({ config, onChangeConfig, open: controlledOpen, onOpenChange, }: ChooseChannelPopoverProps) => { const [internalOpen, setInternalOpen] = useState(false) const isControlled = controlledOpen !== undefined const open = isControlled ? controlledOpen : internalOpen const setOpen = (v: boolean) => { if (isControlled) { onOpenChange?.(v) } else { setInternalOpen(v) } } const { ref } = useParams() const { data: org } = useSelectedOrganizationQuery() const { mutate: sendEvent } = useSendEventMutation() const form = useForm>({ mode: 'onBlur', reValidateMode: 'onBlur', resolver: zodResolver(FormSchema as any), defaultValues: { channel: '', isPrivate: false }, }) const onOpen = (v: boolean) => { // when opening, copy the outside config into the intermediate one if (v === true) { form.setValue('channel', config.channelName) } setOpen(v) } const onSubmit = async () => { setOpen(false) sendEvent({ action: 'realtime_inspector_listen_channel_clicked', groups: { project: ref ?? 'Unknown', organization: org?.slug ?? 'Unknown', }, }) let token = config.token // [Joshen] Refresh if starting to listen + using temp API key, since it has a low refresh rate if (token.startsWith('sb_temp') || !IS_PLATFORM) { const data = await getTemporaryAPIKey({ projectRef: config.projectRef, expiry: 3600 }) token = data.api_key } onChangeConfig({ ...config, token, channelName: form.getValues('channel'), isChannelPrivate: form.getValues('isPrivate'), enabled: true, }) } const channelPopoverTrigger = ( ) return ( {!open && config.channelName.length === 0 ? ( {channelPopoverTrigger} ) : ( channelPopoverTrigger )}
{config.channelName.length === 0 ? ( <>
onSubmit())} className="flex flex-col gap-y-4" > (
The channel you initialize with the Briven Realtime client. Learn more in{' '} our docs
)} /> (
Is channel private?
If the channel is marked as private, it will use RLS policies to filter messages.
)} /> ) : (

Currently joined{' '} {config.isChannelPrivate ? 'private' : 'public'} {' '} channel:

{config.channelName}

If you leave this channel, all of the messages populated on this page will disappear

)}
) }