Skip to content

A local-first personal finance app

5.7 KB
Raw Blame
import type { sortByOpType } from '@actual-app/core/types/models';
import { t } from 'i18next';

const intervalOptions = [
  {
    description: t('Daily'),
    key: 'Daily',
    defaultRange: 'This month',
  },
  {
    description: t('Weekly'),
    key: 'Weekly',
    defaultRange: 'Last 3 months',
  },
  {
    description: t('Monthly'),
    key: 'Monthly',
    defaultRange: 'Last 6 months',
  },
  {
    description: t('Quarterly'),
    key: 'Quarterly',
    defaultRange: 'Last 12 months',
  },
  {
    description: t('Yearly'),
    key: 'Yearly',
    defaultRange: 'Year to date',
  },
];

const currentIntervalOptions = [
  {
    description: t('This week'),
    disableInclude: true,
  },
  {
    description: t('This month'),
    disableInclude: true,
  },
  {
    description: t('Last month'),
    disableInclude: true,
  },
  {
    description: t('Current quarter'),
    disableInclude: true,
  },
  {
    description: t('Previous quarter'),
    disableInclude: true,
  },
  {
    description: t('Last 30 days'),
    disableInclude: true,
  },
  {
    description: t('Year to date'),
    disableInclude: true,
  },
  {
    description: t('Last year'),
    disableInclude: true,
  },
  {
    description: t('Prior year to date'),
    disableInclude: true,
  },
  {
    description: t('All time'),
    disableInclude: true,
  },
];

type graphOptions = {
  description: string;
  disabledSplit: string[];
  defaultSplit: string;
  disabledType: string[];
  defaultType: string;
  defaultSort: sortByOpType;
  disableLegend?: boolean;
  disableLabel?: boolean;
  disableSort?: boolean;
};

const totalGraphOptions: graphOptions[] = [
  {
    description: 'TableGraph',
    // CategoryGroup is only valid for DonutGraph
    disabledSplit: ['CategoryGroup'],
    defaultSplit: 'Category',
    disabledType: [],
    defaultType: 'Payment',
    disableLegend: true,
    disableLabel: true,
    defaultSort: 'budget',
  },
  {
    description: 'BarGraph',
    // CategoryGroup is only valid for DonutGraph
    disabledSplit: ['CategoryGroup'],
    defaultSplit: 'Category',
    disabledType: [],
    defaultType: 'Payment',
    defaultSort: 'desc',
  },
  {
    description: 'AreaGraph',
    // CategoryGroup is only valid for DonutGraph
    disabledSplit: [
      'Category',
      'Group',
      'CategoryGroup',
      'Payee',
      'Account',
      'Tag',
    ],
    defaultSplit: 'Interval',
    disabledType: [],
    defaultType: 'Payment',
    disableLegend: true,
    disableSort: true,
    defaultSort: 'desc',
  },
  {
    description: 'DonutGraph',
    // CategoryGroup is allowed here — it enables the two-ring concentric donut
    disabledSplit: [],
    defaultSplit: 'Category',
    disabledType: ['Net'],
    defaultType: 'Payment',
    defaultSort: 'desc',
  },
];

const timeGraphOptions: graphOptions[] = [
  {
    description: 'TableGraph',
    // CategoryGroup disabled in time mode (DonutGraph not available in time mode)
    disabledSplit: ['Interval', 'CategoryGroup'],
    defaultSplit: 'Category',
    disabledType: ['Net Payment', 'Net Deposit'],
    defaultType: 'Payment',
    disableLegend: true,
    disableLabel: true,
    disableSort: true,
    defaultSort: 'desc',
  },
  {
    description: 'StackedBarGraph',
    // CategoryGroup disabled in time mode
    disabledSplit: ['Interval', 'CategoryGroup'],
    defaultSplit: 'Category',
    disabledType: [],
    defaultType: 'Payment',
    disableSort: true,
    defaultSort: 'desc',
  },
  {
    description: 'LineGraph',
    // CategoryGroup disabled in time mode
    disabledSplit: ['Interval', 'CategoryGroup'],
    defaultSplit: 'Category',
    disabledType: [],
    defaultType: 'Payment',
    disableLegend: false,
    disableLabel: true,
    disableSort: true,
    defaultSort: 'desc',
  },
];

const modeOptions = [
  {
    description: 'total',
    graphs: totalGraphOptions,
    disabledGraph: ['LineGraph'],
    defaultGraph: 'TableGraph',
  },
  {
    description: 'time',
    graphs: timeGraphOptions,
    disabledGraph: ['AreaGraph', 'DonutGraph'],
    defaultGraph: 'TableGraph',
  },
];

export function disabledGraphList(
  item: string,
  newGraph: string,
  type: 'disabledSplit' | 'disabledType',
) {
  const graphList = modeOptions.find(d => d.description === item);
  if (!graphList) {
    return [];
  }

  const disabledList = graphList.graphs.find(e => e.description === newGraph);
  if (!disabledList) {
    return [];
  }

  return disabledList[type];
}

export function disabledLegendLabel(
  item: string,
  newGraph: string,
  type: 'disableLegend' | 'disableLabel',
) {
  const graphList = modeOptions.find(d => d.description === item);
  if (!graphList) {
    return false;
  }

  const disableLegendLabel = graphList.graphs.find(
    e => e.description === newGraph,
  );
  if (!disableLegendLabel) {
    return false;
  }

  return disableLegendLabel[type];
}

export function defaultsGraphList<
  K extends 'defaultSplit' | 'defaultType' | 'defaultSort',
>(item: string, newGraph: string, type: K) {
  const graphList = modeOptions.find(d => d.description === item);
  if (!graphList) {
    return '';
  }

  const defaultItem = graphList.graphs.find(e => e.description === newGraph);
  if (!defaultItem) {
    return '';
  }

  return defaultItem[type];
}

export const disabledList = {
  mode: modeOptions,
  modeGraphsMap: new Map(
    modeOptions.map(item => [item.description, item.disabledGraph]),
  ),
  currentInterval: new Map(
    currentIntervalOptions.map(item => [item.description, item.disableInclude]),
  ),
};

export const defaultsList = {
  mode: modeOptions,
  modeGraphsMap: new Map(
    modeOptions.map(item => [item.description, item.defaultGraph]),
  ),
  intervalRange: new Map(
    intervalOptions.map(item => [item.key, item.defaultRange]),
  ),
};