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

update: frontend from tanega commit:a16dd10 #201

Merged
merged 1 commit into from
Aug 19, 2024
Merged
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
20 changes: 11 additions & 9 deletions frontend/components/core/command/vessel-finder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@/components/ui/command"
import { useMapStore } from "@/components/providers/map-store-provider"
import { useVesselsStore } from "@/components/providers/vessels-store-provider"
import { getVesselFirstExcursionSegments } from "@/services/backend-rest-client"

type Props = {
wideMode: boolean
Expand All @@ -26,23 +27,24 @@ export function VesselFinderDemo({ wideMode }: Props) {
const [open, setOpen] = useState(false)
const [search, setSearch] = useState<string>("")
const {
addTrackedVesselMMSI,
trackedVesselMMSIs,
addTrackedVessel,
trackedVesselIDs,
setActivePosition,
viewState,
setViewState,
} = useMapStore((state) => state)
const { vessels: allVessels } = useVesselsStore((state) => state);
const { latestPositions } = useMapStore((state) => state);

const onSelectVessel = (vesselIdentifier: string) => {
const mmsi = parseInt(vesselIdentifier.split(SEPARATOR)[1])
if (mmsi && !trackedVesselMMSIs.includes(mmsi)) {
addTrackedVesselMMSI(mmsi)
const onSelectVessel = async (vesselIdentifier: string) => {
const vesselId = parseInt(vesselIdentifier.split(SEPARATOR)[3])
const response = await getVesselFirstExcursionSegments(vesselId);
if (vesselId && !trackedVesselIDs.includes(vesselId)) {
addTrackedVessel(vesselId, response.data);
}
if (mmsi) {
if (vesselId) {
const selectedVesselLatestPosition = latestPositions.find(
(position) => position.vessel.mmsi === mmsi
(position) => position.vessel.id === vesselId
)
if (selectedVesselLatestPosition) {
setActivePosition(selectedVesselLatestPosition as VesselPosition)
Expand Down Expand Up @@ -108,7 +110,7 @@ export function VesselFinderDemo({ wideMode }: Props) {
<CommandItem
key={`${vessel.id}`}
onSelect={(value) => onSelectVessel(value)}
value={`${vessel.ship_name}${SEPARATOR}${vessel.mmsi}${SEPARATOR}${vessel.imo}`} // so we can search by name, mmsi, imo
value={`${vessel.ship_name}${SEPARATOR}${vessel.mmsi}${SEPARATOR}${vessel.imo}${SEPARATOR}${vessel.id}`} // so we can search by name, mmsi, imo
>
<span>{vessel.ship_name}</span>
<span className="ml-2 text-xxxs">
Expand Down
70 changes: 39 additions & 31 deletions frontend/components/core/map/main-map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,14 @@ import chroma from "chroma-js"
import { FlyToInterpolator, MapViewState, ScatterplotLayer } from "deck.gl"
import { useTheme } from "next-themes"
import { renderToString } from "react-dom/server"
import Map from "react-map-gl/maplibre"
import { Map as MapGL } from "react-map-gl/maplibre"

import MapTooltip from "@/components/ui/tooltip-map-template"
import { useMapStore } from "@/components/providers/map-store-provider"
import { VesselPosition, VesselPositions, VesselTrailPropertiesType } from "@/types/vessel"
import { VesselPosition, VesselPositions, VesselExcursionSegmentGeo, VesselExcursionSegmentsGeo, VesselExcursionSegment, VesselExcursionSegments } from "@/types/vessel"

const MESH_URL_LOCAL = `../../../data/mesh/boat.obj`

type BartStation = {
name: string
entries: number
exits: number
coordinates: [longitude: number, latitude: number]
}

type CoreMapProps = {
vesselsPositions: VesselPositions;
}
Expand All @@ -39,7 +32,8 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
setViewState,
activePosition,
setActivePosition,
trackedVesselMMSIs,
trackedVesselIDs,
trackedVesselSegments,
setLatestPositions,
} = useMapStore((state) => state)

Expand All @@ -55,7 +49,7 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
useEffect(() => {
// This will change the key of the layer, forcing it to re-render when `activePosition` changes
setLayerKey((prevKey) => prevKey + 1)
}, [activePosition, trackedVesselMMSIs])
}, [activePosition, trackedVesselIDs])

useEffect(() => {
setLatestPositions(vesselsPositions);
Expand All @@ -75,8 +69,8 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
radiusMaxPixels: 25,
radiusScale: 200,
getFillColor: (vp: VesselPosition) => {
return vp.vessel.mmsi === activePosition?.vessel.mmsi ||
trackedVesselMMSIs.includes(vp.vessel.mmsi)
return vp.vessel.id === activePosition?.vessel.id ||
trackedVesselIDs.includes(vp.vessel.id)
? [128, 16, 189, 210]
: [16, 181, 16, 210]
},
Expand All @@ -97,16 +91,14 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
},
})

// TODO(CT): call backend
const tracksByVesselAndVoyage = trackedVesselMMSIs.map(
(trackedVesselMMSI) => {
return new GeoJsonLayer<VesselTrailPropertiesType>({
id: `${trackedVesselMMSI}_vessel_trail_${layerKey}`,
data: `../../../data/geometries/segments_by_vessel_mmsi/${trackedVesselMMSI}_segments.geo.json`,
getFillColor: ({ properties }) => getColorFromValue(properties.speed),
getLineColor: ({ properties }) => {
return getColorFromValue(properties.speed)
},
const tracksByVesselAndVoyage = trackedVesselSegments
.map(segments => toSegmentsGeo(segments))
.map((segmentsGeo: VesselExcursionSegmentsGeo) => {
return new GeoJsonLayer<VesselExcursionSegmentGeo>({
id: `${segmentsGeo.vesselId}_vessel_trail_${layerKey}`,
data: segmentsGeo,
getFillColor: (properties) => getColorFromValue(properties?.speed),
getLineColor: (properties) => getColorFromValue(properties?.speed),
pickable: false,
stroked: false,
filled: true,
Expand All @@ -117,9 +109,8 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
lineWidthScale: 2,
getPointRadius: 4,
getTextSize: 12,
})
}
)
})
});

const positions_mesh_layer = new SimpleMeshLayer({
id: `vessels-positions-mesh-layer-${layerKey}`,
Expand All @@ -130,8 +121,8 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
vp?.position?.coordinates[1],
],
getColor: (vp: VesselPosition) => {
return vp.vessel.mmsi === activePosition?.vessel.mmsi ||
trackedVesselMMSIs.includes(vp.vessel.mmsi)
return vp.vessel.id === activePosition?.vessel.id ||
trackedVesselIDs.includes(vp.vessel.id)
? [128, 16, 189, 210]
: [16, 181, 16, 210]
},
Expand All @@ -154,7 +145,7 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
...viewState,
longitude: object?.position?.coordinates[0],
latitude: object?.position?.coordinates[1],
zoom: 10,
zoom: 7,
transitionInterpolator: new FlyToInterpolator({ speed: 2 }),
transitionDuration: "auto",
})
Expand Down Expand Up @@ -193,10 +184,27 @@ export default function CoreMap({ vesselsPositions }: CoreMapProps) {
: null
}
>
<Map
<MapGL
mapStyle={`https://api.maptiler.com/maps/bb513c96-848e-4775-b150-437395193f26/style.json?key=${process.env.NEXT_PUBLIC_MAPTILER_TO}`}
attributionControl={false}
></Map>
></MapGL>
</DeckGL>
)
}
function toSegmentsGeo({ segments, vesselId }: VesselExcursionSegments): any {
const segmentsGeo = segments.map((segment: VesselExcursionSegment) => {
return {
speed: segment.average_speed,
navigational_status: "unknown",
geometry: {
type: "LineString",
coordinates: [
segment.start_position.coordinates,
segment.end_position.coordinates
]
}
}
})
return { vesselId, type: "FeatureCollection", features: segmentsGeo };
}

35 changes: 17 additions & 18 deletions frontend/components/core/map/preview-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Button } from "@/components/ui/button"
import IconButton from "@/components/ui/icon-button"
import { useMapStore } from "@/components/providers/map-store-provider"
import { VesselPosition } from "@/types/vessel"
import { getVesselFirstExcursionSegments } from "@/services/backend-rest-client"

export interface PreviewCardTypes {
vesselInfo: VesselPosition
Expand All @@ -14,24 +15,22 @@ export interface PreviewCardTypes {
const PreviewCard: React.FC<PreviewCardTypes> = ({ vesselInfo }) => {
const {
setActivePosition,
addTrackedVesselMMSI,
trackedVesselMMSIs,
removeTrackedVesselMMSI,
addTrackedVessel,
trackedVesselIDs,
removeTrackedVessel,
} = useMapStore((state) => state)
const { vessel: { id, mmsi, ship_name, imo, length }, timestamp } = vesselInfo
const isVesselTracked = (mmsi: number) => {
return trackedVesselMMSIs.includes(mmsi)
const { vessel: { id: vesselId, mmsi, ship_name, imo, length }, timestamp } = vesselInfo
const isVesselTracked = (vesselId: number) => {
return trackedVesselIDs.includes(vesselId)
}
console.log(id)

// useEffect(() => {
// console.log("trackedVesselMMSIs", trackedVesselMMSIs)
// }, [trackedVesselMMSIs])

const handleDisplayTrail = (vessel_mmsi: number) => {
isVesselTracked(vessel_mmsi)
? removeTrackedVesselMMSI(vessel_mmsi)
: addTrackedVesselMMSI(vessel_mmsi)
const handleDisplayTrail = async (vesselId: number) => {
if (isVesselTracked(vesselId)) {
removeTrackedVessel(vesselId);
return;
}
const response = await getVesselFirstExcursionSegments(vesselId);
addTrackedVessel(vesselId, response.data);
}
return (
<div className="flex w-wrap flex-col rounded-t-lg bg-white shadow hover:bg-gray-100 dark:border-gray-700 dark:bg-gray-800 dark:hover:bg-gray-700 md:max-w-xl md:flex-row">
Expand Down Expand Up @@ -71,10 +70,10 @@ const PreviewCard: React.FC<PreviewCardTypes> = ({ vesselInfo }) => {
</p>
</section>
<section id="vessel-actions">
<Button onClick={() => handleDisplayTrail(mmsi)}>
{isVesselTracked(mmsi) ? "Hide" : "Display"} track
<Button onClick={() => handleDisplayTrail(vesselId)}>
{isVesselTracked(vesselId) ? "Hide" : "Display"} track
</Button>
{isVesselTracked(mmsi) && <Link href="#" className="ml-2">Show track details</Link>}
{isVesselTracked(vesselId) && <Link href="#" className="ml-2">Show track details</Link>}
</section>

<div className="absolute right-0 top-0 pt-4 pr-3">
Expand Down
10 changes: 5 additions & 5 deletions frontend/components/core/tracked-vessels-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function TrackedVesselsPanel({
parentIsOpen,
openParent,
}: Props) {
const { trackedVesselMMSIs, removeTrackedVesselMMSI } = useMapStore((state) => state);
const { trackedVesselIDs, removeTrackedVessel } = useMapStore((state) => state);
const { vessels: allVessels } = useVesselsStore((state) => state);
const [displayTrackedVessels, setDisplayTrackedVessels] = useState(false)
const [trackedVesselsDetails, setTrackedVesselsDetails] = useState<Vessel[]>()
Expand All @@ -31,10 +31,10 @@ export default function TrackedVesselsPanel({

useEffect(() => {
const vesselsDetails = allVessels.filter((vessel) =>
trackedVesselMMSIs.includes(vessel.mmsi)
trackedVesselIDs.includes(vessel.id)
)
setTrackedVesselsDetails(vesselsDetails)
}, [trackedVesselMMSIs])
}, [trackedVesselIDs])

return (
<>
Expand All @@ -45,7 +45,7 @@ export default function TrackedVesselsPanel({
>
<ShipIcon className="w-8 min-w-8" />
{wideMode && (
<span className="ml-2">{`Selected vessel (${trackedVesselMMSIs.length})`}</span>
<span className="ml-2">{`Selected vessel (${trackedVesselIDs.length})`}</span>
)}
</button>

Expand All @@ -62,7 +62,7 @@ export default function TrackedVesselsPanel({
</div>
<button
className="block"
onClick={() => removeTrackedVesselMMSI(vessel.mmsi)}
onClick={() => removeTrackedVessel(vessel.id)}
>
<X size={15} color="#2CE2B0" strokeWidth={2} />
</button>
Expand Down
82 changes: 0 additions & 82 deletions frontend/mock-data-dashboard.json
rv2931 marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

Loading
Loading