|
1 |
| -import { AnimatePresence, motion } from "framer-motion"; |
2 |
| -import { Box, Rocket } from "lucide-react"; |
3 |
| -import { useState } from "react"; |
4 |
| -import { type MarkerProps, Marker, useMap } from "react-map-gl"; |
5 |
| -import { useMatches, useNavigate, useSearchParams } from "react-router"; |
6 |
| -import { useSharedCompareMode } from "~/components/device-detail/device-detail-box"; |
7 |
| -import { cn } from "~/lib/utils"; |
8 |
| -import { type Device } from "~/schema"; |
| 1 | +import { AnimatePresence, motion } from 'framer-motion' |
| 2 | +import { Box, Rocket } from 'lucide-react' |
| 3 | +import { useState } from 'react' |
| 4 | +import { type MarkerProps, Marker, useMap } from 'react-map-gl' |
| 5 | +import { useMatches, useNavigate, useSearchParams } from 'react-router' |
| 6 | +import { useGlobalCompareMode } from '~/components/device-detail/useGlobalCompareMode' |
| 7 | +import { cn } from '~/lib/utils' |
| 8 | +import { type Device } from '~/schema' |
9 | 9 |
|
10 | 10 | interface BoxMarkerProps extends MarkerProps {
|
11 |
| - device: Device; |
| 11 | + device: Device |
12 | 12 | }
|
13 | 13 |
|
14 | 14 | const getStatusColor = (device: Device) => {
|
15 |
| - if (device.status === "active") { |
16 |
| - if (device.exposure === "mobile") { |
17 |
| - return "bg-blue-100"; |
18 |
| - } |
19 |
| - return "bg-green-300"; |
20 |
| - } else if (device.status === "inactive") { |
21 |
| - return "bg-gray-100"; |
22 |
| - } else { |
23 |
| - return "bg-gray-100 opacity-50"; |
24 |
| - } |
25 |
| -}; |
| 15 | + if (device.status === 'active') { |
| 16 | + if (device.exposure === 'mobile') { |
| 17 | + return 'bg-blue-100' |
| 18 | + } |
| 19 | + return 'bg-green-300' |
| 20 | + } else if (device.status === 'inactive') { |
| 21 | + return 'bg-gray-100' |
| 22 | + } else { |
| 23 | + return 'bg-gray-100 opacity-50' |
| 24 | + } |
| 25 | +} |
26 | 26 |
|
27 | 27 | export default function BoxMarker({ device, ...props }: BoxMarkerProps) {
|
28 |
| - const navigate = useNavigate(); |
29 |
| - const matches = useMatches(); |
30 |
| - const { osem } = useMap(); |
31 |
| - const { compareMode, setCompareMode } = useSharedCompareMode(); |
| 28 | + const navigate = useNavigate() |
| 29 | + const matches = useMatches() |
| 30 | + const { osem } = useMap() |
| 31 | + const [compareMode, setCompareMode] = useGlobalCompareMode() |
32 | 32 |
|
33 |
| - const isFullZoom = osem && osem?.getZoom() >= 14; |
| 33 | + const isFullZoom = osem && osem?.getZoom() >= 14 |
34 | 34 |
|
35 |
| - const [isHovered, setIsHovered] = useState(false); |
36 |
| - const [searchParams] = useSearchParams(); |
| 35 | + const [isHovered, setIsHovered] = useState(false) |
| 36 | + const [searchParams] = useSearchParams() |
37 | 37 |
|
38 |
| - // calculate zIndex based on device status and hover |
39 |
| - const getZIndex = () => { |
40 |
| - if (isHovered) { |
41 |
| - return 30; |
42 |
| - } |
43 |
| - // priority to active devices |
44 |
| - if (device.status === "active") { |
45 |
| - return 20; |
46 |
| - } |
47 |
| - if (device.status === "inactive") { |
48 |
| - return 10; |
49 |
| - } |
| 38 | + // calculate zIndex based on device status and hover |
| 39 | + const getZIndex = () => { |
| 40 | + if (isHovered) { |
| 41 | + return 30 |
| 42 | + } |
| 43 | + // priority to active devices |
| 44 | + if (device.status === 'active') { |
| 45 | + return 20 |
| 46 | + } |
| 47 | + if (device.status === 'inactive') { |
| 48 | + return 10 |
| 49 | + } |
50 | 50 |
|
51 |
| - return 0; |
52 |
| - }; |
| 51 | + return 0 |
| 52 | + } |
53 | 53 |
|
54 |
| - return ( |
55 |
| - <Marker |
56 |
| - {...props} |
57 |
| - style={{ |
58 |
| - zIndex: getZIndex(), |
59 |
| - }} |
60 |
| - > |
61 |
| - <AnimatePresence mode="popLayout"> |
62 |
| - <motion.div |
63 |
| - className={cn( |
64 |
| - "group absolute flex w-fit cursor-pointer items-center rounded-full bg-white p-1 text-sm shadow hover:z-10 hover:shadow-lg", |
65 |
| - isFullZoom ? "-left-4 -top-4" : "-left-[10px] -top-[10px]", |
66 |
| - )} |
67 |
| - onClick={() => { |
68 |
| - if (searchParams.has("sensor")) { |
69 |
| - searchParams.delete("sensor"); |
70 |
| - } |
71 |
| - if (compareMode) { |
72 |
| - void navigate( |
73 |
| - `/explore/${matches[2].params.deviceId}/compare/${device.id}`, |
74 |
| - ); |
75 |
| - setCompareMode(false); |
76 |
| - return; |
77 |
| - } |
78 |
| - void navigate({ |
79 |
| - pathname: `${device.id}`, |
80 |
| - search: searchParams.toString(), |
81 |
| - }); |
82 |
| - }} |
83 |
| - onHoverStart={() => setIsHovered(true)} |
84 |
| - onHoverEnd={() => setIsHovered(false)} |
85 |
| - > |
86 |
| - <span |
87 |
| - className={cn( |
88 |
| - "relative rounded-full transition-colors", |
89 |
| - `${getStatusColor(device)} p-1`, |
90 |
| - )} |
91 |
| - > |
92 |
| - {device.exposure === "mobile" ? ( |
93 |
| - <Rocket className="text-black h-4 w-4" /> |
94 |
| - ) : ( |
95 |
| - <Box className="text-black h-4 w-4" /> |
96 |
| - )} |
97 |
| - {device.status === "active" ? ( |
98 |
| - <div |
99 |
| - className={cn( |
100 |
| - "absolute left-0 top-0 h-full w-full animate-ping rounded-full opacity-50", |
101 |
| - getStatusColor(device), |
102 |
| - )} |
103 |
| - /> |
104 |
| - ) : null} |
105 |
| - </span> |
106 |
| - {isFullZoom ? ( |
107 |
| - <motion.span |
108 |
| - layoutId={device.id} |
109 |
| - className="text-black max-w-[100px] overflow-hidden overflow-ellipsis whitespace-nowrap px-1 group-hover:max-w-fit group-hover:overflow-auto" |
110 |
| - initial={{ opacity: 0, translateX: -20 }} |
111 |
| - animate={{ opacity: 1, translateX: 0 }} |
112 |
| - exit={{ opacity: 0, translateX: -20 }} |
113 |
| - > |
114 |
| - {device.name} |
115 |
| - </motion.span> |
116 |
| - ) : null} |
117 |
| - </motion.div> |
118 |
| - </AnimatePresence> |
119 |
| - </Marker> |
120 |
| - ); |
| 54 | + return ( |
| 55 | + <Marker |
| 56 | + {...props} |
| 57 | + style={{ |
| 58 | + zIndex: getZIndex(), |
| 59 | + }} |
| 60 | + > |
| 61 | + <AnimatePresence mode="popLayout"> |
| 62 | + <motion.div |
| 63 | + className={cn( |
| 64 | + 'group absolute flex w-fit cursor-pointer items-center rounded-full bg-white p-1 text-sm shadow hover:z-10 hover:shadow-lg', |
| 65 | + isFullZoom ? '-left-4 -top-4' : '-left-[10px] -top-[10px]', |
| 66 | + )} |
| 67 | + onClick={() => { |
| 68 | + if (searchParams.has('sensor')) { |
| 69 | + searchParams.delete('sensor') |
| 70 | + } |
| 71 | + if (compareMode) { |
| 72 | + void navigate( |
| 73 | + `/explore/${matches[2].params.deviceId}/compare/${device.id}`, |
| 74 | + ) |
| 75 | + setCompareMode(false) |
| 76 | + return |
| 77 | + } |
| 78 | + void navigate({ |
| 79 | + pathname: `${device.id}`, |
| 80 | + search: searchParams.toString(), |
| 81 | + }) |
| 82 | + }} |
| 83 | + onHoverStart={() => setIsHovered(true)} |
| 84 | + onHoverEnd={() => setIsHovered(false)} |
| 85 | + > |
| 86 | + <span |
| 87 | + className={cn( |
| 88 | + 'relative rounded-full transition-colors', |
| 89 | + `${getStatusColor(device)} p-1`, |
| 90 | + )} |
| 91 | + > |
| 92 | + {device.exposure === 'mobile' ? ( |
| 93 | + <Rocket className="h-4 w-4 text-black" /> |
| 94 | + ) : ( |
| 95 | + <Box className="h-4 w-4 text-black" /> |
| 96 | + )} |
| 97 | + {device.status === 'active' ? ( |
| 98 | + <div |
| 99 | + className={cn( |
| 100 | + 'absolute left-0 top-0 h-full w-full animate-ping rounded-full opacity-50', |
| 101 | + getStatusColor(device), |
| 102 | + )} |
| 103 | + /> |
| 104 | + ) : null} |
| 105 | + </span> |
| 106 | + {isFullZoom ? ( |
| 107 | + <motion.span |
| 108 | + layoutId={device.id} |
| 109 | + className="max-w-[100px] overflow-hidden overflow-ellipsis whitespace-nowrap px-1 text-black group-hover:max-w-fit group-hover:overflow-auto" |
| 110 | + initial={{ opacity: 0, translateX: -20 }} |
| 111 | + animate={{ opacity: 1, translateX: 0 }} |
| 112 | + exit={{ opacity: 0, translateX: -20 }} |
| 113 | + > |
| 114 | + {device.name} |
| 115 | + </motion.span> |
| 116 | + ) : null} |
| 117 | + </motion.div> |
| 118 | + </AnimatePresence> |
| 119 | + </Marker> |
| 120 | + ) |
121 | 121 | }
|
0 commit comments