'use client'; import { GuiProvider } from '@hanzo/gui'; import { QueryClient, QueryClientProvider, useQueryClient, useQuery, useMutation, useInfiniteQuery, } from '@tanstack/react-query'; import React from 'react'; import config from './lux.config'; import { currentHost, resolveWhiteLabel, type WhiteLabel } from './white-label'; const defaultQueryClient = new QueryClient({ defaultOptions: { queries: { refetchOnWindowFocus: false, retry: 1, staleTime: 30_000 } }, }); // --- Tailwind-Preflight baseline reset ------------------------------------- // @hanzo/gui's GuiProvider scopes its subtree in a Tamagui `font_body` context // whose shared CSS rule pins an ABSOLUTE line-height (23px). That cascades into // consumer text authored at an arbitrary size with no line-height utility of its // own (e.g. `text-[11px]` labels), making each line too tall and reflowing dense // layouts. Restore Tailwind Preflight's `line-height: 1.5` baseline at // `body .font_body` specificity — proportional, so any element carrying its own // text-* / leading utility is unaffected. Shipping it from AppProvider means // every Lux surface inherits the fix with no per-app CSS pin. Web-only (there is // no `document` on native), injected once via useInsertionEffect (React's // primitive for style insertion — runs before the browser paints, so no reflow // flash), idempotent by element id. const FONT_BODY_RESET_ID = 'lux-ui-font-body-reset'; const FONT_BODY_RESET_CSS = 'body .font_body{line-height:1.5}'; const useFontBodyReset = (): void => { React.useInsertionEffect(() => { if (typeof document === 'undefined') return; if (document.getElementById(FONT_BODY_RESET_ID)) return; const style = document.createElement('style'); style.id = FONT_BODY_RESET_ID; style.textContent = FONT_BODY_RESET_CSS; document.head.appendChild(style); }, []); }; // --- White-label context --------------------------------------------------- // Resolved once at the root and read anywhere below. Brand, theme, IAM client // and issuer all come from the Host header — see ./white-label. const WhiteLabelContext = React.createContext(resolveWhiteLabel('')); /** The white-label identity for the host this page is being served under. */ export const useWhiteLabel = (): WhiteLabel => React.useContext(WhiteLabelContext); /** * Host used for the FIRST render, then upgraded to the browser's real host. * * Server-rendered surfaces pass `req.headers.host` in, so first paint is already * correct and hydration matches. Surfaces that do not pass one render the * default row on the server and swap to the host-resolved row in an effect — * deterministic on both sides, so there is no hydration mismatch either way. */ const useHostWhiteLabel = (host: string | undefined): WhiteLabel => { const [ resolvedHost, setResolvedHost ] = React.useState(host ?? ''); React.useEffect(() => { if (host !== undefined) return; const browserHost = currentHost(); if (browserHost) setResolvedHost(browserHost); }, [ host ]); return React.useMemo(() => resolveWhiteLabel(resolvedHost), [ resolvedHost ]); }; interface AppProviderProps { children: React.ReactNode; queryClient?: QueryClient; /** * Host header value, for server rendering. Omit in the browser and the host * is read from `location`. Never bake a brand into a deployment — pass the * request's host and the same image serves every org. */ host?: string; /** * Escape hatch: pin the gui child-theme row (`dark_lux` | `dark_zoo` | * `dark_hanzo` | `dark_pars`). Leave unset so the theme follows the host. */ defaultTheme?: string; } // AppProvider wraps GuiProvider (the @hanzogui engine, fed the Lux config) + // tanstack QueryClientProvider. One provider for every Lux surface. // // @tanstack/react-query is a PEER, not a dependency: it hands out a React // context, and a second physical copy is a second context. When this package // carried its own, a surface that also used wagmi got two — AppProvider // published a client into one context and `useAccount` read the other, and the // page died with "No QueryClient set" under a provider that was plainly there. // A context library belongs to the app, once. export const AppProvider = ({ children, queryClient, host, defaultTheme, }: AppProviderProps): React.ReactElement => { useFontBodyReset(); const whiteLabel = useHostWhiteLabel(host); const theme = defaultTheme ?? whiteLabel.theme; return ( {children} ); }; // Re-export tanstack hooks so consumers never import @tanstack/react-query directly export { QueryClient, useQueryClient, useQuery, useMutation, useInfiniteQuery };