Skip to content

Commit

Permalink
Fixed device connection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurczewski committed Aug 14, 2024
1 parent 5aace22 commit d592a88
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { setDiscoveryStatus } from "Core/discovery-device/actions/base.action"
import { DiscoveryStatus } from "Core/discovery-device/reducers/discovery-device.interface"
import ConnectingContent from "Core/connecting/components/connecting-content.component"
import {
getDevicesSelector,
getAvailableDevicesSelector,
getDevicesSelector,
getFailedDevicesSelector,
handleDeviceActivated,
} from "device-manager/feature"
Expand All @@ -26,20 +26,40 @@ import {
URL_ONBOARDING,
} from "Core/__deprecated__/renderer/constants/urls"
import { useNoNewDevicesDetectedHook } from "Core/discovery-device/hooks/use-no-new-devices-detected.hook"
import { useFilteredRoutesHistory } from "shared/utils"

const ConfiguredDevicesDiscovery: FunctionComponent = () => {
const history = useHistory()
const dispatch = useDispatch<TmpDispatch>()
const devices = useSelector(getDevicesSelector)
const failedDevices = useSelector(getFailedDevicesSelector)
const availableDevices = useSelector(getAvailableDevicesSelector)
const [pathToGoBack] = useFilteredRoutesHistory([
URL_MAIN.root,
...Object.values(URL_ONBOARDING),
...Object.values(URL_DISCOVERY_DEVICE),
...Object.values(URL_DEVICE_INITIALIZATION),
])

useEffect(() => {
dispatch(setDiscoveryStatus(DiscoveryStatus.Discovering))
}, [history, dispatch])

const noNewDevicesDetectedState = useNoNewDevicesDetectedHook()

useEffect(() => {
if (!devices.length && !availableDevices.length) {
if (
pathToGoBack.startsWith(URL_MAIN.help) ||
pathToGoBack.startsWith(URL_MAIN.settings)
) {
history.push(pathToGoBack)
} else {
history.push(URL_MAIN.news)
}
}
}, [availableDevices.length, devices.length, history, pathToGoBack])

useEffect(() => {
const handleDeviceActivation = async () => {
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { useDispatch, useSelector } from "react-redux"
import { defineMessages } from "react-intl"
import { useHistory } from "react-router-dom"
import { answerMain } from "shared/utils"
import { setSelectDeviceDrawerOpen } from "device-manager/feature"
import {
getDevicesSelector,
setSelectDeviceDrawerOpen,
} from "device-manager/feature"
import { DeviceProtocolMainEvent } from "device-protocol/models"
import { FunctionComponent } from "Core/core/types/function-component.interface"
import { intl } from "Core/__deprecated__/renderer/utils/intl"
Expand All @@ -25,6 +28,7 @@ import {
CONNECTING_LOADER_MODAL_ID,
useLoaderSkipOnConnect,
} from "Core/modals-manager/components/use-loader-skip-on-connect.hook"
import { DeviceState } from "device-manager/models"

const messages = defineMessages({
subtitle: {
Expand All @@ -34,20 +38,21 @@ const messages = defineMessages({

const ConnectingLoaderModal: FunctionComponent = () => {
const dispatch = useDispatch<Dispatch>()
const [openModal, setOpenModal] = useState<boolean>(false)

const [loaderModalOpened, setLoaderModalOpened] = useState<boolean>(false)
const devices = useSelector(getDevicesSelector)
const history = useHistory()

const discoveryStatus = useSelector(getDiscoveryStatus)
const shouldLoaderSkipOnConnect = useLoaderSkipOnConnect()

const devicesInProgress = devices.filter((device) => {
return [DeviceState.Connected, DeviceState.Initialized].includes(
device.state
)
})

useEffect(() => {
const handler = async () => {
if (shouldLoaderSkipOnConnect()) {
setOpenModal(false)
} else {
setOpenModal(true)
}
const handler = () => {
setLoaderModalOpened(!shouldLoaderSkipOnConnect())
}

const unregisterDeviceConnectedListener = answerMain(
Expand All @@ -68,9 +73,12 @@ const ConnectingLoaderModal: FunctionComponent = () => {
useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout>

if (openModal) {
if (loaderModalOpened) {
timeoutId = setTimeout(() => {
setOpenModal(false)
if (devicesInProgress.length > 0) {
return
}
setLoaderModalOpened(false)
const pathname = history.location.pathname
if (
![
Expand All @@ -88,21 +96,27 @@ const ConnectingLoaderModal: FunctionComponent = () => {
const unregister = history.listen((location) => {
if (location.pathname.includes(URL_DISCOVERY_DEVICE.root)) {
clearTimeout(timeoutId)
setOpenModal(false)
setLoaderModalOpened(false)
}
})

return () => {
clearTimeout(timeoutId)
unregister()
}
}, [openModal, dispatch, discoveryStatus, history])
}, [
devicesInProgress.length,
discoveryStatus,
dispatch,
history,
loaderModalOpened,
])

return (
<LoaderModal
data={{ "modal-id": CONNECTING_LOADER_MODAL_ID }}
subtitle={intl.formatMessage(messages.subtitle)}
open={openModal}
open={loaderModalOpened}
layer={ModalLayers.ConnectingLoader}
/>
)
Expand Down

0 comments on commit d592a88

Please sign in to comment.