"use client"; import { getAccessToken_client } from "@/utils/apiHelper_client"; import React, { useEffect, useMemo, useState } from "react"; type Store = { description?: string; lastOpenedTime?: string; logo?: { url?: string }; name?: string; url?: string; urlPath?: string; }; type Payload = { userid?: string; message?: string; store?: Store; storeId?: string; }; function tryParseData(raw: string | null): Payload | null { if (!raw) return null; try { return JSON.parse(decodeURIComponent(raw)); } catch { return null; } } function formatDate(iso?: string) { if (!iso) return "β€”"; const d = new Date(iso); return isNaN(d.getTime()) ? "β€”" : d.toLocaleString(); } const read = async (r: Response) => r.headers.get("content-type")?.includes("application/json") ? r.json() : r.text(); export default function AuthCompleteClient() { const [payload, setPayload] = useState(null); useEffect(() => { const url = new URL(window.location.href); const hash = window.location.hash; const hashVal = hash.startsWith("#data=") ? hash.slice(6) : null; const hashPayload = tryParseData(hashVal); const qpPayload = tryParseData(url.searchParams.get("data")); const storeIdOnly = url.searchParams.get("storeId"); setPayload( hashPayload || qpPayload || (storeIdOnly ? { storeId: storeIdOnly } : null) ); }, []); const { title, subtitle, logoUrl, desc, link, lastOpen } = useMemo(() => { const store = payload?.store ?? {}; const id = payload?.storeId || store.urlPath || "β€”"; return { title: payload?.message || (store.name ? `eBay connected! Tokens saved for Store ${store.urlPath}.` : `eBay connected! Store ${id}`), subtitle: store.name || id, logoUrl: store.logo?.url, desc: store.description, link: store.url, lastOpen: formatDate(store.lastOpenedTime), }; }, [payload]); useEffect(() => { if (payload) { (async () => { const accessToken = await getAccessToken_client(); payload.userid = sessionStorage.getItem("USERID") || undefined; if (!payload.userid) return; const saveRes = await fetch( "https://ebay.backend.data4autos.com/api/motorstate/auth/ebay/store/save", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), } ); const saveData = await read(saveRes); if (!saveRes.ok) { console.error("Save failed:", saveData); } })(); } }, [payload]); return (
{/* πŸŽ‰ Confetti Animation */} {/* Card */}
βœ… Connected

{title || "eBay connected successfully!"} πŸŽ‰

{logoUrl ? ( {subtitle ) : (
{subtitle?.[0] || "S"}
)}
{subtitle}
Last opened: {lastOpen}
{desc &&

{desc}

} {!payload && (

Waiting for data… redirect with ?data=...

)}
); } /* ===== Tiny Confetti ===== */ const Confetti = () => { const pieces = Array.from({ length: 24 }); return (
{pieces.map((_, i) => { const left = Math.random() * 100; const size = 6 + Math.random() * 8; const duration = 2.8 + Math.random() * 1.6; const delay = Math.random() * 0.8; const color = ["#00d1ff", "#ff69b4", "#16a34a"][Math.floor(Math.random() * 3)]; return ( ); })}
); };