Files
cinny/src/app/hooks/useClientConfig.ts
T

65 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-01-21 23:50:56 +11:00
import { createContext, useContext } from 'react';
export type HashRouterConfig = {
enabled?: boolean;
basename?: string;
};
2024-01-21 23:50:56 +11:00
export type ClientConfig = {
defaultHomeserver?: number;
homeserverList?: string[];
allowCustomHomeservers?: boolean;
featuredCommunities?: {
openAsDefault?: boolean;
spaces?: string[];
rooms?: string[];
servers?: string[];
2024-01-21 23:50:56 +11:00
};
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;
/**
* 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;
2024-01-21 23:50:56 +11:00
};
const ClientConfigContext = createContext<ClientConfig | null>(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;
};