-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec2c3cb
commit 86c6580
Showing
10 changed files
with
226 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
deployables/app/app/components/pilotStatus/PilotStatus.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { GhostButton } from '@zodiac/ui' | ||
import { Power, PowerOff } from 'lucide-react' | ||
import { useState } from 'react' | ||
import { useConnectChangeOnPilotEvents } from './useConnectChnageOnPilotEvents' | ||
import { useDisconnectWhenUnreachable } from './useDisconnectWhenUnreachable' | ||
import { usePingWhileDisconnected } from './usePingWhileDisconnected' | ||
|
||
export const PilotStatus = () => { | ||
const connected = useConnected() | ||
|
||
if (connected) { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<div className="leading-0 flex items-center gap-2 text-xs font-semibold uppercase"> | ||
<Power className="text-green-500" size={16} /> | ||
Connected | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<div className="flex items-center gap-2 text-xs font-semibold uppercase"> | ||
<PowerOff className="text-red-700" size={16} /> | ||
Disconnected | ||
</div> | ||
|
||
<GhostButton id="ZODIAC-PILOT::open-panel-button" size="tiny"> | ||
Open Pilot | ||
</GhostButton> | ||
</div> | ||
) | ||
} | ||
|
||
const useConnected = () => { | ||
const [connected, setConnected] = useState(false) | ||
|
||
useConnectChangeOnPilotEvents({ | ||
onConnect: () => setConnected(true), | ||
onDisconnect: () => setConnected(false), | ||
}) | ||
usePingWhileDisconnected(connected, { onConnect: () => setConnected(true) }) | ||
useDisconnectWhenUnreachable(connected, { | ||
onDisconnect: () => setConnected(false), | ||
}) | ||
|
||
return connected | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { PilotStatus } from './PilotStatus' |
42 changes: 42 additions & 0 deletions
42
deployables/app/app/components/pilotStatus/useConnectChnageOnPilotEvents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { PilotMessageType, type Message } from '@zodiac/messages' | ||
import { useStableHandler } from '@zodiac/ui' | ||
import { useEffect } from 'react' | ||
|
||
type ConnectChangeOnPilotEventOptions = { | ||
onConnect: () => void | ||
onDisconnect: () => void | ||
} | ||
|
||
export const useConnectChangeOnPilotEvents = ({ | ||
onConnect, | ||
onDisconnect, | ||
}: ConnectChangeOnPilotEventOptions) => { | ||
const onConnectRef = useStableHandler(onConnect) | ||
const onDisconnectRef = useStableHandler(onDisconnect) | ||
|
||
useEffect(() => { | ||
const handleMessage = (event: MessageEvent<Message>) => { | ||
if (event.data == null) { | ||
return | ||
} | ||
|
||
switch (event.data.type) { | ||
case PilotMessageType.PILOT_CONNECT: { | ||
onConnectRef.current() | ||
|
||
break | ||
} | ||
|
||
case PilotMessageType.PILOT_DISCONNECT: { | ||
onDisconnectRef.current() | ||
} | ||
} | ||
} | ||
|
||
window.addEventListener('message', handleMessage) | ||
|
||
return () => { | ||
window.removeEventListener('message', handleMessage) | ||
} | ||
}, [onConnectRef, onDisconnectRef]) | ||
} |
67 changes: 67 additions & 0 deletions
67
deployables/app/app/components/pilotStatus/useDisconnectWhenUnreachable.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
CompanionAppMessageType, | ||
PilotMessageType, | ||
type CompanionAppMessage, | ||
type Message, | ||
} from '@zodiac/messages' | ||
import { useEffect, useRef } from 'react' | ||
|
||
type DisconnectWhenUnreachableOptions = { | ||
onDisconnect: () => void | ||
} | ||
|
||
export const useDisconnectWhenUnreachable = ( | ||
connected: boolean, | ||
{ onDisconnect }: DisconnectWhenUnreachableOptions, | ||
) => { | ||
const onDisconnectRef = useRef(onDisconnect) | ||
|
||
useEffect(() => { | ||
onDisconnectRef.current = onDisconnect | ||
}, [onDisconnect]) | ||
|
||
useEffect(() => { | ||
if (connected === false) { | ||
return | ||
} | ||
|
||
let cancelled = false | ||
|
||
const probeConnection = () => { | ||
window.postMessage( | ||
{ type: CompanionAppMessageType.PING } satisfies CompanionAppMessage, | ||
'*', | ||
) | ||
|
||
const disconnectTimeout = setTimeout(() => { | ||
if (cancelled) { | ||
return | ||
} | ||
|
||
onDisconnectRef.current() | ||
}, 1000) | ||
|
||
const handlePong = (event: MessageEvent<Message>) => { | ||
if (event.data == null) { | ||
return | ||
} | ||
|
||
if (event.data.type !== PilotMessageType.PONG) { | ||
return | ||
} | ||
|
||
clearTimeout(disconnectTimeout) | ||
} | ||
|
||
window.addEventListener('message', handlePong) | ||
} | ||
|
||
const interval = setInterval(probeConnection, 1000) | ||
|
||
return () => { | ||
cancelled = true | ||
|
||
clearInterval(interval) | ||
} | ||
}, [connected]) | ||
} |
50 changes: 50 additions & 0 deletions
50
deployables/app/app/components/pilotStatus/usePingWhileDisconnected.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { | ||
CompanionAppMessageType, | ||
PilotMessageType, | ||
type CompanionAppMessage, | ||
type Message, | ||
} from '@zodiac/messages' | ||
import { useStableHandler } from '@zodiac/ui' | ||
import { useEffect } from 'react' | ||
|
||
type PingWhileDisconnectedOptions = { | ||
onConnect: () => void | ||
} | ||
|
||
export const usePingWhileDisconnected = ( | ||
connected: boolean, | ||
{ onConnect }: PingWhileDisconnectedOptions, | ||
) => { | ||
const onConnectRef = useStableHandler(onConnect) | ||
|
||
useEffect(() => { | ||
if (connected) { | ||
return | ||
} | ||
|
||
const interval = setInterval(() => { | ||
window.postMessage({ | ||
type: CompanionAppMessageType.PING, | ||
} satisfies CompanionAppMessage) | ||
}, 500) | ||
|
||
const handlePong = (event: MessageEvent<Message>) => { | ||
if (event.data == null) { | ||
return | ||
} | ||
|
||
if (event.data.type !== PilotMessageType.PONG) { | ||
return | ||
} | ||
|
||
onConnectRef.current() | ||
} | ||
|
||
window.addEventListener('message', handlePong) | ||
|
||
return () => { | ||
window.removeEventListener('message', handlePong) | ||
clearInterval(interval) | ||
} | ||
}, [connected, onConnectRef]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { useStableHandler } from './useStableHandler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useEffect, useRef } from 'react' | ||
|
||
export const useStableHandler = <T>(handler: T) => { | ||
const handlerRef = useRef(handler) | ||
|
||
useEffect(() => { | ||
handlerRef.current = handler | ||
}, [handler]) | ||
|
||
return handlerRef | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters