Organization switcher
A menu of the user's organizations that switches the active one and refreshes the session for its tenant claim. Web and native call useOrganizations from @/hooks/use-organizations.

Add the components
terminal npx shadcn add dropdown-menu sidebarInstall the glue
Install the block with
npx better-supabase add organizations. The glue items are copied into your app, so edit them as you need.terminal npx shadcn add @better-supabase/use-organizationsWrite the component
React and shadcn
View in the exampleorganization-switcher-menu.tsx "use client"; import { DbException } from "better-supabase"; import { useErrorMessage } from "better-supabase/react"; import { CheckIcon, ChevronsUpDownIcon, PlusIcon } from "lucide-react"; import { useExtracted } from "next-intl"; import { useState, useTransition } from "react"; import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { SidebarMenuButton } from "@/components/ui/sidebar"; import { useOrganizations } from "@/hooks/use-organizations"; import { organizationsConfig } from "@/lib/supabase/blocks/slices"; import { toast } from "@/lib/toast"; import type { OrganizationSummary } from "../organization-queries"; import { loadMyOrganizations } from "../organization-actions"; import { useRefreshSession } from "../use-refresh-session"; import { useRoleLabel } from "../use-role-label"; import { CreateOrganizationDialog } from "./create-organization-dialog"; function Mark({ name }: { name: string }) { return ( <div aria-hidden="true" className="flex aspect-square size-8 items-center justify-center rounded-lg bg-sidebar-primary text-sm font-semibold text-sidebar-primary-foreground" > {name.slice(0, 1).toUpperCase()} </div> ); } async function getOrganizations(): Promise<readonly OrganizationSummary[]> { const result = await loadMyOrganizations({}); if (!result.ok) { throw new DbException(result.error); } return result.data; } /** * The organization menu. The server renders it with its rows; switching goes * through `switch_organization`, refreshes the session for the new tenant * claim, then re-renders the server tree. */ export function OrganizationSwitcherMenu({ initial, activeId: serverActiveId, }: { initial: readonly OrganizationSummary[]; activeId: string | null; }) { const t = useExtracted("organization"); const roleLabel = useRoleLabel(); const errorMessage = useErrorMessage(); const refreshSession = useRefreshSession(); const [pending, startTransition] = useTransition(); const [creating, setCreating] = useState(false); const { organizations, active, activeId, switchTo } = useOrganizations({ load: getOrganizations, initial, activeId: serverActiveId, schema: organizationsConfig.api, onSwitched: () => refreshSession("/"), }); const choose = (organization: OrganizationSummary) => { startTransition(async () => { const result = await switchTo(organization.id); if (!result.ok) { toast.error(errorMessage(result.error)); return; } toast.success(t("Switched to {name}", { name: organization.name })); }); }; return ( <> <DropdownMenu> <DropdownMenuTrigger disabled={pending} render={ <SidebarMenuButton size="lg" className="data-popup-open:bg-sidebar-accent" /> } > <Mark name={active?.name ?? "?"} /> <div className="grid flex-1 text-left text-sm leading-tight"> <span className="truncate font-medium" data-testid="organization-name" > {active?.name ?? t("No organization")} </span> <span className="truncate text-xs text-muted-foreground capitalize"> {active ? active.plan : t("Create one to start")} </span> </div> <span className="sr-only">{t("Switch organization")}</span> <ChevronsUpDownIcon className="ml-auto" /> </DropdownMenuTrigger> <DropdownMenuContent className="w-64" align="start" side="bottom"> <DropdownMenuGroup> <DropdownMenuLabel>{t("Organizations")}</DropdownMenuLabel> {(organizations ?? initial).map((organization) => ( <DropdownMenuItem key={organization.id} className="gap-2" onClick={() => { if (organization.id !== activeId) { choose(organization); } }} > <Mark name={organization.name} /> <div className="grid flex-1 leading-tight"> <span className="truncate">{organization.name}</span> <span className="text-xs text-muted-foreground"> {roleLabel(organization.role)} </span> </div> {organization.id === activeId ? <CheckIcon /> : null} </DropdownMenuItem> ))} </DropdownMenuGroup> <DropdownMenuSeparator /> <DropdownMenuItem onClick={() => { setCreating(true); }} > <PlusIcon /> {t("Create organization")} </DropdownMenuItem> </DropdownMenuContent> </DropdownMenu> <CreateOrganizationDialog open={creating} onOpenChange={setCreating} /> </> ); }