Files
cinny/src/app/components/authenticated-media/AuthenticatedMedia.tsx
T
litruv 17d1eef572
Checks / checks (push) Successful in 2m18s
Trigger cinny-mobile / dispatch (push) Successful in 1s
Turn off the accepted rules and fix the design tokens, alt text, and last type errors. [skip build]
2026-11-03 01:10:06 +11:00

45 lines
1.8 KiB
TypeScript

import React, { ImgHTMLAttributes, forwardRef } from 'react';
import { useAuthenticatedMediaUrl } from '../../hooks/useAuthenticatedMediaUrl';
import { useMediaAuthentication } from '../../hooks/useMediaAuthentication';
export interface AuthenticatedMediaProps extends Omit<ImgHTMLAttributes<HTMLImageElement>, 'src'> {
/** The source URL of the media (may be an authenticated media URL) */
src?: string;
/** Override for authentication - if not provided, uses useMediaAuthentication hook */
useAuthentication?: boolean;
}
/**
* Image component that handles Matrix authenticated media.
* Automatically fetches authenticated media URLs with the proper Authorization header
* and renders them as blob URLs.
*/
export const AuthenticatedMedia = forwardRef<HTMLImageElement, AuthenticatedMediaProps>(
({ src, alt, useAuthentication: useAuthOverride, ...props }, ref) => {
const useAuthFromServer = useMediaAuthentication();
const useAuthentication = useAuthOverride ?? useAuthFromServer;
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
return <img ref={ref} alt={alt} {...props} src={authenticatedSrc} />;
},
);
AuthenticatedMedia.displayName = 'AuthenticatedMedia';
/**
* A simpler authenticated img component for use in places like custom HTML parsers.
* This component handles its own authentication state.
*/
export type AuthenticatedImgProps = ImgHTMLAttributes<HTMLImageElement>;
export const AuthenticatedImg = forwardRef<HTMLImageElement, AuthenticatedImgProps>(
({ src, alt, ...props }, ref) => {
const useAuthentication = useMediaAuthentication();
const authenticatedSrc = useAuthenticatedMediaUrl(src, useAuthentication);
return <img ref={ref} alt={alt} {...props} src={authenticatedSrc} />;
},
);
AuthenticatedImg.displayName = 'AuthenticatedImg';