refactor: split out root-loader data getter into a dedicated function
This commit is contained in:
parent
3acf565ccd
commit
915b96c218
2 changed files with 24 additions and 28 deletions
|
|
@ -6,40 +6,15 @@ import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
|
||||||
import { Toaster } from "~/client/components/ui/sonner";
|
import { Toaster } from "~/client/components/ui/sonner";
|
||||||
import { useServerEvents } from "~/client/hooks/use-server-events";
|
import { useServerEvents } from "~/client/hooks/use-server-events";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { ThemeProvider, THEME_COOKIE_NAME } from "~/client/components/theme-provider";
|
import { ThemeProvider } from "~/client/components/theme-provider";
|
||||||
import { createServerFn } from "@tanstack/react-start";
|
|
||||||
import { getCookie, getRequestHeaders } from "@tanstack/react-start/server";
|
|
||||||
import { isAuthRoute } from "~/lib/auth-routes";
|
import { isAuthRoute } from "~/lib/auth-routes";
|
||||||
import { auth } from "~/server/lib/auth";
|
import { getRootLoaderData } from "~/server/lib/functions/root-loader-data";
|
||||||
import type { DateFormatPreference, TimeFormatPreference } from "~/client/lib/datetime";
|
|
||||||
|
|
||||||
const fetchTheme = createServerFn({ method: "GET" }).handler(async () => {
|
|
||||||
const themeCookie = getCookie(THEME_COOKIE_NAME);
|
|
||||||
return (themeCookie === "light" ? "light" : "dark") as "light" | "dark";
|
|
||||||
});
|
|
||||||
|
|
||||||
const fetchTimeConfig = createServerFn({ method: "GET" }).handler(async () => {
|
|
||||||
const headers = getRequestHeaders();
|
|
||||||
const acceptLanguage = headers.get("accept-language");
|
|
||||||
const session = await auth.api.getSession({ headers });
|
|
||||||
|
|
||||||
return {
|
|
||||||
locale: (acceptLanguage?.split(",")[0] || "en-US") as string,
|
|
||||||
timeZone: process.env.TZ || Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",
|
|
||||||
dateFormat: (session?.user.dateFormat ?? "MM/DD/YYYY") as DateFormatPreference,
|
|
||||||
timeFormat: (session?.user.timeFormat ?? "12h") as TimeFormatPreference,
|
|
||||||
now: Date.now(),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
export const Route = createRootRouteWithContext<{ queryClient: QueryClient }>()({
|
export const Route = createRootRouteWithContext<{ queryClient: QueryClient }>()({
|
||||||
server: {
|
server: {
|
||||||
middleware: [apiClientMiddleware],
|
middleware: [apiClientMiddleware],
|
||||||
},
|
},
|
||||||
loader: async () => {
|
loader: async () => getRootLoaderData(),
|
||||||
const [theme, timeConfig] = await Promise.all([fetchTheme(), fetchTimeConfig()]);
|
|
||||||
return { theme, ...timeConfig };
|
|
||||||
},
|
|
||||||
head: () => ({
|
head: () => ({
|
||||||
meta: [{ title: "Zerobyte - Open Source Backup Solution" }],
|
meta: [{ title: "Zerobyte - Open Source Backup Solution" }],
|
||||||
links: [
|
links: [
|
||||||
|
|
|
||||||
21
app/server/lib/functions/root-loader-data.ts
Normal file
21
app/server/lib/functions/root-loader-data.ts
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import { createServerFn } from "@tanstack/react-start";
|
||||||
|
import { getCookie, getRequestHeaders } from "@tanstack/react-start/server";
|
||||||
|
import { THEME_COOKIE_NAME } from "~/client/components/theme-provider";
|
||||||
|
import type { DateFormatPreference, TimeFormatPreference } from "~/client/lib/datetime";
|
||||||
|
import { auth } from "~/server/lib/auth";
|
||||||
|
|
||||||
|
export const getRootLoaderData = createServerFn({ method: "GET" }).handler(async () => {
|
||||||
|
const themeCookie = getCookie(THEME_COOKIE_NAME);
|
||||||
|
const headers = getRequestHeaders();
|
||||||
|
const acceptLanguage = headers.get("accept-language");
|
||||||
|
const session = await auth.api.getSession({ headers });
|
||||||
|
|
||||||
|
return {
|
||||||
|
theme: (themeCookie === "light" ? "light" : "dark") as "light" | "dark",
|
||||||
|
locale: (acceptLanguage?.split(",")[0] || "en-US") as string,
|
||||||
|
timeZone: process.env.TZ || Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC",
|
||||||
|
dateFormat: (session?.user.dateFormat ?? "MM/DD/YYYY") as DateFormatPreference,
|
||||||
|
timeFormat: (session?.user.timeFormat ?? "12h") as TimeFormatPreference,
|
||||||
|
now: Date.now(),
|
||||||
|
};
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue