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

Migrate useSplitsExpanded to TypeScript #3945

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2094,9 +2094,9 @@ export const TransactionTable = forwardRef((props, ref) => {
result = props.transactions.filter((t, idx) => {
if (t.parent_id) {
if (idx >= index) {
return splitsExpanded.expanded(t.parent_id);
return splitsExpanded.isExpanded(t.parent_id);
} else if (prevSplitsExpanded.current) {
return prevSplitsExpanded.current.expanded(t.parent_id);
return prevSplitsExpanded.current.isExpanded(t.parent_id);
}
}
return true;
Expand All @@ -2113,7 +2113,7 @@ export const TransactionTable = forwardRef((props, ref) => {

result = props.transactions.filter(t => {
if (t.parent_id) {
return splitsExpanded.expanded(t.parent_id);
return splitsExpanded.isExpanded(t.parent_id);
}
return true;
});
Expand Down Expand Up @@ -2584,7 +2584,7 @@ export const TransactionTable = forwardRef((props, ref) => {
transactionsByParent={transactionsByParent}
transferAccountsByTransaction={transferAccountsByTransaction}
selectedItems={selectedItems}
isExpanded={splitsExpanded.expanded}
isExpanded={splitsExpanded.isExpanded}
onSave={onSave}
onDelete={onDelete}
onDuplicate={onDuplicate}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,76 @@ import React, {
useEffect,
useContext,
useReducer,
type Dispatch,
type ReactNode,
} from 'react';
import { useSelector, useDispatch } from 'react-redux';

const SplitsExpandedContext = createContext(null);
import {
type SplitMode,
type SplitState,
} from 'loot-core/client/state-types/app';

type ToggleSplitAction = {
type: 'toggle-split';
id: string;
};

type OpenSplitAction = {
type: 'open-split';
id: string;
};

type CloseSplitsAction = {
type: 'close-splits';
ids: string[];
};

type SetModeAction = {
type: 'set-mode';
mode: SplitMode;
};

type SwitchModeAction = {
type: 'switch-mode';
id: string;
};

type FinishSwitchModeAction = {
type: 'finish-switch-mode';
};

type Actions =
| ToggleSplitAction
| OpenSplitAction
| CloseSplitsAction
| SetModeAction
| SwitchModeAction
| FinishSwitchModeAction;

type SplitsStateContext = {
state: SplitState;
dispatch: Dispatch<Actions>;
};

const SplitsExpandedContext = createContext<SplitsStateContext>({
state: {
mode: 'collapse',
ids: new Set(),
transitionId: null,
},
dispatch: () => {
throw new Error('Unitialised context method called: dispatch');
},
});

export function useSplitsExpanded() {
const data = useContext(SplitsExpandedContext);

return useMemo(
() => ({
...data,
expanded: id =>
isExpanded: (id: string) =>
data.state.mode === 'collapse'
? !data.state.ids.has(id)
: data.state.ids.has(id),
Expand All @@ -24,12 +82,20 @@ export function useSplitsExpanded() {
);
}

export function SplitsExpandedProvider({ children, initialMode = 'expand' }) {
type SplitsExpandedProviderProps = {
children?: ReactNode;
initialMode: SplitMode;
};

export function SplitsExpandedProvider({
children,
initialMode = 'expand',
}: SplitsExpandedProviderProps) {
const cachedState = useSelector(state => state.app.lastSplitState);
const reduxDispatch = useDispatch();

const [state, dispatch] = useReducer(
(state, action) => {
(state: SplitState, action: Actions): SplitState => {
switch (action.type) {
case 'toggle-split': {
const ids = new Set([...state.ids]);
Expand Down Expand Up @@ -66,7 +132,7 @@ export function SplitsExpandedProvider({ children, initialMode = 'expand' }) {
return {
...state,
mode: action.mode,
ids: new Set(),
ids: new Set<string>(),
transitionId: null,
};
}
Expand All @@ -80,15 +146,17 @@ export function SplitsExpandedProvider({ children, initialMode = 'expand' }) {
...state,
mode: state.mode === 'expand' ? 'collapse' : 'expand',
transitionId: action.id,
ids: new Set(),
ids: new Set<string>(),
};
case 'finish-switch-mode':
return { ...state, transitionId: null };
default:
throw new Error('Unknown action type: ' + action.type);
}
},
cachedState.current || { ids: new Set(), mode: initialMode },
cachedState.current || {
ids: new Set<string>(),
mode: initialMode,
transitionId: null,
},
);

useEffect(() => {
Expand Down
7 changes: 6 additions & 1 deletion packages/loot-core/src/client/state-types/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { UndoState } from '../../server/undo';
import type * as constants from '../constants';

export type SplitState = { ids: Set<string>; mode: 'collapse' | 'expand' };
export type SplitMode = 'collapse' | 'expand';
export type SplitState = {
ids: Set<string>;
mode: SplitMode;
transitionId: string | null;
};

export type AppState = {
loadingText: string | null;
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3945.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [dkhalife]
---

Migrate useSplitsExpanded to TypeScript
Loading