mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-09-13 14:22:25 +03:00

* fix set power level broken after sdk update * add media authentication hook * fix service worker types * fix service worker not working in dev mode * fix env mode check when registering sw
37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import { ReactNode, useCallback, useEffect } from 'react';
|
|
import { IThumbnailContent } from '../../../../types/matrix/common';
|
|
import { useMatrixClient } from '../../../hooks/useMatrixClient';
|
|
import { AsyncStatus, useAsyncCallback } from '../../../hooks/useAsyncCallback';
|
|
import { getFileSrcUrl } from './util';
|
|
import { mxcUrlToHttp } from '../../../utils/matrix';
|
|
import { useMediaAuthentication } from '../../../hooks/useMediaAuthentication';
|
|
|
|
export type ThumbnailContentProps = {
|
|
info: IThumbnailContent;
|
|
renderImage: (src: string) => ReactNode;
|
|
};
|
|
export function ThumbnailContent({ info, renderImage }: ThumbnailContentProps) {
|
|
const mx = useMatrixClient();
|
|
const useAuthentication = useMediaAuthentication();
|
|
|
|
const [thumbSrcState, loadThumbSrc] = useAsyncCallback(
|
|
useCallback(() => {
|
|
const thumbInfo = info.thumbnail_info;
|
|
const thumbMxcUrl = info.thumbnail_file?.url ?? info.thumbnail_url;
|
|
if (typeof thumbMxcUrl !== 'string' || typeof thumbInfo?.mimetype !== 'string') {
|
|
throw new Error('Failed to load thumbnail');
|
|
}
|
|
return getFileSrcUrl(
|
|
mxcUrlToHttp(mx, thumbMxcUrl, useAuthentication) ?? '',
|
|
thumbInfo.mimetype,
|
|
info.thumbnail_file
|
|
);
|
|
}, [mx, info, useAuthentication])
|
|
);
|
|
|
|
useEffect(() => {
|
|
loadThumbSrc();
|
|
}, [loadThumbSrc]);
|
|
|
|
return thumbSrcState.status === AsyncStatus.Success ? renderImage(thumbSrcState.data) : null;
|
|
}
|