-
Notifications
You must be signed in to change notification settings - Fork 26
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
fix(feed): boundaries and zoom for map feed #1399
Changes from 4 commits
3448435
3529fa4
773a6c6
b63bc03
b42b56d
066f8cc
8835c32
4c743ac
7048c47
49d4069
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
import "./styles.css"; | ||
import "leaflet/dist/leaflet.css"; | ||
import { DivIcon, LatLngBounds, point, PointExpression } from "leaflet"; | ||
import { | ||
DivIcon, | ||
LatLngBounds, | ||
LatLngBoundsLiteral, | ||
point, | ||
PointExpression, | ||
} from "leaflet"; | ||
import { | ||
Dispatch, | ||
FC, | ||
|
@@ -30,6 +36,7 @@ import { | |
getMapPostIconColorRgba, | ||
getMapPostIconSVGString, | ||
MAP_LAYER_URL, | ||
MAP_MAX_BOUND, | ||
} from "@/utils/feed/map"; | ||
import { zodTryParseJSON } from "@/utils/sanitize"; | ||
import { | ||
|
@@ -103,6 +110,43 @@ const MapManager = ({ | |
return null; | ||
}; | ||
|
||
const DynamicMinZoom = ({ | ||
setMinZoom, | ||
}: { | ||
setMinZoom: Dispatch<SetStateAction<number>>; | ||
}) => { | ||
const map = useMap(); | ||
|
||
useEffect(() => { | ||
const lat = 45; | ||
const lng = 90; | ||
|
||
const adjustedWorldMapBounds: LatLngBoundsLiteral = [ | ||
[-lat, -lng], | ||
[lat, lng], | ||
]; | ||
|
||
const updateMinZoom = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not defined the function inside of useEffect which makes the code less readable. I suggest sth like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I disagree, we followed this in berty and it made the code very hard to maintain IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can extract a hook though if you really want
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed here: 066f8cc |
||
if (map) { | ||
const calculatedZoom = map.getBoundsZoom(adjustedWorldMapBounds, false); | ||
setMinZoom(calculatedZoom); | ||
map.setMinZoom(calculatedZoom); | ||
map.setZoom(calculatedZoom); | ||
} | ||
}; | ||
|
||
updateMinZoom(); | ||
|
||
window.addEventListener("resize", updateMinZoom); | ||
|
||
return () => { | ||
window.removeEventListener("resize", updateMinZoom); | ||
}; | ||
}, [map, setMinZoom]); | ||
|
||
return null; | ||
}; | ||
|
||
export const Map: FC<MapProps> = ({ | ||
consultedPostId, | ||
style, | ||
|
@@ -111,6 +155,7 @@ export const Map: FC<MapProps> = ({ | |
}) => { | ||
const selectedNetworkId = useSelectedNetworkId(); | ||
const [bounds, setBounds] = useState<LatLngBounds | null>(null); | ||
const [minZoom, setMinZoom] = useState(2); | ||
|
||
// Fetch the consulted post | ||
const { post: consultedPost } = usePost(consultedPostId); | ||
|
@@ -197,6 +242,7 @@ export const Map: FC<MapProps> = ({ | |
width: "100%", | ||
height: "100%", | ||
alignSelf: "center", | ||
maxHeight: 1000, | ||
}, | ||
style, | ||
]} | ||
|
@@ -205,8 +251,12 @@ export const Map: FC<MapProps> = ({ | |
center={ | ||
consultedPostLocation || creatingPostLocation || DEFAULT_MAP_POSITION | ||
} | ||
zoom={12} | ||
zoom={minZoom} | ||
minZoom={minZoom} | ||
attributionControl={false} | ||
maxBounds={MAP_MAX_BOUND} | ||
maxBoundsViscosity={1.0} | ||
style={{ width: "100%", height: "100%" }} | ||
> | ||
{/*----Loads and displays tiles on the map*/} | ||
<TileLayer noWrap attribution="" url={MAP_LAYER_URL} /> | ||
|
@@ -280,6 +330,7 @@ export const Map: FC<MapProps> = ({ | |
creatingPostLocation={creatingPostLocation} | ||
consultedPostLocation={consultedPostLocation} | ||
/> | ||
<DynamicMinZoom setMinZoom={setMinZoom} /> | ||
</MapContainer> | ||
</View> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part is independent of the Component so we should put it outside of the component and we can use UPPER_CASE to define the constants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't follow this convention, please use normal variable naming as in js, there is no real constants
we can put it outside of the func though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed here: b42b56d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO, if it's a constant and declared outside of function scope, it should be a UPPER_CASE.
if it's just a keyword const and declared in function scope, it's ok to define it with normal case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const in js don't exist

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this makes refacto harder if the variable stops being a constant according to your definition
also we don't have a way to enforce this rule so this means more review work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the rust language it make more sense because constant are real constant and thus when you see UPPER_CASE, you have some guarantees. Even then it doesn't have much value IMO but at least it can be verified by tooling.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can vote on this