import { FC, useCallback, useEffect, useRef, useState } from 'react'; import { Integrations } from '@gitroom/frontend/components/launches/calendar.context'; import { useMoveToIntegrationListener } from '@gitroom/frontend/components/launches/helpers/use.move.to.integration'; import { deleteDialog } from '@gitroom/react/helpers/delete.dialog'; import clsx from 'clsx'; import Image from 'next/image'; export const PickPlatforms: FC<{ integrations: Integrations[]; selectedIntegrations: Integrations[]; onChange: (integrations: Integrations[]) => void; singleSelect: boolean; hide?: boolean; }> = (props) => { const { hide, integrations, selectedIntegrations, onChange } = props; const ref = useRef(null); const [isLeft, setIsLeft] = useState(false); const [isRight, setIsRight] = useState(false); const [selectedAccounts, setSelectedAccounts] = useState(selectedIntegrations); useEffect(() => { if ( props.singleSelect && selectedAccounts.length && integrations.indexOf(selectedAccounts?.[0]) === -1 ) { addPlatform(integrations[0])(); } }, [integrations, selectedAccounts]); const checkLeftRight = (test = true) => { const scrollWidth = ref.current?.scrollWidth; const offsetWidth = +(ref.current?.offsetWidth || 0); const scrollOffset = ref.current?.scrollLeft || 0; const totalScroll = scrollOffset + offsetWidth + 100; setIsLeft(!!scrollOffset); setIsRight(!!scrollWidth && !!offsetWidth && scrollWidth > totalScroll); }; const changeOffset = useCallback( (offset: number) => () => { if (ref.current) { ref.current.scrollLeft += offset; checkLeftRight(); } }, [selectedIntegrations, integrations, selectedAccounts] ); useEffect(() => { checkLeftRight(); }, [selectedIntegrations, integrations]); useMoveToIntegrationListener([integrations], props.singleSelect, (identifier) => { const findIntegration = integrations.find( (p) => p.id === identifier ); if (findIntegration) { addPlatform(findIntegration)(); } }); const addPlatform = useCallback( (integration: Integrations) => async () => { if (props.singleSelect) { onChange([integration]); setSelectedAccounts([integration]); return; } if (selectedAccounts.includes(integration)) { const changedIntegrations = selectedAccounts.filter( ({ id }) => id !== integration.id ); if ( !props.singleSelect && !(await deleteDialog( 'Are you sure you want to remove this platform?' )) ) { return; } onChange(changedIntegrations); setSelectedAccounts(changedIntegrations); } else { const changedIntegrations = [...selectedAccounts, integration]; onChange(changedIntegrations); setSelectedAccounts(changedIntegrations); } }, [selectedAccounts] ); if (hide) { return null; } return (
{props.singleSelect && (
{isLeft && ( )}
)}
{integrations.map((integration) => !props.singleSelect ? (
p.id === integration.id ) === -1 ? 'opacity-40' : '' )} > {integration.identifier} {integration.identifier}
) : (
p.id === integration.id ) === -1 ? 'bg-third border border-third' : 'bg-[#291259] border border-[#5826C2]' )} >
{integration.identifier} {integration.identifier}
{integration.name}
) )}
{props.singleSelect && isRight && (
)}
); };