Skip to content
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
104 changes: 72 additions & 32 deletions src/features/pin/components/PinListSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import { SortTabs } from '@/features/pin/components/SortTabs';
import { usePlaceDetail, useTogglePlaceBookmark } from '@/features/pin/queries/usePlaceBookmark';
import { usePlaceTrack } from '@/features/pin/queries/usePlaceTrack';
import type { Pin, PinSort, PlaceInfo } from '@/features/pin/types';
import { useCurrentPosition } from '@/hooks/useCurrentPosition';
import { cn } from '@/lib/utils';
import type { PlaceSearchHistoryRequest } from '@/types/place.type';

type BookmarkStatus = 'loading' | 'error' | 'ready';

type PinListSheetProps = {
open: boolean;
onClose: () => void;
place: PlaceInfo;
detailLocation: PlaceSearchHistoryRequest | null;
detailLocationError?: string | null;
onPinClick?: (pin: Pin) => void;
};

Expand Down Expand Up @@ -43,6 +47,8 @@ type PinListContentProps = {
sort: PinSort;
onSortChange: (sort: PinSort) => void;
isBookmarked: boolean;
bookmarkStatus: BookmarkStatus;
detailErrorMessage: string | null;
isBookmarkPending: boolean;
onBookmarkToggle: () => void;
onPinClick?: (pin: Pin) => void;
Expand All @@ -54,13 +60,21 @@ function PinListContent({
sort,
onSortChange,
isBookmarked,
bookmarkStatus,
detailErrorMessage,
isBookmarkPending,
onBookmarkToggle,
onPinClick,
}: PinListContentProps) {
const { isFullPage } = useBottomSheet();
const distance = formatDistance(place.distance);
const hasPins = pins.length > 0;
const bookmarkLabel =
bookmarkStatus === 'loading'
? '북마크 상태 불러오는 중'
: bookmarkStatus === 'error'
? '북마크 상태를 불러오지 못함'
: `${place.name} ${isBookmarked ? '북마크 해제' : '북마크 등록'}`;

return (
<>
Expand All @@ -77,9 +91,16 @@ function PinListContent({

<div className="min-w-0">
<BottomSheet.Title className="block truncate head-24-sb text-grayscale-100">
{place.name}
{place.name ||
(detailErrorMessage
? '장소 정보를 불러올 수 없어요'
: '장소 정보를 불러오고 있어요')}
</BottomSheet.Title>
{place.address ? (
{detailErrorMessage ? (
<p role="alert" className="body-15-r text-red">
{detailErrorMessage}
</p>
) : place.address ? (
<p className="truncate body-15-r text-grayscale-500">{place.address}</p>
) : null}
</div>
Expand All @@ -103,14 +124,20 @@ function PinListContent({

<button
type="button"
aria-label={`${place.name} ${isBookmarked ? '북마크 해제' : '북마크 등록'}`}
aria-pressed={isBookmarked}
aria-busy={isBookmarkPending || undefined}
disabled={isBookmarkPending}
aria-label={bookmarkLabel}
aria-pressed={bookmarkStatus === 'ready' ? isBookmarked : undefined}
aria-busy={bookmarkStatus === 'loading' || isBookmarkPending || undefined}
disabled={bookmarkStatus !== 'ready' || isBookmarkPending}
onClick={onBookmarkToggle}
className="flex size-11 shrink-0 items-center justify-center rounded-full bg-pli-black-75 disabled:cursor-not-allowed disabled:opacity-60"
>
{isBookmarked ? (
{bookmarkStatus === 'loading' ? (
<span aria-hidden className="size-6 animate-pulse rounded-md bg-grayscale-700" />
) : bookmarkStatus === 'error' ? (
<span aria-hidden className="body-15-m text-grayscale-500">
!
</span>
) : isBookmarked ? (
<BookmarkActiveIcon className="size-7" aria-hidden />
) : (
<BookmarkIcon className="size-7" aria-hidden />
Expand Down Expand Up @@ -152,43 +179,63 @@ type BookmarkToast = {
message: string;
};

export function PinListSheet({ open, onClose, place, onPinClick }: PinListSheetProps) {
export function PinListSheet({
open,
onClose,
place,
detailLocation,
detailLocationError = null,
onPinClick,
}: PinListSheetProps) {
const [sort, setSort] = useState<PinSort>('POPULAR');
const [bookmarkToast, setBookmarkToast] = useState<BookmarkToast | null>(null);
const normalizedPlaceId = place.id.startsWith('place:')
? place.id.slice('place:'.length)
: place.id;
const parsedPlaceId = place.placeId ?? Number(normalizedPlaceId);
const placeId = Number.isSafeInteger(parsedPlaceId) && parsedPlaceId > 0 ? parsedPlaceId : null;
const { data: currentPosition } = useCurrentPosition({ enabled: open });
const queryLatitude = currentPosition?.latitude ?? place.latitude;
const queryLongitude = currentPosition?.longitude ?? place.longitude;
const queryLatitude = detailLocation?.latitude ?? 0;
const queryLongitude = detailLocation?.longitude ?? 0;
const placeDetailQuery = usePlaceDetail({
placeId,
latitude: queryLatitude,
longitude: queryLongitude,
enabled: open,
latitude: detailLocation?.latitude ?? 0,
longitude: detailLocation?.longitude ?? 0,
enabled: open && detailLocation !== null,
});
const resolvedPlace: PlaceInfo = {
...place,
name: placeDetailQuery.data?.placeName ?? place.name,
distance: placeDetailQuery.data?.distanceMeters ?? place.distance,
address: placeDetailQuery.data
? placeDetailQuery.data.roadAddress || placeDetailQuery.data.address
: place.address,
isMine: placeDetailQuery.data?.pinnedByMe ?? place.isMine,
};
const bookmarkMutation = useTogglePlaceBookmark();
const resolvedBookmarkState =
placeDetailQuery.data?.bookmarkedByMe ?? place.bookmarkedByMe ?? false;
const resolvedBookmarkState = placeDetailQuery.data?.bookmarkedByMe ?? place.bookmarkedByMe;
const detailErrorMessage =
detailLocation === null
? detailLocationError
: placeDetailQuery.isError
? '장소 정보를 불러오지 못했어요. 잠시 후 다시 시도해 주세요.'
: null;
const bookmarkStatus: BookmarkStatus =
resolvedBookmarkState !== undefined ? 'ready' : detailErrorMessage ? 'error' : 'loading';
const isCurrentPlaceMutation = bookmarkMutation.variables?.placeId === placeId;
const isBookmarked =
isCurrentPlaceMutation && bookmarkMutation.isPending
? bookmarkMutation.variables.bookmarked
: resolvedBookmarkState;
const isBookmarkStateLoading =
open && place.bookmarkedByMe === undefined && placeDetailQuery.isPending;
: (resolvedBookmarkState ?? false);
const { data } = usePlaceTrack({
placeId: normalizedPlaceId,
latitude: queryLatitude,
longitude: queryLongitude,
sort: sort === 'LATEST' ? 'LATEST' : 'POPULAR',
enabled: open,
enabled: open && detailLocation !== null,
});

const handleBookmarkToggle = () => {
if (placeId === null || bookmarkMutation.isPending || isBookmarkStateLoading) return;
if (placeId === null || bookmarkMutation.isPending || bookmarkStatus !== 'ready') return;

bookmarkMutation.mutate(
{ placeId, bookmarked: !isBookmarked },
Expand All @@ -212,15 +259,6 @@ export function PinListSheet({ open, onClose, place, onPinClick }: PinListSheetP
liked: track.isLiked,
})) ?? [];

// 호출부가 name/address/distance를 정확히 모르고 열 수도 있어서(예: 지도 핀
// 탭), 실제 장소 상세 조회 결과가 도착하면 그 값으로 덮어써서 채운다.
const resolvedPlace: PlaceInfo = {
...place,
name: placeDetailQuery.data?.placeName ?? place.name,
address: placeDetailQuery.data?.address ?? place.address,
distance: placeDetailQuery.data?.distanceMeters ?? place.distance,
};

return (
<ToastProvider duration={BOOKMARK_TOAST_DURATION_MS}>
<BottomSheet
Expand All @@ -236,7 +274,9 @@ export function PinListSheet({ open, onClose, place, onPinClick }: PinListSheetP
sort={sort}
onSortChange={setSort}
isBookmarked={isBookmarked}
isBookmarkPending={bookmarkMutation.isPending || isBookmarkStateLoading}
bookmarkStatus={bookmarkStatus}
detailErrorMessage={detailErrorMessage}
isBookmarkPending={bookmarkMutation.isPending}
onBookmarkToggle={handleBookmarkToggle}
onPinClick={onPinClick}
/>
Expand Down
15 changes: 11 additions & 4 deletions src/features/pin/components/PinPlaceSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,17 @@ export function PinPlaceSearch({
};

if (!place.searchSource || !currentLocation) {
const constraintError = validatePlace?.(place) ?? null;
const selectedPlace = currentLocation
? { ...place, selectionLocation: currentLocation }
: place;
const constraintError = validatePlace?.(selectedPlace) ?? null;
if (constraintError) {
reportConstraintError(constraintError);
return;
}

setQuery(place.placeName);
onPlaceSelect(place);
onPlaceSelect(selectedPlace);
return;
}

Expand All @@ -188,12 +191,16 @@ export function PinPlaceSearch({
{
onSuccess: (selectedPlaceResult) => {
setQuery(selectedPlaceResult.placeName);
const constraintError = validatePlace?.(selectedPlaceResult) ?? null;
const selectedPlace = {
...selectedPlaceResult,
selectionLocation: currentLocation,
};
const constraintError = validatePlace?.(selectedPlace) ?? null;
if (constraintError) {
reportConstraintError(constraintError);
return;
}
onPlaceSelect(selectedPlaceResult);
onPlaceSelect(selectedPlace);
},
onSettled: () => {
if (selectionControllerRef.current === controller) {
Expand Down
3 changes: 2 additions & 1 deletion src/features/pin/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { PlaceSearchSource } from '@/types/place.type';
import type { PlaceSearchHistoryRequest, PlaceSearchSource } from '@/types/place.type';

// --------------------------------------------------

Expand Down Expand Up @@ -218,6 +218,7 @@ export type PinSearchPlace = PlaceResult & {
isMine?: boolean;
source?: 'PLACE_SEARCH' | 'ADDRESS_SEARCH' | 'MAP_SELECTION';
withinAccessRange?: boolean;
selectionLocation?: PlaceSearchHistoryRequest;
coordinates: {
lat: number;
lng: number;
Expand Down
22 changes: 21 additions & 1 deletion src/pages/MapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const MapPage: React.FC<MapPageProps> = ({
hasApiKey ? null : '지도를 불러올 수 없어요. 잠시 후 다시 시도해주세요.',
);
const [currentLocation, setCurrentLocation] = useState<MapCoordinate | null>(null);
const [currentLocationError, setCurrentLocationError] = useState<string | null>(null);
const [registrationToast, setRegistrationToast] = useState<RegistrationToast | null>(null);
const [viewport, setViewport] = useState<MapViewport | null>(null);
const { data: mapPinsData } = useMapPins(viewport);
Expand Down Expand Up @@ -161,6 +162,11 @@ const MapPage: React.FC<MapPageProps> = ({
setZoom(newZoom);
};

const handleCurrentLocationChanged = (coordinate: MapCoordinate) => {
setCurrentLocation(coordinate);
setCurrentLocationError(null);
};

// --- 지도 빈 영역 탭: 핀 탭으로 연 시트 닫기 ---
const handleMapClick = () => {
onSelectMapPinChange(null);
Expand Down Expand Up @@ -273,13 +279,26 @@ const MapPage: React.FC<MapPageProps> = ({
open={isPlaceSheetOpen}
onClose={() => onClearMapPlace?.()}
place={toPlaceInfo(selectedMapPlace)}
detailLocation={
selectedMapPlace.selectionLocation ??
(currentLocation
? { latitude: currentLocation.lat, longitude: currentLocation.lng }
: null)
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
detailLocationError={currentLocationError}
onPinClick={(pin) => navigate(`/app/pins/${pin.placeTrackId}`)}
/>
) : selectedMapPin ? (
<PinListSheet
open
onClose={() => onSelectMapPinChange(null)}
place={mapPinToPlaceInfo(selectedMapPin)}
detailLocation={
currentLocation
? { latitude: currentLocation.lat, longitude: currentLocation.lng }
: null
}
detailLocationError={currentLocationError}
onPinClick={(pin) => navigate(`/app/pins/${pin.placeTrackId}`)}
/>
) : null}
Expand All @@ -294,7 +313,8 @@ const MapPage: React.FC<MapPageProps> = ({
selectedMapPinId={selectedMapPlace ? null : displayedMapPinId}
centerOnFirstLocation={!selectedMapPlace}
onZoomChanged={handleZoomChange}
onCurrentLocationChanged={setCurrentLocation}
onCurrentLocationChanged={handleCurrentLocationChanged}
onCurrentLocationError={setCurrentLocationError}
onViewportChanged={setViewport}
onSelectMapPin={onSelectMapPinChange}
onMapClick={handleMapClick}
Expand Down
Loading