Skip to content

Commit

Permalink
Remove dependency on local cached responses
Browse files Browse the repository at this point in the history
  • Loading branch information
justinhaaheim committed Jul 4, 2024
1 parent d2a6076 commit d8cf843
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 66 deletions.
92 changes: 26 additions & 66 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,11 @@ import {
import * as ynab from 'ynab';

import packageJson from '../package.json';
// accounts for budgetID 21351b66-d7c6-4e53-895b-b8cd753c2347
import accountsCachedJson from './accountsCached.local.json';
import amazonLabels2024Local from './amazonLabels2024.local';
import budgetsCachedJson from './budgetsCached.local.json';
import ColorSchemeToggle from './ColorSchemeToggle';
import config from './config.json';
import {
convertParsedLabelsToStandardTransaction,
convertYnabToStandardTransaction,
getLabelsFromCsv,
} from './Converters';
import {getDateTimeString, getTimePrettyString} from './DateUtils';
import initiateUserJSONDownload from './initiateUserJSONDownlaod';
Expand All @@ -60,16 +55,12 @@ import {
import {syncLabelsToYnab, undoSyncLabelsToYnab} from './Sync';
import TransactionDataGrid from './TransactionDataGrid';
import {
budgetCompareFunctionForSort,
getYNABErrorHandler,
YNAB_TOKEN_EXPIRATION_TIMESTAMP_LOCAL_STORAGE_KEY,
YNAB_TOKEN_LOCAL_STORAGE_KEY,
} from './YnabHelpers';

const budgetIDForCachedAccounts = '21351b66-d7c6-4e53-895b-b8cd753c2347';

const USE_CACHED_RESPONSES = false; // true;
const CACHED_RESPONSE_ARTIFICIAL_DELAY_MS = 500;

const YNAB_DEFAULT_TOKEN_EXPIRATION_TIME_SECONDS = 7200;
// Err on the side of telling the user it expires earlier than it does
const TOKEN_EXPIRATION_REDUCTION_MS = 1000 * 60;
Expand Down Expand Up @@ -117,9 +108,6 @@ function App() {

const [labelData, setLabelData] = useState<ParsedLabelsTyped | null>(null);
const labelsWithoutPrefix = useMemo<StandardTransactionType[] | null>(() => {
if (USE_CACHED_RESPONSES) {
return getLabelsFromCsv(amazonLabels2024Local);
}
if (labelData != null) {
// Take the raw data that was parsed and determine the best label from it
return convertParsedLabelsToStandardTransaction(labelData);
Expand Down Expand Up @@ -303,64 +291,36 @@ function App() {

useEffect(() => {
if (ynabApi != null && budgets == null) {
const budgetSortFn = (b1: ynab.BudgetSummary, b2: ynab.BudgetSummary) => {
// Use the unary to convert date to number https://github.com/microsoft/TypeScript/issues/5710#issuecomment-157886246
const d1 =
b1.last_modified_on != null
? +new Date(b1.last_modified_on)
: Number.NEGATIVE_INFINITY;
const d2 =
b2.last_modified_on != null
? +new Date(b2.last_modified_on)
: Number.NEGATIVE_INFINITY;
// We want dates in descending order
return d2 - d1;
};
if (!USE_CACHED_RESPONSES) {
(async function () {
console.debug('📡 Fetching budgets data...');
try {
const budgetsResponse = await ynabApi.budgets.getBudgets();
console.debug('📡 Budget data received', budgetsResponse);
setBudgets(budgetsResponse.data.budgets.sort(budgetSortFn));
} catch (error: unknown) {
const handler = getYNABErrorHandler(onAuthError);
handler(error);
}
})();
} else {
console.debug('Using cached budgets data');
setTimeout(() => {
setBudgets(budgetsCachedJson.sort(budgetSortFn));
}, CACHED_RESPONSE_ARTIFICIAL_DELAY_MS);
}
(async function () {
console.debug('📡 Fetching budgets data...');
try {
const budgetsResponse = await ynabApi.budgets.getBudgets();
console.debug('📡 Budget data received', budgetsResponse);
setBudgets(
budgetsResponse.data.budgets.sort(budgetCompareFunctionForSort),
);
} catch (error: unknown) {
const handler = getYNABErrorHandler(onAuthError);
handler(error);
}
})();
}
}, [budgets, onAuthError, ynabApi]);

useEffect(() => {
if (ynabApi != null && selectedBudgetID != null && accounts == null) {
if (
USE_CACHED_RESPONSES &&
selectedBudgetID === budgetIDForCachedAccounts
) {
console.debug('Using cached accounts data');
setTimeout(() => {
setAccounts(accountsCachedJson as ynab.Account[]);
}, CACHED_RESPONSE_ARTIFICIAL_DELAY_MS);
} else {
(async function () {
console.debug('📡 Fetching accounts data...');
try {
const accountsResponse =
await ynabApi.accounts.getAccounts(selectedBudgetID);
console.debug('📡 Accounts data received', accountsResponse);
setAccounts(accountsResponse.data.accounts);
} catch (error: unknown) {
const handler = getYNABErrorHandler(() => setYnabApi(null));
handler(error);
}
})();
}
(async function () {
console.debug('📡 Fetching accounts data...');
try {
const accountsResponse =
await ynabApi.accounts.getAccounts(selectedBudgetID);
console.debug('📡 Accounts data received', accountsResponse);
setAccounts(accountsResponse.data.accounts);
} catch (error: unknown) {
const handler = getYNABErrorHandler(() => setYnabApi(null));
handler(error);
}
})();
}
}, [accounts, selectedBudgetID, ynabApi]);

Expand Down
17 changes: 17 additions & 0 deletions src/YnabHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ export const YNAB_TOKEN_EXPIRATION_TIMESTAMP_LOCAL_STORAGE_KEY =

export type YNABErrorType = {error: ynab.ErrorDetail};

export function budgetCompareFunctionForSort(
b1: ynab.BudgetSummary,
b2: ynab.BudgetSummary,
): number {
// Use the unary to convert date to number https://github.com/microsoft/TypeScript/issues/5710#issuecomment-157886246
const d1 =
b1.last_modified_on != null
? +new Date(b1.last_modified_on)
: Number.NEGATIVE_INFINITY;
const d2 =
b2.last_modified_on != null
? +new Date(b2.last_modified_on)
: Number.NEGATIVE_INFINITY;
// We want dates in descending order
return d2 - d1;
}

export function getYNABErrorHandler(
onAuthError?: (e: YNABErrorType) => void,
): (error: unknown) => void {
Expand Down

0 comments on commit d8cf843

Please sign in to comment.