Updated views component and supabase client

This commit is contained in:
r-freeman 2023-04-10 23:25:29 +01:00
parent ec833fbe22
commit d5c320c88a
2 changed files with 8 additions and 4 deletions

View File

@ -16,6 +16,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
const [views, setViews] = useState(0) const [views, setViews] = useState(0)
useEffect(() => { useEffect(() => {
// subscribe to view updates at the row level
const sub = supabase const sub = supabase
.channel('any') .channel('any')
.on('postgres_changes', { .on('postgres_changes', {
@ -26,7 +27,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
}, payload => { }, payload => {
setViews(payload.new.views) setViews(payload.new.views)
}) })
.subscribe(); .subscribe()
return () => { return () => {
sub.unsubscribe() sub.unsubscribe()
@ -51,7 +52,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
method: 'POST' method: 'POST'
} }
) )
}; }
registerView() registerView()
} }

View File

@ -1,6 +1,9 @@
import {createClient} from '@supabase/supabase-js' import {createClient} from '@supabase/supabase-js'
const NEXT_PUBLIC_SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL ?? ""
const NEXT_PUBLIC_SUPABASE_ANON_KEY = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? ""
export const supabase = createClient( export const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL ?? "", NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY ?? "" NEXT_PUBLIC_SUPABASE_ANON_KEY
) )