mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-04 14:30:29 +03:00
* load auth metadata configs on startup * deep-link cross-signing reset button with oidc * deep-link manage devices and delete device with oidc * fix import typo
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
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;
|
|
|
|
try {
|
|
validatedAuthMetadata = validateAuthMetadata(authMetadata);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
|
|
return {
|
|
capabilities,
|
|
mediaConfig,
|
|
authMetadata: validatedAuthMetadata,
|
|
};
|
|
}, [mx])
|
|
);
|
|
|
|
const configs: ServerConfigs =
|
|
configsState.status === AsyncStatus.Success ? configsState.data : fallbackConfigs;
|
|
|
|
return children(configs);
|
|
}
|