Skip to content

Commit

Permalink
feat: sys tray reduced updates (#1302)
Browse files Browse the repository at this point in the history
  • Loading branch information
PanchoBubble authored Dec 20, 2024
1 parent b302808 commit d6ad36a
Showing 1 changed file with 38 additions and 34 deletions.
72 changes: 38 additions & 34 deletions src/hooks/app/useSystemTray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MinerMetrics>();
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<MinerMetrics>();
const minimizedRef = useRef<boolean>();

const updateMenuItemEnabled = useCallback(async (itemId: string, enabled: boolean) => {
const item = await menu.get(itemId);
Expand All @@ -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());
Expand Down

0 comments on commit d6ad36a

Please sign in to comment.