import React, { useState } from 'react'; import { Toaster } from 'react-hot-toast'; import { sortInsightItems } from '../../utils/insight'; import SelectField from '../common/SelectField'; import InsightItem from './InsightItem'; import InsightStats from './InsightStats'; type SCInsightProps = { domain: DomainType | null, insight: InsightDataType, isLoading: boolean, isConsoleIntegrated: boolean, } const SCInsight = ({ insight, isLoading = true, isConsoleIntegrated = true }: SCInsightProps) => { const [activeTab, setActiveTab] = useState('stats'); const insightItems = insight[activeTab as keyof InsightDataType]; const startDate = insight && insight.stats && insight.stats.length > 0 ? new Date(insight.stats[0].date) : null; const endDate = insight && insight.stats && insight.stats.length > 0 ? new Date(insight.stats[insight.stats.length - 1].date) : null; const switchTab = (tab: string) => { // window.insightTab = tab; setActiveTab(tab); }; const renderTableHeader = () => { const headerNames: {[key:string]: string[]} = { stats: ['Date', 'Avg Position', 'Visits', 'Impressions', 'CTR'], keywords: ['Keyword', 'Avg Position', 'Visits ↑', 'Impressions', 'CTR', 'Countries'], countries: ['Country', 'Avg Position', 'Visits ↑', 'Impressions', 'CTR', 'Keywords'], pages: ['Page', 'Avg Position', 'Visits ↑', 'Impressions', 'CTR', 'Countries', 'Keywords'], }; return ( ); }; const deviceTabStyle = 'select-none cursor-pointer px-3 py-2 rounded-3xl mr-2'; const deviceTabCountStyle = 'px-2 py-0 rounded-3xl bg-[#DEE1FC] text-[0.7rem] font-bold ml-1'; return (
    {['stats', 'keywords', 'countries', 'pages'].map((tabItem) => { const tabInsightItem = insight[tabItem as keyof InsightDataType]; return
  • switchTab(tabItem)}> {tabItem} {tabItem !== 'stats' && ( {tabInsightItem && tabInsightItem.length ? tabInsightItem.length : 0} )}
  • ; })}
{ return { label: d, value: d }; })} selected={[activeTab]} defaultLabel="Select Tab" updateField={(updatedTab:[string]) => switchTab(updatedTab[0])} multiple={false} rounded={'rounded'} />
{isConsoleIntegrated && (
{startDate && new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(new Date(startDate))} - {endDate && new Intl.DateTimeFormat('en-US', { dateStyle: 'medium' }).format(new Date(endDate))} (Last 30 Days)
)}
{isConsoleIntegrated && activeTab === 'stats' && ( )}
{renderTableHeader()}
{['keywords', 'pages', 'countries', 'stats'].includes(activeTab) && insight && insightItems && (activeTab === 'stats' ? [...insightItems].reverse() : sortInsightItems(insightItems)).map( (item:SCInsightItem, index: number) => { const insightItemCount = insight ? insightItems : []; const lastItem = !!(insightItemCount && (index === insightItemCount.length)); return ; }, ) } {isConsoleIntegrated && isLoading && (

Loading Insight...

)} {!isConsoleIntegrated && (

Google Search has not been Integrated yet. Please follow These Steps to integrate Google Search Data for this Domain.

)}
); }; export default SCInsight;