Skip to content
Open
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions src/renderer/components/Console/ConsoleManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { ipcRenderer } from 'electron';
import { EventEmitter } from 'events';

class ConsoleManager extends EventEmitter {
private isVisible: boolean = false;
private consoleElement: HTMLElement | null = null;

constructor() {
super();
this.setupDownloadListener();
}

private setupDownloadListener(): void {
ipcRenderer.on('download-started', () => {
this.showConsole();
});
}

public showConsole(): void {
this.isVisible = true;
if (this.consoleElement) {
this.consoleElement.style.display = 'block';
}
this.emit('console-shown');
}

public hideConsole(): void {
this.isVisible = false;
if (this.consoleElement) {
this.consoleElement.style.display = 'none';
}
this.emit('console-hidden');
}

public toggleConsole(): void {
if (this.isVisible) {
this.hideConsole();
} else {
this.showConsole();
}
}

public setConsoleElement(element: HTMLElement): void {
this.consoleElement = element;
}

public getIsVisible(): boolean {
return this.isVisible;
}
}

export default ConsoleManager;
40 changes: 40 additions & 0 deletions src/renderer/components/MainWindow/MainWindow.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { useState, useEffect } from 'react';
import { DownloadService } from '../../services/DownloadService';
import { ConsoleManager } from '../../managers/ConsoleManager';

interface MainWindowProps {
// existing props
}

const MainWindow: React.FC<MainWindowProps> = (props) => {
const [consoleVisible, setConsoleVisible] = useState(false);

useEffect(() => {
const downloadService = DownloadService.getInstance();
const consoleManager = ConsoleManager.getInstance();

const handleDownloadStart = () => {
consoleManager.setAutoShow(true);
};

const handleDownloadComplete = () => {
consoleManager.setAutoShow(false);
};

downloadService.on('download-start', handleDownloadStart);
downloadService.on('download-complete', handleDownloadComplete);

return () => {
downloadService.off('download-start', handleDownloadStart);
downloadService.off('download-complete', handleDownloadComplete);
};
}, []);

// existing component code

return (
// existing JSX
);
};

export default MainWindow;
36 changes: 36 additions & 0 deletions src/renderer/services/DownloadService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ipcRenderer } from 'electron';
import { EventEmitter } from 'events';

class DownloadService extends EventEmitter {
async downloadFile(url: string, destination: string): Promise<void> {
this.emit('download-start', { url, destination });

try {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const buffer = Buffer.from(arrayBuffer);

await ipcRenderer.invoke('write-file', destination, buffer);
this.emit('download-complete', { url, destination });
} catch (error) {
this.emit('download-error', { url, destination, error });
throw error;
}
}

async downloadMultiple(files: Array<{url: string, destination: string}>): Promise<void> {
this.emit('download-start', { files });

try {
for (const file of files) {
await this.downloadFile(file.url, file.destination);
}
this.emit('download-complete', { files });
} catch (error) {
this.emit('download-error', { files, error });
throw error;
}
}
}

export default new DownloadService();