'use client'; import useSWR from 'swr'; import { useCallback, useMemo, useState } from 'react'; import { capitalize, orderBy } from 'lodash'; import clsx from 'clsx'; import ImageWithFallback from '@gitroom/react/helpers/image.with.fallback'; import SafeImage from '@gitroom/react/helpers/safe.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'; import { Button } from '@gitroom/react/form/button'; import { useRouter } from 'next/navigation'; import { useToaster } from '@gitroom/react/toaster/toaster'; import { useT } from '@gitroom/react/translation/get.transation.service.client'; import { useVariables } from '@gitroom/react/helpers/variable.context'; import useCookie from 'react-use-cookie'; import { SVGLine } from '@gitroom/frontend/components/launches/launches.component'; import { LoadingComponent } from '@gitroom/frontend/components/layout/loading'; const allowedIntegrations = [ 'facebook', 'instagram', 'instagram-standalone', 'linkedin-page', 'tiktok', 'youtube', 'gmb', 'pinterest', 'threads', 'x', ]; export const PlatformAnalytics = () => { const fetch = useFetch(); const t = useT(); const router = useRouter(); const { disableXAnalytics } = useVariables(); const [current, setCurrent] = useState(0); const [key, setKey] = useState(7); const [refresh, setRefresh] = useState(false); const [collapseMenu, setCollapseMenu] = useCookie('collapseMenu', '0'); const toaster = useToaster(); const load = useCallback(async () => { const int = ( await (await fetch('/integrations/list')).json() ).integrations.filter((f: any) => { if (f.identifier === 'x' && disableXAnalytics) { return false; } return true; }); return int.filter((f: any) => allowedIntegrations.includes(f.identifier)); }, []); const { data, isLoading } = useSWR('analytics-list', load, { revalidateOnFocus: false, revalidateOnReconnect: false, revalidateIfStale: false, revalidateOnMount: true, refreshWhenHidden: false, refreshWhenOffline: false, 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', 'instagram-standalone', 'linkedin-page', 'pinterest', 'youtube', 'threads', 'gmb', 'x', 'tiktok', ].indexOf(currentIntegration.identifier) !== -1 ) { arr.push({ key: 7, value: t('7_days', '7 Days'), }); } if ( [ 'facebook', 'instagram', 'instagram-standalone', 'linkedin-page', 'pinterest', 'youtube', 'threads', 'gmb', 'x', 'tiktok', ].indexOf(currentIntegration.identifier) !== -1 ) { arr.push({ key: 30, value: t('30_days', '30 Days'), }); } if ( ['facebook', 'linkedin-page', 'pinterest', 'youtube', 'x', 'gmb'].indexOf( currentIntegration.identifier ) !== -1 ) { arr.push({ key: 90, value: t('90_days', '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]); if (isLoading) { return (