Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

2.1 KB
Raw Blame
import type { IReportInput } from '@openpanel/core/modules/report/report.constants';
import { useQuery } from '@tanstack/react-query';
import { AspectContainer } from '../aspect-container';
import { ChartWithTable, TableCard } from '../common/chart-with-table';
import { ReportChartEmpty } from '../common/empty';
import { ReportChartError } from '../common/error';
import { ReportChartLoading } from '../common/loading';
import { useReportChartContext } from '../context';
import { Chart } from './chart';
import { PathsTable } from './table';
import { useTRPC } from '@/integrations/trpc/react';

export function ReportSankeyChart() {
  const {
    report: {
      series,
      globalFilters,
      range,
      projectId,
      options,
      startDate,
      endDate,
      breakdowns,
      id,
    },
    isLazyLoading,
    shareId,
  } = useReportChartContext();

  const input: IReportInput = {
    series,
    globalFilters,
    range,
    projectId,
    interval: 'day',
    chartType: 'sankey',
    breakdowns,
    options,
    metric: 'sum',
    startDate,
    endDate,
    limit: 20,
    previous: false,
  };
  const trpc = useTRPC();
  const res = useQuery(
    trpc.chart.sankey.queryOptions(
      { ...input, id, shareId },
      {
        enabled: !isLazyLoading && !!options && input.series.length > 0,
      }
    )
  );

  if (!options) {
    return <Empty />;
  }

  if (isLazyLoading || res.isLoading) {
    return <Loading />;
  }

  if (res.isError) {
    return <Error />;
  }

  if (!res.data || res.data.nodes.length === 0) {
    return <Empty />;
  }

  return (
    <ChartWithTable
      chart={<Chart data={res.data} />}
      sizesItself
      tables={
        <TableCard>
          <PathsTable data={res.data} />
        </TableCard>
      }
    />
  );
}

function Loading() {
  return (
    <AspectContainer>
      <ReportChartLoading />
    </AspectContainer>
  );
}

function Error() {
  return (
    <AspectContainer>
      <ReportChartError />
    </AspectContainer>
  );
}

function Empty() {
  return (
    <AspectContainer>
      <ReportChartEmpty />
    </AspectContainer>
  );
}