Add ability to rename exports (#10791)

* Add ability to rename exports

* Address feedback
This commit is contained in:
Nicolas Mowen 2024-04-03 08:02:07 -06:00 committed by GitHub
parent 15dcf1fcc8
commit 476a900708
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 156 additions and 59 deletions

View File

@ -61,7 +61,7 @@ class RecordingExporter(threading.Thread):
)
file_name = (
self.user_provided_name
or f"{self.camera}@{self.get_datetime_from_timestamp(self.start_time)}__{self.get_datetime_from_timestamp(self.end_time)}"
or f"{self.camera}_{self.get_datetime_from_timestamp(self.start_time)}__{self.get_datetime_from_timestamp(self.end_time)}"
)
file_path = f"{EXPORT_DIR}/in_progress.{file_name}.mp4"
final_file_path = f"{EXPORT_DIR}/{file_name}.mp4"

View File

@ -1,21 +1,25 @@
import { baseUrl } from "@/api/baseUrl";
import ActivityIndicator from "../indicators/activity-indicator";
import { LuTrash } from "react-icons/lu";
import { LuPencil, LuTrash } from "react-icons/lu";
import { Button } from "../ui/button";
import { useMemo, useRef, useState } from "react";
import { isDesktop } from "react-device-detect";
import { FaPlay } from "react-icons/fa";
import Chip from "../indicators/Chip";
import { Skeleton } from "../ui/skeleton";
import { Dialog, DialogContent, DialogFooter, DialogTitle } from "../ui/dialog";
import { Input } from "../ui/input";
import useKeyboardListener from "@/hooks/use-keyboard-listener";
type ExportProps = {
file: {
name: string;
};
onRename: (original: string, update: string) => void;
onDelete: (file: string) => void;
};
export default function ExportCard({ file, onDelete }: ExportProps) {
export default function ExportCard({ file, onRename, onDelete }: ExportProps) {
const videoRef = useRef<HTMLVideoElement | null>(null);
const [hovered, setHovered] = useState(false);
const [playing, setPlaying] = useState(false);
@ -25,7 +29,70 @@ export default function ExportCard({ file, onDelete }: ExportProps) {
[file.name],
);
// editing name
const [editName, setEditName] = useState<{
original: string;
update: string;
}>();
useKeyboardListener(
editName != undefined ? ["Enter"] : [],
(_, down, repeat) => {
if (down && !repeat && editName && editName.update.length > 0) {
onRename(editName.original, editName.update.replaceAll(" ", "_"));
setEditName(undefined);
}
},
);
return (
<>
<Dialog
open={editName != undefined}
onOpenChange={(open) => {
if (!open) {
setEditName(undefined);
}
}}
>
<DialogContent>
<DialogTitle>Rename Export</DialogTitle>
{editName && (
<>
<Input
className="mt-3"
type="search"
placeholder={editName?.original}
value={editName?.update}
onChange={(e) =>
setEditName({
original: editName.original ?? "",
update: e.target.value,
})
}
/>
<DialogFooter>
<Button
size="sm"
variant="select"
disabled={(editName?.update?.length ?? 0) == 0}
onClick={() => {
onRename(
editName.original,
editName.update.replaceAll(" ", "_"),
);
setEditName(undefined);
}}
>
Save
</Button>
</DialogFooter>
</>
)}
</DialogContent>
</Dialog>
<div
className="relative aspect-video bg-black rounded-2xl flex justify-center items-center"
onMouseEnter={
@ -34,17 +101,30 @@ export default function ExportCard({ file, onDelete }: ExportProps) {
onMouseLeave={
isDesktop && !inProgress ? () => setHovered(false) : undefined
}
onClick={isDesktop || inProgress ? undefined : () => setHovered(!hovered)}
onClick={
isDesktop || inProgress ? undefined : () => setHovered(!hovered)
}
>
{!playing && hovered && (
{hovered && (
<>
{!playing && (
<div className="absolute inset-0 z-10 bg-black bg-opacity-60 rounded-2xl" />
)}
<div className="absolute top-1 right-1 flex items-center gap-2">
<Chip
className="absolute top-2 right-2 bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500 rounded-md cursor-pointer"
className="bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500 rounded-md cursor-pointer"
onClick={() => setEditName({ original: file.name, update: "" })}
>
<LuPencil className="size-4 text-white" />
</Chip>
<Chip
className="bg-gradient-to-br from-gray-400 to-gray-500 bg-gray-500 rounded-md cursor-pointer"
onClick={() => onDelete(file.name)}
>
<LuTrash className="w-4 h-4 text-destructive fill-destructive" />
<LuTrash className="size-4 text-destructive fill-destructive" />
</Chip>
</div>
{!playing && (
<Button
className="absolute left-1/2 -translate-x-1/2 top-1/2 -translate-y-1/2 w-20 h-20 z-20 text-white hover:text-white hover:bg-transparent"
variant="ghost"
@ -55,6 +135,7 @@ export default function ExportCard({ file, onDelete }: ExportProps) {
>
<FaPlay />
</Button>
)}
</>
)}
{inProgress ? (
@ -78,10 +159,13 @@ export default function ExportCard({ file, onDelete }: ExportProps) {
{!playing && (
<div className="absolute bottom-0 inset-x-0 rounded-b-l z-10 h-[20%] bg-gradient-to-t from-black/60 to-transparent pointer-events-none rounded-2xl">
<div className="flex h-full justify-between items-end mx-3 pb-1 text-white text-sm capitalize">
{file.name.substring(0, file.name.length - 4).replaceAll("_", " ")}
{file.name
.substring(0, file.name.length - 4)
.replaceAll("_", " ")}
</div>
</div>
)}
</div>
</>
);
}

View File

@ -26,6 +26,18 @@ function Export() {
const [deleteClip, setDeleteClip] = useState<string | undefined>();
const onHandleRename = useCallback(
(original: string, update: string) => {
axios.patch(`export/${original}/${update}`).then((response) => {
if (response.status == 200) {
setDeleteClip(undefined);
mutate();
}
});
},
[mutate],
);
const onHandleDelete = useCallback(() => {
if (!deleteClip) {
return;
@ -61,13 +73,14 @@ function Export() {
</AlertDialogContent>
</AlertDialog>
<div className="size-full overflow-hidden">
<div className="w-full overflow-hidden">
{exports && (
<div className="size-full grid gap-2 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 overflow-y-auto">
{Object.values(exports).map((item) => (
<ExportCard
key={item.name}
file={item}
onRename={onHandleRename}
onDelete={(file) => setDeleteClip(file)}
/>
))}