Skip to content

Commit

Permalink
✨ Added Discord Connection and Home Page
Browse files Browse the repository at this point in the history
  • Loading branch information
ZickZenni committed Aug 28, 2024
1 parent 598a7f5 commit c3bad29
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 9 deletions.
21 changes: 17 additions & 4 deletions src/main/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { app, BrowserWindow, ipcMain, Menu, shell, Tray } from 'electron';
import fs from 'fs';
import path from 'path';
import { DiscordClient } from '../common/discord/client/client';

export default class WaveCordApp {
public readonly resourcesPath: string;
Expand All @@ -17,6 +18,8 @@ export default class WaveCordApp {

public token: string = '';

public discord: DiscordClient | null = null;

public constructor() {
app.setPath('userData', path.join(app.getPath('appData'), 'WaveCord'));

Expand All @@ -32,6 +35,11 @@ export default class WaveCordApp {

this.loadUser();

this.discord = new DiscordClient(this.token);
this.discord.on('connect', () => {
this.discord!.ready = true;
});

app.on('ready', async () => {
await this.init();
});
Expand Down Expand Up @@ -100,8 +108,7 @@ export default class WaveCordApp {
return { action: 'deny' };
});

/* Ipc */

/* Window Ipc */
ipcMain.on('WINDOW_MINIMIZE', () => {
this.window?.minimize();
});
Expand All @@ -113,9 +120,15 @@ export default class WaveCordApp {
else this.window.maximize();
});

/* App Ipc */
ipcMain.on('APP_EXIT', () => {
app.exit();
});

/* Discord Ipc */
ipcMain.handle('DISCORD_READY', () => {
return this.discord ? this.discord.ready : false;
});
}

private initTray() {
Expand All @@ -135,9 +148,9 @@ export default class WaveCordApp {

private loadUser() {
const filePath = `${app.getPath('userData')}//user`;
if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, '', 'binary');
if (!fs.existsSync(filePath)) fs.writeFileSync(filePath, '', 'utf8');

this.token = fs.readFileSync(filePath, 'utf8');
this.token = fs.readFileSync(filePath, 'utf8').replaceAll('\n', '');
}

private static resolveHtmlPath(htmlFileName: string) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/ipc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type IpcChannels =
| 'WINDOW_MINIMIZE'
| 'WINDOW_MAXIMIZE'
| 'APP_EXIT'
| 'DISCORD_READY';
12 changes: 7 additions & 5 deletions src/main/preload.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Disable no-unused-vars, broken for spread args
/* eslint no-unused-vars: off */
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';

export type Channels = 'WINDOW_MINIMIZE' | 'WINDOW_MAXIMIZE' | 'APP_EXIT';
import { IpcChannels } from './ipc';

const electronHandler = {
ipcRenderer: {
sendMessage(channel: Channels, ...args: unknown[]) {
sendMessage(channel: IpcChannels, ...args: unknown[]) {
ipcRenderer.send(channel, ...args);
},
on(channel: Channels, func: (...args: unknown[]) => void) {
on(channel: IpcChannels, func: (...args: unknown[]) => void) {
const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
func(...args);
ipcRenderer.on(channel, subscription);
Expand All @@ -18,9 +17,12 @@ const electronHandler = {
ipcRenderer.removeListener(channel, subscription);
};
},
once(channel: Channels, func: (...args: unknown[]) => void) {
once(channel: IpcChannels, func: (...args: unknown[]) => void) {
ipcRenderer.once(channel, (_event, ...args) => func(...args));
},
invoke(channel: IpcChannels, ...args: unknown[]): Promise<any> {
return ipcRenderer.invoke(channel, ...args);
},
},
};

Expand Down
2 changes: 2 additions & 0 deletions src/renderer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import LoadingPage from './pages/Loading';
import './styles/global.css';
import './styles/vars.css';
import Titlebar from './components/Titlebar';
import HomePage from './pages/Home';

export default function App() {
return (
<Router>
<Titlebar />
<Routes>
<Route path="/" element={<LoadingPage />} />
<Route path="/home" element={<HomePage />} />
</Routes>
</Router>
);
Expand Down
3 changes: 3 additions & 0 deletions src/renderer/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function HomePage() {
return <div>Homepage</div>;
}
19 changes: 19 additions & 0 deletions src/renderer/pages/Loading/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import { useEffect } from 'react';
import './Loading.css';
import { useNavigate } from 'react-router-dom';

export default function LoadingPage() {
const navigate = useNavigate();

useEffect(() => {
const interval = setInterval(() => {
window.electron.ipcRenderer
.invoke('DISCORD_READY')
.then((value: boolean) => {
if (value) navigate('/home');
return true;
})
.catch((err) => console.error(err));
}, 1000);
return () => {
clearInterval(interval);
};
}, [navigate]);

return (
<div className="loading__container">
<h1 className="loading__title">WaveCord</h1>
Expand Down

0 comments on commit c3bad29

Please sign in to comment.