Last active
September 14, 2024 13:22
-
-
Save bidah/cc77238fd863f202ed09a7a3d7145fbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { supabase } from "@/lib/supabase"; | |
import { useAuth } from "@/services/auth/useAuth"; | |
import { useEffect } from "react"; | |
import { trpc } from "@/utils/trpc"; | |
export function useRealtime() { | |
// your auth setup to get current user id | |
const { session } = useAuth(); | |
const userId = session?.user.id; | |
const utils = trpc.useUtils(); | |
useEffect(() => { | |
if (!userId) return; | |
const channel = supabase | |
.channel("done-capsules") | |
.on( | |
"postgres_changes", | |
{ | |
event: "*", | |
schema: "public", | |
table: "table_name", | |
filter: `user_id=eq.${userId}`, | |
}, | |
(payload) => { | |
// Invalidate the getTranscript query for the updated transcript | |
console.log("-> realtime triggered"); | |
// this is a sample replace `myQuery` with your own | |
utils.myQuery.invalidate(); | |
}, | |
) | |
.subscribe(); | |
return () => { | |
supabase.removeChannel(channel); | |
}; | |
}, [userId, utils]); | |
} | |
export default function RealtimeProvider({ | |
children, | |
}: { | |
children: React.ReactNode; | |
}) { | |
useRealtime(); | |
return <>{children}</>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment