Skip to content

Commit 1ea6e2b

Browse files
Merge pull request #771 from isucon/fix-near-by-chair-distance
[FE] nearByChairで表示するdistance領域を画面幅から正確に計算する
2 parents 2043560 + 2e318d0 commit 1ea6e2b

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

frontend/app/components/modules/map/map.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ChairIcon } from "~/components/icon/chair";
1717
import { PinIcon } from "~/components/icon/pin";
1818
import { Button } from "~/components/primitives/button/button";
1919
import { Text } from "~/components/primitives/text/text";
20-
import type { Coordinate, DisplayPos, NearByChair } from "~/types";
20+
import type { Coordinate, DisplayPos, Distance, NearByChair } from "~/types";
2121
import { CityObjects, TownList } from "./map-data";
2222

2323
const GridDistance = 20;
@@ -51,6 +51,15 @@ const centerPosFrom = (pos: DisplayPos, outerRect: DOMRect): DisplayPos => {
5151
};
5252
};
5353

54+
const displaySizeToDistance = (rect: DOMRect): Distance => {
55+
const startPos = posToCoordinate({ x: 0, y: 0 });
56+
const endPos = posToCoordinate({ x: rect.right, y: rect.bottom });
57+
return {
58+
horizontalDistance: startPos.latitude - endPos.latitude,
59+
verticalDistance: startPos.longitude - endPos.longitude,
60+
};
61+
};
62+
5463
const draw = (
5564
ctx: CanvasRenderingContext2D,
5665
option: { from?: Coordinate; to?: Coordinate },
@@ -334,6 +343,7 @@ const TownLayer = memo(function TownLayer() {
334343

335344
type MapProps = ComponentProps<"div"> & {
336345
onMove?: (coordinate: Coordinate) => void;
346+
onUpdateViewSize?: (distance: Distance) => void;
337347
selectable?: boolean;
338348
selectorPinColor?: `#${string}`;
339349
from?: Coordinate;
@@ -346,13 +356,15 @@ export const Map: FC<MapProps> = ({
346356
selectable,
347357
selectorPinColor,
348358
onMove,
359+
onUpdateViewSize,
349360
from,
350361
to,
351362
chairs,
352363
initialCoordinate,
353364
className,
354365
}) => {
355366
const onMoveRef = useRef(onMove);
367+
const onUpdateViewSizeRef = useRef(onUpdateViewSize);
356368
const outerRef = useRef<HTMLDivElement>(null);
357369
const canvasRef = useRef<HTMLCanvasElement>(null);
358370
const [isDrag, setIsDrag] = useState(false);
@@ -387,6 +399,12 @@ export const Map: FC<MapProps> = ({
387399
}
388400
}, []);
389401

402+
useEffect(() => {
403+
if (!outerRect) return;
404+
const distance = displaySizeToDistance(outerRect);
405+
onUpdateViewSizeRef.current?.(distance);
406+
}, [outerRect]);
407+
390408
useLayoutEffect(() => {
391409
updateViewLocation(initialCoordinate);
392410
}, [initialCoordinate, updateViewLocation]);

frontend/app/routes/client._index/route.tsx

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Button } from "~/components/primitives/button/button";
1616
import { Modal } from "~/components/primitives/modal/modal";
1717
import { Text } from "~/components/primitives/text/text";
1818
import { useClientContext } from "~/contexts/client-context";
19-
import { NearByChair } from "~/types";
19+
import type { Distance, NearByChair } from "~/types";
2020
import { sendClientReady, sendClientRideRequested } from "~/utils/post-message";
2121
import { Arrived } from "./driving-state/arrived";
2222
import { Carrying } from "./driving-state/carrying";
@@ -44,14 +44,19 @@ export default function Index() {
4444
const [selectedLocation, setSelectedLocation] = useState<Coordinate>();
4545
const [displayedChairs, setDisplayedChairs] = useState<NearByChair[]>([]);
4646
const [centerCoordinate, setCenterCoodirnate] = useState<Coordinate>();
47-
const onCenterMove = useCallback(
47+
const [distance, setDistance] = useState<Distance>();
48+
const onMove = useCallback(
4849
(coordinate: Coordinate) => setCenterCoodirnate(coordinate),
4950
[],
5051
);
5152
const onSelectMove = useCallback(
5253
(coordinate: Coordinate) => setSelectedLocation(coordinate),
5354
[],
5455
);
56+
const onUpdateViewSize = useCallback(
57+
(distance: Distance) => setDistance(distance),
58+
[],
59+
);
5560
const [isLocationSelectorModalOpen, setLocationSelectorModalOpen] =
5661
useState(false);
5762
const locationSelectorModalRef = useRef<HTMLElement & { close: () => void }>(
@@ -132,7 +137,12 @@ export default function Index() {
132137
queryParams: {
133138
latitude,
134139
longitude,
135-
distance: 150,
140+
distance: distance
141+
? Math.max(
142+
distance.horizontalDistance + 10,
143+
distance.verticalDistance + 10,
144+
)
145+
: 150,
136146
},
137147
});
138148
setDisplayedChairs(chairs);
@@ -155,7 +165,7 @@ export default function Index() {
155165
clearTimeout(timeoutId);
156166
abortController?.abort();
157167
};
158-
}, [centerCoordinate, isStatusModalOpen]);
168+
}, [centerCoordinate, isStatusModalOpen, distance]);
159169

160170
useEffect(() => {
161171
sendClientReady(window.parent, { ready: true });
@@ -170,7 +180,8 @@ export default function Index() {
170180
<Map
171181
from={currentLocation}
172182
to={destLocation}
173-
onMove={onCenterMove}
183+
onMove={onMove}
184+
onUpdateViewSize={onUpdateViewSize}
174185
initialCoordinate={selectedLocation}
175186
chairs={[...displayedChairs, ...emulateChairs]}
176187
className="flex-1"

frontend/app/types.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import type {
22
OwnerGetChairsResponse,
33
OwnerGetSalesResponse,
44
} from "./api/api-components";
5-
import type { Coordinate as ApiCoodinate } from "./api/api-schemas";
65

76
export type AccessToken = string;
87

@@ -16,10 +15,11 @@ export type ClientAppChair = {
1615
}>;
1716
};
1817

19-
export type DisplayPos = {
20-
x: number;
21-
y: number;
22-
};
18+
export type Coordinate = { latitude: number; longitude: number };
19+
20+
export type DisplayPos = { x: number; y: number };
21+
22+
export type Distance = { horizontalDistance: number; verticalDistance: number };
2323

2424
export type NearByChair = {
2525
id: string;
@@ -28,8 +28,6 @@ export type NearByChair = {
2828
current_coordinate: Coordinate;
2929
};
3030

31-
export type Coordinate = ApiCoodinate;
32-
3331
export type CampaignData = {
3432
invitationCode: string;
3533
registedAt: string;

0 commit comments

Comments
 (0)