import { createContext, useContext } from 'react'; export type HashRouterConfig = { enabled?: boolean; basename?: string; }; export type ClientConfig = { defaultHomeserver?: number; homeserverList?: string[]; allowCustomHomeservers?: boolean; featuredCommunities?: { openAsDefault?: boolean; spaces?: string[]; rooms?: string[]; servers?: string[]; }; hashRouter?: HashRouterConfig; gifApiKey?: string; /** * [Gitea #43] Absolute https URL of the Element Call page on its own origin. * Unset: the bundled copy on this origin. Ignored in the desktop app. */ elementCallUrl?: string; /** * [Gitea #43] Desktop only: the loopback origin the desktop app's local * server also answers on (e.g. "http://127.0.0.1:44548"), to load the * bundled call page from a different origin than the app * ("http://localhost:44548"). Set by cinny-desktop together with the server * and CSP changes it needs; unset keeps the same-origin call page. */ desktopCallOrigin?: string; /** * [Gitea #124] Uptime Kuma status page per homeserver, for the status banner: * { "": { url, slug, groups?: { homeserver, calls, login } } }. * Homeservers not listed never contact Kuma. */ statusPages?: Record; /** * Absolute https URL of the public web app (e.g. https://chat.lotusguild.org). * The desktop app sets it so it can hand calls it can't make to the browser. */ webAppUrl?: string; }; const ClientConfigContext = createContext(null); export const ClientConfigProvider = ClientConfigContext.Provider; export function useClientConfig(): ClientConfig { const config = useContext(ClientConfigContext); if (!config) throw new Error('Client config are not provided!'); return config; } export const clientDefaultServer = (clientConfig: ClientConfig): string => clientConfig.homeserverList?.[clientConfig.defaultHomeserver ?? 0] ?? 'matrix.org'; export const clientAllowedServer = (clientConfig: ClientConfig, server: string): boolean => { const { homeserverList, allowCustomHomeservers } = clientConfig; if (allowCustomHomeservers) return true; return homeserverList?.includes(server) === true; };