All files / lib/providers/LiveLocation index.tsx

0% Statements 0/23
0% Branches 0/8
0% Functions 0/9
0% Lines 0/23

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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                                                                                                                                     
import React from 'react';
import { useContentAnalyticsContext } from '@lib/providers/ContentAnalytics';
import { useFeature } from '@hackler/react-sdk';
import { FeatureFlag } from '@lib/feature';
 
export function LiveLocationProvider({ children }: { children: React.ReactNode }) {
  const { consumeEvent } = useContentAnalyticsContext();
  const timeoutRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
  const isEnable = useFeature(FeatureFlag.LIVE_LOCATION);
 
  const sendLocation = () => {
    timeoutRef.current = setTimeout(() => {
      navigator.geolocation.getCurrentPosition(
        async (props) => {
          const metadata: {
            latitude: string;
            longitude: string;
            altitude?: string;
          } = {
            latitude: `${props.coords.latitude}`,
            longitude: `${props.coords.longitude}`
          };
 
          if (props.coords.altitude) {
            metadata.altitude = `${props.coords.altitude}`;
          }
 
          await consumeEvent({
            eventType: 'LocationEvent',
            metadata
          });
 
          sendLocation();
        },
        () => {
          console.error('상시 위치 정보를 받을 수 없습니다!');
        }
      );
    }, 1000 * 10);
  };
 
  React.useEffect(() => {
    if (!isEnable)
      return () => {
        // pass
      };
 
    if (!navigator.geolocation) {
      console.log('위치 정보를 받을 수 없는 브라우저입니다!');
      return () => {
        // pass
      };
    }
 
    sendLocation();
 
    return () => {
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current);
      }
    };
  }, [isEnable]);
 
  // eslint-disable-next-line react/jsx-no-useless-fragment
  return <>{children}</>;
}