Add optional chaining for party and people props

Guard against null/undefined values by adding optional chaining to property accesses. In AssignmentDetailComponent.tsx, use party?.name, party?.photo, party?.description and make initials accept a possibly undefined name (name?.split...). In AssignedPeople.tsx, check people?.length and use people?.slice and people?.length in the UI so rendering won't crash when the people prop is missing or undefined. These changes prevent runtime errors when props are absent.
This commit is contained in:
Ichsan Kurniawan
2026-05-07 16:53:23 +07:00
parent 49f766ad27
commit 2853954cbc
2 changed files with 11 additions and 11 deletions
@@ -244,7 +244,7 @@ function MetaTile({ icon: Icon, label, value }: { icon: typeof Briefcase; label:
function PartyCard({ label, party, accent }: { label: string; party: any; accent: string }) {
function initials(name: string) {
return name.split(" ").map((n) => n[0]).filter(Boolean).slice(0, 2).join("").toUpperCase();
return name?.split(" ").map((n) => n[0]).filter(Boolean).slice(0, 2).join("").toUpperCase();
}
return (
@@ -254,17 +254,17 @@ function PartyCard({ label, party, accent }: { label: string; party: any; accent
</div>
<div className="flex items-center gap-4">
<div className={`relative h-14 w-14 shrink-0 overflow-hidden rounded-xl border border-border bg-background ring-1 ${accent}`}>
{party.photo ?
<LazyImage src={party.photo} alt={party.name} className="h-full w-full object-contain" />
{party?.photo ?
<LazyImage src={party.photo} alt={party?.name} className="h-full w-full object-contain" />
:
<div className="flex h-full w-full items-center justify-center text-base font-bold text-foreground/80">
{initials(party.name)}
{initials(party?.name)}
</div>
}
</div>
<div className="min-w-0 flex-1">
<p className="truncate text-base font-semibold text-foreground">{party.name}</p>
<p className="mt-0.5 flex items-center truncate text-xs text-muted-foreground">{party.description ?? 'No description available.'}</p>
<p className="truncate text-base font-semibold text-foreground">{party?.name}</p>
<p className="mt-0.5 flex items-center truncate text-xs text-muted-foreground">{party?.description ?? 'No description available.'}</p>
</div>
</div>
</Card900>
@@ -15,7 +15,7 @@ interface Props {
const AssignedPeople = ({ people, defaultOpen = true }: Props) => {
const [isOpen, setIsOpen] = useState(defaultOpen);
if (people.length === 0) {
if (people?.length === 0) {
return (
<div className="text-center py-8 text-sm text-muted-foreground bg-muted/40 rounded-lg border border-dashed border-border mt-5">
No team assigned yet
@@ -29,17 +29,17 @@ const AssignedPeople = ({ people, defaultOpen = true }: Props) => {
<div className='flex items-center'>
<div className="flex items-center gap-3">
<div className="flex -space-x-3">
{people.slice(0, 5).map((user, i) =>
{people?.slice(0, 5).map((user, i) =>
<LazyImage key={i} src={user?.user.avatar ? `${process.env.BASE_URL_IMG}/${user?.user.avatar}` : '/assets/no-avatar.jpg'} alt={user?.user.fullname!} className="w-10 h-10 rounded-full ring-2 ring-background hover:scale-110 hover:z-10 transition-transform" />
)}
{people.length > 5 &&
{people?.length > 5 &&
<div className="w-10 h-10 rounded-full bg-muted ring-2 ring-background flex items-center justify-center text-xs font-bold text-muted-foreground shadow-md">
+{people.length - 5}
+{people?.length - 5}
</div>
}
</div>
<div className="text-sm">
<p className="font-semibold text-foreground">{people.length} People</p>
<p className="font-semibold text-foreground">{people?.length} People</p>
<p className="text-muted-foreground">assigned to this work order</p>
</div>
</div>