Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sys tray reduced updates #1302

Merged
merged 4 commits into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading