From bca8224f1649e9416e4dfc0dea833a58e0f801fd Mon Sep 17 00:00:00 2001 From: Daniil Date: Mon, 6 Apr 2026 01:18:09 +0300 Subject: [PATCH] feat: add useVideoMetadata hook for aspect ratio calculation --- .../CaptionSettingsStep/useVideoMetadata.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/features/project/CaptionSettingsStep/useVideoMetadata.ts diff --git a/src/features/project/CaptionSettingsStep/useVideoMetadata.ts b/src/features/project/CaptionSettingsStep/useVideoMetadata.ts new file mode 100644 index 0000000..75523c3 --- /dev/null +++ b/src/features/project/CaptionSettingsStep/useVideoMetadata.ts @@ -0,0 +1,41 @@ +import { useMemo } from "react" + +import api from "@shared/api" + +interface UseVideoMetadataResult { + aspectRatio: number + isLoading: boolean + isError: boolean +} + +const DEFAULT_ASPECT_RATIO = 16 / 9 + +export function useVideoMetadata(fileId: string | null): UseVideoMetadataResult { + const { data: mediaFile, isLoading, isError } = api.useQuery( + "get", + "/api/media/mediafiles/{media_file_id}/", + { + params: { + path: { + media_file_id: fileId ?? "", + }, + }, + }, + { + enabled: !!fileId, + } + ) + + const aspectRatio = useMemo(() => { + if (!mediaFile?.width || !mediaFile?.height) { + return DEFAULT_ASPECT_RATIO + } + return mediaFile.width / mediaFile.height + }, [mediaFile]) + + return { + aspectRatio, + isLoading, + isError, + } +}