feat(electron/frontend) Electron Full Rework

- **Electron Full Rework**: Completely rewrote `electron/main.js` — removed child process spawn, backend now runs in-process via `require()`; frameless window with custom title bar; dual-mode support (dev loads Vite at `:4200`, production loads built files)
- **`electron/preload.js`**: Enhanced contextBridge to expose `windowControls` (minimize/maximize/close) and `isElectron` flag alongside existing `ipcRenderer` bridge
- **`electron/package.json`**: Updated scripts (`start`, `dev`, `build-front` → calls React build), removed duplicated backend dependencies (backend now required in-process), updated electron-builder config with proper `files` and `extraResources`
- **`TitleBar` component**: New `react_frontend/src/components/layout/TitleBar.tsx` — custom frameless window title bar with drag region, app branding, and IPC-powered minimize/maximize/close buttons
- **`AppLayout`**: Conditionally renders `<TitleBar>` when built for Electron (`VITE_ELECTRON=true`); wrapped content in an extra flex container to accommodate title bar height
- **`vite.config.ts`**: Added `VITE_ELECTRON` env detection — sets `base: './'` for correct asset resolution in `file://` context
- **`api.ts`**: Dynamic base URL — uses `http://localhost:4000/api/v1` in Electron mode, `/api/v1` (proxy-relative) otherwise
- **`router.tsx`**: Conditional router factory — uses `createHashRouter` in Electron mode (required for `file://` navigation) and `createBrowserRouter` for web
- **`electron.d.ts`**: Added TypeScript declarations for `window.electron` global (windowControls, ipcRenderer, isElectron)
- **`package.json`**: Added `build:electron` script with `cross-env VITE_ELECTRON=true`; added `cross-env` devDependency
- Updated `Electron.md`

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Mohammed AbadandCopilot committed 2026-05-03 02:00:14 +01:00
1 parent 7b84f89b65
commit 4303b33677
14 files changed
+5522 -164

No files matched your search

@@ -9,6 +9,9 @@ import {
SheetTitle,
} from '@/components/ui/sheet';
import { Sidebar, MobileSidebarContent } from './Sidebar';
import { TitleBar } from './TitleBar';
const isElectron = import.meta.env.VITE_ELECTRON === 'true' || !!window.electron?.isElectron;
export function AppLayout() {
const [mobileOpen, setMobileOpen] = useState(false);
@@ -20,35 +23,39 @@ export function AppLayout() {
}, [pathname]);
return (
<div className="flex h-screen overflow-hidden">
{/* Desktop sidebar */}
<div className="hidden md:block">
<Sidebar />
</div>
<div className="flex h-screen flex-col overflow-hidden">
{isElectron && <TitleBar />}
{/* Mobile header */}
<div className="fixed inset-x-0 top-0 z-40 flex h-14 items-center gap-3 border-b border-border bg-background px-4 md:hidden">
<Button variant="ghost" size="icon" onClick={() => setMobileOpen(true)}>
<Menu className="h-5 w-5" />
</Button>
<span className="text-lg font-bold">Showy</span>
</div>
{/* Mobile sidebar sheet */}
<Sheet open={mobileOpen} onOpenChange={setMobileOpen}>
<SheetContent side="left" className="w-64 p-0" showCloseButton={false}>
<SheetHeader className="sr-only">
<SheetTitle>Navigation</SheetTitle>
</SheetHeader>
<MobileSidebarContent onNavigate={() => setMobileOpen(false)} />
</SheetContent>
</Sheet>
<main ref={mainRef} className="flex-1 overflow-y-auto md:ml-56">
<div className="container mx-auto px-4 py-4 pt-18 md:px-6 md:py-6 md:pt-6">
<Outlet />
<div className="flex flex-1 overflow-hidden">
{/* Desktop sidebar */}
<div className="hidden md:block">
<Sidebar />
</div>
</main>
{/* Mobile header */}
<div className="fixed inset-x-0 top-0 z-40 flex h-14 items-center gap-3 border-b border-border bg-background px-4 md:hidden">
<Button variant="ghost" size="icon" onClick={() => setMobileOpen(true)}>
<Menu className="h-5 w-5" />
</Button>
<span className="text-lg font-bold">Showy</span>
</div>
{/* Mobile sidebar sheet */}
<Sheet open={mobileOpen} onOpenChange={setMobileOpen}>
<SheetContent side="left" className="w-64 p-0" showCloseButton={false}>
<SheetHeader className="sr-only">
<SheetTitle>Navigation</SheetTitle>
</SheetHeader>
<MobileSidebarContent onNavigate={() => setMobileOpen(false)} />
</SheetContent>
</Sheet>
<main ref={mainRef} className="flex-1 overflow-y-auto md:ml-56">
<div className="container mx-auto px-4 py-4 pt-18 md:px-6 md:py-6 md:pt-6">
<Outlet />
</div>
</main>
</div>
</div>
);
}
@@ -0,0 +1,38 @@
import { Minus, Square, X } from 'lucide-react';
export function TitleBar() {
const minimize = () => window.electron?.windowControls.minimize();
const maximize = () => window.electron?.windowControls.maximize();
const close = () => window.electron?.windowControls.close();
return (
<div className="flex h-14 select-none items-center justify-between border-b border-border bg-background"
style={{ WebkitAppRegion: 'drag' } as React.CSSProperties}
>
<div className="flex items-center gap-2 pl-3">
<span className="text-xs font-semibold tracking-wide text-foreground/80">SHOWY</span>
</div>
<div className="flex h-full" style={{ WebkitAppRegion: 'no-drag' } as React.CSSProperties}>
<button
onClick={minimize}
className="flex h-full w-11 items-center justify-center transition-colors hover:bg-muted"
>
<Minus className="h-3.5 w-3.5" />
</button>
<button
onClick={maximize}
className="flex h-full w-11 items-center justify-center transition-colors hover:bg-muted"
>
<Square className="h-3 w-3" />
</button>
<button
onClick={close}
className="flex h-full w-11 items-center justify-center transition-colors hover:bg-red-500 hover:text-white"
>
<X className="h-3.5 w-3.5" />
</button>
</div>
</div>
);
}
+20
View File
@@ -0,0 +1,20 @@
export {};
declare global {
interface Window {
electron?: {
isElectron: boolean;
windowControls: {
minimize: () => void;
maximize: () => void;
close: () => void;
};
ipcRenderer: {
send: (channel: string, data?: unknown) => void;
on: (channel: string, listener: (...args: unknown[]) => void) => void;
once: (channel: string, listener: (...args: unknown[]) => void) => void;
removeAllListeners: (channel: string) => void;
};
};
}
}
+3 -2
View File
@@ -1,7 +1,8 @@
import axios from 'axios';
// const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:4000/api/v1';
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://showyback.coolguy.space/api/v1';
const API_BASE_URL = import.meta.env.VITE_ELECTRON === 'true'
? 'http://localhost:4000/api/v1'
: (import.meta.env.VITE_API_URL || '/api/v1');
export const api = axios.create({
baseURL: API_BASE_URL,
+5 -2
View File
@@ -1,4 +1,4 @@
import { createBrowserRouter } from 'react-router-dom';
import { createBrowserRouter, createHashRouter } from 'react-router-dom';
import { lazy, Suspense, type ReactNode } from 'react';
import { AppLayout } from '@/components/layout/AppLayout';
import {
@@ -90,7 +90,10 @@ function SuspensePage({ children, fallback }: { children: ReactNode; fallback?:
return <Suspense fallback={fallback ?? <HomeSkeleton />}>{children}</Suspense>;
}
export const router = createBrowserRouter([
const isElectron = import.meta.env.VITE_ELECTRON === 'true';
const createRouter = isElectron ? createHashRouter : createBrowserRouter;
export const router = createRouter([
{
element: <AppLayout />,
children: [