From 20f25a94f213f03a169f9dec898fdb53c5cf97d7 Mon Sep 17 00:00:00 2001 From: Ezra Khairan Permana Date: Mon, 8 Jul 2024 23:51:14 +0700 Subject: [PATCH] feat: menambahkan ke semua lini yang error --- .../src/context/participant-context.tsx | 24 +++++++++++++++++++ .../chooser/src/context/server-setting.tsx | 19 +++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/apps/clients/chooser/src/context/participant-context.tsx b/apps/clients/chooser/src/context/participant-context.tsx index b9a0fc1..e04b50b 100644 --- a/apps/clients/chooser/src/context/participant-context.tsx +++ b/apps/clients/chooser/src/context/participant-context.tsx @@ -4,11 +4,13 @@ import { useContext, useMemo, useState, + useEffect } from "react"; import { UniversalError } from "@/components/universal-error"; import { api } from "@/utils/api"; import { motion } from "framer-motion"; import { Navigate } from "react-router-dom"; +import { useKeyboardWebsocket } from "./keyboard-websocket"; export interface IParticipantContext { name: string | null; @@ -27,6 +29,8 @@ export const ParticipantProvider = ({ }: { children: React.ReactNode; }) => { + const { wsEnabled, lastMessage } = useKeyboardWebsocket(); + const [qrId, setQrId] = useState(null); const [votedSuccessfully, setVoted] = useState(false); @@ -44,6 +48,26 @@ export const ParticipantProvider = ({ [], ); + useEffect(() => { + if (wsEnabled && lastMessage) { + // Precheck before consuming command + if (lastMessage.data.startsWith("SORA-KEYBIND-")) { + const actualCommand = lastMessage.data.replace("SORA-KEYBIND-", ""); + + switch (actualCommand) { + case "RELOAD": { + if (!!qrId && + participantQuery.isFetched && ( + !participantQuery.data?.alreadyAttended || participantQuery.data?.alreadyChoosing + )) location.reload(); + + break; + } + } + } + } + }, [qrId, participantQuery.isFetched, participantQuery.data, wsEnabled, lastMessage]); + const propsValue = useMemo(() => { if (!qrId) return { diff --git a/apps/clients/chooser/src/context/server-setting.tsx b/apps/clients/chooser/src/context/server-setting.tsx index 333f956..e342ecc 100644 --- a/apps/clients/chooser/src/context/server-setting.tsx +++ b/apps/clients/chooser/src/context/server-setting.tsx @@ -5,6 +5,7 @@ import { motion } from "framer-motion"; import { Loader } from "lucide-react"; import { useParticipant } from "./participant-context"; +import { useKeyboardWebsocket } from "./keyboard-websocket"; interface ISettingContext { canVote: boolean; @@ -19,6 +20,7 @@ export const ServerSettingProvider = ({ }: { children: React.ReactNode; }) => { + const { wsEnabled, lastMessage } = useKeyboardWebsocket(); const { qrId, setQRCode } = useParticipant(); const [errorMessage, setErrorMessage] = useState(""); @@ -28,6 +30,23 @@ export const ServerSettingProvider = ({ refetchIntervalInBackground: true, }); + useEffect(() => { + if (wsEnabled && lastMessage) { + // Precheck before consuming command + if (lastMessage.data.startsWith("SORA-KEYBIND-")) { + const actualCommand = lastMessage.data.replace("SORA-KEYBIND-", ""); + + switch (actualCommand) { + case "RELOAD": { + if (settingsQuery.errorUpdateCount > 0) location.reload(); + + break; + } + } + } + } + }, [settingsQuery.errorUpdateCount, wsEnabled, lastMessage]); + useEffect(() => { if (settingsQuery.error) setErrorMessage(settingsQuery.error.message); }, [settingsQuery.error]);