67 lines
1.6 KiB
TypeScript
67 lines
1.6 KiB
TypeScript
import { createContext, useContext } from 'react';
|
|
|
|
export type HashRouterConfig = {
|
|
enabled?: boolean;
|
|
basename?: string;
|
|
};
|
|
|
|
export type PushConfig = {
|
|
vapidPublicKey: string;
|
|
gatewayUrl: string;
|
|
webAppId?: string;
|
|
fcmAppId?: string;
|
|
};
|
|
|
|
export type BotConfig = {
|
|
id?: string;
|
|
mxid?: string;
|
|
name?: string;
|
|
/** Optional operator override of the localized description. Falls back
|
|
* to i18n key `Bots.description.<id>` when absent. */
|
|
description?: string;
|
|
experience?: {
|
|
type?: string;
|
|
url?: string;
|
|
commandPrefix?: string;
|
|
};
|
|
};
|
|
|
|
export type ClientConfig = {
|
|
defaultHomeserver?: number;
|
|
homeserverList?: string[];
|
|
allowCustomHomeservers?: boolean;
|
|
|
|
featuredCommunities?: {
|
|
openAsDefault?: boolean;
|
|
spaces?: string[];
|
|
rooms?: string[];
|
|
servers?: string[];
|
|
};
|
|
|
|
hashRouter?: HashRouterConfig;
|
|
|
|
push?: PushConfig;
|
|
|
|
bots?: BotConfig[];
|
|
};
|
|
|
|
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;
|
|
};
|