fix(striker-ui): make NIC follow mouse during drag

main
Tsu-ba-me 2 years ago
parent a9cb1da195
commit bc3dc7e36e
  1. 110
      striker-ui/pages/init/index.tsx

@ -90,17 +90,27 @@ const DataGridCellText: FC<BodyTextProps> = ({
/> />
); );
const BriefNetworkInterface: FC<{ const BriefNetworkInterface: FC<
networkInterface: NetworkInterfaceOverviewMetadata; MUIBoxProps & {
}> = ({ networkInterface: NetworkInterfaceOverviewMetadata;
}
> = ({
networkInterface: { networkInterfaceName, networkInterfaceState }, networkInterface: { networkInterfaceName, networkInterfaceState },
sx: rootSx,
...restRootProps
}) => ( }) => (
<MUIBox <MUIBox
sx={{ {...{
display: 'flex', sx: {
flexDirection: 'row', display: 'flex',
flexDirection: 'row',
'& > :not(:first-child)': { marginLeft: '.5em' },
'& > :not(:first-child)': { marginLeft: '.5em' }, ...rootSx,
},
...restRootProps,
}} }}
> >
<Decorator <Decorator
@ -112,7 +122,7 @@ const BriefNetworkInterface: FC<{
); );
const createNetworkInterfaceTableColumns = ( const createNetworkInterfaceTableColumns = (
onMouseDownDragHandler: ( handleDragMouseDown: (
row: NetworkInterfaceOverviewMetadata, row: NetworkInterfaceOverviewMetadata,
...eventArgs: Parameters<Exclude<MUIBoxProps['onMouseDown'], undefined>> ...eventArgs: Parameters<Exclude<MUIBoxProps['onMouseDown'], undefined>>
) => void, ) => void,
@ -126,20 +136,20 @@ const createNetworkInterfaceTableColumns = (
networkInterfaceInputMap[row.networkInterfaceUUID] || false; networkInterfaceInputMap[row.networkInterfaceUUID] || false;
let cursor = 'grab'; let cursor = 'grab';
let handleOnMouseDown: MUIBoxProps['onMouseDown'] = (...eventArgs) => { let handleMouseDown: MUIBoxProps['onMouseDown'] = (...eventArgs) => {
onMouseDownDragHandler(row, ...eventArgs); handleDragMouseDown(row, ...eventArgs);
}; };
let icon = <MUIDragHandleIcon />; let icon = <MUIDragHandleIcon />;
if (isApplied) { if (isApplied) {
cursor = 'auto'; cursor = 'auto';
handleOnMouseDown = undefined; handleMouseDown = undefined;
icon = <MUICheckIcon sx={{ color: BLUE }} />; icon = <MUICheckIcon sx={{ color: BLUE }} />;
} }
return ( return (
<MUIBox <MUIBox
onMouseDown={handleOnMouseDown} onMouseDown={handleMouseDown}
sx={{ sx={{
alignItems: 'center', alignItems: 'center',
display: 'flex', display: 'flex',
@ -211,6 +221,10 @@ const createNetworkInterfaceTableColumns = (
]; ];
const NetworkInterfaceList: FC = () => { const NetworkInterfaceList: FC = () => {
const [dragMousePosition, setDragMousePosition] = useState<{
x: number;
y: number;
}>({ x: 0, y: 0 });
const [networkInterfaceInputMap, setNetworkInterfaceInputMap] = const [networkInterfaceInputMap, setNetworkInterfaceInputMap] =
useState<NetworkInterfaceInputMap>({}); useState<NetworkInterfaceInputMap>({});
const [networkInputs] = useState<NetworkInput[]>([ const [networkInputs] = useState<NetworkInput[]>([
@ -252,6 +266,46 @@ const NetworkInterfaceList: FC = () => {
}, },
}); });
const clearNetworkInterfaceHeld = () => {
setNetworkInterfaceHeld(undefined);
};
let createDropMouseUpHandler: (
interfaces: NetworkInterfaceOverviewMetadata[],
) => MUIBoxProps['onMouseUp'];
let floatingNetworkInterface: JSX.Element = <></>;
let handlePanelMouseMove: MUIBoxProps['onMouseMove'];
if (networkInterfaceHeld) {
createDropMouseUpHandler =
(interfaces: NetworkInterfaceOverviewMetadata[]) => () => {
interfaces.push(networkInterfaceHeld);
networkInterfaceInputMap[
networkInterfaceHeld.networkInterfaceUUID
].isApplied = true;
};
floatingNetworkInterface = (
<BriefNetworkInterface
networkInterface={networkInterfaceHeld}
sx={{
left: `calc(${dragMousePosition.x}px - .4em)`,
position: 'absolute',
top: `calc(${dragMousePosition.y}px - 2em)`,
zIndex: 10,
}}
/>
);
handlePanelMouseMove = ({ nativeEvent: { clientX, clientY } }) => {
setDragMousePosition({
x: clientX,
y: clientY,
});
};
}
useEffect(() => { useEffect(() => {
const map = networkInterfaces.reduce<NetworkInterfaceInputMap>( const map = networkInterfaces.reduce<NetworkInterfaceInputMap>(
(reduceContainer, { networkInterfaceUUID }) => { (reduceContainer, { networkInterfaceUUID }) => {
@ -271,10 +325,15 @@ const NetworkInterfaceList: FC = () => {
}, []); }, []);
return ( return (
<Panel> <Panel
onMouseLeave={clearNetworkInterfaceHeld}
onMouseMove={handlePanelMouseMove}
onMouseUp={clearNetworkInterfaceHeld}
>
<PanelHeader> <PanelHeader>
<HeaderText text="Network Interfaces" /> <HeaderText text="Network Interfaces" />
</PanelHeader> </PanelHeader>
{floatingNetworkInterface}
{isLoading ? ( {isLoading ? (
<Spinner /> <Spinner />
) : ( ) : (
@ -290,9 +349,16 @@ const NetworkInterfaceList: FC = () => {
> >
<MUIDataGrid <MUIDataGrid
autoHeight autoHeight
columns={createNetworkInterfaceTableColumns((row) => { columns={createNetworkInterfaceTableColumns(
setNetworkInterfaceHeld(row); (row, { clientX, clientY }) => {
}, networkInterfaceInputMap)} setDragMousePosition({
x: clientX,
y: clientY,
});
setNetworkInterfaceHeld(row);
},
networkInterfaceInputMap,
)}
disableColumnMenu disableColumnMenu
disableSelectionOnClick disableSelectionOnClick
getRowId={({ networkInterfaceUUID }) => networkInterfaceUUID} getRowId={({ networkInterfaceUUID }) => networkInterfaceUUID}
@ -333,17 +399,7 @@ const NetworkInterfaceList: FC = () => {
}} }}
> >
<MUIBox <MUIBox
onMouseUp={() => { onMouseUp={createDropMouseUpHandler?.call(null, interfaces)}
if (networkInterfaceHeld) {
interfaces.push(networkInterfaceHeld);
networkInterfaceInputMap[
networkInterfaceHeld.networkInterfaceUUID
].isApplied = true;
}
setNetworkInterfaceHeld(undefined);
}}
sx={{ sx={{
borderColor: TEXT, borderColor: TEXT,
borderStyle: 'dashed', borderStyle: 'dashed',

Loading…
Cancel
Save