'use client'; import { Display } from '@/components/display'; import useGlobalStore from '@/config/use-global'; import { Combobox } from '@workspace/ui/custom-components/combobox'; import { formatDate } from '@workspace/ui/utils'; import { useTranslations } from 'next-intl'; import { useCallback, useEffect, useMemo } from 'react'; interface SubscribeSelectorProps { value?: number; data: API.SubscribeDiscountInfo[]; onChange: (value: number) => void; } const SubscribeSelector: React.FC = ({ value, data, onChange }) => { const t = useTranslations('subscribe'); const { common } = useGlobalStore(); const singleModel = common.subscribe.single_model; useEffect(() => { if (singleModel && data.length > 0 && data[0]) { onChange(data[0].id); } }, [data, singleModel, onChange, value]); const handleChange = useCallback( (selectedValue: number) => { if (singleModel) { if (selectedValue) { onChange(selectedValue); } } else { onChange(selectedValue); } }, [singleModel, onChange], ); const options = useMemo(() => { return data.map((item) => ({ children: (
{item.name}
), label: item.name, value: item.id, })); }, [data]); if (!data.length) return null; return ( <>
{t('subscriptionDiscount')}
placeholder={t('selectSubscription')} options={options} value={value} onChange={handleChange} /> ); }; export default SubscribeSelector;