116 lines
4.1 KiB
TypeScript
116 lines
4.1 KiB
TypeScript
import { configureTracking } from "@agent-native/core/client/analytics";
|
|
import { appPath } from "@agent-native/core/client/api-path";
|
|
import { useDbSync } from "@agent-native/core/client/hooks";
|
|
import { createAgentNativeQueryClient } from "@agent-native/core/client/hooks";
|
|
import { getThemeInitScript } from "@agent-native/core/client/ui";
|
|
import { AppProviders } from "@agent-native/toolkit/app/providers";
|
|
import { useQueryClient } from "@tanstack/react-query";
|
|
import { useState } from "react";
|
|
import { Links, Meta, Outlet, Scripts, ScrollRestoration, useLoaderData, useRouteLoaderData } from "react-router";
|
|
import type { LinksFunction } from "react-router";
|
|
|
|
import { Layout as AppLayout } from "@/components/layout/Layout";
|
|
import { AppToolkitProvider } from "@/components/ui/toolkit-provider";
|
|
import { useNavigationState } from "@/hooks/use-navigation-state";
|
|
import { NearMeProvider } from "@/hooks/use-near-me";
|
|
import { APP_NAME, APP_TITLE } from "@/lib/app-config";
|
|
import { ContentProvider } from "@/lib/content-context";
|
|
import { readContent } from "@/lib/content.server";
|
|
import { LocaleProvider, readLocaleFromRequestUrl } from "@/lib/i18n";
|
|
import { TAB_ID } from "@/lib/tab-id";
|
|
|
|
import stylesheet from "./global.css?url";
|
|
|
|
configureTracking({
|
|
getDefaultProps: (_name, properties) => ({
|
|
...properties,
|
|
app: APP_NAME,
|
|
}),
|
|
});
|
|
|
|
export const links: LinksFunction = () => [
|
|
{ rel: "stylesheet", href: stylesheet },
|
|
];
|
|
|
|
const THEME_INIT_SCRIPT = getThemeInitScript();
|
|
|
|
/** เนื้อหาที่แก้ได้จากหลังบ้าน (โรงหนัง/คำศัพท์/footer) โหลดครั้งเดียวที่ root */
|
|
export async function loader({ request }: { request: Request }) {
|
|
const content = await readContent();
|
|
return { content, localeFromUrl: readLocaleFromRequestUrl(request) };
|
|
}
|
|
|
|
export function Layout({ children }: { children: React.ReactNode }) {
|
|
const data = useRouteLoaderData<typeof loader>("root");
|
|
const lang = data?.localeFromUrl === "en" ? "en" : "th";
|
|
return (
|
|
<html lang={lang} suppressHydrationWarning>
|
|
<head>
|
|
<meta charSet="utf-8" />
|
|
<meta
|
|
name="viewport"
|
|
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
|
|
/>
|
|
<script
|
|
suppressHydrationWarning
|
|
dangerouslySetInnerHTML={{ __html: THEME_INIT_SCRIPT }}
|
|
/>
|
|
{/* guard:allow-raw-color - theme-color metadata requires a concrete browser-chrome color. */}
|
|
<meta name="theme-color" content="#18181B" />
|
|
<meta name="mobile-web-app-capable" content="yes" />
|
|
<meta
|
|
name="apple-mobile-web-app-status-bar-style"
|
|
content="black-translucent"
|
|
/>
|
|
<meta name="apple-mobile-web-app-title" content={APP_TITLE} />
|
|
<link rel="icon" type="image/svg+xml" href={appPath("/favicon.svg")} />
|
|
<link rel="apple-touch-icon" href={appPath("/icon-180.svg")} />
|
|
<Meta />
|
|
<Links />
|
|
</head>
|
|
<body>
|
|
{children}
|
|
<ScrollRestoration />
|
|
<Scripts />
|
|
</body>
|
|
</html>
|
|
);
|
|
}
|
|
|
|
function DbSyncSetup() {
|
|
const qc = useQueryClient();
|
|
useNavigationState();
|
|
useDbSync({
|
|
queryClient: qc,
|
|
ignoreSource: TAB_ID,
|
|
});
|
|
return null;
|
|
}
|
|
|
|
export default function Root() {
|
|
const [queryClient] = useState(() => createAgentNativeQueryClient());
|
|
const { content, localeFromUrl } = useLoaderData<typeof loader>();
|
|
return (
|
|
<ContentProvider content={content}>
|
|
<LocaleProvider
|
|
initialLocale={localeFromUrl === "en" ? "en" : "th"}
|
|
localeFromUrl={localeFromUrl}
|
|
terms={content.site.terms}
|
|
>
|
|
<NearMeProvider cinemas={content.cinemas}>
|
|
<AppToolkitProvider>
|
|
<AppProviders queryClient={queryClient}>
|
|
<DbSyncSetup />
|
|
<AppLayout>
|
|
<Outlet />
|
|
</AppLayout>
|
|
</AppProviders>
|
|
</AppToolkitProvider>
|
|
</NearMeProvider>
|
|
</LocaleProvider>
|
|
</ContentProvider>
|
|
);
|
|
}
|
|
|
|
export { ErrorBoundary } from "@agent-native/toolkit/app/shared";
|