'use client'; import React, { FC, useCallback, useEffect, useMemo, useRef, useState, } from 'react'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import useSWR from 'swr'; import { orderBy } from 'lodash'; import { useUser } from '@gitroom/frontend/components/layout/user.context'; import clsx from 'clsx'; import Image from 'next/image'; import { Menu } from '@gitroom/frontend/components/launches/menu/menu'; import { ApiModal } from '@gitroom/frontend/components/launches/add.provider.component'; import { useRouter } from 'next/navigation'; import { useVariables } from '@gitroom/react/helpers/variable.context'; export const ConnectChannels: FC = () => { const fetch = useFetch(); const { isGeneral } = useVariables(); const router = useRouter(); const [identifier, setIdentifier] = useState(undefined); const [popup, setPopups] = useState(undefined); const getIntegrations = useCallback(async () => { return (await fetch('/integrations')).json(); }, []); const [reload, setReload] = useState(false); const getSocialLink = useCallback( (identifier: string) => async () => { const { url } = await ( await fetch('/integrations/social/' + identifier) ).json(); window.open(url, 'Social Connect', 'width=700,height=700'); }, [] ); const load = useCallback(async (path: string) => { const list = (await (await fetch(path)).json()).integrations; setPopups(list.map((p: any) => p.id)); return list; }, []); const { data: integrations, mutate } = useSWR('/integrations/list', load, { fallbackData: [], }); const user = useUser(); const totalNonDisabledChannels = useMemo(() => { return ( integrations?.filter((integration: any) => !integration.disabled) ?.length || 0 ); }, [integrations]); const sortedIntegrations = useMemo(() => { return orderBy( integrations, ['type', 'disabled', 'identifier'], ['desc', 'asc', 'asc'] ); }, [integrations]); useEffect(() => { if (sortedIntegrations.length === 0 || !popup) { return; } const betweenSteps = sortedIntegrations.find((p) => p.inBetweenSteps); if (betweenSteps && popup.indexOf(betweenSteps.id) === -1) { const url = new URL(window.location.href); url.searchParams.append('added', betweenSteps.identifier); url.searchParams.append('continue', betweenSteps.id); router.push(url.toString()); setPopups([...popup, betweenSteps.id]); } }, [sortedIntegrations, popup]); const update = useCallback(async (shouldReload: boolean) => { if (shouldReload) { setReload(true); } await mutate(); if (shouldReload) { setReload(false); } }, []); const continueIntegration = useCallback( (integration: any) => async () => { const url = new URL(window.location.href); url.searchParams.append('added', integration.identifier); url.searchParams.append('continue', integration.id); router.push(url.toString()); }, [] ); const finishUpdate = useCallback(() => { setIdentifier(undefined); update(true); }, []); const { data } = useSWR('get-all-integrations', getIntegrations); return ( <> {!!identifier && (
setIdentifier(undefined)} update={finishUpdate} identifier={identifier.identifier} name={identifier.name} />
)}
Connect Channels
Connect your social media and publishing websites channels to schedule posts later
Social
{data?.social.map((social: any) => (
{social.identifier}
{social.name}
))}
{!isGeneral && (
Publishing Platforms
{data?.article.map((article: any) => (
setIdentifier(article)} key={article.identifier} className="h-[96px] bg-input flex flex-col justify-center items-center gap-[10px] cursor-pointer" >
{article.name}
))}
)}
{sortedIntegrations.length === 0 && (
No channels
)} {sortedIntegrations.map((integration) => (
{integration.inBetweenSteps && (
!
)} {integration.identifier} {integration.identifier}
{integration.name}
totalNonDisabledChannels && integration.disabled } canDisable={!integration.disabled} />
))}
); };