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 (
} on={isToggleOn} setOn={toggleSplit} /> {isToggleOn ? (
{recipients.map((recipient, index) => (
0 && !isAddress(recipient.address) } hideDropdown={isAddress(recipient.address)} onAccountSelected={(account) => updateRecipient(index, account.owner) } onChange={(event) => updateRecipient(index, event.target.value) } placeholder={`${ADDRESS_PLACEHOLDER} or wagmi`} value={recipient.address} />
onChangeRecipientOrPercent( index, event.target.value, "percent" ) } placeholder="5" type="number" value={recipient.percent} />
))}
{recipients.length >= 4 ? (
) : ( )}
{splitTotal > 100 ? (
Splits cannot exceed 100%. Total: {splitTotal}%
) : null} {splitTotal < 100 ? (
Splits cannot be less than 100%. Total: {splitTotal}%
) : null} {isRecipientsDuplicated ? (
Duplicate recipient address found
) : null} ) : null}
); }; export default SplitConfig;