import { useEffect } from "react";
import { useTranslation } from "react-i18next";
import { locationService, shortcutService } from "../services";
import { useAppSelector } from "../store";
import * as utils from "../helpers/utils";
import useToggle from "../hooks/useToggle";
import useLoading from "../hooks/useLoading";
import { closeSidebar } from "../helpers/utils";
import Icon from "./Icon";
import toastHelper from "./Toast";
import showCreateShortcutDialog from "./CreateShortcutDialog";
import "../less/shortcut-list.less";
const ShortcutList = () => {
const query = useAppSelector((state) => state.location.query);
const shortcuts = useAppSelector((state) => state.shortcut.shortcuts);
const loadingState = useLoading();
const { t } = useTranslation();
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);
useEffect(() => {
shortcutService
.getMyAllShortcuts()
.catch(() => {
// do nth
})
.finally(() => {
loadingState.setFinish();
});
}, []);
return (
{t("common.shortcuts")}
{sortedShortcuts.map((s) => {
return ;
})}
);
};
interface ShortcutContainerProps {
shortcut: Shortcut;
isActive: boolean;
}
const ShortcutContainer: React.FC = (props: ShortcutContainerProps) => {
const { shortcut, isActive } = props;
const { t } = useTranslation();
const [showConfirmDeleteBtn, toggleConfirmDeleteBtn] = useToggle(false);
const handleShortcutClick = () => {
closeSidebar();
if (isActive) {
locationService.setMemoShortcut(undefined);
} else {
locationService.setMemoShortcut(shortcut.id);
}
};
const handleDeleteMemoClick = async (event: React.MouseEvent) => {
event.stopPropagation();
if (showConfirmDeleteBtn) {
try {
await shortcutService.deleteShortcutById(shortcut.id);
} catch (error: any) {
console.error(error);
toastHelper.error(error.response.data.message);
}
} else {
toggleConfirmDeleteBtn();
}
};
const handleEditShortcutBtnClick = (event: React.MouseEvent) => {
event.stopPropagation();
showCreateShortcutDialog(shortcut.id);
};
const handlePinShortcutBtnClick = async (event: React.MouseEvent) => {
event.stopPropagation();
try {
const shortcutPatch: ShortcutPatch = {
id: shortcut.id,
rowStatus: shortcut.rowStatus === "ARCHIVED" ? "NORMAL" : "ARCHIVED",
};
await shortcutService.patchShortcut(shortcutPatch);
} catch (error) {
// do nth
}
};
const handleDeleteBtnMouseLeave = () => {
toggleConfirmDeleteBtn(false);
};
return (
<>
{shortcut.title}
{shortcut.rowStatus === "ARCHIVED" ? t("common.unpin") : t("common.pin")}
{t("common.edit")}
{t("common.delete")}
{showConfirmDeleteBtn ? "!" : ""}
>
);
};
export default ShortcutList;