'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 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'; import { Button } from '@gitroom/react/form/button'; import { useRouter } from 'next/navigation'; import { useToaster } from '@gitroom/react/toaster/toaster'; const allowedIntegrations = [ 'facebook', 'instagram', 'instagram-standalone', 'linkedin-page', // 'tiktok', 'youtube', 'pinterest', 'threads', 'x', ]; export const PlatformAnalytics = () => { const fetch = useFetch(); const router = useRouter(); const [current, setCurrent] = useState(0); const [key, setKey] = useState(7); const [refresh, setRefresh] = useState(false); const toaster = useToaster(); const load = useCallback(async () => { const int = (await (await fetch('/integrations/list')).json()).integrations; return int.filter((f: any) => allowedIntegrations.includes(f.identifier)); }, []); const { data, isLoading } = 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', 'instagram-standalone', 'linkedin-page', 'pinterest', 'youtube', 'threads', 'x', ].indexOf(currentIntegration.identifier) !== -1 ) { arr.push({ key: 7, value: '7 Days', }); } if ( [ 'facebook', 'instagram', 'instagram-standalone', 'linkedin-page', 'pinterest', 'youtube', 'threads', 'x', ].indexOf(currentIntegration.identifier) !== -1 ) { arr.push({ key: 30, value: '30 Days', }); } if ( ['facebook', 'linkedin-page', 'pinterest', 'youtube', 'x'].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]); if (isLoading) { return null; } if (!sortedIntegrations.length && !isLoading) { return (