'use client' import { IA_NAV_FLAG, isIaNavVariant, type IaNavVariant } from '~/features/IaNav/constants' import { useDocsPathname, useIaNavRouteVariant } from '~/features/IaNav/IaNavRouteProvider' import { setLayoutChoice } from '~/features/IaNav/layoutChoice' import { useIaNavReady } from '~/features/IaNav/useIaNavReady' import { useIaNavVariant } from '~/features/IaNav/useIaNavVariant' import { BASE_PATH, IS_PLATFORM } from '~/lib/constants' import { supabase } from '~/lib/supabase' import { useSendTelemetryEvent } from '~/lib/telemetry' import { useFeatureFlags } from 'common' import { PanelsTopLeft } from 'lucide-react' import { usePathname } from 'next/navigation' import { useRef, useState } from 'react' import { Button, cn } from 'ui' import { LayoutSwitchFeedbackDialog } from './LayoutSwitchFeedbackDialog' interface LayoutSwitchButtonProps { className?: string } export const LayoutSwitchButton = ({ className }: LayoutSwitchButtonProps) => { const variant = useIaNavVariant() const isNavReady = useIaNavReady() const sendTelemetryEvent = useSendTelemetryEvent() const assignedVariant = useFeatureFlags().posthog[IA_NAV_FLAG] const pathname = usePathname() ?? '' const routeVariant = useIaNavRouteVariant() const docsPathname = useDocsPathname() const [isFeedbackOpen, setIsFeedbackOpen] = useState(false) const buttonRef = useRef(null) const switchLayout = (nextVariant: IaNavVariant) => { setLayoutChoice(nextVariant) sendTelemetryEvent({ action: 'docs_layout_updated', properties: { layoutVariant: nextVariant, ...(isIaNavVariant(assignedVariant) && { assignedVariant }), }, }) // a forced url or route keeps its variant over the choice, so only a load of the public page switches const url = new URL(window.location.href) const isForcedUrl = url.searchParams.has(IA_NAV_FLAG) if (!isForcedUrl && !routeVariant) return url.searchParams.delete(IA_NAV_FLAG) if (routeVariant) url.pathname = `${BASE_PATH}${docsPathname}` window.location.replace(url) } // a switch back asks why first, so the rollout gets a reason for each opt-out const handleClick = () => variant === 'option-1' ? setIsFeedbackOpen(true) : switchLayout('option-1') const handleFeedbackCancel = () => setIsFeedbackOpen(false) const handleFeedbackSubmit = async (comment: string) => { if (comment && IS_PLATFORM) { // the vote and title columns are for page feedback, so the source lives in metadata const { error } = await supabase() .from('feedback_comments') .insert({ page: pathname, comment, metadata: { source: 'docs_layout_switch', layoutVariant: 'control' }, }) // the reason is optional, so a hung request must not block the switch .abortSignal(AbortSignal.timeout(5000)) if (error) console.error(error) } setIsFeedbackOpen(false) switchLayout('control') } // the server renders control, so the button waits for the known variant if (!isNavReady) return null return ( <> ) }