-
Notifications
You must be signed in to change notification settings - Fork 0
feat: dynamic tile creation #9
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -1,11 +1,12 @@ | ||||||||
| 'use client'; | ||||||||
|
|
||||||||
| import React, { useState, ReactElement } from 'react'; | ||||||||
| import React, { useState, ReactElement, useContext, useRef, useEffect, memo } from 'react'; | ||||||||
| import { | ||||||||
| Mosaic, | ||||||||
| MosaicWindow, | ||||||||
| MosaicNode, | ||||||||
| MosaicPath, | ||||||||
| MosaicContext, | ||||||||
| } from 'react-mosaic-component'; | ||||||||
| import 'react-mosaic-component/react-mosaic-component.css'; | ||||||||
| import MapView from './MapView'; | ||||||||
|
|
@@ -17,10 +18,142 @@ import GasSensor from './GasSensor'; | |||||||
| import NetworkHealthTelemetryPanel from './NetworkHealthTelemetryPanel'; | ||||||||
| import VideoControls from './VideoControls'; | ||||||||
|
|
||||||||
| type MosaicKey = 'mapView' | 'rosMonitor' | 'waypointList' | 'videoControls' | 'gasSensor' | 'orientationDisplay' | 'goalSetter' | 'networkHealthMonitor'; | ||||||||
| type MosaicKey = | ||||||||
| | 'mapView' | ||||||||
| | 'rosMonitor' | ||||||||
| | 'waypointList' | ||||||||
| | 'videoControls' | ||||||||
| | 'gasSensor' | ||||||||
| | 'orientationDisplay' | ||||||||
| | 'goalSetter' | ||||||||
| | 'networkHealthMonitor'; | ||||||||
|
|
||||||||
| const TILE_DISPLAY_NAMES: Record<MosaicKey, string> = { | ||||||||
| mapView: 'Map View', | ||||||||
| rosMonitor: 'System Telemetry', | ||||||||
| waypointList: 'Waypoint List', | ||||||||
| videoControls: 'Video Stream', | ||||||||
| gasSensor: 'Science', | ||||||||
| orientationDisplay: 'Rover Orientation', | ||||||||
| goalSetter: 'Nav2', | ||||||||
| networkHealthMonitor: 'Connection Health', | ||||||||
| }; | ||||||||
|
|
||||||||
| const ALL_TILES: MosaicKey[] = [ | ||||||||
| 'mapView', | ||||||||
| 'rosMonitor', | ||||||||
| 'networkHealthMonitor', | ||||||||
| 'orientationDisplay', | ||||||||
| 'videoControls', | ||||||||
| 'waypointList', | ||||||||
| 'gasSensor', | ||||||||
| 'goalSetter', | ||||||||
| ]; | ||||||||
|
|
||||||||
| type PendingAdd = { | ||||||||
| pathKey: string; | ||||||||
| path: MosaicPath; | ||||||||
| direction: 'row' | 'column'; | ||||||||
| } | null; | ||||||||
|
|
||||||||
|
|
||||||||
| const Controls = memo<{ | ||||||||
| id: MosaicKey; | ||||||||
| path: MosaicPath; | ||||||||
| pendingAdd: PendingAdd; | ||||||||
| setPendingAdd: (value: PendingAdd) => void; | ||||||||
| }>(({ id, path, pendingAdd, setPendingAdd }) => { | ||||||||
| const { mosaicActions } = useContext(MosaicContext); | ||||||||
| const pathKey = JSON.stringify(path); | ||||||||
| const showDropdown = pendingAdd?.pathKey === pathKey; | ||||||||
| const dropdownRef = useRef<HTMLDivElement>(null); | ||||||||
|
|
||||||||
| const splitAndAdd = (direction: 'row' | 'column', newTile: MosaicKey) => { | ||||||||
| const splitNode: MosaicNode<MosaicKey> = { | ||||||||
| direction, | ||||||||
| first: id, | ||||||||
| second: newTile, | ||||||||
| splitPercentage: 60, | ||||||||
| }; | ||||||||
|
|
||||||||
| mosaicActions.replaceWith(path, splitNode); | ||||||||
| setPendingAdd(null); | ||||||||
| }; | ||||||||
|
|
||||||||
| // Close dropdown when clicking outside | ||||||||
| useEffect(() => { | ||||||||
| if (!showDropdown) return; | ||||||||
|
|
||||||||
| const handleClickOutside = (event: MouseEvent) => { | ||||||||
| if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { | ||||||||
| setPendingAdd(null); | ||||||||
| } | ||||||||
| }; | ||||||||
|
|
||||||||
| document.addEventListener('mousedown', handleClickOutside); | ||||||||
| return () => { | ||||||||
| document.removeEventListener('mousedown', handleClickOutside); | ||||||||
| }; | ||||||||
| }, [showDropdown, setPendingAdd]); | ||||||||
|
|
||||||||
| return ( | ||||||||
| <div ref={dropdownRef} style={{ display: 'flex', gap: 6, alignItems: 'center' }}> | ||||||||
| <button | ||||||||
| className="tile-btn" | ||||||||
| title="Add tile to the right" | ||||||||
| aria-label="Add tile to the right" | ||||||||
| onClick={(e) => { | ||||||||
| e.stopPropagation(); | ||||||||
| setPendingAdd({ pathKey, path, direction: 'row' }); | ||||||||
| }} | ||||||||
| > | ||||||||
| ➕ (Right) | ||||||||
| </button> | ||||||||
|
|
||||||||
| <button | ||||||||
| className="tile-btn" | ||||||||
| title="Add tile below" | ||||||||
| aria-label="Add tile below" | ||||||||
| onClick={(e) => { | ||||||||
| e.stopPropagation(); | ||||||||
| setPendingAdd({ pathKey, path, direction: 'column' }); | ||||||||
| }} | ||||||||
| > | ||||||||
| ➕ (Below) | ||||||||
ConnorNeed marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| </button> | ||||||||
|
|
||||||||
| {showDropdown ? ( | ||||||||
| <select | ||||||||
| className="tile-select" | ||||||||
| aria-label="Select tile to add" | ||||||||
| onClick={(e) => e.stopPropagation()} | ||||||||
| onChange={(e) => { | ||||||||
| const value = e.target.value as MosaicKey; | ||||||||
| // Validate pendingAdd state and selected value before proceeding | ||||||||
| if (pendingAdd && value) { | ||||||||
| splitAndAdd(pendingAdd.direction, value); | ||||||||
|
||||||||
| splitAndAdd(pendingAdd.direction, value); | |
| splitAndAdd(pendingAdd.direction, value); | |
| e.target.value = ''; |
Uh oh!
There was an error while loading. Please reload this page.