Files
frigate/web/src/components/button/DownloadVideoButton.tsx
T

64 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-10-14 15:23:02 -06:00
import { Button } from "@/components/ui/button";
import { toast } from "sonner";
import { FaDownload } from "react-icons/fa";
import { formatUnixTimestampToDateTime } from "@/utils/dateUtil";
import { cn } from "@/lib/utils";
import { useTranslation } from "react-i18next";
2025-04-22 16:50:21 -05:00
import useSWR from "swr";
import { FrigateConfig } from "@/types/frigateConfig";
import { useDateLocale } from "@/hooks/use-date-locale";
2026-03-29 14:03:07 -05:00
import { useTimeFormat } from "@/hooks/use-date-utils";
2025-04-22 16:50:21 -05:00
import { useMemo } from "react";
2024-10-14 15:23:02 -06:00
type DownloadVideoButtonProps = {
source: string;
camera: string;
startTime: number;
className?: string;
2024-10-14 15:23:02 -06:00
};
export function DownloadVideoButton({
source,
camera,
startTime,
className,
2024-10-14 15:23:02 -06:00
}: DownloadVideoButtonProps) {
const { t } = useTranslation(["components/input"]);
2025-04-22 16:50:21 -05:00
const { data: config } = useSWR<FrigateConfig>("config");
const locale = useDateLocale();
2026-03-29 14:03:07 -05:00
const timeFormat = useTimeFormat(config);
2025-04-22 16:50:21 -05:00
const format = useMemo(() => {
return t(`time.formattedTimestampFilename.${timeFormat}`, { ns: "common" });
}, [t, timeFormat]);
const formattedDate = formatUnixTimestampToDateTime(startTime, {
2025-04-22 16:50:21 -05:00
date_format: format,
locale,
});
const filename = `${camera}_${formattedDate}.mp4`;
2024-10-14 15:23:02 -06:00
const handleDownloadStart = () => {
toast.success(t("button.downloadVideo.toast.success"), {
position: "top-center",
});
};
2024-10-14 15:23:02 -06:00
return (
<div className="flex justify-center">
<Button
asChild
2024-10-14 15:23:02 -06:00
className="flex items-center gap-2"
size="sm"
aria-label={t("button.downloadVideo.label")}
2024-10-14 15:23:02 -06:00
>
<a href={source} download={filename} onClick={handleDownloadStart}>
2024-11-12 06:37:25 -06:00
<FaDownload
className={cn("size-4 text-secondary-foreground", className)}
/>
</a>
2024-10-14 15:23:02 -06:00
</Button>
</div>
);
}