All files / src/components/pages/group/group-descriptions/description-sections/description-map index.tsx

0% Statements 0/48
0% Branches 0/1
0% Functions 0/1
0% Lines 0/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                                                                 
'use client';

import { useEffect, useState } from 'react';
import { Map, MapMarker, useKakaoLoader } from 'react-kakao-maps-sdk';

import { EmptyState } from '@/components/layout/empty-state';
import { GetGroupDetailsResponse } from '@/types/service/group';

interface Props {
  location: GetGroupDetailsResponse['address']['location'];
}

export const DescriptionMap = ({ location }: Props) => {
  const [coords, setCoords] = useState({ lat: 33.450701, lng: 126.570667 });

  const [loading, error] = useKakaoLoader({
    appkey: process.env.NEXT_PUBLIC_KAKAOMAP_KEY as string,
    libraries: ['clusterer', 'drawing', 'services'],
  });

  useEffect(() => {
    // 카카오지도 SDK 로딩 끝나면 실행
    if (loading) return;

    const geocoder = new kakao.maps.services.Geocoder();

    geocoder.addressSearch(location, (result, status) => {
      if (status === kakao.maps.services.Status.OK) {
        setCoords({
          lat: parseFloat(result[0].y),
          lng: parseFloat(result[0].x),
        });
      }
    });
  }, [loading]);

  return (
    <div className='relative mt-6 h-60 w-full overflow-hidden rounded-xl border border-gray-300 bg-gray-100'>
      {!error ? (
        <Map id='map' className='h-60 w-full' center={coords} level={3}>
          <MapMarker position={coords} />
        </Map>
      ) : (
        <EmptyState>지도를 불러올 수 없습니다.</EmptyState>
      )}
    </div>
  );
};