2025-07-15 18:10:16 +05:30
|
|
|
import { ReactNode, useCallback, useMemo } from 'react';
|
|
|
|
|
import { Capabilities, validateAuthMetadata, ValidatedAuthMetadata } from 'matrix-js-sdk';
|
|
|
|
|
import { AsyncStatus, useAsyncCallbackValue } from '../hooks/useAsyncCallback';
|
|
|
|
|
import { useMatrixClient } from '../hooks/useMatrixClient';
|
|
|
|
|
import { MediaConfig } from '../hooks/useMediaConfig';
|
|
|
|
|
import { promiseFulfilledResult } from '../utils/common';
|
|
|
|
|
|
|
|
|
|
export type ServerConfigs = {
|
|
|
|
|
capabilities?: Capabilities;
|
|
|
|
|
mediaConfig?: MediaConfig;
|
|
|
|
|
authMetadata?: ValidatedAuthMetadata;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ServerConfigsLoaderProps = {
|
|
|
|
|
children: (configs: ServerConfigs) => ReactNode;
|
|
|
|
|
};
|
|
|
|
|
export function ServerConfigsLoader({ children }: ServerConfigsLoaderProps) {
|
|
|
|
|
const mx = useMatrixClient();
|
|
|
|
|
const fallbackConfigs = useMemo(() => ({}), []);
|
|
|
|
|
|
|
|
|
|
const [configsState] = useAsyncCallbackValue<ServerConfigs, unknown>(
|
|
|
|
|
useCallback(async () => {
|
|
|
|
|
const result = await Promise.allSettled([
|
|
|
|
|
mx.getCapabilities(),
|
|
|
|
|
mx.getMediaConfig(),
|
|
|
|
|
mx.getAuthMetadata(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const capabilities = promiseFulfilledResult(result[0]);
|
|
|
|
|
const mediaConfig = promiseFulfilledResult(result[1]);
|
|
|
|
|
const authMetadata = promiseFulfilledResult(result[2]);
|
|
|
|
|
let validatedAuthMetadata: ValidatedAuthMetadata | undefined;
|
|
|
|
|
|
2026-05-22 14:24:06 -04:00
|
|
|
// Only validate if the server returned metadata — a rejected promise means no native
|
|
|
|
|
// Matrix OIDC (MSC3861/MAS), which is normal for servers using traditional SSO.
|
|
|
|
|
if (authMetadata !== undefined) {
|
|
|
|
|
try {
|
|
|
|
|
validatedAuthMetadata = validateAuthMetadata(authMetadata);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
2025-07-15 18:10:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
capabilities,
|
|
|
|
|
mediaConfig,
|
|
|
|
|
authMetadata: validatedAuthMetadata,
|
|
|
|
|
};
|
2026-05-21 23:30:50 -04:00
|
|
|
}, [mx]),
|
2025-07-15 18:10:16 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const configs: ServerConfigs =
|
|
|
|
|
configsState.status === AsyncStatus.Success ? configsState.data : fallbackConfigs;
|
|
|
|
|
|
|
|
|
|
return children(configs);
|
|
|
|
|
}
|