Skip to content

Commit d4e70e0

Browse files
committed
refine chat header and unread tracking
1 parent e97c1ef commit d4e70e0

4 files changed

Lines changed: 22 additions & 21 deletions

File tree

src/components/AvatarPair/AvatarPair.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
getListingAvatar,
33
getListingOwnerAvatar,
4-
getProfileAvatar,
4+
getProfileAvatarSource,
55
} from "@/utils/listingUtils";
66

77
import Avatar from "@/components/Avatar";
@@ -107,7 +107,7 @@ export default function AvatarPair({
107107
}: AvatarPairProps) {
108108
const avatarSource =
109109
role === "owner"
110-
? (getProfileAvatar(profile?.avatar) as AvatarSource)
110+
? (getProfileAvatarSource(profile?.avatar) as AvatarSource)
111111
: (getListingAvatar(listing, user) as AvatarSource);
112112

113113
const primaryAvatarProps = toAvatarComponentProps(avatarSource);

src/components/ChatHeader/ChatHeader.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type ChatRole = "initiator" | "owner";
115115
function getChatRole(
116116
thread: ChatThreadRecord | null | undefined,
117117
user: ChatUser | null | undefined,
118+
listing: ChatListing,
118119
isDemo: boolean
119120
): ChatRole {
120121
if (isDemo) {
@@ -125,6 +126,14 @@ function getChatRole(
125126
return "initiator";
126127
}
127128

129+
if (!thread) {
130+
if (listing.owner_id && listing.owner_id === user?.id) {
131+
return "owner";
132+
}
133+
134+
return "initiator";
135+
}
136+
128137
return "owner";
129138
}
130139

@@ -156,7 +165,7 @@ function ChatHeader({
156165
const t = useTranslations();
157166
const router = useRouter();
158167

159-
const role = getChatRole(thread, user, isDemo);
168+
const role = getChatRole(thread, user, listing, isDemo);
160169

161170
const otherPersonName =
162171
role === "initiator"

src/contexts/UnreadMessagesContext.tsx

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createContext, useContext, useEffect, useMemo, useState } from "react";
44
import { createClient } from "@/utils/supabase/client";
5+
import { usePathname } from "next/navigation";
56
import type { Dispatch, PropsWithChildren, SetStateAction } from "react";
67

78
type ThreadReadStatus = Record<string, boolean>;
@@ -40,6 +41,7 @@ export function UnreadMessagesProvider({ children }: PropsWithChildren) {
4041
);
4142
const [hasViewedChats, setHasViewedChats] = useState(false);
4243
const supabase = useMemo(() => createClient(), []);
44+
const pathname = usePathname();
4345

4446
useEffect(() => {
4547
let isActive = true;
@@ -80,22 +82,10 @@ export function UnreadMessagesProvider({ children }: PropsWithChildren) {
8082
}, [supabase]);
8183

8284
useEffect(() => {
83-
function handleRouteChange() {
84-
if (window.location.pathname === "/chats") {
85-
setHasViewedChats(true);
86-
}
85+
if (pathname === "/chats") {
86+
setHasViewedChats(true);
8787
}
88-
89-
handleRouteChange();
90-
91-
const observer = new MutationObserver(handleRouteChange);
92-
observer.observe(document.body, {
93-
childList: true,
94-
subtree: true,
95-
});
96-
97-
return () => observer.disconnect();
98-
}, []);
88+
}, [pathname]);
9989

10090
useEffect(() => {
10191
if (isAuthDebugEnabled) {

src/utils/listingUtils.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ export function getListingOwnerAvatar(
131131
};
132132
}
133133

134-
export function getProfileAvatar(profileId: string | null | undefined) {
135-
if (!profileId) return null;
134+
export function getProfileAvatarSource(
135+
avatarFilename: string | null | undefined
136+
) {
137+
if (!avatarFilename) return null;
136138

137139
return {
138140
bucket: "avatars",
139-
filename: profileId,
141+
filename: avatarFilename,
140142
alt: "The avatar for this profile",
141143
};
142144
}

0 commit comments

Comments
 (0)