2026-04-21 10:41:01 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
import { io } from "socket.io-client";
|
|
|
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
|
|
|
import {
|
|
|
|
|
getListNotificationsQueryKey,
|
|
|
|
|
getGetHomeStatsQueryKey,
|
2026-04-21 18:47:30 +00:00
|
|
|
getListMyServiceOrdersQueryKey,
|
2026-04-21 18:58:19 +00:00
|
|
|
getListIncomingServiceOrdersQueryKey,
|
2026-04-22 10:20:04 +00:00
|
|
|
getListAppsQueryKey,
|
|
|
|
|
getGetMeQueryKey,
|
2026-04-21 10:41:01 +00:00
|
|
|
} from "@workspace/api-client-react";
|
|
|
|
|
import { useAuth } from "@/contexts/AuthContext";
|
|
|
|
|
|
|
|
|
|
const BASE = import.meta.env.BASE_URL.replace(/\/$/, "");
|
|
|
|
|
|
|
|
|
|
export function useNotificationsSocket() {
|
|
|
|
|
const { user } = useAuth();
|
|
|
|
|
const queryClient = useQueryClient();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!user) return;
|
|
|
|
|
|
|
|
|
|
const socket = io(window.location.origin, {
|
|
|
|
|
path: `${BASE}/api/socket.io`,
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-21 18:47:30 +00:00
|
|
|
socket.on("notification_created", (payload?: { type?: string }) => {
|
2026-04-21 10:41:01 +00:00
|
|
|
queryClient.invalidateQueries({ queryKey: getListNotificationsQueryKey() });
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: getGetHomeStatsQueryKey() });
|
2026-04-21 18:47:30 +00:00
|
|
|
if (payload?.type === "order") {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListMyServiceOrdersQueryKey(),
|
|
|
|
|
});
|
2026-04-21 18:58:19 +00:00
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListIncomingServiceOrdersQueryKey(),
|
|
|
|
|
});
|
2026-04-21 18:47:30 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on("order_updated", () => {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListMyServiceOrdersQueryKey(),
|
|
|
|
|
});
|
2026-04-21 18:58:19 +00:00
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListIncomingServiceOrdersQueryKey(),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-22 06:37:01 +00:00
|
|
|
socket.on("order_deleted", () => {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListMyServiceOrdersQueryKey(),
|
|
|
|
|
});
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListIncomingServiceOrdersQueryKey(),
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-21 18:58:19 +00:00
|
|
|
socket.on("order_incoming_changed", () => {
|
|
|
|
|
queryClient.invalidateQueries({
|
|
|
|
|
queryKey: getListIncomingServiceOrdersQueryKey(),
|
|
|
|
|
});
|
2026-04-21 10:41:01 +00:00
|
|
|
});
|
|
|
|
|
|
2026-04-22 10:20:04 +00:00
|
|
|
socket.on("apps_changed", () => {
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: getListAppsQueryKey() });
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: getGetMeQueryKey() });
|
|
|
|
|
});
|
|
|
|
|
|
2026-04-21 10:41:01 +00:00
|
|
|
return () => {
|
|
|
|
|
socket.disconnect();
|
|
|
|
|
};
|
|
|
|
|
}, [user, queryClient]);
|
|
|
|
|
}
|