'use client'; import useSWR from 'swr'; import { useCallback, useMemo, useState } from 'react'; import { orderBy } from 'lodash'; import clsx from 'clsx'; import ImageWithFallback from '@gitroom/react/helpers/image.with.fallback'; import Image from 'next/image'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import { RenderAnalytics } from '@gitroom/frontend/components/platform-analytics/render.analytics'; import { Select } from '@gitroom/react/form/select'; const allowedIntegrations = [ 'facebook', 'instagram', 'linkedin-page', 'tiktok', 'youtube', 'pinterest' ]; export const PlatformAnalytics = () => { const fetch = useFetch(); const [current, setCurrent] = useState(0); const [key, setKey] = useState(7); const load = useCallback(async () => { const int = (await (await fetch('/integrations/list')).json()).integrations; return int.filter((f: any) => allowedIntegrations.includes(f.identifier)); }, []); const { data } = useSWR('analytics-list', load, { fallbackData: [], }); const sortedIntegrations = useMemo(() => { return orderBy( data, ['type', 'disabled', 'identifier'], ['desc', 'asc', 'asc'] ); }, [data]); const currentIntegration = useMemo(() => { return sortedIntegrations[current]; }, [current, sortedIntegrations]); const options = useMemo(() => { if (!currentIntegration) { return []; } const arr = []; if ( ['facebook', 'instagram', 'linkedin-page', 'pinterest', 'youtube'].indexOf( currentIntegration.identifier ) !== -1 ) { arr.push({ key: 7, value: '7 Days', }); } if ( ['facebook', 'instagram', 'linkedin-page', 'pinterest', 'youtube'].indexOf( currentIntegration.identifier ) !== -1 ) { arr.push({ key: 30, value: '30 Days', }); } if ( ['facebook', 'linkedin-page', 'pinterest', 'youtube'].indexOf(currentIntegration.identifier) !== -1 ) { arr.push({ key: 90, value: '90 Days', }); } return arr; }, [currentIntegration]); const keys = useMemo(() => { if (!currentIntegration) { return 7; } if (options.find((p) => p.key === key)) { return key; } return options[0]?.key; }, [key, currentIntegration]); return (
Channels
{sortedIntegrations.map((integration, index) => (
setCurrent(index)} className={clsx( 'flex gap-[8px] items-center', currentIntegration.id !== integration.id && 'opacity-20 hover:opacity-100 cursor-pointer' )} >
{(integration.inBetweenSteps || integration.refreshNeeded) && (
!
)} {integration.identifier}
{integration.name}
))}
{!!keys && !!currentIntegration && ( )}
); };