Skip to content

A local-first personal finance app

9.2 KB
Raw Blame
import { useState } from 'react';
import type { Ref } from 'react';
import { Trans } from 'react-i18next';

import { Button } from '@actual-app/components/button';
import { styles } from '@actual-app/components/styles';
import { theme } from '@actual-app/components/theme';
import { Tooltip } from '@actual-app/components/tooltip';
import { View } from '@actual-app/components/view';
import type { SyncedPrefs } from '@actual-app/core/types/prefs';
import {
  addDays,
  format as formatDate,
  getDate,
  isSameMonth,
  startOfMonth,
  startOfWeek,
} from 'date-fns';

import { FinancialText } from '#components/FinancialText';
import { PrivacyFilter } from '#components/PrivacyFilter';
import { useFormat } from '#hooks/useFormat';
import { useResizeObserver } from '#hooks/useResizeObserver';

type CalendarGraphProps = {
  data: {
    date: Date;
    incomeValue: number;
    expenseValue: number;
    incomeSize: number;
    expenseSize: number;
  }[];
  start: Date;
  firstDayOfWeekIdx?: SyncedPrefs['firstDayOfWeekIdx'];
  isEditing?: boolean;
  onDayClick: (date: Date | null) => void;
};
export function CalendarGraph({
  data,
  start,
  firstDayOfWeekIdx,
  isEditing,
  onDayClick,
}: CalendarGraphProps) {
  const format = useFormat();

  const startingDate = startOfWeek(start, {
    weekStartsOn:
      firstDayOfWeekIdx !== undefined &&
      !Number.isNaN(parseInt(firstDayOfWeekIdx)) &&
      parseInt(firstDayOfWeekIdx) >= 0 &&
      parseInt(firstDayOfWeekIdx) <= 6
        ? (parseInt(firstDayOfWeekIdx) as 0 | 1 | 2 | 3 | 4 | 5 | 6)
        : 0,
  });
  const [fontSize, setFontSize] = useState(14);

  const buttonRef = useResizeObserver(rect => {
    const newValue = Math.floor(rect.height / 2);
    if (newValue > 14) {
      setFontSize(14);
    } else {
      setFontSize(newValue);
    }
  });

  return (
    <>
      <View
        style={{
          color: theme.pageTextSubdued,
          display: 'grid',
          gridTemplateColumns: 'repeat(7, 1fr)',
          gridAutoRows: '1fr',
          gap: 2,
        }}
        onClick={() => onDayClick(null)}
      >
        {Array.from({ length: 7 }, (_, index) => (
          <View
            key={index}
            style={{
              textAlign: 'center',
              fontSize: 14,
              fontWeight: 500,
              padding: '3px 0',
              height: '100%',
              width: '100%',
              position: 'relative',
              marginBottom: 4,
            }}
          >
            {formatDate(addDays(startingDate, index), 'EEEEE')}
          </View>
        ))}
      </View>
      <View
        style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(7, 1fr)',
          gridAutoRows: '1fr',
          gap: 2,
          width: '100%',
          height: '100%',
          zIndex: isEditing ? -1 : 'auto', // Prevents interaction with calendar buttons when editing dashboard.
        }}
      >
        {data.map((day, index) =>
          !isSameMonth(day.date, startOfMonth(start)) ? (
            <View
              key={`empty-${day.date.getTime()}`}
              onClick={() => onDayClick(null)}
            />
          ) : day.incomeValue !== 0 || day.expenseValue !== 0 ? (
            <Tooltip
              key={day.date.getTime()}
              content={
                <View>
                  <View style={{ marginBottom: 10 }}>
                    <strong>{formatDate(day.date, 'MMM dd')}</strong>
                  </View>
                  <View style={{ lineHeight: 1.5 }}>
                    <View
                      style={{
                        display: 'grid',
                        gridTemplateColumns: '70px 1fr 60px',
                        gridAutoRows: '1fr',
                      }}
                    >
                      <View
                        style={{
                          textAlign: 'right',
                          marginRight: 4,
                        }}
                      >
                        <Trans>Income:</Trans>
                      </View>
                      <View
                        style={{
                          color: theme.reportsNumberPositive,
                          flexDirection: 'row',
                        }}
                      >
                        {day.incomeValue !== 0 ? (
                          <PrivacyFilter>
                            <FinancialText>
                              {format(day.incomeValue, 'financial')}
                            </FinancialText>
                          </PrivacyFilter>
                        ) : (
                          ''
                        )}
                      </View>
                      <View style={{ marginLeft: 4, flexDirection: 'row' }}>
                        (
                        <PrivacyFilter>
                          {Math.round(day.incomeSize * 100) / 100 + '%'}
                        </PrivacyFilter>
                        )
                      </View>
                      <View
                        style={{
                          textAlign: 'right',
                          marginRight: 4,
                        }}
                      >
                        <Trans>Expenses:</Trans>
                      </View>
                      <View
                        style={{
                          color: theme.reportsNumberNegative,
                          flexDirection: 'row',
                        }}
                      >
                        {day.expenseValue !== 0 ? (
                          <PrivacyFilter>
                            <FinancialText>
                              {format(day.expenseValue, 'financial')}
                            </FinancialText>
                          </PrivacyFilter>
                        ) : (
                          ''
                        )}
                      </View>
                      <View style={{ marginLeft: 4, flexDirection: 'row' }}>
                        (
                        <PrivacyFilter>
                          {Math.round(day.expenseSize * 100) / 100 + '%'}
                        </PrivacyFilter>
                        )
                      </View>
                    </View>
                  </View>
                </View>
              }
              placement="bottom end"
              style={{
                ...styles.tooltip,
                lineHeight: 1.5,
                padding: '6px 10px',
              }}
            >
              <DayButton
                key={day.date.getTime()}
                resizeRef={el => {
                  if (index === 15 && el) {
                    buttonRef(el);
                  }
                }}
                fontSize={fontSize}
                day={day}
                onPress={() => onDayClick(day.date)}
              />
            </Tooltip>
          ) : (
            <DayButton
              key={day.date.getTime()}
              resizeRef={el => {
                if (index === 15 && el) {
                  buttonRef(el);
                }
              }}
              fontSize={fontSize}
              day={day}
              onPress={() => onDayClick(day.date)}
            />
          ),
        )}
      </View>
    </>
  );
}

type DayButtonProps = {
  fontSize: number;
  resizeRef: Ref<HTMLButtonElement>;
  day: {
    date: Date;
    incomeSize: number;
    expenseSize: number;
  };
  onPress: () => void;
};
function DayButton({ day, onPress, fontSize, resizeRef }: DayButtonProps) {
  return (
    <Button
      ref={resizeRef}
      aria-label={formatDate(day.date, 'MMMM d, yyyy')}
      style={{
        borderColor: 'transparent',
        backgroundColor: theme.calendarCellBackground,
        position: 'relative',
        padding: 'unset',
        height: '100%',
        minWidth: 0,
        minHeight: 0,
        margin: 0,
      }}
      onPress={() => onPress()}
    >
      {day.expenseSize !== 0 && (
        <View
          style={{
            position: 'absolute',
            width: '50%',
            height: '100%',
            background: theme.reportsNumberNegative,
            opacity: 0.2,
            right: 0,
          }}
        />
      )}
      {day.incomeSize !== 0 && (
        <View
          style={{
            position: 'absolute',
            width: '50%',
            height: '100%',
            background: theme.reportsNumberPositive,
            opacity: 0.2,
            left: 0,
          }}
        />
      )}
      <View
        style={{
          position: 'absolute',
          left: 0,
          bottom: 0,
          opacity: 0.9,
          height: `${Math.ceil(day.incomeSize)}%`,
          backgroundColor: theme.reportsNumberPositive,
          width: '50%',
          transition: 'height 0.5s ease-out',
        }}
      />

      <View
        style={{
          position: 'absolute',
          right: 0,
          bottom: 0,
          opacity: 0.9,
          height: `${Math.ceil(day.expenseSize)}%`,
          backgroundColor: theme.reportsNumberNegative,
          width: '50%',
          transition: 'height 0.5s ease-out',
        }}
      />
      <span
        style={{
          fontSize: `${fontSize}px`,
          fontWeight: 500,
          position: 'relative',
        }}
      >
        {getDate(day.date)}
      </span>
    </Button>
  );
}