|
- import type { Metadata } from "next";
- import { GeistSans } from "geist/font/sans";
- import { GeistMono } from "geist/font/mono";
- import fs from "fs/promises";
- import path from "path";
- import { getPortals } from "@/lib/db";
- import { DEFAULT_FONT } from "@/lib/config";
- import "./globals.css";
-
- export const metadata: Metadata = {
- title: "Captive Portal",
- description: "Welcome",
- };
-
- function fontFormat(ext: string): string {
- switch (ext.toLowerCase()) {
- case ".woff2": return "woff2";
- case ".woff": return "woff";
- case ".ttf": return "truetype";
- case ".otf": return "opentype";
- default: return "woff2";
- }
- }
-
- async function findItalicSibling(regularFilename: string): Promise<string | null> {
- const m = regularFilename.match(/^(.*)(\.(?:woff2?|ttf|otf))$/i);
- if (!m) return null;
- const [, base, ext] = m;
- const candidates = [
- `${base}-Italic${ext}`, `${base}-italic${ext}`,
- `${base}_Italic${ext}`, `${base}_italic${ext}`,
- `${base} Italic${ext}`, `${base} italic${ext}`,
- `${base}Italic${ext}`, `${base}italic${ext}`,
- ];
- try {
- const files = await fs.readdir(path.join(process.cwd(), "data", "fonts"));
- for (const c of candidates) {
- if (files.includes(c)) return c;
- }
- } catch {}
- return null;
- }
-
- export default async function RootLayout({
- children,
- }: Readonly<{ children: React.ReactNode }>) {
- // Leggi il portale per scegliere il font
- let chosenFont = DEFAULT_FONT;
- try {
- const portals = await getPortals();
- const portal = portals[0];
- if (portal && portal.fontFamily !== undefined) chosenFont = portal.fontFamily;
- } catch {}
-
- let fontStyleCss = "";
- if (chosenFont) {
- const regularExt = path.extname(chosenFont);
- const italicFile = await findItalicSibling(chosenFont);
- const italicExt = italicFile ? path.extname(italicFile) : "";
- const regularUrl = `/api/fonts?name=${encodeURIComponent(chosenFont)}`;
- const italicUrl = italicFile ? `/api/fonts?name=${encodeURIComponent(italicFile)}` : "";
-
- fontStyleCss = `
- @font-face {
- font-family: 'PortalFont';
- src: url('${regularUrl}') format('${fontFormat(regularExt)}');
- font-style: normal;
- font-display: swap;
- }${italicFile ? `
- @font-face {
- font-family: 'PortalFont';
- src: url('${italicUrl}') format('${fontFormat(italicExt)}');
- font-style: italic;
- font-display: swap;
- }` : ""}
- body { font-family: 'PortalFont', Arial, Helvetica, sans-serif; }
- `;
- }
-
- return (
- <html
- lang="it"
- className={`${GeistSans.variable} ${GeistMono.variable} h-full antialiased`}
- >
- <head>
- {fontStyleCss && <style dangerouslySetInnerHTML={{ __html: fontStyleCss }} />}
- </head>
- <body className="min-h-full flex flex-col">{children}</body>
- </html>
- );
- }
|