mirror of
https://github.com/domfelipe/mika-agent-assist.git
synced 2026-08-07 12:36:50 +00:00
Adicionou realtime na tabela
X-Lovable-Edit-ID: edt-8ba62592-9cb5-47f0-a37a-42480f0c393b Co-authored-by: domfelipe <53182096+domfelipe@users.noreply.github.com>
This commit is contained in:
commit
82d0cb0bfe
3 changed files with 48 additions and 0 deletions
42
src/hooks/use-subscription-realtime.ts
Normal file
42
src/hooks/use-subscription-realtime.ts
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { supabase } from "@/integrations/supabase/client";
|
||||||
|
import { useAuth } from "@/hooks/use-auth";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribes to realtime changes on the user's subscription row.
|
||||||
|
* When the payments webhook upserts a subscription, the affected
|
||||||
|
* queries are invalidated so the UI updates without a page reload.
|
||||||
|
*/
|
||||||
|
export function useSubscriptionRealtime() {
|
||||||
|
const { user } = useAuth();
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!user?.id) return;
|
||||||
|
|
||||||
|
const channel = supabase
|
||||||
|
.channel(`subscriptions:user:${user.id}`)
|
||||||
|
.on(
|
||||||
|
"postgres_changes",
|
||||||
|
{
|
||||||
|
event: "*",
|
||||||
|
schema: "public",
|
||||||
|
table: "subscriptions",
|
||||||
|
filter: `user_id=eq.${user.id}`,
|
||||||
|
},
|
||||||
|
() => {
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["subscription", user.id] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["has-active-subscription", user.id] });
|
||||||
|
queryClient.invalidateQueries({ queryKey: ["plan"] });
|
||||||
|
},
|
||||||
|
)
|
||||||
|
.subscribe();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
supabase.removeChannel(channel);
|
||||||
|
};
|
||||||
|
}, [user?.id, queryClient]);
|
||||||
|
}
|
||||||
|
|
@ -17,6 +17,7 @@ import { useQuery } from "@tanstack/react-query";
|
||||||
import { supabase } from "@/integrations/supabase/client";
|
import { supabase } from "@/integrations/supabase/client";
|
||||||
import { useAuth } from "@/hooks/use-auth";
|
import { useAuth } from "@/hooks/use-auth";
|
||||||
import { useProfile } from "@/hooks/use-profile";
|
import { useProfile } from "@/hooks/use-profile";
|
||||||
|
import { useSubscriptionRealtime } from "@/hooks/use-subscription-realtime";
|
||||||
|
|
||||||
const PADDLE_ENV = (import.meta.env.VITE_PADDLE_ENVIRONMENT as string) === "production" ? "live" : "sandbox";
|
const PADDLE_ENV = (import.meta.env.VITE_PADDLE_ENVIRONMENT as string) === "production" ? "live" : "sandbox";
|
||||||
import { Logo } from "@/components/mika/Logo";
|
import { Logo } from "@/components/mika/Logo";
|
||||||
|
|
@ -83,6 +84,9 @@ function PainelLayout() {
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [mobileOpen, setMobileOpen] = useState(false);
|
const [mobileOpen, setMobileOpen] = useState(false);
|
||||||
|
|
||||||
|
// Atualiza queries quando o webhook gravar/alterar a assinatura
|
||||||
|
useSubscriptionRealtime();
|
||||||
|
|
||||||
// Faturamento permanece acessível mesmo sem assinatura ativa
|
// Faturamento permanece acessível mesmo sem assinatura ativa
|
||||||
// (para que o usuário possa ver o estado e assinar / gerenciar).
|
// (para que o usuário possa ver o estado e assinar / gerenciar).
|
||||||
const skipSubGuard = location.pathname.startsWith("/painel/faturamento");
|
const skipSubGuard = location.pathname.startsWith("/painel/faturamento");
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
ALTER TABLE public.subscriptions REPLICA IDENTITY FULL;
|
||||||
|
ALTER PUBLICATION supabase_realtime ADD TABLE public.subscriptions;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue