import { useEventQueryFilters } from '@/hooks/use-event-query-filters'; import { useMemo, useState } from 'react'; import type { IChartType } from '@openpanel/validation'; import { useTRPC } from '@/integrations/trpc/react'; import { pushModal } from '@/modals'; import { countries } from '@/translations/countries'; import { NOT_SET_VALUE } from '@openpanel/constants'; import { useQuery } from '@tanstack/react-query'; import { ChevronRightIcon } from 'lucide-react'; import { SerieIcon } from '../report-chart/common/serie-icon'; import { Widget, WidgetBody } from '../widget'; import { OVERVIEW_COLUMNS_NAME } from './overview-constants'; import OverviewDetailsButton from './overview-details-button'; import { OverviewLineChart, OverviewLineChartLoading, } from './overview-line-chart'; import { OverviewMap } from './overview-map'; import { OverviewViewToggle, useOverviewView } from './overview-view-toggle'; import { WidgetFooter, WidgetHead, WidgetHeadSearchable, } from './overview-widget'; import { OverviewWidgetTableGeneric, OverviewWidgetTableLoading, } from './overview-widget-table'; import { useOverviewOptions } from './useOverviewOptions'; import { useOverviewWidgetV2 } from './useOverviewWidget'; interface OverviewTopGeoProps { projectId: string; shareId?: string; } export default function OverviewTopGeo({ projectId, shareId, }: OverviewTopGeoProps) { const { interval, range, previous, startDate, endDate } = useOverviewOptions(); const [chartType, setChartType] = useState('bar'); const [filters, setFilter] = useEventQueryFilters(); const [searchQuery, setSearchQuery] = useState(''); const isPageFilter = filters.find((filter) => filter.name === 'path'); const [widget, setWidget, widgets] = useOverviewWidgetV2('geo', { country: { title: 'Top countries', btn: 'Countries', }, region: { title: 'Top regions', btn: 'Regions', }, city: { title: 'Top cities', btn: 'Cities', }, }); const trpc = useTRPC(); const [view] = useOverviewView(); const query = useQuery( trpc.overview.topGeneric.queryOptions({ projectId, shareId, range, filters, column: widget.key, startDate, endDate, }), ); const seriesQuery = useQuery( trpc.overview.topGenericSeries.queryOptions( { projectId, shareId, range, filters, column: widget.key, startDate, endDate, interval, }, { enabled: view === 'chart', }, ), ); const filteredData = useMemo(() => { const data = query.data ?? []; if (!searchQuery.trim()) { return data; } const queryLower = searchQuery.toLowerCase(); return data.filter( (item) => item.name?.toLowerCase().includes(queryLower) || item.prefix?.toLowerCase().includes(queryLower) || countries[item.name as keyof typeof countries] ?.toLowerCase() .includes(queryLower), ); }, [query.data, searchQuery]); const tabs = widgets.map((w) => ({ key: w.key, label: w.btn, })); return ( <> {view === 'chart' ? ( seriesQuery.isLoading ? ( ) : seriesQuery.data ? ( ) : ( ) ) : query.isLoading ? ( ) : ( ); }, }} /> )} pushModal('OverviewTopGenericModal', { projectId, column: widget.key, }) } />
Geo data provided by{' '} MaxMind
Map
); }