Skip to content

Commit a7e114a

Browse files
committed
Adds the profile list
1 parent 685b1ed commit a7e114a

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

example/next-storage/components/ProfileList.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,27 @@ import { Subscription, SupabaseRealtimePayload } from '@supabase/supabase-js'
44
import { supabase } from '../lib/api'
55
import { useState, useEffect } from 'react'
66

7+
var realtimeProfiles: Subscription | null = null
8+
79
export default function ProfileList() {
810
const [profiles, setProfiles] = useState<Profile[]>([])
911

1012
useEffect(() => {
1113
getPublicProfiles()
1214

13-
const realtimeProfiles = supabase
15+
realtimeProfiles = supabase
1416
.from('profiles')
15-
.on('*', (payload: SupabaseRealtimePayload<Profile>) => profileUpdated(payload.new))
17+
.on('*', (payload: SupabaseRealtimePayload<Profile>) => profileUpdated(profiles, payload.new))
1618
.subscribe()
1719

1820
return () => {
19-
supabase.removeSubscription(realtimeProfiles)
21+
if (realtimeProfiles) supabase.removeSubscription(realtimeProfiles)
2022
}
2123
}, [])
2224

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])
2728
}
2829

2930
async function getPublicProfiles() {

0 commit comments

Comments
 (0)