import { useState } from "react"; import { useParams, useNavigate } from "react-router"; import { createDataSDK } from "@salesforce/sdk-data"; import { AlertCircle, ChevronDown, ChevronRight, FileQuestion } from "lucide-react"; import GET_ACCOUNT_DETAIL from "../api/account/query/getAccountDetail.graphql?raw"; import type { GetAccountDetailQuery, GetAccountDetailQueryVariables, } from "../api/graphql-operations-types"; import { Alert, AlertTitle, AlertDescription } from "../components/ui/alert"; import { Button } from "../components/ui/button"; import { Card, CardContent } from "../components/ui/card"; import { fieldValue, getAddressFieldLines, formatDateTimeField, } from "../features/object-search/utils/fieldUtils"; import { Collapsible, CollapsibleTrigger, CollapsibleContent, } from "../components/ui/collapsible"; import { Separator } from "../components/ui/separator"; import { Skeleton } from "../components/ui/skeleton"; import { useCachedAsyncData } from "../features/object-search/hooks/useCachedAsyncData"; import { ObjectBreadcrumb } from "../features/object-search/components/ObjectBreadcrumb"; type AccountNode = NonNullable< NonNullable< NonNullable["edges"]>[number] >["node"] >; async function fetchAccountDetail(recordId: string): Promise { const data = await createDataSDK(); const response = await data.graphql?.( GET_ACCOUNT_DETAIL, { id: recordId }, ); if (response?.errors?.length) { throw new Error(response.errors.map((e) => e.message).join("; ")); } return response?.data?.uiapi?.query?.Account?.edges?.[0]?.node; } export default function AccountObjectDetail() { const { recordId } = useParams(); const navigate = useNavigate(); const { data: account, loading, error, } = useCachedAsyncData(() => fetchAccountDetail(recordId!), [recordId], { key: `account:${recordId}`, }); return (
{/* Loading state */} {loading && } {/* Error state */} {error && navigate(-1)} />} {/* Not found state */} {!loading && !error && !account && navigate(-1)} />} {/* Content */} {account && }
); } function AccountDetailContent({ account }: { account: AccountNode }) { const billingAddress = getAddressFieldLines({ street: fieldValue(account.BillingStreet), city: fieldValue(account.BillingCity), state: fieldValue(account.BillingState), postalCode: fieldValue(account.BillingPostalCode), country: fieldValue(account.BillingCountry), }); const shippingAddress = getAddressFieldLines({ street: fieldValue(account.ShippingStreet), city: fieldValue(account.ShippingCity), state: fieldValue(account.ShippingState), postalCode: fieldValue(account.ShippingPostalCode), country: fieldValue(account.ShippingCountry), }); const dateTimeOptions = { dateStyle: "medium", timeStyle: "short" } as const; const createdDate = formatDateTimeField( fieldValue(account.CreatedDate), undefined, dateTimeOptions, ); const lastModifiedDate = formatDateTimeField( fieldValue(account.LastModifiedDate), undefined, dateTimeOptions, ); return ( <>

Account: {fieldValue(account.Name)}

{/* Top section */}
{fieldValue(account.Owner?.Name)} {fieldValue(account.Name)} {fieldValue(account.Parent?.Name)} {fieldValue(account.Website)}
{/* Additional Information */}
{fieldValue(account.Type)} {fieldValue(account.NumberOfEmployees)} {fieldValue(account.Industry)} {fieldValue(account.AnnualRevenue)} {fieldValue(account.Description)}
{/* Address Information */}
{billingAddress ? billingAddress.map((line, i) =>
{line}
) : null}
{shippingAddress ? shippingAddress.map((line, i) =>
{line}
) : null}
{/* System Information */}
{[fieldValue(account.CreatedBy?.Name), createdDate].filter(Boolean).join(" ") || null} {[fieldValue(account.LastModifiedBy?.Name), lastModifiedDate] .filter(Boolean) .join(" ") || null}
); } function TelephoneField({ value }: { value?: string | null }) { if (!value) return null; return ( {value} ); } function FieldItem({ label, children }: { label: string; children: React.ReactNode }) { return (
{label}
{children ?? "—"}
); } function FieldRow({ children }: { children: React.ReactNode }) { return
{children}
; } function Section({ title, children }: { title: string; children: React.ReactNode }) { const [open, setOpen] = useState(true); return ( {open ? : } {title}
{children}
); } function AccountDetailError({ onBack }: { onBack: () => void }) { return ( <>

Failed to load account

Something went wrong while loading this account. Please try again later.
); } function AccountDetailNotFound({ onBack }: { onBack: () => void }) { return (

Account not found

The account you're looking for doesn't exist or may have been deleted.

); } function AccountDetailSkeleton() { return ( <> {/* Top section: field rows */}
{/* Additional Information */} {/* Address Information */}
{/* System Information */}
); } function SkeletonField() { return (
); } function SkeletonFieldRow() { return (
); } function SkeletonSection() { return (
); }