2023-12-13 00:21:42 +01:00
|
|
|
import { baseUrl } from "@/api/baseUrl";
|
|
|
|
import ExportCard from "@/components/card/ExportCard";
|
|
|
|
import {
|
|
|
|
AlertDialog,
|
|
|
|
AlertDialogCancel,
|
|
|
|
AlertDialogContent,
|
|
|
|
AlertDialogDescription,
|
|
|
|
AlertDialogFooter,
|
|
|
|
AlertDialogHeader,
|
|
|
|
AlertDialogTitle,
|
|
|
|
} from "@/components/ui/alert-dialog";
|
|
|
|
import { Button } from "@/components/ui/button";
|
2024-04-07 22:35:45 +02:00
|
|
|
import { Input } from "@/components/ui/input";
|
2023-12-13 00:21:42 +01:00
|
|
|
import axios from "axios";
|
2024-04-07 22:35:45 +02:00
|
|
|
import { useCallback, useMemo, useState } from "react";
|
2023-12-13 00:21:42 +01:00
|
|
|
import useSWR from "swr";
|
|
|
|
|
|
|
|
type ExportItem = {
|
|
|
|
name: string;
|
|
|
|
};
|
2023-12-08 14:33:22 +01:00
|
|
|
|
|
|
|
function Export() {
|
2024-04-07 22:35:45 +02:00
|
|
|
const { data: allExports, mutate } = useSWR<ExportItem[]>(
|
2023-12-13 00:21:42 +01:00
|
|
|
"exports/",
|
2024-02-28 23:23:56 +01:00
|
|
|
(url: string) => axios({ baseURL: baseUrl, url }).then((res) => res.data),
|
2023-12-13 00:21:42 +01:00
|
|
|
);
|
|
|
|
|
2024-04-07 22:35:45 +02:00
|
|
|
// Search
|
2023-12-13 00:21:42 +01:00
|
|
|
|
2024-04-07 22:35:45 +02:00
|
|
|
const [search, setSearch] = useState("");
|
|
|
|
|
|
|
|
const exports = useMemo(() => {
|
|
|
|
if (!search || !allExports) {
|
|
|
|
return allExports;
|
|
|
|
}
|
|
|
|
|
|
|
|
return allExports.filter((exp) =>
|
|
|
|
exp.name
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(search.toLowerCase().replaceAll(" ", "_")),
|
|
|
|
);
|
|
|
|
}, [allExports, search]);
|
|
|
|
|
|
|
|
// Deleting
|
|
|
|
|
|
|
|
const [deleteClip, setDeleteClip] = useState<string | undefined>();
|
2024-04-03 16:02:07 +02:00
|
|
|
|
2023-12-13 00:21:42 +01:00
|
|
|
const onHandleDelete = useCallback(() => {
|
|
|
|
if (!deleteClip) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
axios.delete(`export/${deleteClip}`).then((response) => {
|
|
|
|
if (response.status == 200) {
|
|
|
|
setDeleteClip(undefined);
|
|
|
|
mutate();
|
|
|
|
}
|
|
|
|
});
|
2024-02-28 23:23:56 +01:00
|
|
|
}, [deleteClip, mutate]);
|
2023-12-13 00:21:42 +01:00
|
|
|
|
2024-04-07 22:35:45 +02:00
|
|
|
// Renaming
|
|
|
|
|
|
|
|
const onHandleRename = useCallback(
|
|
|
|
(original: string, update: string) => {
|
|
|
|
axios.patch(`export/${original}/${update}`).then((response) => {
|
|
|
|
if (response.status == 200) {
|
|
|
|
setDeleteClip(undefined);
|
|
|
|
mutate();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
[mutate],
|
|
|
|
);
|
|
|
|
|
2023-12-08 14:33:22 +01:00
|
|
|
return (
|
2024-04-07 22:35:45 +02:00
|
|
|
<div className="size-full p-2 overflow-hidden flex flex-col gap-2">
|
2023-12-13 00:21:42 +01:00
|
|
|
<AlertDialog
|
|
|
|
open={deleteClip != undefined}
|
2024-02-28 23:23:56 +01:00
|
|
|
onOpenChange={() => setDeleteClip(undefined)}
|
2023-12-13 00:21:42 +01:00
|
|
|
>
|
|
|
|
<AlertDialogContent>
|
|
|
|
<AlertDialogHeader>
|
|
|
|
<AlertDialogTitle>Delete Export</AlertDialogTitle>
|
|
|
|
<AlertDialogDescription>
|
|
|
|
Confirm deletion of {deleteClip}.
|
|
|
|
</AlertDialogDescription>
|
|
|
|
</AlertDialogHeader>
|
|
|
|
<AlertDialogFooter>
|
|
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
|
|
<Button variant="destructive" onClick={() => onHandleDelete()}>
|
|
|
|
Delete
|
|
|
|
</Button>
|
|
|
|
</AlertDialogFooter>
|
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialog>
|
|
|
|
|
2024-04-07 22:35:45 +02:00
|
|
|
<div className="w-full p-2 flex items-center justify-center">
|
|
|
|
<Input
|
|
|
|
className="w-full md:w-1/3 bg-muted"
|
|
|
|
placeholder="Search"
|
|
|
|
value={search}
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
2024-04-03 16:02:07 +02:00
|
|
|
<div className="w-full overflow-hidden">
|
2024-04-07 22:35:45 +02:00
|
|
|
{allExports && exports && (
|
2024-03-10 14:25:16 +01:00
|
|
|
<div className="size-full grid gap-2 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 overflow-y-auto">
|
2024-04-07 22:35:45 +02:00
|
|
|
{Object.values(allExports).map((item) => (
|
2023-12-13 00:21:42 +01:00
|
|
|
<ExportCard
|
|
|
|
key={item.name}
|
2024-04-07 22:35:45 +02:00
|
|
|
className={
|
|
|
|
search == "" || exports.includes(item) ? "" : "hidden"
|
|
|
|
}
|
2023-12-13 00:21:42 +01:00
|
|
|
file={item}
|
2024-04-03 16:02:07 +02:00
|
|
|
onRename={onHandleRename}
|
2023-12-13 00:21:42 +01:00
|
|
|
onDelete={(file) => setDeleteClip(file)}
|
|
|
|
/>
|
|
|
|
))}
|
2024-03-10 14:25:16 +01:00
|
|
|
</div>
|
2023-12-13 00:21:42 +01:00
|
|
|
)}
|
|
|
|
</div>
|
2024-02-24 01:26:26 +01:00
|
|
|
</div>
|
2023-12-08 14:33:22 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Export;
|