Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE][Fix] QA #448

Merged
merged 1 commit into from
Dec 5, 2024
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
41 changes: 6 additions & 35 deletions frontend/src/hooks/getUserLocation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react';
import { useEffect, useState } from 'react';

interface IGetUserLocation {
lat: number | null;
Expand All @@ -7,10 +7,6 @@ interface IGetUserLocation {
error: string | null;
}

interface IGetUserLocationRes extends IGetUserLocation {
updateLocation: () => void;
}

export interface IDeviceOrientationEventWithPermission extends DeviceOrientationEvent {
requestPermission?: () => Promise<'granted' | 'denied'>;
}
Expand All @@ -37,37 +33,14 @@ export interface IDeviceOrientationEventWithPermission extends DeviceOrientation
* ```
*/

export const getUserLocation = (): IGetUserLocationRes => {
export const getUserLocation = (): IGetUserLocation => {
const [location, setLocation] = useState<IGetUserLocation>({
lat: null,
lng: null,
alpha: null,
error: null,
});

const updateLocation = useCallback(() => {
navigator.geolocation.getCurrentPosition(
position => {
setLocation(prev => ({
...prev,
lat: position.coords.latitude,
lng: position.coords.longitude,
error: null,
}));
},
error => {
setLocation(prev => ({ ...prev, error: error.message }));
},
);

window.addEventListener('deviceorientation', event => {
setLocation(prev => ({
...prev,
alpha: event.alpha ?? null,
}));
});
}, []);

useEffect(() => {
let watchId: number;

Expand Down Expand Up @@ -134,17 +107,15 @@ export const getUserLocation = (): IGetUserLocationRes => {
}
};

requestOrientationPermission();
requestOrientationPermission().then(() => {
window.addEventListener('deviceorientation', handleOrientation);
});

return () => {
if (watchId) navigator.geolocation.clearWatch(watchId);
window.removeEventListener('deviceorientation', handleOrientation);
};
}, []);

useEffect(() => {
updateLocation();
}, [updateLocation]);

return { ...location, updateLocation };
return location;
};
40 changes: 29 additions & 11 deletions frontend/src/pages/GuestView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import { IPoint } from '@/lib/types/canvasInterface.ts';
import { guestEntity } from '@/api/dto/channel.dto.ts';
import { GuestMarker } from '@/component/IconGuide/GuestMarker.tsx';
import { LoadingSpinner } from '@/component/common/loadingSpinner/LoadingSpinner.tsx';
import { getUserLocation } from '@/hooks/getUserLocation.ts';
import { getUserLocation, IDeviceOrientationEventWithPermission } from '@/hooks/getUserLocation.ts';
import { loadLocalData, saveLocalData } from '@/utils/common/manageLocalData.ts';
import { AppConfig } from '@/lib/constants/commonConstants.ts';
import { v4 as uuidv4 } from 'uuid';
import { AlertUI } from '@/component/common/alert/Alert.tsx';
import { PATH_COLOR } from '@/lib/constants/canvasConstants.ts';

export const GuestView = () => {
const { lat, lng, alpha, error, updateLocation } = getUserLocation();

const { lat, lng, alpha, error } = getUserLocation();
const location = useLocation();
const navigate = useNavigate(); // ๋„ค๋น„๊ฒŒ์ด์…˜ ํ›… ์ถ”๊ฐ€

Expand All @@ -32,6 +31,33 @@ export const GuestView = () => {

const wsRef = useRef<WebSocket | null>(null);

useEffect(() => {
const requestOrientationPermission = async () => {
const DeviceOrientationEventTyped =
DeviceOrientationEvent as unknown as IDeviceOrientationEventWithPermission;

if (
typeof DeviceOrientationEventTyped !== 'undefined' &&
typeof DeviceOrientationEventTyped.requestPermission === 'function'
) {
try {
const permission = await DeviceOrientationEventTyped.requestPermission();
if (permission === 'granted') {
console.log('Device Orientation permission granted.');
} else {
console.error('Device Orientation permission denied.');
}
} catch {
console.error('Failed to request Device Orientation permission:');
}
} else {
console.warn('DeviceOrientationEvent.requestPermission is not supported on this browser.');
}
};

requestOrientationPermission();
}, []);

useEffect(() => {
// ์†Œ์ผ“ ์—ฐ๊ฒฐ ์ดˆ๊ธฐํ™”
const token = loadLocalData(AppConfig.KEYS.BROWSER_TOKEN) || uuidv4();
Expand Down Expand Up @@ -60,14 +86,6 @@ export const GuestView = () => {
}
}, [lat, lng, alpha]);

useEffect(() => {
const interval = setInterval(() => {
updateLocation();
}, 5000);

return () => clearInterval(interval);
}, [updateLocation]);

const transformTypeGuestEntityToIGuest = (props: guestEntity | undefined): IGuest => {
return {
id: props?.id ?? '',
Expand Down
Loading