Skip to content

Commit

Permalink
Fix markNoficationRead for default account
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 8, 2024
1 parent 829b6d0 commit a3e94ae
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
20 changes: 20 additions & 0 deletions src/renderer/lib/accounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { AccountInfo } from '../../shared-types';
import type { AppReduxState } from '../types';
import { defaultAccountInfo } from './constants';

export function getAllAccounts(state: AppReduxState): AccountInfo[] {
// FIXME: This should be a permanent migration to the account system somewhere
const doesIncludeMainGithubAccount = state.accounts.some(
(account) => account.serverUrl === defaultAccountInfo.serverUrl
);
if (!doesIncludeMainGithubAccount && state.token) {
return [
...state.accounts,
{
...defaultAccountInfo,
apiKey: state.token,
},
];
}
return state.accounts;
}
10 changes: 5 additions & 5 deletions src/renderer/lib/github-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Middleware } from 'redux';
import type { AccountInfo, AppReduxState, Note } from '../types';
import { isAction } from './helpers';
import { getAllAccounts } from './accounts';

export function createGitHubMiddleware(): Middleware<{}, AppReduxState> {
return (store) => (next) => (action) => {
Expand All @@ -18,11 +19,10 @@ export function createGitHubMiddleware(): Middleware<{}, AppReduxState> {
next(action);
return;
}
const account = store
.getState()
.accounts.find(
(account) => account.id === action.note.gitnewsAccountId
);
const accounts = getAllAccounts(store.getState());
const account = accounts.find(
(account) => account.id === action.note.gitnewsAccountId
);
if (!account) {
console.error(
'Cannot find account for notification to mark as read',
Expand Down
39 changes: 9 additions & 30 deletions src/renderer/lib/gitnews-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { AccountInfo, AppReduxState, Note, UnknownFetchError } from '../types';
import { AppDispatch } from './store';
import { createDemoNotifications } from './demo-mode';
import { defaultAccountInfo } from './constants';
import { getAllAccounts } from './accounts';

const debug = debugFactory('gitnews-menubar');

Expand Down Expand Up @@ -71,19 +71,11 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
return;
};

async function performFetch(
{
fetchingInProgress,
token,
fetchingStartedAt,
isDemoMode,
accounts,
}: AppReduxState,
next: AppDispatch
) {
async function performFetch(state: AppReduxState, next: AppDispatch) {
const fetchingMaxTime = secsToMs(120); // 2 minutes
if (fetchingInProgress) {
const timeSinceFetchingStarted = Date.now() - (fetchingStartedAt || 0);
if (state.fetchingInProgress) {
const timeSinceFetchingStarted =
Date.now() - (state.fetchingStartedAt || 0);
if (timeSinceFetchingStarted > fetchingMaxTime) {
const message = `It has been too long since we started fetching (${timeSinceFetchingStarted} ms). Giving up.`;
debug(message);
Expand All @@ -103,7 +95,7 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
next(changeToOffline());
return;
}
if (!token) {
if (!state.token) {
next(changeToOffline());
return;
}
Expand All @@ -112,21 +104,8 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
// or the app will get stuck never updating again.
next(fetchBegin());

// FIXME: This should be a permanent migration to the account system somewhere
const doesIncludeMainGithubAccount = accounts.some(
(account) => account.serverUrl === defaultAccountInfo.serverUrl
);
if (!doesIncludeMainGithubAccount) {
accounts = [
...accounts,
{
...defaultAccountInfo,
apiKey: token,
},
];
}

const getGithubNotifications = getFetcher(accounts, isDemoMode);
const accounts = getAllAccounts(state);
const getGithubNotifications = getFetcher(accounts, state.isDemoMode);
try {
const notes = await getGithubNotifications();
debug('notifications retrieved', notes);
Expand All @@ -143,7 +122,7 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
'warn'
);
next(fetchDone());
getErrorHandler(next)(err as Error, token);
getErrorHandler(next)(err as Error, state.token);
}
}

Expand Down

0 comments on commit a3e94ae

Please sign in to comment.