Skip to content
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
6 changes: 5 additions & 1 deletion app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from "react";
import React, { useRef, useState } from "react";
import { View, StyleSheet, Alert } from "react-native";
import type { Region } from "react-native-maps";
import MapView, { Circle, PROVIDER_GOOGLE } from "react-native-maps";
Expand All @@ -8,6 +8,7 @@ import useSupercluster from "use-supercluster";
import MarkerWithWrapper from "@/components/MarkerWithWrapper";
import jobs from "@/constants/jobs.json";
import { SearchJobAdRo } from "@/models";
import useTrackChanges from "@/hooks/useTrackChanges";

export interface PointProperties {
cluster: boolean;
Expand Down Expand Up @@ -120,6 +121,7 @@ const Map = () => {
},
});

const { renderCount, shouldTrack } = useTrackChanges(points);
function onPointPress() {
Alert.alert(`Clicked on point!`);
}
Expand All @@ -134,6 +136,8 @@ const Map = () => {
onPointPress={onPointPress}
point={point}
zoom={zoom}
shouldTrack={shouldTrack}
renderCount={renderCount}
></MarkerWithWrapper>
);
});
Expand Down
18 changes: 15 additions & 3 deletions components/Marker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import React from "react";
import { Image, Text, View } from "react-native";
import React, { Dispatch, MutableRefObject } from "react";
import { Image, Platform, View } from "react-native";

import { MapMarker } from "react-native-maps";
import styles from "./styles";

interface Props {
companyLogo: string | null;
renderCount: MutableRefObject<number>;
}

export const Marker = ({ companyLogo }: Props) => {
export const Marker = ({ companyLogo, renderCount }: Props) => {
const onLoadEnd = () => {
renderCount.current++;
};

const onError = () => {
renderCount.current++;
};
return (
<View
style={[
Expand All @@ -23,6 +32,9 @@ export const Marker = ({ companyLogo }: Props) => {
style={[styles.logo]}
resizeMode="contain"
source={{ uri: companyLogo }}
onLoadEnd={onLoadEnd}
fadeDuration={0}
onError={onError}
/>
) : (
<Image
Expand Down
12 changes: 8 additions & 4 deletions components/MarkerWithWrapper/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { MutableRefObject, useRef } from "react";
import type { MapMarker } from "react-native-maps";
import { Marker } from "react-native-maps";
import { Marker as MarkerMap } from "@/components/Marker";
Expand All @@ -10,13 +10,16 @@ type Props = {
onPointPress: () => void;
isSelected: boolean;
zoom: number;

shouldTrack: boolean;
renderCount: MutableRefObject<number>;
};

export default function MarkerWithWrapper({
point,
onPointPress,
zoom,
isSelected,
shouldTrack,
renderCount,
}: Props) {
const coordinates = point.geometry.coordinates;
const markerRef = useRef<MapMarker>(null);
Expand All @@ -29,7 +32,7 @@ export default function MarkerWithWrapper({
return (
<Marker
ref={markerRef}
tracksViewChanges={true}
tracksViewChanges={shouldTrack}
coordinate={{
latitude: lat,
longitude: lng,
Expand All @@ -38,6 +41,7 @@ export default function MarkerWithWrapper({
>
<MarkerMap
companyLogo={job.company.logoImg?.variants.min_dim_64_url ?? null}
renderCount={renderCount}
/>
</Marker>
);
Expand Down
20 changes: 20 additions & 0 deletions hooks/useTrackChanges.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { PointWithProperties } from "@/app";
import { countNonNullMinDim64Urls } from "@/utils/map";
import React, { useEffect, useRef, useState } from "react";

export default function useTrackChanges(points: PointWithProperties[]) {
const renderCount = useRef(0);

const [shouldTrack, setShouldTrack] = useState(true);
useEffect(() => {
const nonNullURI = countNonNullMinDim64Urls(points);
if (nonNullURI <= renderCount.current) {
setShouldTrack(false);
}
}, [renderCount.current]);

return {
shouldTrack,
renderCount,
};
}
12 changes: 12 additions & 0 deletions utils/map.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PointWithProperties } from "@/app";

export function countNonNullMinDim64Urls(points: PointWithProperties[]) {
let count = 0;
for (let obj of points) {
const url = obj.properties.job?.company?.logoImg?.variants?.min_dim_64_url;
if (url) {
count++;
}
}
return count;
}