import { useEffect, useId, useRef, useState } from "react"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import { Camera, LoaderCircle, Save, Trash2, UserRoundPen } from "lucide-react"; import type { AuthSession, CurrentUserProfile, UpdateCurrentUserProfile } from "@paperclipai/shared"; import { authApi } from "@/api/auth"; import { useAccountBoundMutation } from "@/hooks/useAccountBoundMutation"; import { assetsApi } from "@/api/assets"; import { QueryErrorState, useQueryView } from "@/components/QueryView"; import { useBreadcrumbs } from "../context/BreadcrumbContext"; import { useCompany } from "../context/CompanyContext"; import { queryKeys } from "../lib/queryKeys"; import { InboxAgentPolicyControl } from "@/components/InboxAgentPolicyControl"; import { ProductAnalyticsSetting } from "@/components/ProductAnalyticsSetting"; import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; import { Card } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { describeError } from "@/api/errors"; function deriveInitials(name: string) { const parts = name.trim().split(/\s+/).filter(Boolean); if (parts.length >= 2) return `${parts[0]?.[0] ?? ""}${parts[parts.length - 1]?.[0] ?? ""}`.toUpperCase(); return name.slice(0, 2).toUpperCase(); } export function ProfileSettings() { const { setBreadcrumbs } = useBreadcrumbs(); const { selectedCompanyId, selectedCompany } = useCompany(); const queryClient = useQueryClient(); const avatarInputId = useId(); const avatarInputRef = useRef(null); // Unedited fields show the session's saved values from the first frame; a // restored session must not render an empty name until an effect fills it. const [nameDraft, setName] = useState(null); const [imageDraft, setImage] = useState(null); const [actionError, setActionError] = useState(null); const sessionQuery = useQuery({ queryKey: queryKeys.auth.session, queryFn: () => authApi.getSession(), }); const sessionView = useQueryView(sessionQuery); const name = nameDraft ?? sessionQuery.data?.user.name ?? ""; const image = imageDraft ?? sessionQuery.data?.user.image ?? ""; useEffect(() => { setBreadcrumbs([ { label: "Settings", href: "/company/settings" }, { label: "Profile" }, ]); }, [setBreadcrumbs]); useEffect(() => { setName(null); setImage(null); }, [sessionQuery.data]); function syncSessionProfile(profile: CurrentUserProfile) { queryClient.setQueryData(queryKeys.auth.session, (current) => { if (!current) return current; return { ...current, user: { ...current.user, ...profile, }, }; }); } // A restored session can show one account while the cookie belongs to // another, and the server saves for the cookie's user. Account-bound // mutations wait for a verified session and abort if the user changed. async function persistProfile(input: UpdateCurrentUserProfile, assertCurrentAccount: () => void) { assertCurrentAccount(); const profile = await authApi.updateProfile(input); assertCurrentAccount(); syncSessionProfile(profile); return profile; } function resolveProfileName() { return name.trim() || sessionQuery.data?.user.name || "Board"; } const updateMutation = useAccountBoundMutation({ mutationFn: (input: UpdateCurrentUserProfile, { assertCurrentAccount }) => persistProfile(input, assertCurrentAccount), onSuccess: (profile) => { setActionError(null); setName(profile.name ?? ""); setImage(profile.image ?? ""); }, onError: (error) => { setActionError(describeError(error).body); }, }); const uploadAvatarMutation = useAccountBoundMutation({ mutationFn: async (file: File, { assertCurrentAccount }) => { if (!selectedCompanyId) { throw new Error("Select an organization before uploading a profile avatar."); } const asset = await assetsApi.uploadImage( selectedCompanyId, file, `profiles/${sessionQuery.data?.user.id ?? "board-user"}`, ); return persistProfile({ name: resolveProfileName(), image: asset.contentPath }, assertCurrentAccount); }, onSuccess: (profile) => { setActionError(null); setName(profile.name ?? ""); setImage(profile.image ?? ""); }, onError: (error) => { setActionError(describeError(error).body); }, }); const removeAvatarMutation = useAccountBoundMutation({ mutationFn: (_input, { assertCurrentAccount }) => persistProfile({ name: resolveProfileName(), image: null }, assertCurrentAccount), onSuccess: (profile) => { setActionError(null); setName(profile.name ?? ""); setImage(profile.image ?? ""); }, onError: (error) => { setActionError(describeError(error).body); }, }); if (sessionQuery.isLoading || sessionView.kind === "reconnecting") { return
Loading profile...
; } if (sessionView.kind === "error") { return ( ); } if (!sessionQuery.data) { return
Failed to load profile.
; } const currentName = name.trim() || sessionQuery.data.user.name || "Board"; const currentImage = image.trim() || null; const initials = deriveInitials(currentName); const isSavingProfile = updateMutation.isPending || uploadAvatarMutation.isPending || removeAvatarMutation.isPending; const uploadHint = selectedCompany ? `Stored in Paperclip file storage for ${selectedCompany.name}.` : "Select an organization to upload an avatar into Paperclip storage."; return (

Profile

Control how your account appears in the sidebar and other board surfaces.

{actionError ? (
{actionError}
) : null}
{currentImage ? ( ) : null}

{currentName}

{sessionQuery.data.user.email ?? "No email"}

Click the avatar to upload a new image. {uploadHint}

{ event.preventDefault(); updateMutation.mutate({ name: resolveProfileName(), image: image.trim() || null }); }} >
setName(event.target.value)} maxLength={120} placeholder="Board" />

Shown in the sidebar account footer and comment author surfaces.

Email is managed by your auth session and is read-only here.

); }