Skip to content

Commit 91a5330

Browse files
committed
Refactored PoAInstaller class to handle different platforms and added new properties and methods to PoAViewContent component.
1 parent 5fd976a commit 91a5330

File tree

6 files changed

+86
-24
lines changed

6 files changed

+86
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ build/
1717

1818
.env
1919

20-
temp
20+
temp
21+
.aider*

src/main/AutoUpdaterPoA.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PoAInstaller extends EventEmitter {
5252
return null;
5353
}
5454
}
55-
async install() {
55+
async install() {
5656
const installDir = Path.join(os.homedir(), (await this.getDefaultPath()) || '');
5757
console.log(`Installing PoA to ${installDir}`);
5858
if (!fs.existsSync(installDir)) {
@@ -69,7 +69,14 @@ class PoAInstaller extends EventEmitter {
6969
if (compareVersions.compare(tag_name, currentVersion, '>')) {
7070
console.log('Update available');
7171
this.emit('update-available', tag_name);
72-
const asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe') && isWin);
72+
73+
let asset;
74+
75+
if (isWin) {
76+
asset = assets.find((a) => a.name.includes('win-main') && a.name.includes('exe'));
77+
} else if (process.platform === 'linux') {
78+
asset = assets.find((a) => a.name.includes(`linux-main-${tag_name}`)); // Modified this line
79+
}
7380

7481
if (!asset) {
7582
console.error('Could not find PoA asset for this platform');
@@ -84,7 +91,7 @@ class PoAInstaller extends EventEmitter {
8491
responseType: 'arraybuffer',
8592
});
8693

87-
const installPath = Path.join(installDir, 'PoA.exe');
94+
const installPath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA');
8895

8996
fs.writeFileSync(installPath, Buffer.from(response.data));
9097

src/main/core/components/ProgramRunner.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ProgramRunner.ts
2-
import { exec, ChildProcess } from 'child_process';
2+
import { spawn, SpawnOptions, ChildProcess } from 'child_process';
33
import treeKill from 'tree-kill';
44

55
class ProgramRunner {
@@ -15,23 +15,24 @@ class ProgramRunner {
1515
public setupProgram(onExit: () => void): void {
1616
console.log(`Setting up command: ${this.command}`);
1717

18-
this.process = exec(this.command, (error, stdout, stderr) => {
19-
if (error && this.process && !this.process.killed) {
20-
console.error(`Error running program: ${error.message}`);
21-
console.error('Error details:', error);
22-
return;
23-
}
18+
const commandParts = this.command.split(' ');
19+
const cmd = commandParts[0];
20+
const args = commandParts.slice(1);
2421

25-
console.log(`stdout: ${stdout}`);
26-
console.error(`stderr: ${stderr}`);
27-
});
22+
const options: SpawnOptions = {
23+
stdio: 'pipe',
24+
detached: true, // This might help in some scenarios to open the program in a new process group.
25+
shell: true // Running in a shell can sometimes help with GUI apps.
26+
};
27+
28+
this.process = spawn(cmd, args, options);
2829

2930
this.process.stdout.on('data', (data) => {
30-
this.outputHandler(data);
31+
this.outputHandler(data.toString());
3132
});
3233

3334
this.process.stderr.on('data', (data) => {
34-
this.outputHandler(data);
35+
this.outputHandler(data.toString());
3536
});
3637

3738
this.process.on('exit', () => {

src/renderer/views/PoAView.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export function PoAView() {
1818
const { programRunner, setProgramRunner, terminalRef } = usePoAProgramRunnerContext();
1919

2020
const updater = usePoAInstaller();
21-
const { terminal, setTerminal, isPoARunning, runPoA, contextValue } = usePoAProgramRunner();
21+
const { terminal, setTerminal, isPoARunning, runPoA, contextValue, storageSize, autoPin, setAutoPin, setStorageSize } = usePoAProgramRunner();
2222
const { stopPoA } = contextValue;
2323
useEffect(() => {
2424
if (terminalRef.current && !terminal) {
@@ -40,8 +40,12 @@ export function PoAView() {
4040
isPoARunning={isPoARunning}
4141
updatePoA={updater.updatePoA}
4242
runPoA={runPoA}
43-
stopPoA={stopPoA} // Pass the stopPoA function as a prop
43+
stopPoA={stopPoA}
4444
terminalRef={terminalRef}
45+
storageSize={storageSize}
46+
autoPin={autoPin}
47+
setStorageSize={setStorageSize}
48+
setAutoPin={setAutoPin}
4549
/>
4650
);
4751
}

src/renderer/views/PoAView/PoAViewContent.tsx

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,33 @@
1+
//Path: src\renderer\views\PoAView\PoAViewContent.tsx
12
import React, { useEffect, useRef, useState, RefObject } from 'react';
2-
import { Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap';
3+
import { Form, Button, OverlayTrigger, Tooltip, Card } from 'react-bootstrap';
34
import { IoIosRadioButtonOn } from 'react-icons/io';
45
import { usePoAState } from './PoAStateContext';
56
import { usePoAProgramRunner } from './usePoAProgramRunner';
67

78
interface PoAViewContentProps {
89
isPoARunning: boolean;
910
updatePoA: () => Promise<void>;
10-
runPoA: () => void;
11+
runPoA: () => Promise<void>;
1112
stopPoA: () => void;
1213
terminalRef: RefObject<HTMLDivElement>;
14+
15+
// Add the new properties
16+
storageSize: number;
17+
autoPin: boolean;
18+
setStorageSize: React.Dispatch<React.SetStateAction<number>>;
19+
setAutoPin: React.Dispatch<React.SetStateAction<boolean>>;
1320
}
1421

1522
export const PoAViewContent: React.FC<PoAViewContentProps> = ({
1623
isPoARunning,
1724
updatePoA,
1825
runPoA,
1926
terminalRef,
27+
autoPin,
28+
storageSize,
29+
setAutoPin,
30+
setStorageSize,
2031
}) => {
2132
const { logs, validatorOnline, setValidatorOnline } = usePoAState();
2233
const [isDataReceived, setIsDataReceived] = useState(false);
@@ -97,6 +108,14 @@ export const PoAViewContent: React.FC<PoAViewContentProps> = ({
97108
}
98109
}, [logs, terminalRef]);
99110

111+
112+
const handleAutoPinChange = (e: React.ChangeEvent<HTMLInputElement>) => {
113+
setAutoPin(e.target.checked);
114+
};
115+
116+
const handleStorageSizeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
117+
setStorageSize(Number(e.target.value));
118+
};
100119
const startPoA = async () => {
101120
if (!isPoARunning && !isDataReceived && !isUpdating) {
102121
console.log('starting PoA');
@@ -149,6 +168,23 @@ export const PoAViewContent: React.FC<PoAViewContentProps> = ({
149168
</Button>
150169
</OverlayTrigger>
151170
<IoIosRadioButtonOn style={{ color: validatorOnline ? 'green' : 'red' }} />
171+
<Form.Check
172+
type="checkbox"
173+
label="Auto Pin"
174+
checked={autoPin}
175+
onChange={handleAutoPinChange}
176+
disabled={isPoARunning}
177+
/>
178+
<p>Enter storage size in GB to auto pin</p>
179+
<Form.Control
180+
type="number"
181+
title="Enter storage size in GB to auto pin"
182+
value={storageSize}
183+
onChange={handleStorageSizeChange}
184+
min={0}
185+
placeholder="Enter storage size in GB"
186+
disabled={isPoARunning}
187+
/>
152188
</div>
153189
</p>
154190
<h1>SPK Network Stats</h1>

src/renderer/views/PoAView/usePoAProgramRunner.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import os from 'os';
99
import ArraySearch from 'arraysearch';
1010
import PromiseIPC from 'electron-promise-ipc';
1111

12+
const isWin = process.platform === 'win32';
1213
export const usePoAProgramRunner = () => {
1314
const [terminal, setTerminal] = useState<Terminal | null>(null);
1415
const [isPoARunning, setIsPoARunning] = useState(false);
@@ -17,6 +18,8 @@ export const usePoAProgramRunner = () => {
1718
const Finder = ArraySearch.Finder;
1819
const { logs, setLogs } = usePoAState();
1920
const isMountedRef = useRef(true);
21+
const [autoPin, setAutoPin] = useState(false);
22+
const [storageSize, setStorageSize] = useState(0);
2023

2124
const runPoA = useCallback(async () => {
2225
let command = ''; // Define command here
@@ -25,8 +28,18 @@ export const usePoAProgramRunner = () => {
2528
const getAccount = (await PromiseIPC.send('accounts.get', profileID as any)) as any;
2629
const hiveInfo = Finder.one.in(getAccount.keyring).with({ type: 'hive' });
2730
const installDir = Path.join(os.homedir(), (await poaInstaller.current.getDefaultPath()) || '');
28-
const executablePath = Path.join(installDir, 'PoA.exe');
29-
command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`; // Assign command here
31+
const executablePath = isWin ? Path.join(installDir, 'PoA.exe') : Path.join(installDir, 'PoA');
32+
command = `"${executablePath}" -node=2 -username=${hiveInfo.username} -useWS=true -IPFS_PORT=5004`;
33+
console.log(command);
34+
console.log(autoPin);
35+
console.log(storageSize);
36+
if (autoPin) {
37+
command += " -getVids=true -pinVideos=true";
38+
if (storageSize > 0) {
39+
command += ` -storageLimit=${storageSize}`;
40+
}
41+
}
42+
console.log(command);
3043
if (!runner.current) {
3144
runner.current = new ProgramRunner(command, (data: string) => {
3245
if (!isMountedRef.current) return;
@@ -45,7 +58,7 @@ export const usePoAProgramRunner = () => {
4558
runner.current.stopProgram();
4659
setIsPoARunning(false);
4760
}
48-
}, [terminal, isPoARunning]);
61+
}, [terminal, isPoARunning, autoPin, storageSize]);
4962

5063
const contextValue = {
5164
isPoARunning,
@@ -54,5 +67,5 @@ export const usePoAProgramRunner = () => {
5467
stopPoA: () => runner.current?.stopProgram(),
5568
};
5669

57-
return { terminal, setTerminal, isPoARunning, runPoA, contextValue };
70+
return { terminal, setTerminal, isPoARunning, runPoA, contextValue, setAutoPin, setStorageSize, storageSize, autoPin };
5871
}

0 commit comments

Comments
 (0)