feat: connect with double-click

This commit is contained in:
Thomas Nordquist
2024-05-24 22:23:26 +02:00
parent ae0ce79e26
commit 79fbd34cfa

View File

@@ -1,26 +1,40 @@
import React from 'react' import React, { useCallback } from 'react'
import { connect } from 'react-redux' import { connect } from 'react-redux'
import { ListItem, Typography } from '@material-ui/core' import { ListItem, Typography } from '@material-ui/core'
import { toMqttConnection, ConnectionOptions } from '../../../model/ConnectionOptions' import { toMqttConnection, ConnectionOptions } from '../../../model/ConnectionOptions'
import { withStyles, Theme } from '@material-ui/core/styles' import { withStyles, Theme } from '@material-ui/core/styles'
import { bindActionCreators } from 'redux' import { bindActionCreators } from 'redux'
import { connectionManagerActions } from '../../../actions' import { connectionActions, connectionManagerActions } from '../../../actions'
export interface Props { export interface Props {
connection: ConnectionOptions connection: ConnectionOptions
actions: any actions: {
connection: any
connectionManager: any
}
selected: boolean selected: boolean
classes: any classes: any
} }
const ConnectionItem = (props: Props) => { const ConnectionItem = (props: Props) => {
const connect = useCallback(() => {
const mqttOptions = toMqttConnection(props.connection)
if (mqttOptions) {
props.actions.connection.connect(mqttOptions, props.connection.id)
}
}, [props.connection, props])
const connection = props.connection.host && toMqttConnection(props.connection) const connection = props.connection.host && toMqttConnection(props.connection)
return ( return (
<ListItem <ListItem
button={true} button={true}
selected={props.selected} selected={props.selected}
style={{ display: 'block' }} style={{ display: 'block' }}
onClick={() => props.actions.selectConnection(props.connection.id)} onClick={() => props.actions.connectionManager.selectConnection(props.connection.id)}
onDoubleClick={() => {
props.actions.connectionManager.selectConnection(props.connection.id)
connect()
}}
> >
<Typography className={props.classes.name}>{props.connection.name || 'mqtt broker'}</Typography> <Typography className={props.classes.name}>{props.connection.name || 'mqtt broker'}</Typography>
<Typography className={props.classes.details}>{connection && connection.url}</Typography> <Typography className={props.classes.details}>{connection && connection.url}</Typography>
@@ -30,10 +44,12 @@ const ConnectionItem = (props: Props) => {
export const mapDispatchToProps = (dispatch: any) => { export const mapDispatchToProps = (dispatch: any) => {
return { return {
actions: bindActionCreators(connectionManagerActions, dispatch), actions: {
connection: bindActionCreators(connectionActions, dispatch),
connectionManager: bindActionCreators(connectionManagerActions, dispatch),
},
} }
} }
export const connectionItemStyle = (theme: Theme) => ({ export const connectionItemStyle = (theme: Theme) => ({
name: { name: {
width: '100%', width: '100%',