import { ArrowsRightLeftIcon, PlusIcon, UsersIcon, XCircleIcon } from "@heroicons/react/24/outline"; import { ADDRESS_PLACEHOLDER } from "@hey/data/constants"; import type { CollectActionType } from "@hey/types/hey"; import { motion } from "motion/react"; import { useState } from "react"; import { isAddress } from "viem"; import SearchAccounts from "@/components/Shared/Account/SearchAccounts"; import ToggleWithHelper from "@/components/Shared/ToggleWithHelper"; import { Button, H6, Input } from "@/components/Shared/UI"; import splitNumber from "@/helpers/splitNumber"; import { useCollectActionStore } from "@/store/non-persisted/post/useCollectActionStore"; import { useAccountStore } from "@/store/persisted/useAccountStore"; import { EXPANSION_EASE } from "@/variants"; interface SplitConfigProps { isRecipientsDuplicated: boolean; setCollectType: (data: CollectActionType) => void; } const SplitConfig = ({ isRecipientsDuplicated, setCollectType }: SplitConfigProps) => { const { currentAccount } = useAccountStore(); const { collectAction } = useCollectActionStore((state) => state); const currentAddress = currentAccount?.address || ""; const recipients = collectAction.payToCollect?.recipients || []; const [isToggleOn, setIsToggleOn] = useState( recipients.length > 1 || (recipients.length === 1 && recipients[0].address !== currentAddress) ); const splitTotal = recipients.reduce((acc, curr) => acc + curr.percent, 0); const handleSplitEvenly = () => { const equalSplits = splitNumber(100, recipients.length); const splits = recipients.map((recipient, i) => { return { address: recipient.address, percent: equalSplits[i] }; }); if (!collectAction.payToCollect) return; setCollectType({ payToCollect: { ...collectAction.payToCollect, recipients: [...splits] } }); }; const onChangeRecipientOrPercent = ( index: number, value: string, type: "address" | "percent" ) => { const getRecipients = (value: string) => { return recipients.map((recipient, i) => { if (i === index) { return { ...recipient, [type]: type === "address" ? value : Number.parseInt(value, 10) }; } return recipient; }); }; if (!collectAction.payToCollect) return; setCollectType({ payToCollect: { ...collectAction.payToCollect, recipients: getRecipients(value) } }); }; const updateRecipient = (index: number, value: string) => { onChangeRecipientOrPercent(index, value, "address"); }; const handleRemoveRecipient = (index: number) => { const updatedRecipients = recipients.filter((_, i) => i !== index); if (updatedRecipients.length) { if (!collectAction.payToCollect) return; setCollectType({ payToCollect: { ...collectAction.payToCollect, recipients: updatedRecipients } }); } else { if (!collectAction.payToCollect) return; setCollectType({ payToCollect: { ...collectAction.payToCollect, recipients: [{ address: currentAddress, percent: 100 }] } }); setIsToggleOn(false); } }; const toggleSplit = () => { if (!collectAction.payToCollect) return; setCollectType({ payToCollect: { ...collectAction.payToCollect, recipients: [{ address: currentAddress, percent: 100 }] } }); setIsToggleOn(!isToggleOn); }; return (