@@ -4,26 +4,27 @@ import { Subscription, SupabaseRealtimePayload } from '@supabase/supabase-js'
4
4
import { supabase } from '../lib/api'
5
5
import { useState , useEffect } from 'react'
6
6
7
+ var realtimeProfiles : Subscription | null = null
8
+
7
9
export default function ProfileList ( ) {
8
10
const [ profiles , setProfiles ] = useState < Profile [ ] > ( [ ] )
9
11
10
12
useEffect ( ( ) => {
11
13
getPublicProfiles ( )
12
14
13
- const realtimeProfiles = supabase
15
+ realtimeProfiles = supabase
14
16
. from ( 'profiles' )
15
- . on ( '*' , ( payload : SupabaseRealtimePayload < Profile > ) => profileUpdated ( payload . new ) )
17
+ . on ( '*' , ( payload : SupabaseRealtimePayload < Profile > ) => profileUpdated ( profiles , payload . new ) )
16
18
. subscribe ( )
17
19
18
20
return ( ) => {
19
- supabase . removeSubscription ( realtimeProfiles )
21
+ if ( realtimeProfiles ) supabase . removeSubscription ( realtimeProfiles )
20
22
}
21
23
} , [ ] )
22
24
23
- function profileUpdated ( profile : Profile ) {
24
- const otherProfiles = profiles ?. filter ( ( x ) => x . id != profile . id )
25
- console . log ( 'otherProfiles' , otherProfiles )
26
- setProfiles ( [ profile , ...otherProfiles ] )
25
+ function profileUpdated ( profileList : Profile [ ] , updated : Profile ) {
26
+ const otherProfiles = profileList . filter ( ( x ) => x . id != updated . id )
27
+ setProfiles ( [ updated , ...otherProfiles ] )
27
28
}
28
29
29
30
async function getPublicProfiles ( ) {
0 commit comments