frigate/web/src/components/button/DownloadVideoButton.tsx
Josh Hawkins b6e0e5698a
Proper i18n date/time handling (#17858)
* install date-fns-tz

* add date locale hook

* refactor formatUnixTimestampToDateTime

Use date-fns style instead of using strftime. This requires changing the i18n keys to the way date-fns represents dates (eg: "MMM d, h:mm:ss aaa"  instead of "%b %-d, %H:%M"

* refactor calendar to use new hook

* fix useFormattedTimestamp to use new formatUnixTimestampToDateTime date_format

* change i18n keys to new format

* fix timeline

* fix review

* fix explore

* fix metrics

* fix notifications

* fix face library

* clean up
2025-04-22 15:50:21 -06:00

63 lines
1.7 KiB
TypeScript

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";
import useSWR from "swr";
import { FrigateConfig } from "@/types/frigateConfig";
import { useDateLocale } from "@/hooks/use-date-locale";
import { useMemo } from "react";
type DownloadVideoButtonProps = {
source: string;
camera: string;
startTime: number;
className?: string;
};
export function DownloadVideoButton({
source,
camera,
startTime,
className,
}: DownloadVideoButtonProps) {
const { t } = useTranslation(["components/input"]);
const { data: config } = useSWR<FrigateConfig>("config");
const locale = useDateLocale();
const timeFormat = config?.ui.time_format === "24hour" ? "24hour" : "12hour";
const format = useMemo(() => {
return t(`time.formattedTimestampFilename.${timeFormat}`, { ns: "common" });
}, [t, timeFormat]);
const formattedDate = formatUnixTimestampToDateTime(startTime, {
date_format: format,
locale,
});
const filename = `${camera}_${formattedDate}.mp4`;
const handleDownloadStart = () => {
toast.success(t("button.downloadVideo.toast.success"), {
position: "top-center",
});
};
return (
<div className="flex justify-center">
<Button
asChild
className="flex items-center gap-2"
size="sm"
aria-label={t("button.downloadVideo.label")}
>
<a href={source} download={filename} onClick={handleDownloadStart}>
<FaDownload
className={cn("size-4 text-secondary-foreground", className)}
/>
</a>
</Button>
</div>
);
}