File size: 19,975 Bytes
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
be2241b
7642467
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bac223c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
 
 
 
 
 
 
 
 
 
 
75cbb74
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b313702
f7cecf3
 
b313702
f7cecf3
b313702
 
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
b313702
f7cecf3
 
 
 
052f3f2
 
4fc7402
 
7ef9f95
 
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
1648544
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
 
1648544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d4bfa4
f7cecf3
1648544
 
f7cecf3
1648544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
 
1648544
f7cecf3
1648544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
1648544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7cecf3
1648544
 
 
 
7ef9f95
 
 
1648544
7ef9f95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1648544
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ef9f95
f7cecf3
 
052f3f2
f7cecf3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be2241b
7642467
f7cecf3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import React, { useState, useContext, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
import {
  Activity,
  BarChart2,
  Shield,
  Calendar,
  Zap,
  LogIn,
  Settings,
  X,
} from "lucide-react";
import { Analytics } from "@vercel/analytics/react";
import { SpeedInsights } from "@vercel/speed-insights/react";

import { LandingPage } from "./components/LandingPage";
import ProjectionsTable from "./components/ProjectionsTable";
import AccuracyDashboard from "./components/AccuracyDashboard";
import TeamRatings from "./components/TeamRatings";
import Fixtures from "./components/Fixtures";
import Solver from "./components/Solver";
import LoginModal from "./components/LoginModal";
import { PlayerProvider, PlayerContext } from "./PlayerContext";


const tabs = [
  { id: "solver", label: "Solver", icon: Zap },
  { id: "projections", label: "Projections", icon: Activity },
  { id: "accuracy", label: "Accuracy", icon: BarChart2 },
  { id: "ratings", label: "Team Ratings", icon: Shield },
  { id: "fixtures", label: "Fixtures", icon: Calendar },
];

function AppContent() {
  const [activeTab, setActiveTab] = useState(() => {
    if (typeof window !== 'undefined') {
      const params = new URLSearchParams(window.location.search);
      return params.get("tab") || tabs[0].id;
    }
    return tabs[0].id;
  });

  useEffect(() => {
    if (typeof window !== 'undefined') {
      const url = new URL(window.location);
      url.searchParams.set("tab", activeTab);
      window.history.replaceState({}, "", url);
    }
  }, [activeTab]);
  const [showLoginModal, setShowLoginModal] = useState(false);
  const [showSettings, setShowSettings] = useState(false);

  const {
    isLoggedIn,
    setIsLoggedIn,
    userProfile,
    setUserProfile,
    hasGuestMadeEdits,
    setHasGuestMadeEdits,
    isCheckingAuth, // <-- Pull in the new state
    isLoadingDB,
  } = useContext(PlayerContext);

  const [newDefaultId, setNewDefaultId] = useState("");
  const [isSaved, setIsSaved] = useState(false);

  // Sync local input with context profile
  useEffect(() => {
    if (userProfile?.defaultTeamId) {
      setNewDefaultId(userProfile.defaultTeamId);
    }
  }, [userProfile]);

  const handleUpdateDefaultId = () => {
    const parsedId = parseInt(newDefaultId);
    if (!parsedId) return;

    const token = localStorage.getItem('fpl_token');
    if (token) {
      fetch('https://anayshukla-fpl-solver.hf.space/api/auth/save_session', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}` },
        body: JSON.stringify({ default_team_id: parsedId })
      }).then(() => {
        setUserProfile(prev => ({ ...prev, defaultTeamId: parsedId }));
        setIsSaved(true);
        setTimeout(() => setIsSaved(false), 2000);
      });
    }
  };

  // --- THE NEW LOADING INTERCEPTOR ---
  // If we are actively checking the token, show a sleek loading screen instead of the landing page
  if (isCheckingAuth || isLoadingDB) {
    return (
      <div className="min-h-screen bg-slate-950 flex flex-col items-center justify-center">
        <div className="w-12 h-12 border-4 border-slate-800 border-t-luigi-500 rounded-full animate-spin shadow-[0_0_15px_rgba(16,185,129,0.5)]"></div>
        <p className="mt-4 text-luigi-400 font-bold tracking-widest uppercase text-xs animate-pulse">Entering Mansion...</p>
        {isLoadingDB && <p className="text-slate-500 text-[10px] mt-2 tracking-wider font-mono">Loading database...</p>}
        {isCheckingAuth && !isLoadingDB && <p className="text-slate-500 text-[10px] mt-2 tracking-wider font-mono">Verifying access...</p>}
      </div>
    );
  }

  if (!isLoggedIn) {
    return <LandingPage />;
  }

  const handleLogout = () => {
    localStorage.removeItem("fpl_token");
    setIsLoggedIn(false);
    setUserProfile({ username: "Guest", defaultTeamId: null, isAdmin: false });
    setShowSettings(false);
    setActiveTab("solver");
  };

  return (
    <div className="min-h-screen bg-slate-950 text-slate-200 font-sans selection:bg-luigi-500/30">
      <a href="#main-content" className="skip-to-content">Skip to main content</a>
      <header className="border-b border-slate-800 bg-slate-950/80 backdrop-blur-md sticky top-0 z-sticky shadow-sm">
        <div className="max-w-[1600px] w-full mx-auto px-4 sm:px-6 lg:px-8 py-3 flex flex-col md:flex-row md:items-center md:justify-between gap-2">
          <div className="flex items-center justify-between md:contents">
            {/* THE RESTORED TITLE */}
            <div
              onClick={() => setActiveTab("solver")}
              className="flex items-center gap-3 cursor-pointer hover:opacity-80 transition-opacity"
            >
              <img
                src="/l-logo.png"
                alt="Luigi's Mansion Logo"
                className="w-8 h-8 object-contain drop-shadow-[0_0_12px_rgba(16,185,129,0.5)]"
              />
              <h1 className="text-2xl font-black text-transparent bg-clip-text bg-gradient-to-r from-emerald-400 to-cyan-500 tracking-tight whitespace-nowrap">
                Luigi's Mansion
              </h1>
            </div>

            <div className="relative md:hidden">
              {isLoggedIn ? (
                <div className="flex items-center gap-3 relative">
                  {/* Desktop: username left of button. Mobile: hidden here, shown below button */}
                  <div className="hidden md:flex flex-col text-right">
                    <div className="flex items-center gap-1.5 justify-end">
                      {userProfile.isAdmin && (
                        <Shield size={14} className="text-yellow-500" title="Admin Mode" />
                      )}
                      <span className="text-sm font-bold text-slate-200">
                        {userProfile.username}
                      </span>
                    </div>
                  </div>

                  <div className="flex flex-col items-center gap-1">
                    <button
                      onClick={() => setShowSettings(!showSettings)}
                      className="p-2 bg-slate-900 border border-slate-700 rounded-full hover:bg-slate-800 hover:border-luigi-500 hover:text-luigi-400 transition-colors shadow-sm"
                    >
                      <Settings size={18} />
                    </button>
                    {/* Mobile only: username under the button */}
                    <div className="flex md:hidden items-center gap-1">
                      {userProfile.isAdmin && (
                        <Shield size={10} className="text-yellow-500" />
                      )}
                      <span className="text-[10px] font-bold text-slate-400 whitespace-nowrap">
                        {userProfile.username}
                      </span>
                    </div>
                  </div>

                  {showSettings && (
                    <div className="absolute top-full right-0 mt-2 w-64 sm:w-72 bg-slate-900 border border-slate-700 rounded-xl shadow-2xl p-3 sm:p-4 z-dropdown animate-in fade-in slide-in-from-top-2 flex flex-col gap-3 sm:gap-4">

                      {/* Default ID Setting */}
                      <div>
                        <h4 className="text-[10px] font-black text-slate-500 uppercase tracking-wider mb-2">
                          Default FPL ID
                        </h4>
                        <div className="flex items-center gap-2">
                          <input
                            type="number"
                            value={newDefaultId}
                            onChange={(e) => setNewDefaultId(e.target.value)}
                            placeholder="e.g. 123456"
                            className="bg-slate-950 border border-slate-700 rounded py-1.5 px-3 text-xs font-bold text-slate-200 outline-none focus:border-luigi-500 flex-1 shadow-inner"
                          />
                          <button
                            onClick={handleUpdateDefaultId}
                            className={`px-3 py-1.5 rounded text-xs font-bold transition-all shadow-md ${isSaved
                              ? 'bg-luigi-500 text-slate-950'
                              : 'bg-slate-800 hover:bg-slate-700 border border-slate-600 text-white active:scale-95'
                              }`}
                          >
                            {isSaved ? 'Saved ✓' : 'Save'}
                          </button>
                        </div>
                      </div>

                      <div className="h-px w-full bg-slate-800"></div>

                      <button
                        onClick={handleLogout}
                        className="w-full text-left px-3 py-2 text-sm font-bold text-red-400 hover:bg-slate-800 hover:text-red-300 rounded-lg transition-colors flex items-center justify-between"
                      >
                        <span>Log Out</span>
                        <LogIn size={14} className="rotate-180 opacity-50" />
                      </button>
                    </div>
                  )}
                </div>
              ) : (
                <div className="relative">
                  <button
                    onClick={() => setShowLoginModal(true)}
                    className="flex items-center gap-2 px-4 py-2 bg-slate-900 border border-slate-700 hover:border-luigi-500 rounded-lg text-sm font-bold text-slate-300 hover:text-luigi-400 transition-colors shadow-sm"
                  >
                    <LogIn size={16} /> Log In
                  </button>

                  {hasGuestMadeEdits && (
                    <div className="absolute top-full right-0 mt-3 w-64 bg-slate-900 border border-luigi-500/50 shadow-[0_0_20px_rgba(16,185,129,0.15)] rounded-xl p-4 animate-in fade-in slide-in-from-top-4 z-50">
                      <button
                        onClick={() => setHasGuestMadeEdits(false)}
                        className="absolute top-2 right-2 text-slate-500 hover:text-slate-300 transition-colors"
                      >
                        <X size={14} />
                      </button>
                      <div className="flex items-start gap-3">
                        <div className="text-xl">💡</div>
                        <p className="text-xs font-medium text-slate-300 leading-tight">
                          You've made custom adjustments!{" "}
                          <span
                            className="text-luigi-400 font-bold cursor-pointer hover:underline"
                            onClick={() => setShowLoginModal(true)}
                          >
                            Log in
                          </span>{" "}
                          to save your session.
                        </p>
                      </div>
                      <div className="absolute -top-2 right-6 w-4 h-4 bg-slate-900 border-t border-l border-luigi-500/50 transform rotate-45"></div>
                    </div>
                  )}
                </div>
              )}
            </div>
          </div> {/* closes Row 1 */}

          {/* Row 2 — nav tabs */}
          <nav className="flex space-x-1 overflow-x-auto pb-1 md:pb-0 hide-scrollbar order-last md:order-none md:flex-1 md:justify-center" role="navigation" aria-label="Main navigation">
            {tabs.map((tab) => {
              const Icon = tab.icon;
              return (
                <button
                  key={tab.id}
                  onClick={() => setActiveTab(tab.id)}
                  aria-label={`Switch to ${tab.label} tab`}
                  aria-current={activeTab === tab.id ? "page" : undefined}
                  className={`flex items-center px-3 sm:px-4 py-2 sm:py-2.5 rounded-lg text-xs sm:text-sm font-semibold whitespace-nowrap transition-all duration-200 ease-in-out touch-feedback ${activeTab === tab.id ? "bg-luigi-500/10 text-luigi-400 shadow-[inset_0_-2px_0_rgba(16,185,129,1)]" : "text-slate-400 hover:text-slate-200 hover:bg-slate-800/50"}`}
                >
                  <Icon className="w-3.5 h-3.5 sm:w-4 sm:h-4 mr-1.5 sm:mr-2" aria-hidden="true" />
                  <span className="hidden sm:inline">{tab.label}</span>
                  <span className="sm:hidden">{tab.label === "Solver" ? "Solve" : tab.label === "Projections" ? "Proj" : tab.label === "Accuracy" ? "Acc" : tab.label === "Team Ratings" ? "Ratings" : tab.label}</span>
                </button>
              );
            })}
          </nav>

          <div className="relative hidden md:flex">
              {isLoggedIn ? (
                <div className="flex items-center gap-3 relative">
                  {/* Desktop: username left of button. Mobile: hidden here, shown below button */}
                  <div className="hidden md:flex flex-col text-right">
                    <div className="flex items-center gap-1.5 justify-end">
                      {userProfile.isAdmin && (
                        <Shield size={14} className="text-yellow-500" title="Admin Mode" />
                      )}
                      <span className="text-sm font-bold text-slate-200">
                        {userProfile.username}
                      </span>
                    </div>
                  </div>

                  <div className="flex flex-col items-center gap-1">
                    <button
                      onClick={() => setShowSettings(!showSettings)}
                      className="p-2 bg-slate-900 border border-slate-700 rounded-full hover:bg-slate-800 hover:border-luigi-500 hover:text-luigi-400 transition-colors shadow-sm"
                    >
                      <Settings size={18} />
                    </button>
                    {/* Mobile only: username under the button */}
                    <div className="flex md:hidden items-center gap-1">
                      {userProfile.isAdmin && (
                        <Shield size={10} className="text-yellow-500" />
                      )}
                      <span className="text-[10px] font-bold text-slate-400 whitespace-nowrap">
                        {userProfile.username}
                      </span>
                    </div>
                  </div>

                  {showSettings && (
                    <div className="absolute top-full right-0 mt-2 w-64 sm:w-72 bg-slate-900 border border-slate-700 rounded-xl shadow-2xl p-3 sm:p-4 z-dropdown animate-in fade-in slide-in-from-top-2 flex flex-col gap-3 sm:gap-4">

                      {/* Default ID Setting */}
                      <div>
                        <h4 className="text-[10px] font-black text-slate-500 uppercase tracking-wider mb-2">
                          Default FPL ID
                        </h4>
                        <div className="flex items-center gap-2">
                          <input
                            type="number"
                            value={newDefaultId}
                            onChange={(e) => setNewDefaultId(e.target.value)}
                            placeholder="e.g. 123456"
                            className="bg-slate-950 border border-slate-700 rounded py-1.5 px-3 text-xs font-bold text-slate-200 outline-none focus:border-luigi-500 flex-1 shadow-inner"
                          />
                          <button
                            onClick={handleUpdateDefaultId}
                            className={`px-3 py-1.5 rounded text-xs font-bold transition-all shadow-md ${isSaved
                              ? 'bg-luigi-500 text-slate-950'
                              : 'bg-slate-800 hover:bg-slate-700 border border-slate-600 text-white active:scale-95'
                              }`}
                          >
                            {isSaved ? 'Saved ✓' : 'Save'}
                          </button>
                        </div>
                      </div>

                      <div className="h-px w-full bg-slate-800"></div>

                      <button
                        onClick={handleLogout}
                        className="w-full text-left px-3 py-2 text-sm font-bold text-red-400 hover:bg-slate-800 hover:text-red-300 rounded-lg transition-colors flex items-center justify-between"
                      >
                        <span>Log Out</span>
                        <LogIn size={14} className="rotate-180 opacity-50" />
                      </button>
                    </div>
                  )}
                </div>
              ) : (
                <div className="relative">
                  <button
                    onClick={() => setShowLoginModal(true)}
                    className="flex items-center gap-2 px-4 py-2 bg-slate-900 border border-slate-700 hover:border-luigi-500 rounded-lg text-sm font-bold text-slate-300 hover:text-luigi-400 transition-colors shadow-sm"
                  >
                    <LogIn size={16} /> Log In
                  </button>

                  {hasGuestMadeEdits && (
                    <div className="absolute top-full right-0 mt-3 w-64 bg-slate-900 border border-luigi-500/50 shadow-[0_0_20px_rgba(16,185,129,0.15)] rounded-xl p-4 animate-in fade-in slide-in-from-top-4 z-50">
                      <button
                        onClick={() => setHasGuestMadeEdits(false)}
                        className="absolute top-2 right-2 text-slate-500 hover:text-slate-300 transition-colors"
                      >
                        <X size={14} />
                      </button>
                      <div className="flex items-start gap-3">
                        <div className="text-xl">💡</div>
                        <p className="text-xs font-medium text-slate-300 leading-tight">
                          You've made custom adjustments!{" "}
                          <span
                            className="text-luigi-400 font-bold cursor-pointer hover:underline"
                            onClick={() => setShowLoginModal(true)}
                          >
                            Log in
                          </span>{" "}
                          to save your session.
                        </p>
                      </div>
                      <div className="absolute -top-2 right-6 w-4 h-4 bg-slate-900 border-t border-l border-luigi-500/50 transform rotate-45"></div>
                    </div>
                  )}
                </div>
              )}
          </div>

        </div> {/* closes outer flex-col */}
      </header>

      <main id="main-content" className="max-w-[1600px] w-full mx-auto px-4 sm:px-6 lg:px-8 py-6 sm:py-8">
        {/* Solver is always mounted so local state (snapshot, pairs, highlights) survives tab switches */}
        <div
          className={activeTab !== "solver" ? "hidden" : ""}
          aria-hidden={activeTab !== "solver"}
        >
          <Solver />
        </div>

        {/* Every other tab is animated and only rendered when active */}
        {activeTab !== "solver" && (
          <AnimatePresence mode="wait">
            <motion.div
              key={activeTab}
              initial={{ opacity: 0, y: 10 }}
              animate={{ opacity: 1, y: 0 }}
              exit={{ opacity: 0, y: -10 }}
              transition={{ duration: 0.2 }}
            >
              {activeTab === "projections" && <ProjectionsTable />}
              {activeTab === "accuracy" && <AccuracyDashboard />}
              {activeTab === "ratings" && <TeamRatings />}
              {activeTab === "fixtures" && <Fixtures />}
            </motion.div>
          </AnimatePresence>
        )}
      </main>

      <LoginModal
        isOpen={showLoginModal}
        onClose={() => setShowLoginModal(false)}
      />
    </div>
  );
}

export default function App() {
  return (
    <PlayerProvider>
      <AppContent />
      <Analytics />
      <SpeedInsights />
    </PlayerProvider>
  );
}