From 6a429f9f39b7d196ebc16fad32a0c7f8e102ae4e Mon Sep 17 00:00:00 2001 From: Kelvin Date: Mon, 13 Jan 2025 01:38:29 -0300 Subject: [PATCH] added logic and some translate --- src/locales/en/translation.json | 1 + src/locales/pt-BR/translation.json | 1 + .../confirmation-modal/confirmation-modal.tsx | 7 ++- .../settings/settings-download-sources.css.ts | 5 ++ .../settings/settings-download-sources.tsx | 57 ++++++++++++++++++- .../src/workers/download-sources.worker.ts | 20 ++++++- 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/locales/en/translation.json b/src/locales/en/translation.json index 4e3dcb37..9fcd11de 100644 --- a/src/locales/en/translation.json +++ b/src/locales/en/translation.json @@ -258,6 +258,7 @@ "download_source_errored": "Errored", "sync_download_sources": "Sync sources", "removed_download_source": "Download source removed", + "removed_download_sources": "Download sources removed", "added_download_source": "Added download source", "download_sources_synced": "All download sources are synced", "insert_valid_json_url": "Insert a valid JSON url", diff --git a/src/locales/pt-BR/translation.json b/src/locales/pt-BR/translation.json index 2a80084f..07dc0571 100644 --- a/src/locales/pt-BR/translation.json +++ b/src/locales/pt-BR/translation.json @@ -246,6 +246,7 @@ "download_source_errored": "Falhou", "sync_download_sources": "Sincronizar", "removed_download_source": "Fonte removida", + "removed_download_sources": "Fontes removidas", "added_download_source": "Fonte adicionada", "download_sources_synced": "As fontes foram sincronizadas", "insert_valid_json_url": "Insira a url de um JSON válido", diff --git a/src/renderer/src/components/confirmation-modal/confirmation-modal.tsx b/src/renderer/src/components/confirmation-modal/confirmation-modal.tsx index 31929c60..f36474f8 100644 --- a/src/renderer/src/components/confirmation-modal/confirmation-modal.tsx +++ b/src/renderer/src/components/confirmation-modal/confirmation-modal.tsx @@ -10,6 +10,8 @@ export interface ConfirmationModalProps extends Omit { onConfirm: () => void; onCancel?: () => void; + + buttonsIsDisabled?: boolean } export function ConfirmationModal({ @@ -18,6 +20,7 @@ export function ConfirmationModal({ descriptionText, onConfirm, onCancel, + buttonsIsDisabled = false, ...props }: ConfirmationModalProps) { const handleCancelClick = () => { @@ -35,10 +38,10 @@ export function ConfirmationModal({

{descriptionText}

- -
diff --git a/src/renderer/src/pages/settings/settings-download-sources.css.ts b/src/renderer/src/pages/settings/settings-download-sources.css.ts index caa93ce8..5b383587 100644 --- a/src/renderer/src/pages/settings/settings-download-sources.css.ts +++ b/src/renderer/src/pages/settings/settings-download-sources.css.ts @@ -56,3 +56,8 @@ export const navigateToCatalogueButton = style({ textDecoration: "none", }, }); + +export const removeAllSourcesButton = style({ + display: 'flex', + justifyContent: 'flex-end' +}) diff --git a/src/renderer/src/pages/settings/settings-download-sources.tsx b/src/renderer/src/pages/settings/settings-download-sources.tsx index cacd1910..859d0e16 100644 --- a/src/renderer/src/pages/settings/settings-download-sources.tsx +++ b/src/renderer/src/pages/settings/settings-download-sources.tsx @@ -1,11 +1,11 @@ import { useContext, useEffect, useState } from "react"; -import { TextField, Button, Badge } from "@renderer/components"; +import { TextField, Button, Badge, ConfirmationModal } from "@renderer/components"; import { useTranslation } from "react-i18next"; import * as styles from "./settings-download-sources.css"; import type { DownloadSource } from "@types"; -import { NoEntryIcon, PlusCircleIcon, SyncIcon } from "@primer/octicons-react"; +import { NoEntryIcon, PlusCircleIcon, SyncIcon, XIcon } from "@primer/octicons-react"; import { AddDownloadSourceModal } from "./add-download-source-modal"; import { useAppDispatch, useRepacks, useToast } from "@renderer/hooks"; import { DownloadSourceStatus } from "@shared"; @@ -16,6 +16,8 @@ import { useNavigate } from "react-router-dom"; import { setFilters, clearFilters } from "@renderer/features"; export function SettingsDownloadSources() { + const [showConfirmationDeleteAllSourcesModal, setShowConfirmationDeleteAllSourcesModal] = + useState(false); const [showAddDownloadSourceModal, setShowAddDownloadSourceModal] = useState(false); const [downloadSources, setDownloadSources] = useState([]); @@ -23,6 +25,8 @@ export function SettingsDownloadSources() { useState(false); const [isRemovingDownloadSource, setIsRemovingDownloadSource] = useState(false); + const [isFetchingSources, setIsFetchingSources] = + useState(true); const { sourceUrl, clearSourceUrl } = useContext(settingsContext); @@ -41,7 +45,9 @@ export function SettingsDownloadSources() { .sortBy("createdAt") .then((sources) => { setDownloadSources(sources.reverse()); - }); + }).finally(() => { + setIsFetchingSources(false) + }) }; useEffect(() => { @@ -68,6 +74,24 @@ export function SettingsDownloadSources() { }; }; + const handleRemoveAllDowloadSources = () => { + setIsRemovingDownloadSource(true); + + const id = crypto.randomUUID(); + const channel = new BroadcastChannel(`download_sources:delete_all:${id}`); + + downloadSourcesWorker.postMessage(["DELETE_ALL_DOWNLOAD_SOURCES", id]); + + channel.onmessage = () => { + showSuccessToast(t("removed_download_sources")); + + getDownloadSources(); + setShowConfirmationDeleteAllSourcesModal(false) + channel.close(); + updateRepacks(); + } + } + const handleAddDownloadSource = async () => { await getDownloadSources(); showSuccessToast(t("added_download_source")); @@ -116,6 +140,18 @@ export function SettingsDownloadSources() { onAddDownloadSource={handleAddDownloadSource} /> + setShowConfirmationDeleteAllSourcesModal(false)} + buttonsIsDisabled={isRemovingDownloadSource} + /> +

{t("download_sources_description")}

@@ -196,6 +232,21 @@ export function SettingsDownloadSources() { ))} + + + {!isFetchingSources && downloadSources.length >= 2 && ( +
+ +
+ )} ); } diff --git a/src/renderer/src/workers/download-sources.worker.ts b/src/renderer/src/workers/download-sources.worker.ts index 893572e1..9ff863a2 100644 --- a/src/renderer/src/workers/download-sources.worker.ts +++ b/src/renderer/src/workers/download-sources.worker.ts @@ -21,9 +21,10 @@ export const downloadSourceSchema = z.object({ type Payload = | ["IMPORT_DOWNLOAD_SOURCE", string] - | ["DELETE_DOWNLOAD_SOURCE", number] + | ["DELETE_DOWNLOAD_SOURCE", number] | ["VALIDATE_DOWNLOAD_SOURCE", string] - | ["SYNC_DOWNLOAD_SOURCES", string]; + | ["SYNC_DOWNLOAD_SOURCES", string] + | ["DELETE_ALL_DOWNLOAD_SOURCES", string]; export type SteamGamesByLetter = Record; @@ -114,6 +115,13 @@ const deleteDownloadSource = async (id: number) => { }); }; +const deleteAllDowloadsSources = async () => { + await db.transaction("rw", repacksTable, downloadSourcesTable, async () => { + await repacksTable.clear() + await downloadSourcesTable.clear(); + }); +}; + self.onmessage = async (event: MessageEvent) => { const [type, data] = event.data; @@ -132,6 +140,14 @@ self.onmessage = async (event: MessageEvent) => { }); } + if (type === 'DELETE_ALL_DOWNLOAD_SOURCES') { + await deleteAllDowloadsSources() + + const channel = new BroadcastChannel(`download_sources:delete_all:${data}`); + + channel.postMessage(true) + } + if (type === "DELETE_DOWNLOAD_SOURCE") { await deleteDownloadSource(data);