33 useState ,
44 useEffect ,
55 useContext ,
6+ useRef ,
67 ReactNode ,
78} from "react" ;
89import { useAuth } from "../hooks/useAuth" ;
@@ -33,12 +34,6 @@ interface ChatContextType {
3334
3435const ChatContext = createContext < ChatContextType | null > ( null ) ;
3536
36- /* -------------------------------------------------------------------------- */
37- /* Storage key */
38- /* -------------------------------------------------------------------------- */
39-
40- const CHAT_STORAGE_KEY = "brocode_chat_messages" ;
41-
4237/* -------------------------------------------------------------------------- */
4338/* Provider */
4439/* -------------------------------------------------------------------------- */
@@ -55,6 +50,16 @@ export function ChatProvider({ children }: { children: ReactNode }) {
5550 const [ unreadCount , setUnreadCount ] = useState ( 0 ) ;
5651 const [ isChatActive , setIsChatActive ] = useState ( false ) ;
5752 const [ loading , setLoading ] = useState ( true ) ;
53+ const isChatActiveRef = useRef ( false ) ;
54+ const currentUserIdsRef = useRef < string [ ] > ( [ ] ) ;
55+
56+ useEffect ( ( ) => {
57+ isChatActiveRef . current = isChatActive ;
58+ } , [ isChatActive ] ) ;
59+
60+ useEffect ( ( ) => {
61+ currentUserIdsRef . current = [ user ?. id , profile ?. id ] . filter ( Boolean ) as string [ ] ;
62+ } , [ user ?. id , profile ?. id ] ) ;
5863
5964 /* ------------------------------------------------------------------------ */
6065 /* Load messages */
@@ -137,7 +142,19 @@ export function ChatProvider({ children }: { children: ReactNode }) {
137142 profile_pic_url : newMsg . profiles ?. profile_pic_url || 'https://api.dicebear.com/7.x/thumbs/svg?seed=default' ,
138143 } ,
139144 } ;
140- setMessages ( ( prev ) => [ ...prev , chatMessage ] ) ;
145+
146+ setMessages ( ( prev ) => {
147+ if ( prev . some ( ( existing ) => existing . id === chatMessage . id ) ) {
148+ return prev ;
149+ }
150+
151+ return [ ...prev , chatMessage ] ;
152+ } ) ;
153+
154+ const isOwnMessage = currentUserIdsRef . current . includes ( chatMessage . user_id ) ;
155+ if ( ! isOwnMessage && ! isChatActiveRef . current ) {
156+ setUnreadCount ( ( prev ) => prev + 1 ) ;
157+ }
141158 }
142159 } else if ( payload . eventType === 'DELETE' ) {
143160 setMessages ( ( prev ) => prev . filter ( ( m ) => m . id !== payload . old . id ) ) ;
@@ -151,18 +168,6 @@ export function ChatProvider({ children }: { children: ReactNode }) {
151168 } ;
152169 } , [ ] ) ;
153170
154- // Removed localStorage persistence - using Supabase now
155-
156- /* ------------------------------------------------------------------------ */
157- /* Unread count logic */
158- /* ------------------------------------------------------------------------ */
159-
160- useEffect ( ( ) => {
161- if ( ! isChatActive && messages . length > 0 ) {
162- setUnreadCount ( ( prev ) => prev + 1 ) ;
163- }
164- } , [ messages , isChatActive ] ) ;
165-
166171 const setChatActive = ( isActive : boolean ) => {
167172 setIsChatActive ( isActive ) ;
168173 if ( isActive ) setUnreadCount ( 0 ) ;
@@ -298,22 +303,9 @@ export function ChatProvider({ children }: { children: ReactNode }) {
298303
299304 if ( error ) throw error ;
300305
301- // Message will be added via real-time subscription
302- // But we can also add it immediately for better UX
303- if ( data ) {
304- const chatMessage : ChatMessage = {
305- id : data . id ,
306- user_id : data . user_id ,
307- content_text : data . content_text ,
308- content_image_urls : data . content_image_urls || [ ] ,
309- created_at : data . created_at ,
310- reactions : data . reactions || { } ,
311- profiles : {
312- name : data . profiles ?. name || profile ?. name || 'Unknown' ,
313- profile_pic_url : data . profiles ?. profile_pic_url || profile ?. profile_pic_url || 'https://api.dicebear.com/7.x/thumbs/svg?seed=default' ,
314- } ,
315- } ;
316- setMessages ( ( prev ) => [ ...prev , chatMessage ] ) ;
306+ // Message is added via real-time subscription to avoid duplicates.
307+ if ( ! data ) {
308+ throw new Error ( 'Failed to create chat message' ) ;
317309 }
318310
319311 if ( profile ) {
0 commit comments