forked from ArnabXD/Dropbox-Index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.tsx
151 lines (143 loc) · 4.81 KB
/
List.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import React, { useState, useEffect } from "react";
import {
DownloadIcon,
DocumentIcon,
FolderIcon,
ClipboardIcon,
} from "@heroicons/react/solid";
import { DropboxResponse, files } from "dropbox";
import { useClipboard } from "@chakra-ui/hooks";
import { Toaster, toast } from "react-hot-toast";
import { useRouter } from "next/router";
import { IconButton } from "./Icons";
import ky from "ky";
import prettyBytes from "pretty-bytes";
interface ListProps {
entries: (files.FileMetadataReference | files.FolderMetadataReference)[];
path?: string;
}
export const List = (props: ListProps) => {
const [link, setLink] = useState("");
const { onCopy } = useClipboard(link);
const router = useRouter();
useEffect(() => {
if (link) {
onCopy();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [link]);
const getDownloadLink = async (path: string) =>
await ky
.get("/api/download" + path)
.json<
DropboxResponse<
files.GetTemporaryLinkResult | files.GetTemporaryLinkError
>
>();
const copyToClipboard = async (
data: files.FileMetadataReference | files.FolderMetadataReference
) => {
if (data[".tag"] === "folder") {
setLink(window.location.origin + data.path_display);
} else {
let resp = await getDownloadLink(data.path_display!);
if ("link" in resp.result) {
setLink(resp.result.link);
} else {
throw new Error("Failed to generate download link");
}
}
};
return (
<div className="container max-w-screen-md mx-auto my-2">
<div className="grid grid-cols-12 p-4 mx-2 mt-2 bg-slate-800 border-b-2 border-b-slate-700">
<div className="col-span-9 font-bold">Name</div>
<div className="hidden md:flex md:col-span-2 flex-row font-bold justify-center">
Info
</div>
<div className="col-span-3 md:col-span-1 font-bold flex flex-row justify-center">
Actions
</div>
</div>
<Toaster />
{props.entries.map((file) => {
return (
<div
key={file.id}
className="grid grid-cols-12 px-4 py-2 mx-2 bg-slate-800 hover:bg-slate-700"
>
<div
className="col-span-9 md:col-span-9 flex flex-row items-center cursor-pointer"
onClick={() => {
if (file[".tag"] === "folder") {
router.push(file.path_display || "/");
} else {
getDownloadLink(file.path_display!).then((resp) => {
if ("link" in resp.result) {
router.push(resp.result.link);
} else {
toast.error("Failed to Download Generate Link");
}
});
}
}}
>
{file[".tag"] === "file" ? (
<DocumentIcon
height={18}
width={18}
className="flex-shrink-0"
/>
) : (
<FolderIcon height={18} width={18} className="flex-shrink-0" />
)}
<p className="mx-2 truncate">{file.name}</p>
</div>
<div className="hidden md:flex md:col-span-2 flex-row items-center justify-center font-thin text-sm">
{file[".tag"] === "file" && prettyBytes(file.size)}
</div>
<div className="col-span-3 md:col-span-1 flex flex-row">
<IconButton
onClick={() =>
toast.promise(
copyToClipboard(file),
{
success: "Link copied successfully",
error: "Failed to generate link",
loading: "Generating link",
},
{
style: {
backgroundColor: "#1e293b",
borderWidth: 2,
borderColor: "#475569",
color: "white",
},
}
)
}
>
<ClipboardIcon height={18} />
</IconButton>
{file[".tag"] === "file" && (
<IconButton
onClick={() => {
getDownloadLink(file.path_display!).then((resp) => {
if ("link" in resp.result) {
router.push(resp.result.link);
} else {
toast.error("Failed to Download Generate Link");
}
});
}}
>
<DownloadIcon height={18} />
</IconButton>
)}
</div>
</div>
);
})}
</div>
);
};