You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/components/ShortcutList.tsx

149 lines
4.7 KiB
TypeScript

import { useEffect } from "react";
import { locationService, shortcutService, userService } from "../services";
import { useAppSelector } from "../store";
import * as utils from "../helpers/utils";
4 years ago
import useToggle from "../hooks/useToggle";
import useLoading from "../hooks/useLoading";
import toastHelper from "./Toast";
import showCreateShortcutDialog from "./CreateShortcutDialog";
import "../less/shortcut-list.less";
4 years ago
interface Props {}
const ShortcutList: React.FC<Props> = () => {
const query = useAppSelector((state) => state.location.query);
const shortcuts = useAppSelector((state) => state.shortcut.shortcuts);
4 years ago
const loadingState = useLoading();
const pinnedShortcuts = shortcuts
.filter((s) => s.rowStatus === "ARCHIVED")
.sort((a, b) => utils.getTimeStampByDate(b.createdTs) - utils.getTimeStampByDate(a.createdTs));
const unpinnedShortcuts = shortcuts
.filter((s) => s.rowStatus === "NORMAL")
.sort((a, b) => utils.getTimeStampByDate(b.createdTs) - utils.getTimeStampByDate(a.createdTs));
const sortedShortcuts = pinnedShortcuts.concat(unpinnedShortcuts);
4 years ago
useEffect(() => {
shortcutService
.getMyAllShortcuts()
4 years ago
.catch(() => {
// do nth
})
.finally(() => {
loadingState.setFinish();
});
}, []);
return (
<div className="shortcuts-wrapper">
4 years ago
<p className="title-text">
<span className="normal-text">Shortcuts</span>
{userService.isNotVisitor() && (
<span className="btn" onClick={() => showCreateShortcutDialog()}>
<img src="/icons/add.svg" alt="add shortcut" />
</span>
)}
4 years ago
</p>
<div className="shortcuts-container">
{sortedShortcuts.map((s) => {
return <ShortcutContainer key={s.id} shortcut={s} isActive={s.id === Number(query?.shortcutId)} />;
4 years ago
})}
</div>
</div>
);
};
interface ShortcutContainerProps {
shortcut: Shortcut;
4 years ago
isActive: boolean;
}
const ShortcutContainer: React.FC<ShortcutContainerProps> = (props: ShortcutContainerProps) => {
const { shortcut, isActive } = props;
4 years ago
const [showConfirmDeleteBtn, toggleConfirmDeleteBtn] = useToggle(false);
const handleShortcutClick = () => {
4 years ago
if (isActive) {
locationService.setMemoShortcut(undefined);
4 years ago
} else {
if (!["/"].includes(locationService.getState().pathname)) {
4 years ago
locationService.setPathname("/");
}
locationService.setMemoShortcut(shortcut.id);
4 years ago
}
};
const handleDeleteMemoClick = async (event: React.MouseEvent) => {
event.stopPropagation();
if (showConfirmDeleteBtn) {
try {
await shortcutService.deleteShortcutById(shortcut.id);
4 years ago
} catch (error: any) {
toastHelper.error(error.message);
}
} else {
toggleConfirmDeleteBtn();
}
};
const handleEditShortcutBtnClick = (event: React.MouseEvent) => {
4 years ago
event.stopPropagation();
showCreateShortcutDialog(shortcut.id);
4 years ago
};
const handlePinShortcutBtnClick = async (event: React.MouseEvent) => {
4 years ago
event.stopPropagation();
try {
const shortcutPatch: ShortcutPatch = {
id: shortcut.id,
rowStatus: shortcut.rowStatus === "ARCHIVED" ? "NORMAL" : "ARCHIVED",
};
await shortcutService.patchShortcut(shortcutPatch);
4 years ago
} catch (error) {
// do nth
}
};
const handleDeleteBtnMouseLeave = () => {
toggleConfirmDeleteBtn(false);
};
return (
<>
<div className={`shortcut-container ${isActive ? "active" : ""}`} onClick={handleShortcutClick}>
<div className="shortcut-text-container">
<span className="shortcut-text">{shortcut.title}</span>
4 years ago
</div>
{userService.isNotVisitor() && (
<div className="btns-container">
<span className="action-btn toggle-btn">
<img className="icon-img" src="/icons/more.svg" />
</span>
<div className="action-btns-wrapper">
<div className="action-btns-container">
<span className="btn" onClick={handlePinShortcutBtnClick}>
{shortcut.rowStatus === "ARCHIVED" ? "Unpin" : "Pin"}
</span>
<span className="btn" onClick={handleEditShortcutBtnClick}>
Edit
</span>
<span
className={`btn delete-btn ${showConfirmDeleteBtn ? "final-confirm" : ""}`}
onClick={handleDeleteMemoClick}
onMouseLeave={handleDeleteBtnMouseLeave}
>
{showConfirmDeleteBtn ? "Delete!" : "Delete"}
</span>
</div>
4 years ago
</div>
</div>
)}
4 years ago
</div>
</>
);
};
export default ShortcutList;