"use client"; import { useShallowEffect } from "@mantine/hooks"; import { useAtom } from "jotai"; import { WibuRealtime } from "wibu-pkg"; import { gs_admin_ntf, gs_adminEvent_triggerReview, gs_adminJob_triggerReview, gs_adminVoting_triggerReview, gs_eventTriggerBeranda, gs_jobTiggerBeranda, gs_realtimeData, gs_user_ntf, gs_votingTiggerBeranda, IRealtimeData, } from "./global_state"; // const WIBU_REALTIME_TOKEN: string | undefined = // process.env.NEXT_PUBLIC_WIBU_REALTIME_TOKEN; // Pasang tipe data di package WibuRealtime sebagai type adata setData export type TypeNotification = { type: "message" | "notification" | "trigger"; pushNotificationTo: "ADMIN" | "USER"; dataMessage?: IRealtimeData; userLoginId?: string; }; export default function RealtimeProvider({ userLoginId, WIBU_REALTIME_TOKEN, }: { userLoginId: string; WIBU_REALTIME_TOKEN: string; }) { const [dataRealtime, setDataRealtime] = useAtom(gs_realtimeData); const [newAdminNtf, setNewAdminNtf] = useAtom(gs_admin_ntf); const [newUserNtf, setNewUserNtf] = useAtom(gs_user_ntf); // JOB const [isTriggerJobBeranda, setIsTriggerJobBeranda] = useAtom(gs_jobTiggerBeranda); const [isAdminJob_TriggerReview, setIsAdminJob_TriggerReview] = useAtom( gs_adminJob_triggerReview ); // EVENT const [isTriggerEventBeranda, setIsTriggerEventBeranda] = useAtom( gs_eventTriggerBeranda ); const [isAdminEvent_TriggerReview, setIsAdminEvent_TriggerReview] = useAtom( gs_adminEvent_triggerReview ); // VOTING const [isTriggerVotingBeranda, setIsTriggerVotingBeranda] = useAtom( gs_votingTiggerBeranda ); const [isAdminVoting_TriggerReview, setIsAdminVoting_TriggerReview] = useAtom( gs_adminVoting_triggerReview ); useShallowEffect(() => { // if (WIBU_REALTIME_TOKEN === undefined) return alert("gak dapet key"); try { WibuRealtime.init({ project: "hipmi", WIBU_REALTIME_TOKEN: WIBU_REALTIME_TOKEN, onData(data: TypeNotification) { if ( data.type == "notification" && data.pushNotificationTo == "ADMIN" ) { setNewAdminNtf((e) => e + 1); } // Notifikasi ke semua user , yang datanya di acc admin if ( data.type == "notification" && data.pushNotificationTo == "USER" && data.dataMessage?.userId == userLoginId ) { setNewUserNtf((e) => e + 1); setDataRealtime(data.dataMessage as any); } // ---------------------- JOB ------------------------- // if ( data.type == "trigger" && data.pushNotificationTo == "ADMIN" && data.dataMessage?.kategoriApp == "JOB" ) { setIsAdminJob_TriggerReview(true); } if ( data.type == "trigger" && data.pushNotificationTo == "USER" && data.dataMessage?.kategoriApp == "JOB" && data.dataMessage.status == "Publish" ) { setIsTriggerJobBeranda(true); } // ---------------------- JOB ------------------------- // // ---------------------- EVENT ------------------------- // if ( data.type == "trigger" && data.pushNotificationTo == "ADMIN" && data.dataMessage?.kategoriApp == "EVENT" ) { setIsAdminEvent_TriggerReview(true); } if ( data.type == "trigger" && data.pushNotificationTo == "USER" && data.dataMessage?.kategoriApp == "EVENT" && data.dataMessage.status == "Publish" ) { setIsTriggerEventBeranda(true); } if ( data.type == "notification" && data.pushNotificationTo == "USER" && data.dataMessage?.status == "Peserta Event" && userLoginId !== data.dataMessage?.userId ) { setNewUserNtf((e) => e + 1); } // ---------------------- EVENT ------------------------- // // ---------------------- VOTING ------------------------- // if ( data.type == "trigger" && data.pushNotificationTo == "ADMIN" && data.dataMessage?.kategoriApp == "VOTING" ) { setIsAdminVoting_TriggerReview(true); } if ( data.type == "trigger" && data.pushNotificationTo == "USER" && data.dataMessage?.kategoriApp == "VOTING" && data.dataMessage.status == "Publish" ) { setIsTriggerVotingBeranda(true); } if ( data.type == "notification" && data.pushNotificationTo == "USER" && data.dataMessage?.status == "Voting Masuk" && userLoginId !== data.dataMessage?.userId ) { setNewUserNtf((e) => e + 1); } // ---------------------- VOTING ------------------------- // }, }); } catch (error) { console.log(error); } }, []); return null; }