"use client"; import { useEffect, useState } from "react"; import { MdOutlineSwitchAccount } from "react-icons/md"; import { getClientCookie, setClientCookie } from "@/lib/cookies"; export const FEMALE_TOKEN = "545f61bb3e061ccb9b19f84715eb1b1ed4740331"; export const MALE_TOKEN = "f3a7543b44ef0a713d1ee0d4f7866b3825cf1308"; export const MALE_2_TOKEN = "56cd0fceb93f7cecf7ffa84c590135026cf54a1d"; export const FEMALE_EMAIL = "m.a.ghorbani01@gmail.com"; export const MALE_EMAIL = "muhammadamin.ghorbani@gmail.com"; export const MALE_2_EMAIL = "habibwabackup@gmail.com"; const TOKEN_COOKIE_NAME = "HABIB_TOKEN"; type TokenSwitcherProps = { variant?: "default" | "transparent"; className?: string; }; export function TokenSwitcher({ variant = "default", className, }: TokenSwitcherProps) { const [isOpen, setIsOpen] = useState(false); const [currentToken, setCurrentToken] = useState(""); useEffect(() => { if (typeof window !== "undefined") { const token = (window as any).HABIB_TOKEN ?? getClientCookie(TOKEN_COOKIE_NAME) ?? getClientCookie("habib_token") ?? sessionStorage.getItem(TOKEN_COOKIE_NAME) ?? process.env.NEXT_PUBLIC_DEFAULT_TOKEN ?? MALE_TOKEN; setCurrentToken(token); } }, []); const isMale = currentToken === MALE_TOKEN; const isMale2 = currentToken === MALE_2_TOKEN; const isFemale = currentToken === FEMALE_TOKEN; const isNoToken = currentToken === "NO_TOKEN" || !currentToken; const handleSelectToken = (newToken: string) => { if (typeof window !== "undefined") { // Clear local storage and session storage so answers and cached state from previous user are removed try { window.localStorage.clear(); window.sessionStorage.clear(); } catch (e) { console.error("Failed to clear storage", e); } setClientCookie(TOKEN_COOKIE_NAME, newToken); setClientCookie("habib_token", newToken); try { sessionStorage.setItem(TOKEN_COOKIE_NAME, newToken); } catch (e) { console.warn("sessionStorage is not accessible:", e); } (window as any).HABIB_TOKEN = newToken; setCurrentToken(newToken); setIsOpen(false); // Navigate to the beginning of the flow (intro page) window.location.href = "/"; } }; const handleResetUser = async (userId: number | null, label: string) => { const message = userId !== null ? `آیا از پاک کردن تمامی اطلاعات هویتی، پاسخ‌ها و پیشرفت این کاربر (${label}) و شروع مجدد از مرحله اول آنبوردینگ مطمئن هستید؟` : `آیا از پاک کردن تمامی اطلاعات محلی و کوکی‌های مربوط به این حالت (بدون توکن) و شروع مجدد مطمئن هستید؟`; if (!window.confirm(message)) { return; } try { // 1. Clear local/session storage if (typeof window !== "undefined") { window.localStorage.clear(); window.sessionStorage.clear(); } // 2. Call backend reset script via API route if it is a real DB user if (userId !== null) { const response = await fetch("/api/dev-reset-profile", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ userId }), }); if (!response.ok) { const errData = await response.json(); alert(`خطا در ریست پروفایل: ${errData.error || response.statusText}`); return; } } // 3. Set the target token in the cookies and redirect to / const targetToken = userId === 17119 ? MALE_TOKEN : userId === 147714 ? FEMALE_TOKEN : userId === 146024 ? MALE_2_TOKEN : "NO_TOKEN"; setClientCookie(TOKEN_COOKIE_NAME, targetToken); setClientCookie("habib_token", targetToken); if (typeof window !== "undefined") { try { sessionStorage.setItem(TOKEN_COOKIE_NAME, targetToken); } catch (e) { console.warn("sessionStorage is not accessible:", e); } (window as any).HABIB_TOKEN = targetToken; setCurrentToken(targetToken); } setIsOpen(false); window.location.href = "/"; } catch (error) { console.error("Failed to reset user:", error); alert("خطایی در انجام عملیات رخ داد."); } }; return ( <> {isOpen && (
e.stopPropagation()} >

انتخاب حساب کاربری (تست)

{/* Male option card */}
{isMale && ( فعال )}
{/* Male 2 option card */}
{isMale2 && ( فعال )}
{/* Female option card */}
{isFemale && ( فعال )}
{/* No token option card */}
{isNoToken && ( فعال )}
)} ); } export default TokenSwitcher;