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'; import { useCopilotAction, useCopilotReadable } from '@copilotkit/react-core'; import { useStateCallback } from '@gitroom/react/helpers/use.state.callback'; import { timer } from '@gitroom/helpers/utils/timer'; import dayjs from 'dayjs'; export const PickPlatforms: FC<{ integrations: Integrations[]; selectedIntegrations: Integrations[]; onChange: (integrations: Integrations[], callback: () => void) => void; singleSelect: boolean; hide?: boolean; isMain: boolean; }> = (props) => { const { hide, isMain, integrations, selectedIntegrations, onChange } = props; const ref = useRef(null); const [isLeft, setIsLeft] = useState(false); const [isRight, setIsRight] = useState(false); const [selectedAccounts, setSelectedAccounts] = useStateCallback< Integrations[] >(selectedIntegrations.slice(0).map((p) => ({ ...p }))); 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 () => { const promises = []; if (props.singleSelect) { promises.push( new Promise((res) => { onChange([integration], () => { res(''); }); }) ); promises.push( new Promise((res) => { setSelectedAccounts([integration], () => { res(''); }); }) ); 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; } promises.push( new Promise((res) => { onChange(changedIntegrations, () => { res(''); }); }) ); promises.push( new Promise((res) => { setSelectedAccounts(changedIntegrations, () => { res(''); }); }) ); } else { const changedIntegrations = [...selectedAccounts, integration]; promises.push( new Promise((res) => { onChange(changedIntegrations, () => { res(''); }); }) ); promises.push( new Promise((res) => { setSelectedAccounts(changedIntegrations, () => { res(''); }); }) ); } await timer(500); await Promise.all(promises); }, [selectedAccounts] ); const handler = useCallback( async ({ integrationId }: { integrationId: string }) => { console.log('setSelectedIntegration', integrations, integrationId, dayjs().unix()); const findIntegration = integrations.find((p) => p.id === integrationId)!; await addPlatform(findIntegration)(); }, [selectedAccounts, integrations, selectedAccounts] ); useCopilotReadable({ description: isMain ? 'All available platforms channels' : 'Possible platforms channels to edit', value: JSON.stringify(integrations), }); useCopilotAction( { name: isMain ? `addOrRemovePlatform` : 'setSelectedIntegration', description: isMain ? `Add or remove one channel to schedule your post to, always pass the id` : 'Set selected integration', parameters: [ { name: 'integrationId', type: 'string', description: 'The id of the integration', required: true, }, ], handler, }, [addPlatform, selectedAccounts, integrations] ); if (hide) { return null; } return (
{props.singleSelect && (
{isLeft && ( )}
)}
{integrations .filter((f) => !f.inBetweenSteps) .map((integration) => !props.singleSelect ? (
p.id === integration.id ) === -1 ? 'opacity-40' : '' )} > {integration.identifier} {integration.identifier === 'youtube' ? ( ) : ( {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 && (
)}
); };