import { produce } from "immer";
import { AnimatePresence, motion } from "motion/react";
import { useContext, useEffect, useState } from "react";
import bg7 from "~/assets/images/map_bi/bottom_utils/bg7.png";
import bg8 from "~/assets/images/map_bi/bottom_utils/bg8.png";
import titleImg from "~/assets/images/map_bi/bottom_utils/title.png";
import titleOnImg from "~/assets/images/map_bi/bottom_utils/title_on.png";
import { branchOfficeUtilsList } from "~/pages/Container/Map/components/BottomUtils/branchOfficeUtilsList";
import { Context } from "~/pages/Container/Map/js/context";
import { portUtilsList } from "./portUtilsList";
import { usePortUtilsAnimation } from "./usePortUtilsAnimation";
import "./index.less";
const bottomUtilsAnimation = {
initial: { y: 100, opacity: 0 },
animate: { y: 0, opacity: 1, transition: { duration: 0.5, ease: "easeOut" } },
exit: { y: 100, opacity: 0, transition: { duration: 0.3, ease: "easeIn" } },
};
// 分公司子项按顺序进入、反向退出,保留原来的波浪式效果。
const branchOfficeContainerAnimation = {
hidden: {
y: 200,
opacity: 0,
transition: { duration: 0.2, ease: "easeIn", staggerChildren: 0.03, staggerDirection: -1 },
},
visible: {
y: 0,
opacity: 1,
transition: { duration: 0.3, ease: "easeOut", staggerChildren: 0.05, staggerDirection: 1 },
},
};
const branchOfficeItemAnimation = {
hidden: { y: 50, opacity: 0 },
visible: { y: 0, opacity: 1, transition: { duration: 0.3, ease: "easeOut" } },
};
function BottomUtils() {
const {
currentPort,
pureMap,
currentBranchOffice,
bottomUtilsCurrentIndex,
bottomUtilsResetVersion,
actions,
} = useContext(Context);
const bottomUtilsMode = !pureMap && currentPort && !currentBranchOffice
? "port"
: !pureMap && currentPort && currentBranchOffice ? "branchOffice" : null;
const [list, setList] = useState(() => bottomUtilsMode === "port"
? portUtilsList
: bottomUtilsMode === "branchOffice" ? branchOfficeUtilsList : []);
const [listMode, setListMode] = useState(bottomUtilsMode);
const {
parentControls: portUtilsParentControls,
childControls: portUtilsChildControls,
optionRefs: portUtilsOptionRefs,
isAnimating: portUtilsIsAnimating,
hasInteracted: portUtilsHasInteracted,
shouldHideInactive: portUtilsShouldHideInactive,
animationConfig: portUtilsAnimationConfig,
} = usePortUtilsAnimation(
portUtilsList.length,
bottomUtilsCurrentIndex,
bottomUtilsMode === "port",
);
const resetAllCheck = () => {
setList(produce((draft) => {
draft.forEach((item) => {
item.list.forEach((item1) => {
item1.check = false;
});
});
}));
};
useEffect(() => {
if (bottomUtilsMode === "port") {
setList(portUtilsList);
}
else if (bottomUtilsMode === "branchOffice") {
setList(branchOfficeUtilsList);
}
else {
setList([]);
}
setListMode(bottomUtilsMode);
}, [bottomUtilsMode]);
// reducer 递增版本号时重置本组件维护的子项勾选状态,不再依赖全局事件订阅。
useEffect(() => {
resetAllCheck();
}, [bottomUtilsResetVersion]);
const optionsClick = (index) => {
actions.setBottomUtilsIndex(bottomUtilsCurrentIndex === index ? -1 : index);
};
const optionsItemsClick = (index, index1, item, item1) => {
// 子选项点击处理
setList(produce((draft) => {
draft[index].list[index1].check = !draft[index].list[index1].check;
}));
};
const renderPortUtils = () => {
if (bottomUtilsMode === "port" && listMode === bottomUtilsMode) {
return list.map((item, index) => {
const isCurrentActive = bottomUtilsCurrentIndex === index;
const hasActiveChildren = bottomUtilsCurrentIndex !== -1;
const isAllowClick = hasActiveChildren && !isCurrentActive && portUtilsShouldHideInactive;
return (