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

Add typings to most of the Redux logic #1269

Merged
merged 25 commits into from
Jul 18, 2023
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
7 changes: 4 additions & 3 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,10 @@ module.exports = {
},
{
files: [
'**/icons/**/*.js',
'**/mocks/**/*.{js,ts,tsx}',
'**/{mocks,__mocks__}/*.{js,ts,tsx}',
'packages/loot-core/src/types/**/*',
'packages/loot-core/src/client/state-types/**/*',
'**/icons/**/*',
'**/{mocks,__mocks__}/**/*',
// can't correctly resolve usages
'**/*.{testing,electron,browser,web,api}.ts',
],
Expand Down
1 change: 1 addition & 0 deletions packages/desktop-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.1",
"@types/react-modal": "^3.16.0",
"@types/react-redux": "^7.1.25",
"@types/react-router-dom": "^5.3.3",
"@types/uuid": "^9.0.2",
"@types/webpack-bundle-analyzer": "^4.6.0",
Expand Down
13 changes: 0 additions & 13 deletions packages/desktop-client/src/components/Modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import useSyncServerStatus from '../hooks/useSyncServerStatus';

import BudgetSummary from './modals/BudgetSummary';
import CloseAccount from './modals/CloseAccount';
import ConfigureLinkedAccounts from './modals/ConfigureLinkedAccounts';
import ConfirmCategoryDelete from './modals/ConfirmCategoryDelete';
import CreateAccount from './modals/CreateAccount';
import CreateEncryptionKey from './modals/CreateEncryptionKey';
Expand Down Expand Up @@ -97,18 +96,6 @@ function Modals({
/>
);

case 'configure-linked-accounts':
return (
<ConfigureLinkedAccounts
modalProps={modalProps}
institution={options.institution}
publicToken={options.publicToken}
accounts={options.accounts}
upgradingId={options.upgradingId}
actions={actions}
/>
);

case 'confirm-category-delete':
return (
<ConfirmCategoryDelete
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import React, { useState, useEffect, useMemo } from 'react';
import { connect } from 'react-redux';
import React, {
useState,
useEffect,
useMemo,
type SetStateAction,
} from 'react';
import { connect, useSelector } from 'react-redux';

import { bindActionCreators } from 'redux';

import * as actions from 'loot-core/src/client/actions';
import type { NotificationWithId } from 'loot-core/src/client/state-types/notifications';

import Loading from '../icons/AnimatedLoading';
import Delete from '../icons/v0/Delete';
Expand All @@ -19,7 +25,12 @@ import {
LinkButton,
} from './common';

function compileMessage(message, actions, setLoading, onRemove) {
function compileMessage(
message: string,
actions: Record<string, () => void>,
setLoading: (arg: SetStateAction<boolean>) => void,
onRemove?: () => void,
) {
return (
<Stack spacing={2}>
{message.split(/\n\n/).map((paragraph, idx) => {
Expand Down Expand Up @@ -65,7 +76,13 @@ function compileMessage(message, actions, setLoading, onRemove) {
);
}

function Notification({ notification, onRemove }) {
function Notification({
notification,
onRemove,
}: {
notification: NotificationWithId;
onRemove: () => void;
}) {
let { type, title, message, pre, messageActions, sticky, internal, button } =
notification;

Expand Down Expand Up @@ -203,7 +220,8 @@ function Notification({ notification, onRemove }) {
);
}

function Notifications({ notifications, removeNotification, style }) {
function Notifications({ removeNotification, style }) {
let notifications = useSelector(state => state.notifications.notifications);
return (
<View
style={[
Expand Down Expand Up @@ -232,7 +250,6 @@ function Notifications({ notifications, removeNotification, style }) {
);
}

export default connect(
state => ({ notifications: state.notifications.notifications }),
dispatch => bindActionCreators(actions, dispatch),
)(Notifications);
export default connect(null, dispatch => bindActionCreators(actions, dispatch))(
Notifications,
);
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getErrorMessage(error) {
}
}

function Import({ modalProps, availableImports }) {
function Import({ modalProps }) {
const dispatch = useDispatch();
const [error, setError] = useState(false);
const [importing, setImporting] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function getErrorMessage(error) {
}
}

function Import({ modalProps, availableImports }) {
function Import({ modalProps }) {
const dispatch = useDispatch();
const [error, setError] = useState(false);
const [importing, setImporting] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function getErrorMessage(error) {
}
}

function Import({ modalProps, availableImports }) {
function Import({ modalProps }) {
const dispatch = useDispatch();
const [error, setError] = useState(false);
const [importing, setImporting] = useState(false);
Expand Down
12 changes: 2 additions & 10 deletions packages/desktop-client/src/components/manager/Modals.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import ImportActual from './ImportActual';
import ImportYNAB4 from './ImportYNAB4';
import ImportYNAB5 from './ImportYNAB5';

function Modals({ modalStack, isHidden, availableImports, actions }) {
function Modals({ modalStack, isHidden, actions }) {
let stack = modalStack.map(({ name, options }, idx) => {
const modalProps = {
onClose: actions.popModal,
Expand All @@ -38,14 +38,7 @@ function Modals({ modalStack, isHidden, availableImports, actions }) {
/>
);
case 'import':
return (
<Import
key={name}
modalProps={modalProps}
actions={actions}
availableImports={availableImports}
/>
);
return <Import key={name} modalProps={modalProps} actions={actions} />;
case 'import-ynab4':
return (
<ImportYNAB4 key={name} modalProps={modalProps} actions={actions} />
Expand Down Expand Up @@ -102,7 +95,6 @@ export default connect(
modalStack: state.modals.modalStack,
isHidden: state.modals.isHidden,
budgets: state.budgets.budgets,
availableImports: state.budgets.availableImports,
}),
dispatch => ({ actions: bindActionCreators(actions, dispatch) }),
)(Modals);

This file was deleted.

15 changes: 15 additions & 0 deletions packages/desktop-client/src/hooks/useActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useMemo } from 'react';
import { useDispatch } from 'react-redux';

import { bindActionCreators } from 'redux';

import * as actions from 'loot-core/src/client/actions';

// https://react-redux.js.org/api/hooks#recipe-useactions
// eslint-disable-next-line import/no-unused-modules
export function useActions() {
const dispatch = useDispatch();
return useMemo(() => {
return bindActionCreators(actions, dispatch);
}, [dispatch]);
}
8 changes: 5 additions & 3 deletions packages/desktop-client/src/hooks/useFeatureFlag.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { useSelector } from 'react-redux';

const DEFAULT_FEATURE_FLAG_STATE: Record<string, boolean> = {
import { type FeatureFlag } from 'loot-core/src/client/state-types/prefs';

const DEFAULT_FEATURE_FLAG_STATE: Record<FeatureFlag, boolean> = {
reportBudget: false,
goalTemplatesEnabled: false,
};

export default function useFeatureFlag(name: string): boolean {
export default function useFeatureFlag(name: FeatureFlag): boolean {
return useSelector(state => {
const value = state.prefs.local[`flags.${name}`];

Expand All @@ -15,7 +17,7 @@ export default function useFeatureFlag(name: string): boolean {
});
}

export function useAllFeatureFlags(): Record<string, boolean> {
export function useAllFeatureFlags(): Record<FeatureFlag, boolean> {
return useSelector(state => {
return {
...DEFAULT_FEATURE_FLAG_STATE,
Expand Down
1 change: 1 addition & 0 deletions packages/loot-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@types/jest": "^27.5.0",
"@types/jlongster__sql.js": "npm:@types/sql.js@latest",
"@types/pegjs": "^0.10.3",
"@types/react-redux": "^7.1.25",
"@types/uuid": "^9.0.2",
"@types/webpack": "^5.28.0",
"@types/webpack-bundle-analyzer": "^4.6.0",
Expand Down
Loading
Loading