feat(ui): add ContextMenu component styled with the design system

Renderer-rendered replacement for the native OS context menu (which
couldn't use any CSS at all): connection status, login/logout, voice
toggle, the new "Ecrire un message" chat toggle, and quit. Positioned
at the click point and clamped to stay inside the window, dismissed on
outside click, Escape, or after a selection.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
william
2026-10-31 22:38:27 +01:00
co-authored by Claude Sonnet 5
parent cb9b4177e4
commit b072960012
2 changed files with 237 additions and 0 deletions
@@ -0,0 +1,149 @@
import { useEffect, useLayoutEffect, useRef, useState } from 'react'
import { Check, LogOut, MessageSquare, Mic, Power } from 'lucide-react'
export interface ContextMenuProps {
x: number
y: number
loggedIn: boolean
email: string | null
listening: boolean
chatOpen: boolean
onClose: () => void
onLogin: () => void
onLogout: () => void
onToggleListening: () => void
onToggleChat: () => void
onQuit: () => void
}
/**
* Menu contextuel de Max, rendu cote renderer (docs/Max_Design_System.md) : remplace le menu
* natif Electron (impossible a styliser en CSS) par un composant React stylise avec les tokens du
* design system. Positionne au clic droit, referme au clic exterieur, a Echap, ou apres un choix.
*/
export default function ContextMenu({
x,
y,
loggedIn,
email,
listening,
chatOpen,
onClose,
onLogin,
onLogout,
onToggleListening,
onToggleChat,
onQuit
}: ContextMenuProps): React.JSX.Element {
const menuRef = useRef<HTMLDivElement | null>(null)
const [position, setPosition] = useState({ top: y, left: x })
useLayoutEffect(() => {
const el = menuRef.current
if (!el) return
const { innerWidth, innerHeight } = window
const rect = el.getBoundingClientRect()
const left = Math.max(4, Math.min(x, innerWidth - rect.width - 4))
const top = Math.max(4, Math.min(y, innerHeight - rect.height - 4))
setPosition({ left, top })
}, [x, y])
useEffect(() => {
const handlePointerDown = (event: MouseEvent): void => {
if (menuRef.current && !menuRef.current.contains(event.target as Node)) onClose()
}
const handleKeyDown = (event: KeyboardEvent): void => {
if (event.key === 'Escape') onClose()
}
document.addEventListener('mousedown', handlePointerDown)
document.addEventListener('keydown', handleKeyDown)
return () => {
document.removeEventListener('mousedown', handlePointerDown)
document.removeEventListener('keydown', handleKeyDown)
}
}, [onClose])
const runAndClose =
(action: () => void): (() => void) =>
() => {
action()
onClose()
}
return (
<div
ref={menuRef}
role="menu"
className="context-menu"
style={{ top: position.top, left: position.left }}
>
{loggedIn ? (
<div className="context-menu__item context-menu__item--disabled">Connecte : {email}</div>
) : (
<button
type="button"
role="menuitem"
className="context-menu__item"
aria-label="Me connecter"
onClick={runAndClose(onLogin)}
>
Me connecter
</button>
)}
{loggedIn && (
<button
type="button"
role="menuitem"
className="context-menu__item"
aria-label="Se deconnecter"
onClick={runAndClose(onLogout)}
>
<LogOut size={14} />
<span>Se deconnecter</span>
</button>
)}
<div className="context-menu__separator" />
<button
type="button"
role="menuitem"
className="context-menu__item"
aria-label="Ecoute vocale"
disabled={!loggedIn}
onClick={runAndClose(onToggleListening)}
>
<Mic size={14} />
<span>Ecoute vocale</span>
{loggedIn && listening && <Check size={14} className="context-menu__check" />}
</button>
<button
type="button"
role="menuitem"
className="context-menu__item"
aria-label="Ecrire un message"
disabled={!loggedIn}
onClick={runAndClose(onToggleChat)}
>
<MessageSquare size={14} />
<span>Ecrire un message</span>
{loggedIn && chatOpen && <Check size={14} className="context-menu__check" />}
</button>
<div className="context-menu__separator" />
<button
type="button"
role="menuitem"
className="context-menu__item"
aria-label="Quitter"
onClick={runAndClose(onQuit)}
>
<Power size={14} />
<span>Quitter</span>
</button>
</div>
)
}