Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance] Extract menus to separate files (changes split from #2472) #2511

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/desktop-client/src/components/budget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { DynamicBudgetTable } from './DynamicBudgetTable';
import * as report from './report/ReportComponents';
import { ReportProvider } from './report/ReportContext';
import * as rollover from './rollover/RolloverComponents';
import { RolloverContext } from './rollover/RolloverContext';
import { RolloverProvider } from './rollover/RolloverContext';
import { prewarmAllMonths, prewarmMonth, switchBudgetType } from './util';

type ReportComponents = {
Expand Down Expand Up @@ -378,7 +378,7 @@ function BudgetInner(props: BudgetInnerProps) {
);
} else {
table = (
<RolloverContext
<RolloverProvider
summaryCollapsed={summaryCollapsed}
onBudgetAction={onBudgetAction}
onToggleSummaryCollapse={onToggleCollapse}
Expand All @@ -400,7 +400,7 @@ function BudgetInner(props: BudgetInnerProps) {
onReorderCategory={onReorderCategory}
onReorderGroup={onReorderGroup}
/>
</RolloverContext>
</RolloverProvider>
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { type ComponentPropsWithoutRef } from 'react';

import { reportBudget } from 'loot-core/src/client/queries';

import { Menu } from '../../common/Menu';
import { useSheetValue } from '../../spreadsheet/useSheetValue';

type BalanceMenuProps = Omit<
ComponentPropsWithoutRef<typeof Menu>,
'onMenuSelect' | 'items'
> & {
categoryId: string;
onCarryover: (carryover: boolean) => void;
};

export function BalanceMenu({
categoryId,
onCarryover,
...props
}: BalanceMenuProps) {
const carryover = useSheetValue(reportBudget.catCarryover(categoryId));
return (
<Menu
{...props}
onMenuSelect={name => {
switch (name) {
case 'carryover':
onCarryover?.(!carryover);
break;
default:
throw new Error(`Unsupported item: ${name}`);
}
}}
items={[
{
name: 'carryover',
text: carryover
? 'Remove overspending rollover'
: 'Rollover overspending',
},
]}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import React from 'react';

import { reportBudget } from 'loot-core/src/client/queries';

import { Menu } from '../../common/Menu';
import { useSheetValue } from '../../spreadsheet/useSheetValue';
import { Tooltip } from '../../tooltips';

import { BalanceMenu } from './BalanceMenu';

type BalanceTooltipProps = {
categoryId: string;
tooltip: { close: () => void };
Expand All @@ -22,8 +20,6 @@ export function BalanceTooltip({
onClose,
...tooltipProps
}: BalanceTooltipProps) {
const carryover = useSheetValue(reportBudget.catCarryover(categoryId));

const _onClose = () => {
tooltip.close();
onClose?.();
Expand All @@ -37,22 +33,15 @@ export function BalanceTooltip({
onClose={_onClose}
{...tooltipProps}
>
<Menu
onMenuSelect={() => {
onBudgetAction(monthIndex, 'carryover', {
<BalanceMenu
categoryId={categoryId}
onCarryover={carryover => {
onBudgetAction?.(monthIndex, 'carryover', {
category: categoryId,
flag: !carryover,
flag: carryover,
});
_onClose();
MatissJanis marked this conversation as resolved.
Show resolved Hide resolved
}}
items={[
{
name: 'carryover',
text: carryover
? 'Remove overspending rollover'
: 'Rollover overspending',
},
]}
/>
</Tooltip>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React, { type ComponentPropsWithoutRef } from 'react';

import { rolloverBudget } from 'loot-core/src/client/queries';

import { Menu } from '../../common/Menu';
import { useSheetValue } from '../../spreadsheet/useSheetValue';

type BalanceMenuProps = Omit<
ComponentPropsWithoutRef<typeof Menu>,
'onMenuSelect' | 'items'
> & {
categoryId: string;
onTransfer: () => void;
onCarryover: (carryOver: boolean) => void;
onCover: () => void;
};

export function BalanceMenu({
categoryId,
onTransfer,
onCarryover,
onCover,
...props
}: BalanceMenuProps) {
const carryover = useSheetValue(rolloverBudget.catCarryover(categoryId));
const balance = useSheetValue(rolloverBudget.catBalance(categoryId));
return (
<Menu
MatissJanis marked this conversation as resolved.
Show resolved Hide resolved
{...props}
onMenuSelect={name => {
switch (name) {
case 'transfer':
onTransfer?.();
break;
case 'carryover':
onCarryover?.(!carryover);
break;
case 'cover':
onCover?.();
break;
default:
throw new Error(`Unsupported item: ${name}`);
}
}}
items={[
{
name: 'transfer',
text: 'Transfer to another category',
},
{
name: 'carryover',
text: carryover
? 'Remove overspending rollover'
: 'Rollover overspending',
},
...(balance < 0
? [
{
name: 'cover',
text: 'Cover overspending',
},
]
: []),
]}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React, { useState } from 'react';

import { rolloverBudget } from 'loot-core/src/client/queries';

import { Menu } from '../../common/Menu';
import { useSheetValue } from '../../spreadsheet/useSheetValue';
import { Tooltip } from '../../tooltips';

import { BalanceMenu } from './BalanceMenu';
import { CoverTooltip } from './CoverTooltip';
import { TransferTooltip } from './TransferTooltip';

Expand All @@ -16,6 +16,7 @@ type BalanceTooltipProps = {
onBudgetAction: (idx: number, action: string, arg?: unknown) => void;
onClose?: () => void;
};

export function BalanceTooltip({
categoryId,
tooltip,
Expand All @@ -24,8 +25,7 @@ export function BalanceTooltip({
onClose,
...tooltipProps
}: BalanceTooltipProps) {
const carryover = useSheetValue(rolloverBudget.catCarryover(categoryId));
const balance = useSheetValue(rolloverBudget.catBalance(categoryId));
const catBalance = useSheetValue(rolloverBudget.catBalance(categoryId));
const [menu, setMenu] = useState('menu');

const _onClose = () => {
Expand All @@ -43,52 +43,31 @@ export function BalanceTooltip({
onClose={_onClose}
{...tooltipProps}
>
<Menu
onMenuSelect={type => {
if (type === 'carryover') {
onBudgetAction(monthIndex, 'carryover', {
category: categoryId,
flag: !carryover,
});
_onClose();
} else {
setMenu(type);
}
<BalanceMenu
categoryId={categoryId}
onCarryover={carryover => {
onBudgetAction(monthIndex, 'carryover', {
category: categoryId,
flag: carryover,
});
_onClose();
}}
items={[
{
name: 'transfer',
text: 'Transfer to another category',
},
{
name: 'carryover',
text: carryover
? 'Remove overspending rollover'
: 'Rollover overspending',
},
...(balance < 0
? [
{
name: 'cover',
text: 'Cover overspending',
},
]
: []),
]}
onTransfer={() => setMenu('transfer')}
onCover={() => setMenu('cover')}
/>
</Tooltip>
)}

{menu === 'transfer' && (
<TransferTooltip
initialAmountName={rolloverBudget.catBalance(categoryId)}
initialAmount={catBalance}
showToBeBudgeted={true}
onClose={_onClose}
onSubmit={(amount, toCategory) => {
onSubmit={(amount, toCategoryId) => {
onBudgetAction(monthIndex, 'transfer-category', {
amount,
from: categoryId,
to: toCategory,
to: toCategoryId,
});
}}
/>
Expand All @@ -97,10 +76,10 @@ export function BalanceTooltip({
{menu === 'cover' && (
<CoverTooltip
onClose={_onClose}
onSubmit={fromCategory => {
onSubmit={fromCategoryId => {
onBudgetAction(monthIndex, 'cover', {
to: categoryId,
from: fromCategory,
from: fromCategoryId,
});
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,18 @@ import { addToBeBudgetedGroup } from '../util';

type CoverTooltipProps = {
tooltipProps?: ComponentProps<typeof Tooltip>;
onSubmit: (category: unknown) => void;
onSubmit: (categoryId: string) => void;
onClose: () => void;
};
export function CoverTooltip({
tooltipProps,
onSubmit,
onClose,
}: CoverTooltipProps) {
const { grouped } = useCategories();
const categoryGroups = addToBeBudgetedGroup(
grouped.filter(g => !g.is_income),
);
const [category, setCategory] = useState<string | null>(null);

function submit() {
if (category) {
onSubmit(category);
onClose();
}
}
const _onSubmit = (categoryId: string) => {
onSubmit?.(categoryId);
onClose?.();
};

return (
<Tooltip
Expand All @@ -39,23 +31,46 @@ export function CoverTooltip({
{...tooltipProps}
onClose={onClose}
>
<Cover onSubmit={_onSubmit} />
</Tooltip>
);
}

type CoverProps = {
onSubmit: (categoryId: string) => void;
};

function Cover({ onSubmit }: CoverProps) {
const { grouped: originalCategoryGroups } = useCategories();
const categoryGroups = addToBeBudgetedGroup(
originalCategoryGroups.filter(g => !g.is_income),
);
const [categoryId, setCategoryId] = useState<string | null>(null);

function submit() {
if (categoryId) {
onSubmit(categoryId);
}
}
return (
<>
<View style={{ marginBottom: 5 }}>Cover from category:</View>

<InitialFocus>
{node => (
<CategoryAutocomplete
categoryGroups={categoryGroups}
value={null}
value={categoryGroups.find(g => g.id === categoryId)}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔨 warning: ‏doesn't seem to work

Untitled.mov

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is existing behavior, the value gets set but the autocomplete component clears the selection on blur by default.

openOnFocus={true}
onUpdate={() => {}}
onSelect={(id: string | undefined) => setCategory(id || null)}
onSelect={(id: string | undefined) => setCategoryId(id || null)}
inputProps={{
inputRef: node,
onKeyDown: e => {
if (e.key === 'Enter') {
submit();
}
},
placeholder: '(none)',
}}
showHiddenCategories={false}
/>
Expand All @@ -79,6 +94,6 @@ export function CoverTooltip({
Transfer
</Button>
</View>
</Tooltip>
</>
);
}
Loading
Loading