"use client" import { useState } from "react" import { Switch } from "@/components/ui/switch" import { setAnonymousAccessStatus } from "@/actions" import { SINGLE_TENANT_ORG_DOMAIN } from "@/lib/constants" import { isServiceError } from "@/lib/utils" import { useToast } from "@/components/hooks/use-toast" interface AnonymousAccessToggleProps { hasAnonymousAccessEntitlement: boolean; anonymousAccessEnabled: boolean forceEnableAnonymousAccess: boolean onToggleChange?: (checked: boolean) => void } export function AnonymousAccessToggle({ hasAnonymousAccessEntitlement, anonymousAccessEnabled, forceEnableAnonymousAccess, onToggleChange }: AnonymousAccessToggleProps) { const [enabled, setEnabled] = useState(anonymousAccessEnabled) const [isLoading, setIsLoading] = useState(false) const { toast } = useToast() const handleToggle = async (checked: boolean) => { setIsLoading(true) try { const result = await setAnonymousAccessStatus(SINGLE_TENANT_ORG_DOMAIN, checked) if (isServiceError(result)) { toast({ title: "Error", description: result.message || "Failed to update anonymous access setting", variant: "destructive", }) return } setEnabled(checked) onToggleChange?.(checked) } catch (error) { console.error("Error updating anonymous access setting:", error) toast({ title: "Error", description: "Failed to update anonymous access setting", variant: "destructive", }) } finally { setIsLoading(false) } } const isDisabled = isLoading || !hasAnonymousAccessEntitlement || forceEnableAnonymousAccess; const showPlanMessage = !hasAnonymousAccessEntitlement; const showForceEnableMessage = !showPlanMessage && forceEnableAnonymousAccess; return (
When enabled, users can access your deployment without logging in.
{showPlanMessage && (Your current plan doesn't allow for anonymous access. Please{" "} reach out {" "}for assistance.
The forceEnableAnonymousAccess is set, so this cannot be changed from the UI.