Skip to content

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

1.3 KB
Raw Blame
import { LineChartIcon, TableIcon } from 'lucide-react';
import { parseAsStringEnum, useQueryState } from 'nuqs';
import { Button } from '../ui/button';

type ViewType = 'table' | 'chart';

interface OverviewViewToggleProps {
  defaultView?: ViewType;
  className?: string;
}

export function OverviewViewToggle({
  defaultView = 'table',
  className,
}: OverviewViewToggleProps) {
  const [view, setView] = useQueryState<ViewType>(
    'view',
    parseAsStringEnum(['table', 'chart'])
      .withDefault(defaultView)
      .withOptions({ history: 'push' })
  );

  return (
    <div className={className}>
      <Button
        onClick={() => {
          setView(view === 'table' ? 'chart' : 'table');
        }}
        size="icon"
        title={
          view === 'table' ? 'Switch to chart view' : 'Switch to table view'
        }
        variant="ghost"
      >
        {view === 'table' ? (
          <LineChartIcon size={16} />
        ) : (
          <TableIcon size={16} />
        )}
      </Button>
    </div>
  );
}

export function useOverviewView() {
  const [view, setView] = useQueryState<ViewType>(
    'view',
    parseAsStringEnum(['table', 'chart'])
      .withDefault('table')
      .withOptions({ history: 'push' })
  );

  return [view, setView] as const;
}