Merge branch 'main' into hyd-229-improve-visibility-of-update-message

This commit is contained in:
Zamitto 2024-07-05 12:10:30 -03:00
commit dde40f39e9
5 changed files with 45 additions and 21 deletions

View File

@ -1,17 +1,12 @@
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import axios from "axios";
import { downloadSourceRepository } from "@main/repository"; import { downloadSourceRepository } from "@main/repository";
import { downloadSourceSchema } from "../helpers/validators";
import { RepacksManager } from "@main/services"; import { RepacksManager } from "@main/services";
import { downloadSourceWorker } from "@main/workers";
const validateDownloadSource = async ( const validateDownloadSource = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
url: string url: string
) => { ) => {
const response = await axios.get(url);
const source = downloadSourceSchema.parse(response.data);
const existingSource = await downloadSourceRepository.findOne({ const existingSource = await downloadSourceRepository.findOne({
where: { url }, where: { url },
}); });
@ -21,14 +16,12 @@ const validateDownloadSource = async (
const repacks = RepacksManager.repacks; const repacks = RepacksManager.repacks;
const existingUris = source.downloads return downloadSourceWorker.run(
.flatMap((download) => download.uris) { url, repacks },
.filter((uri) => repacks.some((repack) => repack.magnet === uri)); {
name: "validateDownloadSource",
return { }
name: source.name, );
downloadCount: source.downloads.length - existingUris.length,
};
}; };
registerEvent("validateDownloadSource", validateDownloadSource); registerEvent("validateDownloadSource", validateDownloadSource);

View File

@ -18,7 +18,7 @@ import { HydraApi } from "./services/hydra-api";
import { uploadGamesBatch } from "./services/library-sync"; import { uploadGamesBatch } from "./services/library-sync";
const loadState = async (userPreferences: UserPreferences | null) => { const loadState = async (userPreferences: UserPreferences | null) => {
await RepacksManager.updateRepacks(); RepacksManager.updateRepacks();
import("./events"); import("./events");

View File

@ -1,6 +1,6 @@
import { downloadSourceSchema } from "@main/events/helpers/validators"; import { downloadSourceSchema } from "@main/events/helpers/validators";
import { DownloadSourceStatus } from "@shared"; import { DownloadSourceStatus } from "@shared";
import type { DownloadSource } from "@types"; import type { DownloadSource, GameRepack } from "@types";
import axios, { AxiosError, AxiosHeaders } from "axios"; import axios, { AxiosError, AxiosHeaders } from "axios";
import { z } from "zod"; import { z } from "zod";
@ -48,3 +48,24 @@ export const getUpdatedRepacks = async (downloadSources: DownloadSource[]) => {
return results; return results;
}; };
export const validateDownloadSource = async ({
url,
repacks,
}: {
url: string;
repacks: GameRepack[];
}) => {
const response = await axios.get(url);
const source = downloadSourceSchema.parse(response.data);
const existingUris = source.downloads
.flatMap((download) => download.uris)
.filter((uri) => repacks.some((repack) => repack.magnet === uri));
return {
name: source.name,
downloadCount: source.downloads.length - existingUris.length,
};
};

View File

@ -1,4 +1,4 @@
import { useCallback, useContext, useEffect, useState } from "react"; import { useCallback, useContext, useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import parseTorrent from "parse-torrent"; import parseTorrent from "parse-torrent";
@ -12,6 +12,7 @@ import { format } from "date-fns";
import { DownloadSettingsModal } from "./download-settings-modal"; import { DownloadSettingsModal } from "./download-settings-modal";
import { gameDetailsContext } from "@renderer/context"; import { gameDetailsContext } from "@renderer/context";
import { Downloader } from "@shared"; import { Downloader } from "@shared";
import { orderBy } from "lodash-es";
export interface RepacksModalProps { export interface RepacksModalProps {
visible: boolean; visible: boolean;
@ -38,6 +39,10 @@ export function RepacksModal({
const { t } = useTranslation("game_details"); const { t } = useTranslation("game_details");
const sortedRepacks = useMemo(() => {
return orderBy(repacks, (repack) => repack.uploadDate, "desc");
}, [repacks]);
const getInfoHash = useCallback(async () => { const getInfoHash = useCallback(async () => {
const torrent = await parseTorrent(game?.uri ?? ""); const torrent = await parseTorrent(game?.uri ?? "");
@ -47,10 +52,10 @@ export function RepacksModal({
}, [game]); }, [game]);
useEffect(() => { useEffect(() => {
setFilteredRepacks(repacks); setFilteredRepacks(sortedRepacks);
if (game?.uri) getInfoHash(); if (game?.uri) getInfoHash();
}, [repacks, visible, game, getInfoHash]); }, [sortedRepacks, visible, game, getInfoHash]);
const handleRepackClick = (repack: GameRepack) => { const handleRepackClick = (repack: GameRepack) => {
setRepack(repack); setRepack(repack);
@ -61,7 +66,7 @@ export function RepacksModal({
const term = event.target.value.toLocaleLowerCase(); const term = event.target.value.toLocaleLowerCase();
setFilteredRepacks( setFilteredRepacks(
repacks.filter((repack) => { sortedRepacks.filter((repack) => {
const lowerCaseTitle = repack.title.toLowerCase(); const lowerCaseTitle = repack.title.toLowerCase();
const lowerCaseRepacker = repack.repacker.toLowerCase(); const lowerCaseRepacker = repack.repacker.toLowerCase();

View File

@ -51,10 +51,15 @@ export const removeSpecialEditionFromName = (name: string) =>
export const removeDuplicateSpaces = (name: string) => export const removeDuplicateSpaces = (name: string) =>
name.replace(/\s{2,}/g, " "); name.replace(/\s{2,}/g, " ");
export const replaceUnderscoreWithSpace = (name: string) =>
name.replace(/_/g, " ");
export const formatName = pipe<string>( export const formatName = pipe<string>(
removeReleaseYearFromName, removeReleaseYearFromName,
removeSymbolsFromName,
removeSpecialEditionFromName, removeSpecialEditionFromName,
replaceUnderscoreWithSpace,
(str) => str.replace(/DIRECTOR'S CUT/g, ""),
removeSymbolsFromName,
removeDuplicateSpaces, removeDuplicateSpaces,
(str) => str.trim() (str) => str.trim()
); );