"use client"; import { useState } from "react"; import { Button } from "@app/components/ui/button"; import { Alert, AlertDescription } from "@app/components/ui/alert"; import { useTranslations } from "next-intl"; import { Separator } from "./ui/separator"; import LoginPasswordForm from "./LoginPasswordForm"; import IdpLoginButtons from "./private/IdpLoginButtons"; import { LookupUserResponse } from "@server/routers/auth/lookupUser"; import UserProfileCard from "./UserProfileCard"; type LoginOrgSelectorProps = { identifier: string; lookupResult: LookupUserResponse; redirect?: string; forceLogin?: boolean; onUseDifferentAccount?: () => void; }; export default function LoginOrgSelector({ identifier, lookupResult, redirect, forceLogin, onUseDifferentAccount }: LoginOrgSelectorProps) { const t = useTranslations(); const [showPasswordForm, setShowPasswordForm] = useState(false); // Collect all unique orgs from all accounts const orgMap = new Map< string, { orgId: string; orgName: string; idps: Array<{ idpId: number; name: string; variant: string | null; }>; hasInternalAuth: boolean; } >(); for (const account of lookupResult.accounts) { for (const org of account.orgs) { if (!orgMap.has(org.orgId)) { orgMap.set(org.orgId, { orgId: org.orgId, orgName: org.orgName, idps: org.idps, hasInternalAuth: org.hasInternalAuth }); } else { // Merge IdPs if org appears in multiple accounts const existing = orgMap.get(org.orgId)!; const existingIdpIds = new Set( existing.idps.map((i) => i.idpId) ); for (const idp of org.idps) { if (!existingIdpIds.has(idp.idpId)) { existing.idps.push(idp); } } if (org.hasInternalAuth) { existing.hasInternalAuth = true; } } } } const orgs = Array.from(orgMap.values()); // Check if there's an internal account (can only be one) const hasInternalAccount = lookupResult.accounts.some( (acc) => acc.hasInternalAuth ); // If user selected password auth, show password form if (showPasswordForm) { return (
); } return (
{hasInternalAccount && (
)}
{orgs.map((org, index) => { const hasIdps = org.idps.length > 0; if (!hasIdps) { return null; } // Convert org.idps to LoginFormIDP format const idps = org.idps.map((idp) => ({ idpId: idp.idpId, name: idp.name, variant: idp.variant || undefined })); return (

{org.orgName}

); })}
); }