import { Button, message, Tag } from 'antd'; import React, { useMemo } from 'react'; import { Column, ColumnConfig } from '@ant-design/charts'; import { ArrowRightOutlined, SyncOutlined } from '@ant-design/icons'; import { DateFilter } from './DateFilter'; import { HealthBar } from './HealthBar'; import { useWorkspaceWebsitePageview, useWorspaceWebsites, WebsiteInfo, } from '../api/model/website'; import { Loading } from './Loading'; import dayjs from 'dayjs'; import { DateUnit, formatDate, formatDateWithUnit, getDateArray, } from '../utils/date'; import { useEvent } from '../hooks/useEvent'; interface WebsiteOverviewProps { workspaceId: string; } export const WebsiteOverview: React.FC = React.memo( (props) => { const { isLoading, websites } = useWorspaceWebsites(props.workspaceId); if (isLoading) { return ; } return (
{websites.map((website) => ( ))}
); } ); WebsiteOverview.displayName = 'WebsiteOverview'; const WebsiteOverviewItem: React.FC<{ website: WebsiteInfo; }> = React.memo((props) => { const unit: DateUnit = 'hour'; const startDate = dayjs().subtract(1, 'day').add(1, unit).startOf(unit); const endDate = dayjs().endOf(unit); const { pageviews, sessions, isLoading, refetch } = useWorkspaceWebsitePageview( props.website.workspaceId, props.website.id, startDate.unix() * 1000, endDate.unix() * 1000 ); const handleRefresh = useEvent(async () => { await refetch(); message.success('Refreshed'); }); const chartData = useMemo(() => { const pageviewsArr = getDateArray(pageviews, startDate, endDate, unit); const sessionsArr = getDateArray(sessions, startDate, endDate, unit); return [ ...pageviewsArr.map((item) => ({ ...item, type: 'pageview' })), ...sessionsArr.map((item) => ({ ...item, type: 'session' })), ]; }, [pageviews, sessions, unit]); if (isLoading) { return ; } return (
{props.website.name} ({ status: 'health', }))} />
); }); WebsiteOverviewItem.displayName = 'WebsiteOverviewItem'; const MetricCard: React.FC<{ label: string; value: number; diff: number; unit?: string; }> = React.memo((props) => { const unit = props.unit ?? ''; return (
{String(props.value)} {unit}
{props.label} = 0 ? 'green' : 'red'}> {props.diff >= 0 ? `+${props.diff}${unit}` : `${props.diff}${unit}`}
); }); MetricCard.displayName = 'MetricCard'; export const StatsChart: React.FC<{ data: { x: string; y: number; type: string }[]; unit: DateUnit; }> = React.memo((props) => { const config: ColumnConfig = useMemo( () => ({ data: props.data, isStack: true, xField: 'x', yField: 'y', seriesField: 'type', label: { position: 'middle' as const, style: { fill: '#FFFFFF', opacity: 0.6, }, }, tooltip: { title: (t) => formatDate(t), }, xAxis: { label: { autoHide: true, autoRotate: false, formatter: (text) => formatDateWithUnit(text, props.unit), }, }, }), [props.data, props.unit] ); return ; }); StatsChart.displayName = 'StatsChart';