import { useEffect, useState } from "react"; import * as utils from "../helpers/utils"; import useLoading from "../hooks/useLoading"; import { resourceService } from "../services"; import Dropdown from "./common/Dropdown"; import { generateDialog } from "./Dialog"; import { showCommonDialog } from "./Dialog/CommonDialog"; import toastHelper from "./Toast"; import Icon from "./Icon"; import "../less/resources-dialog.less"; interface Props extends DialogProps {} interface State { resources: Resource[]; isUploadingResource: boolean; } const ResourcesDialog: React.FC = (props: Props) => { const { destroy } = props; const loadingState = useLoading(); const [state, setState] = useState({ resources: [], isUploadingResource: false, }); useEffect(() => { fetchResources() .catch((error) => { toastHelper.error("Failed to fetch archived memos: ", error); }) .finally(() => { loadingState.setFinish(); }); }, []); const fetchResources = async () => { const data = await resourceService.getResourceList(); setState({ ...state, resources: data, }); }; const handleUploadFileBtnClick = async () => { if (state.isUploadingResource) { return; } const inputEl = document.createElement("input"); inputEl.type = "file"; inputEl.multiple = false; inputEl.accept = "image/png, image/gif, image/jpeg"; inputEl.onchange = async () => { if (!inputEl.files || inputEl.files.length === 0) { return; } setState({ ...state, isUploadingResource: true, }); const file = inputEl.files[0]; try { await resourceService.upload(file); } catch (error: any) { toastHelper.error("Failed to upload resource\n" + JSON.stringify(error, null, 4)); } finally { setState({ ...state, isUploadingResource: false, }); await fetchResources(); } }; inputEl.click(); }; const handleCopyResourceLinkBtnClick = (resource: Resource) => { utils.copyTextToClipboard(`${window.location.origin}/h/r/${resource.id}/${resource.filename}`); toastHelper.success("Succeed to copy resource link to clipboard"); }; const handleDeleteResourceBtnClick = (resource: Resource) => { showCommonDialog({ title: `Delete Resource`, content: `Are you sure to delete this resource? THIS ACTION IS IRREVERSIABLE.❗️`, style: "warning", onConfirm: async () => { await resourceService.deleteResourceById(resource.id); await fetchResources(); }, }); }; return ( <>

🌄 Resources

(👨‍💻WIP) View your static resources in memos. e.g. images
handleUploadFileBtnClick()}>
Upload
{loadingState.isLoading ? (

fetching data...

) : (
ID NAME TYPE
{state.resources.length === 0 ? (

No resource.

) : ( state.resources.map((resource) => (
{resource.id} {resource.filename} {resource.type}
)) )}
)}
); }; export default function showResourcesDialog() { generateDialog( { className: "resources-dialog", }, ResourcesDialog, {} ); }