Skip to content

A local-first personal finance app

8.6 KB
Raw Blame
import type { ReactNode } from 'react';
import { Trans, useTranslation } from 'react-i18next';

import { Text } from '@actual-app/components/text';
import { theme } from '@actual-app/components/theme';
import { spacing } from '@actual-app/components/tokens';
import { View } from '@actual-app/components/view';
import type { FeatureFlag, ServerPrefs } from '@actual-app/core/types/prefs';

import { useAuth } from '#auth/AuthProvider';
import { Permissions } from '#auth/types';
import { Link } from '#components/common/Link';
import { Checkbox } from '#components/forms';
import { useLoginMethod, useMultiuserEnabled } from '#components/ServerContext';
import { useFeatureFlag } from '#hooks/useFeatureFlag';
import { useServerPref } from '#hooks/useServerPref';
import { useSyncedPref } from '#hooks/useSyncedPref';
import { useSyncServerStatus } from '#hooks/useSyncServerStatus';

import { SettingsGroup } from './SettingsGroup';
import { SettingsRow } from './SettingsRow';

type FeatureToggleProps = {
  flag: FeatureFlag;
  disableToggle?: boolean;
  error?: ReactNode;
  children: ReactNode;
  feedbackLink?: string;
  note?: ReactNode;
  isSubFeature?: boolean;
  subFeatures?: ReactNode;
  onDisable?: () => void;
};

function FeatureToggle({
  flag: flagName,
  disableToggle = false,
  feedbackLink,
  error,
  children,
  note,
  isSubFeature = false,
  subFeatures,
  onDisable,
}: FeatureToggleProps) {
  const enabled = useFeatureFlag(flagName);
  const [_, setFlagPref] = useSyncedPref(`flags.${flagName}`);
  const id = `settings-flag-${flagName}`;

  return (
    <SettingsRow
      htmlFor={id}
      title={children}
      isNested={isSubFeature}
      description={
        (note || (disableToggle && error)) && (
          <FeatureDetails note={note} error={disableToggle ? error : null} />
        )
      }
      control={
        <FeatureControl feedbackLink={feedbackLink}>
          <Checkbox
            id={id}
            checked={enabled}
            onChange={() => {
              setFlagPref(String(!enabled));
              if (enabled) {
                onDisable?.();
              }
            }}
            disabled={disableToggle}
            style={{ marginRight: 0 }}
          />
        </FeatureControl>
      }
    >
      {subFeatures}
    </SettingsRow>
  );
}

type FeatureDetailsProps = {
  note?: ReactNode;
  error?: ReactNode;
};

function FeatureDetails({ note, error }: FeatureDetailsProps) {
  return (
    <>
      {error && (
        <Text style={{ color: theme.errorText, fontWeight: 500 }}>
          {error}{' '}
        </Text>
      )}
      {note && <Text style={{ color: theme.warningText }}>{note}</Text>}
    </>
  );
}

type FeatureControlProps = {
  feedbackLink?: string;
  children: ReactNode;
};

function FeatureControl({ feedbackLink, children }: FeatureControlProps) {
  return (
    <View
      style={{ flexDirection: 'row', alignItems: 'center', gap: spacing.md }}
    >
      {feedbackLink && (
        <Link variant="external" to={feedbackLink} linkColor="purple">
          <Trans>Give feedback</Trans>
        </Link>
      )}
      {children}
    </View>
  );
}

type ServerFeatureToggleProps = {
  prefName: keyof ServerPrefs;
  disableToggle?: boolean;
  error?: ReactNode;
  children: ReactNode;
  feedbackLink?: string;
};

function ServerFeatureToggle({
  prefName,
  disableToggle = false,
  feedbackLink,
  error,
  children,
}: ServerFeatureToggleProps) {
  const [enabled, setEnabled] = useServerPref(prefName);

  const syncServerStatus = useSyncServerStatus();
  const isUsingServer = syncServerStatus !== 'no-server';
  const isServerOffline = syncServerStatus === 'offline';
  const { hasPermission } = useAuth();
  const loginMethod = useLoginMethod();
  const multiuserEnabled = useMultiuserEnabled();

  if (!isUsingServer || isServerOffline) {
    return null;
  }

  // Show to admins if OIDC is enabled, or to everyone if multi-user is not enabled
  const isAdmin = hasPermission(Permissions.ADMINISTRATOR);
  const oidcEnabled = loginMethod === 'openid';
  const shouldShow = (oidcEnabled && isAdmin) || !multiuserEnabled;

  if (!shouldShow) {
    return null;
  }

  const id = `settings-flag-${prefName}`;

  return (
    <SettingsRow
      htmlFor={id}
      title={children}
      description={disableToggle && error && <FeatureDetails error={error} />}
      control={
        <FeatureControl feedbackLink={feedbackLink}>
          <Checkbox
            id={id}
            checked={enabled === 'true'}
            onChange={() => {
              setEnabled(enabled === 'true' ? 'false' : 'true');
            }}
            disabled={disableToggle}
            style={{ marginRight: 0 }}
          />
        </FeatureControl>
      }
    />
  );
}

export function ExperimentalFeatures() {
  const { t } = useTranslation();
  const goalTemplatesEnabled = useFeatureFlag('goalTemplatesEnabled');
  const [, setGoalTemplatesUIPref] = useSyncedPref(
    'flags.goalTemplatesUIEnabled',
  );
  const isUsingServer = useSyncServerStatus() !== 'no-server';

  const showServerPrefs =
    localStorage.getItem('devEnableServerPrefs') === 'true';

  return (
    <SettingsGroup
      title={t('Experimental features')}
      description={t(
        'These features are not fully tested and may not work as expected. THEY MAY CAUSE IRRECOVERABLE DATA LOSS. They may do nothing at all. Only enable them if you know what you are doing.',
      )}
    >
      <FeatureToggle
        flag="goalTemplatesEnabled"
        onDisable={() => setGoalTemplatesUIPref('false')}
        subFeatures={
          goalTemplatesEnabled && (
            <FeatureToggle
              flag="goalTemplatesUIEnabled"
              feedbackLink="https://github.com/actualbudget/actual/issues/7692"
              isSubFeature
            >
              <Trans>Budget automations UI</Trans>
            </FeatureToggle>
          )
        }
      >
        <Trans>Goal templates</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="actionTemplating"
        feedbackLink="https://github.com/actualbudget/actual/issues/3606"
        note={
          <Trans>
            Deprecated: this feature will be removed in a future release. Use
            Excel formula mode (Rule formulae) instead.
          </Trans>
        }
      >
        <Trans>Rule action templating</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="formulaMode"
        feedbackLink="https://github.com/actualbudget/actual/issues/5949"
      >
        <Trans>Excel formula mode (Formula cards & Rule formulas)</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="currency"
        feedbackLink="https://github.com/actualbudget/actual/issues/5191"
      >
        <Trans>Currency support</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="mobileCalculator"
        feedbackLink="https://github.com/actualbudget/actual/issues/8255"
      >
        <Trans>Mobile calculator</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="newSidebarUI"
        feedbackLink="https://github.com/actualbudget/actual/issues/9007"
      >
        <Trans>Redesigned sidebar</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="sankeyReport"
        feedbackLink="https://github.com/actualbudget/actual/issues/1919"
      >
        <Trans>Sankey report</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="balanceForecastReport"
        feedbackLink="https://github.com/actualbudget/actual/issues/7669"
      >
        <Trans>Balance Forecast Report</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="budgetAnalysisReport"
        feedbackLink="https://github.com/actualbudget/actual/issues/6742"
      >
        <Trans>Budget Analysis Report</Trans>
      </FeatureToggle>
      <FeatureToggle
        flag="monteCarloReport"
        feedbackLink="https://github.com/actualbudget/actual/issues/8571"
      >
        <Trans>Monte Carlo Analysis Report</Trans>
      </FeatureToggle>
      {isUsingServer && (
        <FeatureToggle
          flag="enableBanking"
          feedbackLink="https://github.com/actualbudget/actual/issues/7799"
        >
          <Trans>Enable Banking sync (EU banks)</Trans>
        </FeatureToggle>
      )}
      {isUsingServer && (
        <FeatureToggle
          flag="akahuBankSync"
          feedbackLink="https://github.com/actualbudget/actual/issues/8020"
        >
          <Trans>Akahu Bank Sync (NZ banks)</Trans>
        </FeatureToggle>
      )}
      {showServerPrefs && (
        <ServerFeatureToggle
          prefName="flags.plugins"
          disableToggle
          feedbackLink="https://github.com/actualbudget/actual/issues/5950"
        >
          <Trans>Client-Side plugins (soon)</Trans>
        </ServerFeatureToggle>
      )}
    </SettingsGroup>
  );
}