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

161 lines
4.8 KiB
TypeScript

import { useContext, useEffect } from "react";
4 years ago
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";
4 years ago
interface Props {}
const ShortcutList: React.FC<Props> = () => {
4 years ago
const {
shortcutState: { shortcuts },
4 years ago
locationState: {
query: { shortcutId },
4 years ago
},
} = useContext(appContext);
const loadingState = useLoading();
const sortedShortcuts = shortcuts
4 years ago
.sort((a, b) => utils.getTimeStampByDate(b.createdAt) - utils.getTimeStampByDate(a.createdAt))
.sort((a, b) => utils.getTimeStampByDate(b.updatedAt) - utils.getTimeStampByDate(a.updatedAt));
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>
4 years ago
<span className="btn" onClick={() => showCreateQueryDialog()}>
+
</span>
</p>
<Only when={loadingState.isSucceed && sortedShortcuts.length === 0}>
<div className="create-shortcut-btn-container">
4 years ago
<span className="btn" onClick={() => showCreateQueryDialog()}>
New shortcut
4 years ago
</span>
</div>
</Only>
<div className="shortcuts-container">
{sortedShortcuts.map((s) => {
return <ShortcutContainer key={s.id} shortcut={s} isActive={s.id === shortcutId} />;
4 years ago
})}
</div>
</div>
);
};
interface ShortcutContainerProps {
shortcut: Model.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("");
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.deleteShortcut(shortcut.id);
4 years ago
} catch (error: any) {
toastHelper.error(error.message);
}
} else {
toggleConfirmDeleteBtn();
}
};
const handleEditQueryBtnClick = (event: React.MouseEvent) => {
event.stopPropagation();
showCreateQueryDialog(shortcut.id);
4 years ago
};
const handlePinQueryBtnClick = async (event: React.MouseEvent) => {
event.stopPropagation();
try {
if (shortcut.rowStatus === "ARCHIVED") {
await shortcutService.unpinShortcut(shortcut.id);
shortcutService.editShortcut({
...shortcut,
rowStatus: "NORMAL",
4 years ago
});
} else {
await shortcutService.pinShortcut(shortcut.id);
shortcutService.editShortcut({
...shortcut,
rowStatus: "ARCHIVED",
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="icon-text">#</span> */}
<span className="shortcut-text">{shortcut.title}</span>
4 years ago
</div>
<div className="btns-container">
<span className="action-btn toggle-btn">
4 years ago
<img className="icon-img" src={`/icons/more${isActive ? "-white" : ""}.svg`} />
</span>
<div className="action-btns-wrapper">
4 years ago
<div className="action-btns-container">
<span className="btn" onClick={handlePinQueryBtnClick}>
{shortcut.rowStatus === "ARCHIVED" ? "Unpin" : "Pin"}
4 years ago
</span>
<span className="btn" onClick={handleEditQueryBtnClick}>
Edit
4 years ago
</span>
<span
className={`btn delete-btn ${showConfirmDeleteBtn ? "final-confirm" : ""}`}
onClick={handleDeleteMemoClick}
onMouseLeave={handleDeleteBtnMouseLeave}
>
{showConfirmDeleteBtn ? "Delete!" : "Delete"}
4 years ago
</span>
</div>
</div>
</div>
</div>
</>
);
};
export default ShortcutList;