import { useEffect, useState } from 'react'; import { createRoot } from 'react-dom/client'; import { LockKeyhole } from 'lucide-react'; import '@fontsource-variable/lora'; import '@fontsource/ibm-plex-sans-arabic/400.css'; import '@fontsource/ibm-plex-sans-arabic/500.css'; import '@fontsource/ibm-plex-sans-arabic/600.css'; import '@fontsource/ibm-plex-sans-arabic/700.css'; import { Admin } from './admin.jsx'; import { Account, PublicNav } from './customer.jsx'; import { Offer } from './offer.jsx'; import { api, Field, note, Panel, Submit, Ui, useUi, words } from './ui.jsx'; import './styles.css'; const path = location.pathname.replace(/\/$/, ''); const page = path === '/rewards' ? 'offer' : path === '/account' ? 'customer' : 'admin'; function App() { const [lang, setLang] = useState(() => localStorage.getItem('kewi-language') === 'en' ? 'en' : 'ar'); const [context, setContext] = useState(null); const [data, setData] = useState(null); const [error, setError] = useState(''); const [loading, setLoading] = useState(true); const [toast, setToast] = useState(null); const t = (ar, en) => lang === 'ar' ? ar : en; const locale = t('ar-EG-u-nu-latn', 'en-EG-u-nu-latn'); const numeral = new Intl.NumberFormat(locale); const currency = new Intl.NumberFormat(locale, { style: 'currency', currency: 'EGP', minimumFractionDigits: 0, maximumFractionDigits: 2 }); const shortDate = new Intl.DateTimeFormat(locale, { month: 'short', day: 'numeric', timeZone: 'Africa/Cairo' }); const label = (key) => words[key] ? t(...words[key]) : t(...words.serverError); const copy = async (value) => { try { await navigator.clipboard.writeText(value); setToast({ message: t('تم النسخ.', 'Copied.'), id: Date.now() }); } catch { setToast({ message: t('النسخ التلقائي غير متاح في المتصفح.', 'Clipboard access is unavailable.'), id: Date.now() }); } }; useEffect(() => { document.documentElement.lang = lang; document.documentElement.dir = lang === 'ar' ? 'rtl' : 'ltr'; localStorage.setItem('kewi-language', lang); }, [lang]); useEffect(() => { if (!toast) return; const timer = setTimeout(() => setToast(null), 3200); return () => clearTimeout(timer); }, [toast]); useEffect(() => { async function load() { try { const current = await api('context'); const token = page === 'customer' ? new URLSearchParams(location.hash.slice(1)).get('access') : ''; if (token) { history.replaceState(null, '', location.pathname + location.search); try { current.role = (await api('login', { type: 'customer', token })).role; } catch (failure) { current.role = null; setError(failure.message); } } if (current.demo && current.role !== page && !token && !sessionStorage.getItem('kewi-signed-out') && page !== 'offer') { current.role = (await api('login', { type: page })).role; } setContext(current); if (current.role === page) setData(await api(page)); } catch (failure) { setError(failure.message); } finally { setLoading(false); } } load(); }, []); const refresh = async () => { try { setData(await api(page)); } catch (failure) { if (failure.message === 'forbidden') { setData(null); setContext((current) => ({ ...current, role: null })); } throw failure; } }; const login = async (values) => { await api('login', { ...values, type: page }); sessionStorage.removeItem('kewi-signed-out'); setData(await api(page)); setContext((current) => ({ ...current, role: page })); setError(''); }; const logout = async () => { try { await api('logout', {}); sessionStorage.setItem('kewi-signed-out', 'true'); setData(null); setContext((current) => ({ ...current, role: null })); } catch (failure) { setToast({ message: label(failure.message), id: Date.now() }); } }; let content; if (loading) content = {t('جاري تحميل مكافآت كيوي…', 'Loading KEWI Rewards…')}; else if (!context || (context.role === page && !data)) content = {label(error || 'offline')} location.reload()}>{t('إعادة المحاولة', 'Try again')}; else if (page === 'offer') content = ; else if (!data) content = ; else if (page === 'admin') content = ; else content = ; return currency.format((minor || 0) / 100), number: numeral.format, date: (value) => value ? shortDate.format(new Date(value)) : '—' }}> {content}{toast && {toast.message}} ; } function Login({ context, initialError, login }) { const { t, label } = useUi(); const [error, setError] = useState(initialError); const [busy, setBusy] = useState(false); const admin = page === 'admin'; return <>}> {error && {label(error)}} { event.preventDefault(); const values = note(event); setBusy(true); setError(''); try { await login(values); } catch (failure) { setError(failure.message); } finally { setBusy(false); } }}> {!context.demo && (admin ? : <>{t('استخدمي رابط الدخول الخاص اللي وصلك من فريق كيوي أو دخّلي الرمز الموجود فيه.', 'Open the private access link from KEWI or enter the token from that link.')}>)} {context.demo ? t('الدخول للوضع التجريبي', 'Enter demo') : t('دخول', 'Sign in')} {t('صفحة العرض', 'View offer')} >; } createRoot(document.getElementById('root')).render();
{label(error || 'offline')}
{label(error)}
{t('استخدمي رابط الدخول الخاص اللي وصلك من فريق كيوي أو دخّلي الرمز الموجود فيه.', 'Open the private access link from KEWI or enter the token from that link.')}