import {
EventMessage,
EventType,
PingMessage,
PongMessage,
PongMessageKindEnum,
StatsMessage,
StatusApi,
SubscribeMessage,
SubscribeMessageKindEnum,
TaskStatusResponse,
UserWalletBalance,
UserWalletBalanceResponse,
UserLedgerWallets,
UserLedgerWalletAccountTypeEnum,
UserDisplayedWalletBalance,
WalletApi,
CashoutMethodsResponse,
} from "@/api_fsb";
import Footer from "@/components/Footer";
import { useAppDispatch } from "@/hooks";
import { bpid, routeBasename, tagManager } from "@/lib/utils";
import { useGetMeQuery } from "@/models/authAPI";
import {
setAssignmentID,
setProductUserID,
setUserDisplayedWalletBalance,
setUserWallet,
setUserAttemptCreditWallet,
} from "@/models/appSlice";
import { addCashoutMethods } from "@/models/cashoutMethodsSlice";
import { addEvent } from "@/models/grlEventsSlice";
import { addStatsData } from "@/models/grlStatsSlice";
import Home from "@/pages/Home";
import MagicLink from "@/pages/MagicLink";
import Preview from "@/pages/Preview";
import Result from "@/pages/Result";
import clsx from "clsx";
import Work from "@/pages/Work";
import Transfer from "@/pages/Transfer";
import {
BrowserRouter,
Outlet,
useLocation,
useMatch,
Route,
Routes,
useSearchParams,
} from "react-router-dom";
import useWebSocket from "react-use-websocket";
import NewsTicker from "./components/EventMarquee";
import { useEffect } from "react";
import Wallet from "@/components/Wallet";
import "@/index.css";
import { setTaskStatus, setUserWalletBalance } from "@/models/appSlice";
import { Profiling } from "./components/Profiling";
import Redemption from "./components/Redemption";
type Message =
| PingMessage
| PongMessage
| SubscribeMessage
| EventMessage
| StatsMessage;
function isStatsMessage(msg: Message): msg is StatsMessage {
return msg.kind === "stats";
}
function isEventMessage(msg: Message): msg is EventMessage {
return msg.kind === "event";
}
function isPingMessage(msg: Message): msg is PingMessage {
return msg.kind === "ping";
}
function QueryParamProcessor() {
const dispatch = useAppDispatch();
const [searchParams] = useSearchParams();
const { data: user, isLoading, isError } = useGetMeQuery();
useEffect(() => {
if (user?.product_user_id) {
dispatch(setProductUserID(user.product_user_id));
new WalletApi()
.getCashoutMethodsProductIdCashoutMethodsGet(bpid, user.product_user_id)
.then((res) => {
const response = res.data as CashoutMethodsResponse;
dispatch(addCashoutMethods(response.cashout_methods));
});
new WalletApi()
.getUserWalletBalanceProductIdWalletGet(
bpid, // productId
user.product_user_id,
)
.then((res) => {
const response = res.data as UserWalletBalanceResponse;
const balance = response.wallet as UserWalletBalance;
dispatch(setUserWalletBalance(balance));
});
new WalletApi()
.getUserWalletsProductIdWalletsGet(bpid, user.product_user_id)
.then((res) => {
const response = res.data as UserLedgerWallets;
if (response.displayed_balances) {
setUserDisplayedWalletBalance(response.displayed_balances[0]);
}
const userWallet = response.wallets?.find(
(w) =>
w.account_type === UserLedgerWalletAccountTypeEnum.UserWallet,
);
if (userWallet) {
setUserWallet(userWallet);
}
const userAttemptCreditWallet = response.wallets?.find(
(w) =>
w.account_type ===
UserLedgerWalletAccountTypeEnum.UserAttemptCredit,
);
if (userAttemptCreditWallet) {
setUserAttemptCreditWallet(userAttemptCreditWallet);
}
});
}
}, [user]);
useEffect(() => {
const assignment_id = searchParams.get("assignmentId");
const tsid = searchParams.get("tsid");
const worker_id = searchParams.get("workerId");
if (worker_id) {
dispatch(setProductUserID(worker_id));
}
if (assignment_id) {
dispatch(setAssignmentID(assignment_id));
}
if (tsid) {
new StatusApi()
.getTaskStatusProductIdStatusTsidGet(
bpid, // productId
tsid,
)
.then((res) => {
const response: TaskStatusResponse = res.data;
dispatch(setTaskStatus(response));
})
.catch((error) => {
console.error("Error fetching task status:", error);
});
}
}, [searchParams]);
return