import Button from '@/components/Button' import ColorPicker from '@/components/ColorPicker' import Input from '@/components/Input' import Select from '@/components/Select' import { AlignRight, AlignBottom, AlignHorizontally, AlignLeft, AlignTop, AlignVertically, Add, Edit, Trash, Link2 } from 'iconsax-react' import React, { FC, useState, useEffect } from 'react' import { useTranslation } from 'react-i18next' import { usePersonalityStore } from '../store/Store' import { SocialNetworkType, SocialIconSize, SocialIconStyle, HorizontalAlignment, VerticalAlignment, ElementType } from '../types/Types' import { getSocialIconPath, hasPngIcon, getSocialSvgIcon } from '../utils/socialIcons' const SocialSidebar: FC = () => { const { t } = useTranslation() const { addSocialToActiveItem, selectedElement, setSelectedElement, data, updateSocial, deleteSocial } = usePersonalityStore() const [networkType, setNetworkType] = useState(SocialNetworkType.INSTAGRAM) const [link, setLink] = useState('') const [size, setSize] = useState(SocialIconSize.MEDIUM) const [style, setStyle] = useState(SocialIconStyle.CIRCLE) const [color, setColor] = useState('#000000') const [horizontalAlignment, setHorizontalAlignment] = useState(HorizontalAlignment.CENTER) const [verticalAlignment, setVerticalAlignment] = useState(VerticalAlignment.MIDDLE) const isEditMode = selectedElement?.type === ElementType.SOCIAL; // Load selected social data when element is selected useEffect(() => { if (isEditMode && selectedElement) { try { const sectionData = data[selectedElement.sectionKey]; if (!sectionData || !sectionData.items) { console.error('Section data or items not found'); return; } const item = sectionData.items.find(item => item.id === selectedElement.itemId); if (!item) { console.error('Item not found'); return; } const social = item.socials?.find(s => s.id === selectedElement.elementId); if (social) { setNetworkType(social.networkType); setLink(social.link); setSize(social.size); setStyle(social.style); setColor(social.color || '#000000'); setHorizontalAlignment(social.alignment || HorizontalAlignment.CENTER); setVerticalAlignment(social.verticalAlignment || VerticalAlignment.MIDDLE); } else { console.error('Social element not found'); } } catch (error) { console.error('Error loading social data:', error); } } else { // Reset form when not in edit mode setNetworkType(SocialNetworkType.INSTAGRAM) setLink('') setSize(SocialIconSize.MEDIUM) setStyle(SocialIconStyle.CIRCLE) setColor('#000000') setHorizontalAlignment(HorizontalAlignment.CENTER) setVerticalAlignment(VerticalAlignment.MIDDLE) } }, [selectedElement, isEditMode, data]); const networkOptions = [ { label: 'Instagram', value: SocialNetworkType.INSTAGRAM }, { label: 'Facebook', value: SocialNetworkType.FACEBOOK }, { label: 'Twitter', value: SocialNetworkType.TWITTER }, { label: 'LinkedIn', value: SocialNetworkType.LINKEDIN }, { label: 'YouTube', value: SocialNetworkType.YOUTUBE }, { label: 'Telegram', value: SocialNetworkType.TELEGRAM }, { label: 'WhatsApp', value: SocialNetworkType.WHATSAPP }, { label: 'TikTok', value: SocialNetworkType.TIKTOK }, { label: 'Pinterest', value: SocialNetworkType.PINTEREST }, { label: 'Snapchat', value: SocialNetworkType.SNAPCHAT }, { label: 'GitHub', value: SocialNetworkType.GITHUB }, { label: 'Discord', value: SocialNetworkType.DISCORD }, ] const sizeOptions = [ { label: 'کوچک', value: SocialIconSize.SMALL }, { label: 'متوسط', value: SocialIconSize.MEDIUM }, { label: 'بزرگ', value: SocialIconSize.LARGE } ] const styleOptions = [ { label: 'دایره', value: SocialIconStyle.CIRCLE }, { label: 'مربع', value: SocialIconStyle.SQUARE }, { label: 'گرد', value: SocialIconStyle.ROUNDED } ] const handleAddSocial = () => { if (link.trim()) { addSocialToActiveItem({ networkType, link, size, style, color, alignment: horizontalAlignment, verticalAlignment, }) // Reset form after adding setNetworkType(SocialNetworkType.INSTAGRAM) setLink('') setSize(SocialIconSize.MEDIUM) setStyle(SocialIconStyle.CIRCLE) setColor('#000000') setHorizontalAlignment(HorizontalAlignment.CENTER) setVerticalAlignment(VerticalAlignment.MIDDLE) } } const handleUpdateSocial = () => { if (selectedElement && link.trim()) { updateSocial( selectedElement.sectionKey, selectedElement.itemId, selectedElement.elementId, { networkType, link, size, style, color, alignment: horizontalAlignment, verticalAlignment, } ); // Exit edit mode setSelectedElement(null); } } const handleDeleteSocial = () => { if (selectedElement) { deleteSocial( selectedElement.sectionKey, selectedElement.itemId, selectedElement.elementId ); // Exit edit mode setSelectedElement(null); } } const handleCancelEdit = () => { setSelectedElement(null); } // Preview component for selected social network const renderPreview = () => { const getIconSize = () => { switch (size) { case SocialIconSize.SMALL: return { width: '24px', height: '24px' } case SocialIconSize.MEDIUM: return { width: '32px', height: '32px' } case SocialIconSize.LARGE: return { width: '40px', height: '40px' } default: return { width: '32px', height: '32px' } } } const getIconStyle = () => { const iconSize = getIconSize() const baseStyle = { ...iconSize, display: 'inline-flex', alignItems: 'center', justifyContent: 'center', color: color, transition: 'all 0.2s ease', } switch (style) { case SocialIconStyle.CIRCLE: return { ...baseStyle, borderRadius: '50%', } case SocialIconStyle.SQUARE: return { ...baseStyle, borderRadius: '0', } case SocialIconStyle.ROUNDED: return { ...baseStyle, borderRadius: '8px', } default: return baseStyle } } const renderIcon = () => { if (hasPngIcon(networkType)) { const iconPath = getSocialIconPath(networkType) return ( {`${networkType} ) } // Use shared SVG function for fallback return getSocialSvgIcon(networkType, true) as React.ReactElement } return (
{t('setting.preview')}
{renderIcon()}
) } return (
{t('setting.social_networks')} {isEditMode && (
{t('setting.edit_mode')}
)}
setLink(e.target.value)} endIcon={} placeholder="https://..." />
setStyle(e.target.value as SocialIconStyle)} placeholder={t('setting.select')} />
{t('setting.horizontal_position')}
setHorizontalAlignment(HorizontalAlignment.RIGHT)} className='cursor-pointer' >
setHorizontalAlignment(HorizontalAlignment.CENTER)} className='cursor-pointer' >
setHorizontalAlignment(HorizontalAlignment.LEFT)} className='cursor-pointer' >
{t('setting.vertical_position')}
setVerticalAlignment(VerticalAlignment.TOP)} className='cursor-pointer' >
setVerticalAlignment(VerticalAlignment.MIDDLE)} className='cursor-pointer' >
setVerticalAlignment(VerticalAlignment.BOTTOM)} className='cursor-pointer' >
{isEditMode ? (
) : ( )}
) } export default SocialSidebar