2026-04-22 19:14:16 +08:00
|
|
|
import { APP_VERSION } from "@ashim/shared";
|
|
|
|
|
import { Component, type ErrorInfo, lazy, type ReactNode, Suspense, useEffect } from "react";
|
2026-03-25 09:24:37 +08:00
|
|
|
import { BrowserRouter, Navigate, Route, Routes, useLocation } from "react-router-dom";
|
2026-04-05 00:23:21 +08:00
|
|
|
import { Toaster } from "sonner";
|
2026-04-21 10:19:08 +08:00
|
|
|
import { ConnectionMonitor } from "./components/common/connection-monitor";
|
2026-03-25 09:24:37 +08:00
|
|
|
import { KeyboardShortcutProvider } from "./components/common/keyboard-shortcut-provider";
|
|
|
|
|
import { useAuth } from "./hooks/use-auth";
|
2026-04-22 19:14:16 +08:00
|
|
|
import { identify, initAnalytics } from "./lib/analytics";
|
|
|
|
|
import { useAnalyticsStore } from "./stores/analytics-store";
|
2026-04-15 18:13:23 +08:00
|
|
|
|
2026-04-21 10:19:08 +08:00
|
|
|
// Lazy-load all pages so each page's JS (and its icons/deps) is only
|
|
|
|
|
// downloaded when the user navigates there, shrinking the main bundle.
|
|
|
|
|
const AutomatePage = lazy(() =>
|
2026-04-15 18:13:23 +08:00
|
|
|
import("./pages/automate-page").then((m) => ({ default: m.AutomatePage })),
|
|
|
|
|
);
|
2026-04-21 10:19:08 +08:00
|
|
|
const ChangePasswordPage = lazy(() =>
|
2026-04-15 18:13:23 +08:00
|
|
|
import("./pages/change-password-page").then((m) => ({ default: m.ChangePasswordPage })),
|
|
|
|
|
);
|
2026-04-21 10:19:08 +08:00
|
|
|
const FilesPage = lazy(() => import("./pages/files-page").then((m) => ({ default: m.FilesPage })));
|
|
|
|
|
const FullscreenGridPage = lazy(() =>
|
2026-04-15 18:13:23 +08:00
|
|
|
import("./pages/fullscreen-grid-page").then((m) => ({ default: m.FullscreenGridPage })),
|
|
|
|
|
);
|
2026-04-21 10:19:08 +08:00
|
|
|
const HomePage = lazy(() => import("./pages/home-page").then((m) => ({ default: m.HomePage })));
|
|
|
|
|
const LoginPage = lazy(() => import("./pages/login-page").then((m) => ({ default: m.LoginPage })));
|
|
|
|
|
const PrivacyPolicyPage = lazy(() =>
|
2026-04-15 18:13:23 +08:00
|
|
|
import("./pages/privacy-policy-page").then((m) => ({ default: m.PrivacyPolicyPage })),
|
|
|
|
|
);
|
2026-04-22 19:10:52 +08:00
|
|
|
const AnalyticsConsentPage = lazy(() =>
|
|
|
|
|
import("./pages/analytics-consent-page").then((m) => ({ default: m.AnalyticsConsentPage })),
|
|
|
|
|
);
|
2026-04-21 10:19:08 +08:00
|
|
|
const ToolPage = lazy(() => import("./pages/tool-page").then((m) => ({ default: m.ToolPage })));
|
2026-03-22 11:19:41 +08:00
|
|
|
|
2026-03-23 19:22:52 +08:00
|
|
|
class ErrorBoundary extends Component<
|
|
|
|
|
{ children: ReactNode },
|
2026-04-21 10:19:08 +08:00
|
|
|
{ hasError: boolean; error: Error | null }
|
2026-03-23 19:22:52 +08:00
|
|
|
> {
|
|
|
|
|
constructor(props: { children: ReactNode }) {
|
|
|
|
|
super(props);
|
2026-04-21 10:19:08 +08:00
|
|
|
this.state = { hasError: false, error: null };
|
2026-03-23 19:22:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static getDerivedStateFromError(error: Error) {
|
2026-04-21 10:19:08 +08:00
|
|
|
return { hasError: true, error };
|
2026-03-23 19:22:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidCatch(error: Error, info: ErrorInfo) {
|
|
|
|
|
console.error("Uncaught render error:", error, info.componentStack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
if (this.state.hasError) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-screen items-center justify-center bg-background text-foreground">
|
|
|
|
|
<div className="text-center space-y-4 max-w-md px-6">
|
|
|
|
|
<h1 className="text-xl font-semibold">Something went wrong</h1>
|
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
|
{this.state.error?.message || "An unexpected error occurred."}
|
|
|
|
|
</p>
|
|
|
|
|
<button
|
2026-03-26 01:10:13 +08:00
|
|
|
type="button"
|
2026-03-23 19:22:52 +08:00
|
|
|
onClick={() => {
|
2026-04-21 10:19:08 +08:00
|
|
|
this.setState({ hasError: false, error: null });
|
2026-03-23 19:22:52 +08:00
|
|
|
window.location.href = "/";
|
|
|
|
|
}}
|
|
|
|
|
className="px-4 py-2 rounded-lg bg-primary text-primary-foreground text-sm font-medium"
|
|
|
|
|
>
|
|
|
|
|
Go Home
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.props.children;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 11:19:41 +08:00
|
|
|
function AuthGuard({ children }: { children: React.ReactNode }) {
|
2026-04-22 19:10:52 +08:00
|
|
|
const {
|
|
|
|
|
loading,
|
|
|
|
|
authEnabled,
|
|
|
|
|
isAuthenticated,
|
|
|
|
|
mustChangePassword,
|
|
|
|
|
analyticsEnabled,
|
|
|
|
|
analyticsConsentShownAt,
|
|
|
|
|
} = useAuth();
|
2026-04-23 10:06:46 +08:00
|
|
|
const storeConsent = useAnalyticsStore((s) => s.consent);
|
2026-04-23 10:08:24 +08:00
|
|
|
const setStoreConsent = useAnalyticsStore((s) => s.setConsent);
|
2026-03-22 11:19:41 +08:00
|
|
|
const location = useLocation();
|
|
|
|
|
|
2026-04-23 11:14:48 +08:00
|
|
|
// Hydrate the analytics store from session data on initial load.
|
|
|
|
|
// Only hydrate if the store is still in its initial state (user hasn't taken
|
|
|
|
|
// an explicit action like accepting/declining on the consent page).
|
2026-04-23 10:08:24 +08:00
|
|
|
useEffect(() => {
|
2026-04-23 11:14:48 +08:00
|
|
|
if (
|
|
|
|
|
!loading &&
|
|
|
|
|
analyticsEnabled !== undefined &&
|
|
|
|
|
storeConsent.analyticsConsentShownAt === null &&
|
|
|
|
|
storeConsent.analyticsEnabled === null
|
|
|
|
|
) {
|
2026-04-23 10:08:24 +08:00
|
|
|
setStoreConsent({
|
|
|
|
|
analyticsEnabled: analyticsEnabled ?? null,
|
|
|
|
|
analyticsConsentShownAt: analyticsConsentShownAt ?? null,
|
|
|
|
|
analyticsConsentRemindAt: null,
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-04-23 11:14:48 +08:00
|
|
|
// eslint-disable-next-line -- only hydrate on session load, not on store changes
|
2026-04-23 10:08:24 +08:00
|
|
|
}, [loading, analyticsEnabled, analyticsConsentShownAt, setStoreConsent]);
|
|
|
|
|
|
2026-03-28 14:02:07 +08:00
|
|
|
// Don't guard the login or change-password pages
|
2026-03-29 23:57:08 +08:00
|
|
|
if (
|
|
|
|
|
location.pathname === "/login" ||
|
|
|
|
|
location.pathname === "/change-password" ||
|
2026-04-22 19:10:52 +08:00
|
|
|
location.pathname === "/privacy" ||
|
|
|
|
|
location.pathname === "/analytics-consent"
|
2026-03-29 23:57:08 +08:00
|
|
|
) {
|
2026-03-22 11:19:41 +08:00
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-screen items-center justify-center bg-background text-foreground">
|
|
|
|
|
<div className="text-center space-y-3">
|
|
|
|
|
<div className="h-8 w-8 border-2 border-primary border-t-transparent rounded-full animate-spin mx-auto" />
|
|
|
|
|
<p className="text-sm text-muted-foreground">Loading...</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (authEnabled && !isAuthenticated) {
|
|
|
|
|
return <Navigate to="/login" replace />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 14:02:07 +08:00
|
|
|
// Force password change before allowing access to the app
|
|
|
|
|
if (authEnabled && mustChangePassword) {
|
|
|
|
|
return <Navigate to="/change-password" replace />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-23 10:06:46 +08:00
|
|
|
// Check both session state (useAuth) and real-time store state.
|
|
|
|
|
// After the consent page calls acceptAnalytics(), the store updates immediately
|
|
|
|
|
// but useAuth won't re-fetch until the next session check.
|
|
|
|
|
const effectiveEnabled = storeConsent.analyticsEnabled ?? analyticsEnabled;
|
|
|
|
|
const effectiveShownAt = storeConsent.analyticsConsentShownAt ?? analyticsConsentShownAt;
|
|
|
|
|
if (authEnabled && effectiveEnabled === null && effectiveShownAt === null) {
|
2026-04-22 19:10:52 +08:00
|
|
|
return <Navigate to="/analytics-consent" replace />;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 11:19:41 +08:00
|
|
|
return <>{children}</>;
|
|
|
|
|
}
|
2026-03-22 02:57:49 +08:00
|
|
|
|
2026-04-15 18:13:23 +08:00
|
|
|
// Single page-level loading fallback — shown while JS for a route downloads.
|
|
|
|
|
function PageLoader() {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex h-screen items-center justify-center bg-background text-foreground">
|
|
|
|
|
<div className="h-8 w-8 border-2 border-primary border-t-transparent rounded-full animate-spin" />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-22 02:57:49 +08:00
|
|
|
export function App() {
|
2026-04-22 19:14:16 +08:00
|
|
|
const analyticsConfig = useAnalyticsStore((s) => s.config);
|
|
|
|
|
const analyticsConfigLoaded = useAnalyticsStore((s) => s.configLoaded);
|
|
|
|
|
const fetchAnalyticsConfig = useAnalyticsStore((s) => s.fetchConfig);
|
|
|
|
|
const analyticsConsent = useAnalyticsStore((s) => s.consent);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchAnalyticsConfig();
|
|
|
|
|
}, [fetchAnalyticsConfig]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (analyticsConfigLoaded && analyticsConfig?.enabled) {
|
|
|
|
|
initAnalytics(analyticsConfig);
|
|
|
|
|
}
|
|
|
|
|
}, [analyticsConfigLoaded, analyticsConfig]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (
|
|
|
|
|
!analyticsConfigLoaded ||
|
|
|
|
|
!analyticsConfig?.enabled ||
|
|
|
|
|
analyticsConsent.analyticsEnabled !== true
|
|
|
|
|
)
|
|
|
|
|
return;
|
|
|
|
|
identify(analyticsConfig.instanceId, {
|
|
|
|
|
$set: { version: APP_VERSION },
|
|
|
|
|
$set_once: { instance_id: analyticsConfig.instanceId },
|
|
|
|
|
});
|
|
|
|
|
}, [analyticsConfigLoaded, analyticsConfig, analyticsConsent.analyticsEnabled]);
|
|
|
|
|
|
2026-03-22 02:57:49 +08:00
|
|
|
return (
|
2026-04-21 10:19:08 +08:00
|
|
|
<ErrorBoundary>
|
2026-04-20 22:03:58 +08:00
|
|
|
<ConnectionMonitor />
|
2026-04-21 10:19:08 +08:00
|
|
|
<Toaster position="bottom-right" />
|
|
|
|
|
<BrowserRouter>
|
|
|
|
|
<KeyboardShortcutProvider>
|
|
|
|
|
<AuthGuard>
|
|
|
|
|
<Suspense fallback={<PageLoader />}>
|
|
|
|
|
<Routes>
|
|
|
|
|
<Route path="/login" element={<LoginPage />} />
|
|
|
|
|
<Route path="/change-password" element={<ChangePasswordPage />} />
|
|
|
|
|
<Route path="/automate" element={<AutomatePage />} />
|
|
|
|
|
<Route path="/files" element={<FilesPage />} />
|
|
|
|
|
<Route path="/fullscreen" element={<FullscreenGridPage />} />
|
|
|
|
|
<Route path="/privacy" element={<PrivacyPolicyPage />} />
|
|
|
|
|
{/* Redirects: old color tools consolidated into adjust-colors */}
|
|
|
|
|
<Route
|
|
|
|
|
path="/brightness-contrast"
|
|
|
|
|
element={<Navigate to="/adjust-colors" replace />}
|
|
|
|
|
/>
|
|
|
|
|
<Route path="/saturation" element={<Navigate to="/adjust-colors" replace />} />
|
|
|
|
|
<Route path="/color-channels" element={<Navigate to="/adjust-colors" replace />} />
|
|
|
|
|
<Route path="/color-effects" element={<Navigate to="/adjust-colors" replace />} />
|
2026-04-22 19:10:52 +08:00
|
|
|
<Route path="/analytics-consent" element={<AnalyticsConsentPage />} />
|
2026-04-21 10:19:08 +08:00
|
|
|
<Route path="/:toolId" element={<ToolPage />} />
|
|
|
|
|
<Route path="/" element={<HomePage />} />
|
|
|
|
|
</Routes>
|
|
|
|
|
</Suspense>
|
|
|
|
|
</AuthGuard>
|
|
|
|
|
</KeyboardShortcutProvider>
|
|
|
|
|
</BrowserRouter>
|
|
|
|
|
</ErrorBoundary>
|
2026-03-22 02:57:49 +08:00
|
|
|
);
|
|
|
|
|
}
|