Showing
2 changed files
with
37 additions
and
8 deletions
| 1 | -import React, { Fragment } from "react"; | 1 | +import React from "react"; |
| 2 | import { FileItem } from "./useFileList"; | 2 | import { FileItem } from "./useFileList"; |
| 3 | import { Link } from "react-router-dom"; | 3 | import { Link } from "react-router-dom"; |
| 4 | +import { Button } from "antd"; | ||
| 4 | 5 | ||
| 5 | import { FolderFilled, FileFilled } from "@ant-design/icons"; | 6 | import { FolderFilled, FileFilled } from "@ant-design/icons"; |
| 7 | +import { useDownload } from "./useDownload"; | ||
| 6 | 8 | ||
| 7 | export function FileListItem({ item }: { item: FileItem }) { | 9 | export function FileListItem({ item }: { item: FileItem }) { |
| 10 | + const download = useDownload(); | ||
| 8 | return item.is_folder ? ( | 11 | return item.is_folder ? ( |
| 9 | - <Fragment> | 12 | + <Link to={`/folder/${item.id}`}> |
| 10 | - <Link to={`/folder/${item.id}`}> | 13 | + <FolderFilled /> {item.name} |
| 11 | - <FolderFilled /> {item.name} | 14 | + </Link> |
| 12 | - </Link> | ||
| 13 | - </Fragment> | ||
| 14 | ) : ( | 15 | ) : ( |
| 15 | - <Fragment> | 16 | + <Button |
| 17 | + type="link" | ||
| 18 | + size="small" | ||
| 19 | + onClick={() => download(item.id)} | ||
| 20 | + style={{ padding: 0 }} | ||
| 21 | + > | ||
| 16 | <FileFilled /> {item.name} | 22 | <FileFilled /> {item.name} |
| 17 | - </Fragment> | 23 | + </Button> |
| 18 | ); | 24 | ); |
| 19 | } | 25 | } | ... | ... |
frontend/src/file/useDownload.ts
0 → 100644
| 1 | +import { useApi } from "util/useApi"; | ||
| 2 | +import { useCallback } from "react"; | ||
| 3 | + | ||
| 4 | +function downloadURL(url: string, name: string) { | ||
| 5 | + const link = document.createElement("a"); | ||
| 6 | + link.setAttribute("download", name); | ||
| 7 | + link.href = url; | ||
| 8 | + link.target = "_blank"; | ||
| 9 | + link.click(); | ||
| 10 | +} | ||
| 11 | + | ||
| 12 | +export function useDownload() { | ||
| 13 | + const api = useApi(); | ||
| 14 | + const download = useCallback( | ||
| 15 | + async (id: number) => { | ||
| 16 | + const response = await api.get(`/items/${id}/`).json<any>(); | ||
| 17 | + const { signed_url, name } = response.data; | ||
| 18 | + downloadURL(signed_url, name); | ||
| 19 | + }, | ||
| 20 | + [api] | ||
| 21 | + ); | ||
| 22 | + return download; | ||
| 23 | +} |
-
Please register or login to post a comment