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

Electron build + Router Updates #22

Merged
merged 6 commits into from
Feb 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion backend/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0)
cmake_minimum_required(VERSION 3.5)
project(Tubender)

set(CMAKE_CXX_STANDARD 14)
Expand Down
37 changes: 26 additions & 11 deletions frontend/electron.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { app, BrowserWindow } = require('electron');
const path = require('path');
const isDev = require('electron-is-dev');
const express = require('express');

// Implement NSApplicationDelegate.applicationSupportsSecureRestorableState
app.applicationSupportsSecureRestorableState = () => true;

let mainWindow;

Expand All @@ -9,22 +12,34 @@ function createWindow() {
width: 800,
height: 600,
webPreferences: {
nodeIntegration: false, // nodeIntegration is set to false
contextIsolation: true, // enable context isolation
preload: path.join(__dirname, 'preload.js'), // specify preload script
nodeIntegration: false,
contextIsolation: true,
},
});

const startURL = isDev
? 'http://localhost:3000'
: `file://${path.join(__dirname, '../build/index.html')}`;

mainWindow.loadURL(startURL);
// Load the production build of the React application
mainWindow.loadFile(path.join(__dirname, 'build', 'index.html'));

mainWindow.on('closed', () => (mainWindow = null));
}

app.on('ready', createWindow);
app.on('ready', () => {
createWindow();

// Create an express server to serve static files
const expressApp = express();
const staticPath = path.join(__dirname, 'build', 'static');
expressApp.use(express.static(staticPath));

// Error handling
expressApp.on('error', (err) => {
console.error('Express server error:', err);
});

expressApp.listen(3000, () => {
console.log('Static server running on port 3000');
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
Expand All @@ -36,4 +51,4 @@ app.on('activate', () => {
if (mainWindow === null) {
createWindow();
}
});
});
8 changes: 4 additions & 4 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@types/electron": "^1.6.10",
"@types/react": "^18.2.53",
"@types/react-dom": "^18.2.18",
"electron": "^28.1.1",
"electron": "^28.2.3",
"electron-builder": "^24.9.1",
"electron-is-dev": "^2.0.0",
"tailwindcss": "^3.4.1",
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";
import { Route } from "react-router-dom";
import HomePage from './pages/HomePage';
import RoutingComponent from './components/RoutingComponent';
import AboutPage from './pages/AboutPage';
import MainPage from './pages/MainPage';
import PersuasivePage from './pages/PersuasivePage';
Expand All @@ -9,17 +10,14 @@ import ReviewsPage from './pages/ReviewsPage';

function App() {
return (
<Router>
<Routes>
<RoutingComponent>
<Route path="/" element={<HomePage />} />
<Route path="/main" element={<MainPage />} />
<Route path="/about" element={<AboutPage />} />
<Route path="/persuasive" element={<PersuasivePage />} />
<Route path="/contact" element={<ContactPage />} />
<Route path="/reviews" element={<ReviewsPage />} />
</Routes>
</Router>

</RoutingComponent>
);
}

Expand Down
30 changes: 30 additions & 0 deletions frontend/src/components/RoutingComponent.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React, { ReactNode } from 'react';
import { BrowserRouter as BrowserRouterImpl, HashRouter as HashRouterImpl, Routes } from 'react-router-dom';

interface RoutingComponentProps {
children: ReactNode;
}

const RoutingComponent: React.FC<RoutingComponentProps> = ({ children }) => {
if (isElectron()) {
return (
<HashRouterImpl>
<Routes>{children}</Routes>
</HashRouterImpl>
);
} else {
return (
<BrowserRouterImpl>
<Routes>{children}</Routes>
</BrowserRouterImpl>
);
}
}

function isElectron() {
// Check if running in Electron environment
const isElectron = navigator.userAgent.toLowerCase().indexOf(' electron/') > -1;
return isElectron;
}

export default RoutingComponent;
Loading