Skip to content

Commit

Permalink
Added button to check for all mod updates at once
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbl committed Jul 2, 2024
1 parent 44fe267 commit 650783b
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/renderer/react/context/UpdatesContext.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import React, { useCallback, useContext, useMemo, useState } from 'react';
import type { Mod } from 'bridge/BridgeAPI';
import type { IModUpdaterAPI, ModUpdaterDownload } from 'bridge/ModUpdaterAPI';
import type {
IModUpdaterAPI,
ModUpdaterDownload,
ModUpdaterNexusDownload,
} from 'bridge/ModUpdaterAPI';
import { consumeAPI } from 'renderer/IPC';
import { compareVersions } from 'renderer/utils/version';
import { useMods } from './ModsContext';
import { INexusAuthState } from './NexusModsContext';
import getNexusModID from './utils/getNexusModID';

Expand Down Expand Up @@ -170,3 +175,57 @@ export function useCheckModForUpdates(
[modOuter, nexusAuthState.apiKey, setUpdates],
);
}

export function useCheckModsForUpdates(
nexusAuthState: INexusAuthState,
): () => Promise<void> {
const [mods] = useMods();
const [, setUpdates] = useModUpdates();

return useCallback(async (): Promise<void> => {
const modsToCheck = mods.filter((mod) => getNexusModID(mod) != null);
if (nexusAuthState.apiKey == null || modsToCheck.length === 0) {
return;
}

// TODO: handle errors in a better way
const results = await Promise.all(
modsToCheck.map(
async (
mod,
): Promise<
[Mod, ModUpdaterNexusDownload[], ModUpdaterNexusDownload[]]
> => {
const currentVersion = mod.info.version ?? '0';

const nexusDownloads = (
await ModUpdaterAPI.getDownloadsViaNexus(
nexusAuthState.apiKey as string,
getNexusModID(mod) as string,
)
).sort((a, b) => compareVersions(a.version, b.version));

const nexusUpdates = getUpdatesFromDownloads(
currentVersion,
nexusDownloads,
);

return [mod, nexusDownloads, nexusUpdates];
},
),
);

setUpdates((oldUpdates) => {
const newUpdates = new Map(oldUpdates);
results.forEach(([mod, nexusDownloads, nexusUpdates]) =>
newUpdates.set(mod.id, {
isUpdateChecked: true,
isUpdateAvailable: nexusUpdates.length > 0,
nexusUpdates,
nexusDownloads,
}),
);
return newUpdates;
});
}, [mods, nexusAuthState.apiKey, setUpdates]);
}
26 changes: 26 additions & 0 deletions src/renderer/react/modlist/CheckForAllModUpdatesMenuItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useCallback } from 'react';
import { Update } from '@mui/icons-material';
import { MenuItem } from '@mui/material';
import { useNexusAuthState } from '../context/NexusModsContext';
import { useCheckModsForUpdates } from '../context/UpdatesContext';

export default function CheckForAllModUpdatesMenuItem({
onHideMenu,
}: {
onHideMenu: () => void;
}): JSX.Element {
const { nexusAuthState } = useNexusAuthState();
const checkModsForUpdates = useCheckModsForUpdates(nexusAuthState);

const onCheckForUpdates = useCallback(async () => {
onHideMenu();
await checkModsForUpdates();
}, [checkModsForUpdates, onHideMenu]);

return (
<MenuItem disableRipple={true} onClick={onCheckForUpdates}>
<Update sx={{ marginRight: 1 }} />
Check for Mod Updates
</MenuItem>
);
}
2 changes: 2 additions & 0 deletions src/renderer/react/modlist/OverflowActionsButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useCallback, useState } from 'react';
import { MoreVert } from '@mui/icons-material';
import { Button, Menu } from '@mui/material';
import AddSectionHeaderMenuItem from './AddSectionHeaderMenuItem';
import CheckForAllModUpdatesMenuItem from './CheckForAllModUpdatesMenuItem';
import RefreshModListMenuItem from './RefreshModListMenuItem';

type Props = Record<string, never>;
Expand Down Expand Up @@ -38,6 +39,7 @@ export default function OverflowActionsButton(_props: Props): JSX.Element {
open={isMenuShown}
>
<RefreshModListMenuItem onHideMenu={onHideMenu} />
<CheckForAllModUpdatesMenuItem onHideMenu={onHideMenu} />
<AddSectionHeaderMenuItem onHideMenu={onHideMenu} />
</Menu>
</>
Expand Down

0 comments on commit 650783b

Please sign in to comment.