Skip to content

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

1.2 KB
Raw Blame
import { parseAsStringEnum, useQueryState } from 'nuqs';
import type { ReportChartProps } from '../report-chart/context';
import { mapKeys } from '@/utils/object-to-zod-enums';

export function useOverviewWidget<T extends string>(
  key: string,
  widgets: Record<
    T,
    { title: string; btn: string; chart: ReportChartProps; hide?: boolean }
  >
) {
  const keys = Object.keys(widgets) as T[];
  const [widget, setWidget] = useQueryState<T>(
    key,
    parseAsStringEnum(keys)
      .withDefault(keys[0]!)
      .withOptions({ history: 'push' })
  );
  return [
    {
      ...widgets[widget],
      key: widget,
    },
    setWidget,
    mapKeys(widgets).map((key) => ({
      ...widgets[key],
      key,
    })),
  ] as const;
}

export function useOverviewWidgetV2<T extends string>(
  key: string,
  widgets: Record<T, { title: string; btn: string; meta?: any; hide?: boolean }>
) {
  const keys = Object.keys(widgets) as T[];
  const [widget, setWidget] = useQueryState<T>(
    key,
    parseAsStringEnum(keys)
      .withDefault(keys[0]!)
      .withOptions({ history: 'push' })
  );
  return [
    {
      ...widgets[widget],
      key: widget,
    },
    setWidget,
    mapKeys(widgets).map((key) => ({
      ...widgets[key],
      key,
    })),
  ] as const;
}