import { useContext, useEffect } from "react"; import appContext from "../stores/appContext"; import useToggle from "../hooks/useToggle"; import useLoading from "../hooks/useLoading"; import Only from "./common/OnlyWhen"; import utils from "../helpers/utils"; import toastHelper from "./Toast"; import { locationService, shortcutService } from "../services"; import showCreateQueryDialog from "./CreateShortcutDialog"; import "../less/shortcut-list.less"; interface Props {} const ShortcutList: React.FC = () => { const { shortcutState: { shortcuts }, locationState: { query: { shortcutId }, }, } = useContext(appContext); const loadingState = useLoading(); const sortedShortcuts = shortcuts .sort((a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt)) .sort((a, b) => utils.getTimeStampByDate(b.updatedAt) - utils.getTimeStampByDate(a.updatedAt)); useEffect(() => { shortcutService .getMyAllShortcuts() .catch(() => { // do nth }) .finally(() => { loadingState.setFinish(); }); }, []); return (

Shortcuts showCreateQueryDialog()}> +

showCreateQueryDialog()}> New shortcut
{sortedShortcuts.map((s) => { return ; })}
); }; interface ShortcutContainerProps { shortcut: Model.Shortcut; isActive: boolean; } const ShortcutContainer: React.FC = (props: ShortcutContainerProps) => { const { shortcut, isActive } = props; const [showConfirmDeleteBtn, toggleConfirmDeleteBtn] = useToggle(false); const handleShortcutClick = () => { if (isActive) { locationService.setMemoShortcut(""); } else { if (!["/"].includes(locationService.getState().pathname)) { locationService.setPathname("/"); } locationService.setMemoShortcut(shortcut.id); } }; const handleDeleteMemoClick = async (event: React.MouseEvent) => { event.stopPropagation(); if (showConfirmDeleteBtn) { try { await shortcutService.deleteShortcut(shortcut.id); } catch (error: any) { toastHelper.error(error.message); } } else { toggleConfirmDeleteBtn(); } }; const handleEditQueryBtnClick = (event: React.MouseEvent) => { event.stopPropagation(); showCreateQueryDialog(shortcut.id); }; const handlePinQueryBtnClick = async (event: React.MouseEvent) => { event.stopPropagation(); try { if (shortcut.rowStatus === "ARCHIVED") { await shortcutService.unpinShortcut(shortcut.id); shortcutService.editShortcut({ ...shortcut, rowStatus: "NORMAL", }); } else { await shortcutService.pinShortcut(shortcut.id); shortcutService.editShortcut({ ...shortcut, rowStatus: "ARCHIVED", }); } } catch (error) { // do nth } }; const handleDeleteBtnMouseLeave = () => { toggleConfirmDeleteBtn(false); }; return ( <>
{/* # */} {shortcut.title}
{shortcut.rowStatus === "ARCHIVED" ? "Unpin" : "Pin"} Edit {showConfirmDeleteBtn ? "Delete!" : "Delete"}
); }; export default ShortcutList;