Skip to content

Commit

Permalink
feat: menambahkan ke semua lini yang error
Browse files Browse the repository at this point in the history
  • Loading branch information
reacto11mecha committed Jul 8, 2024
1 parent d12d454 commit 20f25a9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
24 changes: 24 additions & 0 deletions apps/clients/chooser/src/context/participant-context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +29,8 @@ export const ParticipantProvider = ({
}: {
children: React.ReactNode;
}) => {
const { wsEnabled, lastMessage } = useKeyboardWebsocket();

const [qrId, setQrId] = useState<string | null>(null);
const [votedSuccessfully, setVoted] = useState(false);

Expand All @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions apps/clients/chooser/src/context/server-setting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,7 @@ export const ServerSettingProvider = ({
}: {
children: React.ReactNode;
}) => {
const { wsEnabled, lastMessage } = useKeyboardWebsocket();
const { qrId, setQRCode } = useParticipant();

const [errorMessage, setErrorMessage] = useState("");
Expand All @@ -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]);
Expand Down

0 comments on commit 20f25a9

Please sign in to comment.