diff --git a/src/hooks/app/useSystemTray.ts b/src/hooks/app/useSystemTray.ts index 038ae8b35..867cc903e 100644 --- a/src/hooks/app/useSystemTray.ts +++ b/src/hooks/app/useSystemTray.ts @@ -9,22 +9,22 @@ import { } from '@app/utils'; import { listen } from '@tauri-apps/api/event'; import { getCurrentWindow } from '@tauri-apps/api/window'; -import { useCallback, useEffect, useMemo, useState } from 'react'; +import { useCallback, useEffect, useRef } from 'react'; import { formatHashrate, formatNumber, FormatPreset } from '@app/utils'; import { MenuItem } from '@tauri-apps/api/menu/menuItem'; const currentWindow = getCurrentWindow(); -export function useUpdateSystemTray() { - const [metrics, setMetrics] = useState(); +const SysTrayCopy = { + [CPU_HASH_ITEM_ID]: (cpu: string) => `CPU Hashrate: ${cpu}`, + [GPU_HASH_ITEM_ID]: (gpu: string) => `GPU Hashrate: ${gpu}`, + [EARNINGS_ITEM_ID]: (earnings: string) => `Est earning: ${earnings} tXTM/day`, +}; - const totalEarningsFormatted = useMemo(() => { - const cpu_est = metrics?.cpu?.mining?.estimated_earnings || 0; - const gpu_est = metrics?.gpu?.mining?.estimated_earnings || 0; - const total = cpu_est + gpu_est; - return total > 0 ? formatNumber(total, FormatPreset.TXTM_COMPACT) : '0'; - }, [metrics]); +export function useUpdateSystemTray() { + const cachedMetrics = useRef(); + const minimizedRef = useRef(); const updateMenuItemEnabled = useCallback(async (itemId: string, enabled: boolean) => { const item = await menu.get(itemId); @@ -37,45 +37,49 @@ export function useUpdateSystemTray() { } } }, []); - const updateMenuItem = useCallback(async ({ itemId, itemText }: { itemId: string; itemText?: string }) => { + + const updateMenuItem = useCallback(async (itemId: string, itemText: string) => { const item = await menu.get(itemId); if (item && itemText) { await item.setText(itemText); } }, []); - const items = useMemo(() => { - const { cpu, gpu } = metrics || {}; - const cpu_h = cpu?.mining?.hash_rate || 0; - const gpu_h = gpu?.mining?.hash_rate || 0; - - const cpuHashItemText = `CPU Hashrate: ${cpu_h ? `${formatHashrate(cpu_h)}` : '-'}`; - const gpuHashItemText = `GPU Hashrate: ${gpu_h ? `${formatHashrate(gpu_h)}` : '-'}`; - const estEarningsItemText = `Est earning: ${totalEarningsFormatted !== '0' ? totalEarningsFormatted : '-'} tXTM/day`; + useEffect(() => { + const handleUpdateMenu = () => { + const { cpu, gpu } = cachedMetrics.current || {}; - return [ - { itemId: CPU_HASH_ITEM_ID, itemText: cpuHashItemText }, - { itemId: GPU_HASH_ITEM_ID, itemText: gpuHashItemText }, - { itemId: EARNINGS_ITEM_ID, itemText: estEarningsItemText }, - ]; - }, [metrics, totalEarningsFormatted]); + // --- Update CPU + const cpuHashItemText = cpu?.mining?.hash_rate ? `${formatHashrate(cpu?.mining?.hash_rate)}` : '-'; + updateMenuItem(CPU_HASH_ITEM_ID, SysTrayCopy[CPU_HASH_ITEM_ID](cpuHashItemText)); + // --- Update GPU + const gpuHashItemText = gpu?.mining?.hash_rate ? `${formatHashrate(gpu?.mining?.hash_rate)}` : '-'; + updateMenuItem(GPU_HASH_ITEM_ID, SysTrayCopy[GPU_HASH_ITEM_ID](gpuHashItemText)); + // --- Update Total + const cpu_est = cpu?.mining?.estimated_earnings || 0; + const gpu_est = gpu?.mining?.estimated_earnings || 0; + const total = cpu_est + gpu_est; + const totalFormatted = total > 0 ? formatNumber(total, FormatPreset.TXTM_COMPACT) : '-'; + updateMenuItem(EARNINGS_ITEM_ID, SysTrayCopy[EARNINGS_ITEM_ID](totalFormatted)); + }; + handleUpdateMenu(); + const interval = setInterval(handleUpdateMenu, 1000 * 10); // 10s - useEffect(() => { - items.forEach(async (item) => { - await updateMenuItem({ ...item }); - }); - }, [items, updateMenuItem]); + return () => clearInterval(interval); + }, [updateMenuItem]); useEffect(() => { const ul = listen('miner_metrics', async ({ payload }) => { - const minimized = await currentWindow.isMinimized(); - if (payload) { - setMetrics(payload as MinerMetrics); + cachedMetrics.current = { ...payload } as MinerMetrics; } - await updateMenuItemEnabled(UNMINIMIZE_ITEM_ID, minimized); - await updateMenuItemEnabled(MINIMIZE_ITEM_ID, !minimized); + const minimized = await currentWindow.isMinimized(); + if (minimizedRef.current !== minimized) { + minimizedRef.current = minimized; + await updateMenuItemEnabled(UNMINIMIZE_ITEM_ID, minimized); + await updateMenuItemEnabled(MINIMIZE_ITEM_ID, !minimized); + } }); return () => { ul.then((unlisten) => unlisten());