Skip to content

Commit

Permalink
Change note fetching to use actual account info
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Sep 8, 2024
1 parent fea7d3f commit b4c0d0f
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions src/renderer/lib/gitnews-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import {
addConnectionError,
setIsTokenInvalid,
} from '../lib/reducer';
import {
AccountInfo,
AppReduxState,
Note,
NoteReason,
UnknownFetchError,
} from '../types';
import { AccountInfo, AppReduxState, Note, UnknownFetchError } from '../types';
import { AppDispatch } from './store';
import { createDemoNotifications } from './demo-mode';

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

let currentDemoNotifications = createDemoNotifications();

const defaultAccountInfo = {
id: 'main-github-api',
serverUrl: 'https://api.github.com',
apiKey: '',
};

export function createFetcher(): Middleware<{}, AppReduxState> {
const fetcher: Middleware<object, AppReduxState> =
(store) => (next) => (action) => {
Expand Down Expand Up @@ -77,7 +77,13 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
};

async function performFetch(
{ fetchingInProgress, token, fetchingStartedAt, isDemoMode }: AppReduxState,
{
fetchingInProgress,
token,
fetchingStartedAt,
isDemoMode,
accounts,
}: AppReduxState,
next: AppDispatch
) {
const fetchingMaxTime = secsToMs(120); // 2 minutes
Expand Down Expand Up @@ -110,7 +116,18 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
// NOTE: After this point, any return action MUST disable fetchingInProgress
// or the app will get stuck never updating again.
next(fetchBegin());
const getGithubNotifications = getFetcher(token, isDemoMode);

// FIXME: This should be a permanent migration to the account system somewhere
if (accounts.length === 0) {
accounts = [
{
...defaultAccountInfo,
apiKey: token,
},
];
}

const getGithubNotifications = getFetcher(accounts, isDemoMode);
try {
const notes = await getGithubNotifications();
debug('notifications retrieved', notes);
Expand All @@ -132,18 +149,20 @@ export function createFetcher(): Middleware<{}, AppReduxState> {
}

function getFetcher(
token: string,
accounts: AccountInfo[],
isDemoMode: boolean
): () => Promise<Note[]> {
if (isDemoMode) {
return () => getDemoNotifications();
}
return () =>
fetchNotifications({
id: 'main-github-api', // FIXME: use the account info from the state
serverUrl: 'https://api.github.com',
apiKey: token,
});
return async () => {
let allNotes: Note[] = [];
for (const account of accounts) {
const notes = await fetchNotifications(account);
allNotes = [...allNotes, ...notes];
}
return allNotes;
};
}

return fetcher;
Expand Down

0 comments on commit b4c0d0f

Please sign in to comment.