refactor: fetch release information the same way for manual and automatic checks

This commit is contained in:
zhom
2025-06-17 06:17:57 +04:00
parent 251016609f
commit 2d92cbb0e5
7 changed files with 109 additions and 232 deletions
+10 -27
View File
@@ -1,7 +1,7 @@
import { getBrowserDisplayName } from "@/lib/browser-utils";
import { dismissToast, showToast } from "@/lib/toast-utils";
import { invoke } from "@tauri-apps/api/core";
import { useCallback, useEffect, useRef, useState } from "react";
import { useCallback, useRef, useState } from "react";
interface UpdateNotification {
id: string;
@@ -25,6 +25,11 @@ export function useUpdateNotifications(
Set<string>
>(new Set());
const isUpdating = useCallback(
(browser: string) => updatingBrowsers.has(browser),
[updatingBrowsers],
);
// Add refs to track ongoing operations to prevent duplicates
const isCheckingForUpdates = useRef(false);
const activeDownloads = useRef<Set<string>>(new Set()); // Track "browser-version" keys
@@ -85,10 +90,9 @@ export function useUpdateNotifications(
// Mark download as active and disable browser
activeDownloads.current.add(downloadKey);
setUpdatingBrowsers((prev) => new Set(prev).add(browser));
try {
// Set browser as updating FIRST before any async operations
setUpdatingBrowsers((prev) => new Set(prev).add(browser));
const browserDisplayName = getBrowserDisplayName(browser);
// Dismiss the notification in the backend
@@ -136,7 +140,6 @@ export function useUpdateNotifications(
});
// Download the browser - this will trigger download progress events automatically
// The use-browser-download hook will handle showing the download progress toasts
await invoke("download_browser", {
browserStr: browser,
version: newVersion,
@@ -193,21 +196,11 @@ export function useUpdateNotifications(
});
throw downloadError;
}
// Don't call checkForUpdates() again here as it can cause recursion and duplicates
// The periodic checks will handle finding any remaining updates
} catch (error) {
console.error("Failed to start auto-update:", error);
const browserDisplayName = getBrowserDisplayName(browser);
showToast({
id: `auto-update-error-${browser}-${newVersion}`,
type: "error",
title: `Failed to update ${browserDisplayName}`,
description: String(error),
duration: 8000,
});
throw error;
} finally {
// Remove from active downloads and updating browsers
// Clean up
activeDownloads.current.delete(downloadKey);
setUpdatingBrowsers((prev) => {
const next = new Set(prev);
@@ -219,19 +212,9 @@ export function useUpdateNotifications(
[onProfilesUpdated],
);
// Clean up notifications when they're no longer needed
useEffect(() => {
// Remove notifications that have been processed
setNotifications((prev) =>
prev.filter(
(notification) => !processedNotifications.has(notification.id),
),
);
}, [processedNotifications]);
return {
notifications,
isUpdating,
checkForUpdates,
isUpdating: (browser: string) => updatingBrowsers.has(browser),
};
}
+23 -22
View File
@@ -1,9 +1,13 @@
import { getBrowserDisplayName } from "@/lib/browser-utils";
import { dismissToast, showUnifiedVersionUpdateToast } from "@/lib/toast-utils";
import {
dismissToast,
showErrorToast,
showSuccessToast,
showUnifiedVersionUpdateToast,
} from "@/lib/toast-utils";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import { useCallback, useEffect, useState } from "react";
import { toast } from "sonner";
interface VersionUpdateProgress {
current_browser: string;
@@ -80,12 +84,12 @@ export function useVersionUpdater() {
dismissToast("unified-version-update");
if (progress.new_versions_found > 0) {
toast.success("Browser versions updated successfully", {
showSuccessToast("Browser versions updated successfully", {
duration: 5000,
description: `Found ${progress.new_versions_found} new browser versions. Update notifications will appear shortly.`,
description: "Updates will start automatically.",
});
} else {
toast.success("No new browser versions found", {
showSuccessToast("No new browser versions found", {
duration: 3000,
description: "All browser versions are up to date",
});
@@ -98,7 +102,7 @@ export function useVersionUpdater() {
setUpdateProgress(null);
dismissToast("unified-version-update");
toast.error("Failed to update browser versions", {
showErrorToast("Failed to update browser versions", {
duration: 6000,
description: "Check your internet connection and try again",
});
@@ -146,17 +150,17 @@ export function useVersionUpdater() {
).length;
if (failedUpdates > 0) {
toast.warning("Update completed with some errors", {
showErrorToast("Update completed with some errors", {
description: `${totalNewVersions} new versions found, ${failedUpdates} browsers failed to update`,
duration: 5000,
});
} else if (totalNewVersions > 0) {
toast.success("Browser versions updated successfully", {
description: `Updated ${successfulUpdates} browsers successfully`,
showSuccessToast("Browser versions updated successfully", {
description: `Found ${totalNewVersions} new versions across ${successfulUpdates} browsers. Updates will start automatically.`,
duration: 4000,
});
} else {
toast.success("No new browser versions found", {
showSuccessToast("No new browser versions found", {
description: "All browser versions are up to date",
duration: 3000,
});
@@ -166,7 +170,7 @@ export function useVersionUpdater() {
return results;
} catch (error) {
console.error("Failed to trigger manual update:", error);
toast.error("Failed to update browser versions", {
showErrorToast("Failed to update browser versions", {
description:
error instanceof Error ? error.message : "Unknown error occurred",
duration: 4000,
@@ -188,7 +192,7 @@ export function useVersionUpdater() {
// Show notification about new versions if any were found
if (result.new_versions_count && result.new_versions_count > 0) {
const browserName = getBrowserDisplayName(browserStr);
toast.success(
showSuccessToast(
`Found ${result.new_versions_count} new ${browserName} versions!`,
{
duration: 3000,
@@ -207,18 +211,15 @@ export function useVersionUpdater() {
);
const formatTimeUntilUpdate = useCallback((seconds: number): string => {
if (seconds <= 0) return "Update overdue";
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (hours > 0) {
return `${hours}h ${minutes}m`;
if (seconds < 60) {
return `${seconds} seconds`;
}
if (minutes > 0) {
return `${minutes}m`;
const minutes = Math.floor(seconds / 60);
if (minutes < 60) {
return `${minutes} minute${minutes === 1 ? "" : "s"}`;
}
return "< 1m";
const hours = Math.floor(minutes / 60);
return `${hours} hour${hours === 1 ? "" : "s"}`;
}, []);
const formatLastUpdateTime = useCallback(