import { useMemo, useState } from 'react' import { allCableTypes, cableTypesForConnection, getPortType } from '../../domain/diagram' import { useCatalogStore } from '../../state/catalogStore' import { useDiagramStore } from '../../state/diagramStore' import { useCanEdit } from '../../hooks/useCanEdit' export default function ConnectionInspector({ connectionId }: { connectionId: string }) { const diagram = useDiagramStore((s) => s.diagram) const catalog = useCatalogStore((s) => s.catalog) const canEdit = useCanEdit() const updateConnection = useDiagramStore((s) => s.updateConnection) const removeConnection = useDiagramStore((s) => s.removeConnection) const startBundle = useDiagramStore((s) => s.startBundle) const addConnectionToBundle = useDiagramStore((s) => s.addConnectionToBundle) const removeConnectionFromBundle = useDiagramStore((s) => s.removeConnectionFromBundle) const updateBundle = useDiagramStore((s) => s.updateBundle) const selectConnection = useDiagramStore((s) => s.selectConnection) const [startingBundle, setStartingBundle] = useState(false) const [newBundleName, setNewBundleName] = useState('') const [newBundleCableTypeId, setNewBundleCableTypeId] = useState('') const connection = diagram.connections.find((c) => c.id === connectionId) const sourceDevice = diagram.devices.find((d) => d.id === connection?.sourceDeviceId) const targetDevice = diagram.devices.find((d) => d.id === connection?.targetDeviceId) const sourcePort = sourceDevice?.ports.find((p) => p.id === connection?.sourcePortId) const targetPort = targetDevice?.ports.find((p) => p.id === connection?.targetPortId) const availableCableTypes = useMemo(() => { if (!sourcePort || !targetPort) return [] const sourcePortType = getPortType(catalog, sourcePort.portTypeId) const targetPortType = getPortType(catalog, targetPort.portTypeId) return sourcePortType && targetPortType ? cableTypesForConnection(catalog, sourcePortType, targetPortType) : [] }, [catalog, sourcePort, targetPort]) const cableCostPerUnit = availableCableTypes.find((ct) => ct.id === connection?.cableTypeId)?.costPerUnit // Every cable type, unfiltered by port compatibility — a bundle's own // cable type is a separate, BOM-only choice from any member's individually // validated cableTypeId (see CableBundle's own doc comment), so it isn't // constrained to what fits this specific port pair. const allBundleCableTypes = useMemo( () => [...allCableTypes(catalog)].sort((a, b) => a.name.localeCompare(b.name)), [catalog], ) const bundle = diagram.bundles.find((b) => b.id === connection?.bundleId) const otherBundles = diagram.bundles.filter((b) => b.id !== connection?.bundleId) const otherMembers = diagram.connections.filter((c) => c.bundleId === bundle?.id && c.id !== connectionId) const describeConnectionEnd = (deviceId: string, portId: string) => { const device = diagram.devices.find((d) => d.id === deviceId) const port = device?.ports.find((p) => p.id === portId) return device && port ? `${device.name} · ${port.name}` : 'Unknown' } if (!connection || !sourceDevice || !targetDevice || !sourcePort || !targetPort) return null return (
From {sourceDevice.name} · {sourcePort.name}
To {targetDevice.name} · {targetPort.name}