diff --git a/src/renderer/components/Console/ConsoleManager.ts b/src/renderer/components/Console/ConsoleManager.ts new file mode 100644 index 000000000..901488b45 --- /dev/null +++ b/src/renderer/components/Console/ConsoleManager.ts @@ -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; \ No newline at end of file diff --git a/src/renderer/components/MainWindow/MainWindow.tsx b/src/renderer/components/MainWindow/MainWindow.tsx new file mode 100644 index 000000000..0701117f5 --- /dev/null +++ b/src/renderer/components/MainWindow/MainWindow.tsx @@ -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 = (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; \ No newline at end of file diff --git a/src/renderer/services/DownloadService.ts b/src/renderer/services/DownloadService.ts new file mode 100644 index 000000000..a69efc3df --- /dev/null +++ b/src/renderer/services/DownloadService.ts @@ -0,0 +1,36 @@ +import { ipcRenderer } from 'electron'; +import { EventEmitter } from 'events'; + +class DownloadService extends EventEmitter { + async downloadFile(url: string, destination: string): Promise { + 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 { + 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(); \ No newline at end of file