Skip to content
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

[FRONT-1872] Make connection drawing smarter #353

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/components/MapConnectionLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Layer, Source } from 'react-map-gl'
import { useNodeConnections } from '../utils'
import { useStore } from '../Store'
import { ConnectionsMode } from '../types'
import { useStreamIdParam } from '../hooks'

export function MapConnectionLayer() {
const connections = useNodeConnections()
const { connectionsMode, selectedNode } = useStore()
const streamId = useStreamIdParam()

const { connectionsMode } = useStore()

const visible = connectionsMode === ConnectionsMode.Always
const visible = connectionsMode === ConnectionsMode.Always || !!streamId || !!selectedNode

const lineData = useMemo(
function getFeatureConnection(): GeoJSON.FeatureCollection {
Expand Down
13 changes: 10 additions & 3 deletions src/components/MapNavigationControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Tooltip } from './Tooltip'
export function MapNavigationControl() {
const map = useMap()

const { setConnectionsMode } = useStore()
const { setConnectionsMode, connectionsMode } = useStore()

const { showConnectionsToggle, showResetViewportButton, showZoomButtons } = useHud()

Expand All @@ -25,6 +25,7 @@ export function MapNavigationControl() {
<Tooltip value="Show node connections">
<ConnectionButton
type="button"
$isActive={connectionsMode === ConnectionsMode.Always}
onClick={() => {
setConnectionsMode((current) => {
return current === ConnectionsMode.Always
Expand Down Expand Up @@ -109,10 +110,16 @@ const Button = styled.button`
}
`

const ConnectionButton = styled(Button)`
const ConnectionButton = styled(Button)<{ $isActive?: boolean }>`
color: ${({ $isActive }) => ($isActive ? '#0324ff' : 'inherit')};

&:hover {
color: ${({ $isActive }) => ($isActive ? '#7b8fff' : '#a3a3a3')};
}

&:active,
&:focus {
color: #0324ff;
color: ${({ $isActive }) => ($isActive ? '#0324ff' : '#323232')};
}

svg {
Expand Down
15 changes: 12 additions & 3 deletions src/utils/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useParams, useSearchParams } from 'react-router-dom'
import { useStore } from '../Store'
import { DefaultViewState } from '../consts'
import { useMap, useStreamIdParam } from '../hooks'
import { OperatorNode } from '../types'
import { ConnectionsMode, OperatorNode } from '../types'
import { getNodeLocationId } from './map'
import { useOperatorNodeNeighborsQuery } from './neighbors'
import { useOperatorNodesForStreamQuery } from './nodes'
Expand All @@ -13,16 +13,25 @@ export function useNodeConnections() {

const { data: nodes } = useOperatorNodesForStreamQuery(streamId)

const { selectedNode } = useStore()
const { selectedNode, connectionsMode } = useStore()

const { data: neighbors } = useOperatorNodeNeighborsQuery(selectedNode?.id, { streamId })

return useMemo(
function getConnectionsFromNodesAndNeighbors() {
// Return early if we don't have the required data
if (!nodes || !neighbors) {
return []
}

// Only show connections if:
// 1. We have a streamId (showing all connections for a stream), OR
// 2. We have a selected node (showing connections for that node), OR
// 3. ConnectionsMode is set to Always
if (!streamId && !selectedNode?.id && connectionsMode !== ConnectionsMode.Always) {
return []
}

const nodesById: Record<string, OperatorNode | undefined> = {}

for (const node of nodes) {
Expand Down Expand Up @@ -65,7 +74,7 @@ export function useNodeConnections() {

return connections
},
[nodes, neighbors],
[nodes, neighbors, selectedNode, streamId, connectionsMode],
)
}

Expand Down