Skip to content

Commit e97c1ef

Browse files
committed
tighten listing utility fallbacks
1 parent 137a9bf commit e97c1ef

2 files changed

Lines changed: 47 additions & 17 deletions

File tree

src/utils/listingUtils.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@ type GenerateListingMetadataOptions = {
3434
includeFullMetadata?: boolean;
3535
};
3636

37+
function normaliseListingType(
38+
listingType: ListingLike["type"]
39+
): ListingType | null {
40+
if (
41+
listingType === "business" ||
42+
listingType === "community" ||
43+
listingType === "residential"
44+
) {
45+
return listingType;
46+
}
47+
48+
return null;
49+
}
50+
3751
function compactTextParts(parts: Array<string | null | undefined>) {
3852
return parts
3953
.map((part) => part?.trim())
@@ -46,12 +60,14 @@ export function getListingDisplayName(
4660
) {
4761
if (!listing) return "";
4862

49-
if (listing.type === "residential") {
63+
const listingType = normaliseListingType(listing.type);
64+
65+
if (listingType === "residential") {
5066
if (!user) return "Private Host";
5167
return listing.owner_first_name || "Private Host";
5268
}
5369

54-
return listing.name || "";
70+
return listing.name || "Listing";
5571
}
5672

5773
export function getListingAvatar(
@@ -60,17 +76,20 @@ export function getListingAvatar(
6076
): AvatarDescriptor {
6177
if (!listing) return null;
6278

79+
const listingType = normaliseListingType(listing.type);
80+
const listingDisplayName = getListingDisplayName(listing, user);
81+
6382
if (listing.is_demo) {
6483
const demoAvatarFilename = listing.avatar?.split("/").pop();
6584

6685
return {
6786
isDemo: true,
6887
path: `/avatars/demo/${demoAvatarFilename}`,
69-
alt: `${listing.name}’s avatar`,
88+
alt: `${listingDisplayName} avatar`,
7089
};
7190
}
7291

73-
if (listing.type === "residential") {
92+
if (listingType === "residential") {
7493
if (!user) {
7594
return {
7695
bucket: "public",
@@ -82,21 +101,21 @@ export function getListingAvatar(
82101
return {
83102
bucket: "avatars",
84103
filename: listing.owner_avatar || null,
85-
alt: `${listing.owner_first_name}’s avatar`,
104+
alt: `${listing.owner_first_name || "Private Host"} avatar`,
86105
};
87106
}
88107

89108
if (!listing.avatar) {
90109
return {
91-
path: `/avatars/default/${listing.type}.png`,
92-
alt: `${listing.name}’s avatar`,
110+
path: `/avatars/default/${listingType || "community"}.png`,
111+
alt: `${listingDisplayName} avatar`,
93112
};
94113
}
95114

96115
return {
97116
bucket: "listing_avatars",
98117
filename: listing.avatar || null,
99-
alt: `${listing.name}’s avatar`,
118+
alt: `${listingDisplayName} avatar`,
100119
};
101120
}
102121

@@ -108,7 +127,7 @@ export function getListingOwnerAvatar(
108127
return {
109128
bucket: "avatars",
110129
filename: listing.owner_avatar || null,
111-
alt: `${listing.owner_first_name}’s avatar`,
130+
alt: `${listing.owner_first_name || "Listing owner"} avatar`,
112131
};
113132
}
114133

@@ -125,15 +144,21 @@ export function getProfileAvatar(profileId: string | null | undefined) {
125144
export function getListingDisplayType(listing: ListingLike | null | undefined) {
126145
if (!listing) return "";
127146

128-
if (listing.type === "residential") {
147+
const listingType = normaliseListingType(listing.type);
148+
149+
if (listingType === "residential") {
129150
return "Local resident";
130151
}
131152

132-
if (listing.type === "community") {
153+
if (listingType === "community") {
133154
return "Community spot";
134155
}
135156

136-
return `Local ${listing.type}`;
157+
if (listingType === "business") {
158+
return "Local business";
159+
}
160+
161+
return "Local listing";
137162
}
138163

139164
export function generateListingMetadata(
@@ -148,6 +173,7 @@ export function generateListingMetadata(
148173
}
149174

150175
const listingDisplayName = getListingDisplayName(listing, user);
176+
const listingType = normaliseListingType(listing.type);
151177
const listingCountryName = countries.find(
152178
(country) => country.code === listing.country_code
153179
)?.name;
@@ -157,19 +183,21 @@ export function generateListingMetadata(
157183
]);
158184
const listingFullLocation = listingLocationParts.join(", ");
159185
const listingBaseDescriptor =
160-
listing.type === "residential"
186+
listingType === "residential"
161187
? `${listingDisplayName} is a local resident`
162-
: `${listingDisplayName} is a ${listing.type}`;
188+
: listingType
189+
? `${listingDisplayName} is a ${listingType}`
190+
: `${listingDisplayName} is a local listing`;
163191
const listingDescriptionParts = [
164192
listingBaseDescriptor,
165193
listingFullLocation ? `based in ${listingFullLocation}.` : null,
166-
listing.type === "residential"
194+
listingType === "residential"
167195
? null
168196
: listing.description?.trim()
169197
? `${listing.description.trim()}`
170198
: null,
171199
`Connect with ${
172-
listing.type === "residential"
200+
listingType === "residential"
173201
? "them"
174202
: listing.name || listingDisplayName
175203
} on ${siteConfig.name}, ${siteConfig.meta.explainer}.`,

src/utils/mediaUtils.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,14 @@ export async function deleteListingPhoto(
130130
if (storageError) throw storageError;
131131

132132
if (listingSlug) {
133-
const { data: listing } = await supabase
133+
const { data: listing, error: fetchError } = await supabase
134134
.from("listings")
135135
.select("photos")
136136
.eq("slug", listingSlug)
137137
.single();
138138

139+
if (fetchError) throw fetchError;
140+
139141
const updatedPhotos = (
140142
(listing as ListingPhotoRecord | null)?.photos ?? []
141143
).filter((photo) => photo !== filePath);

0 commit comments

Comments
 (0)