Skip to content

Commit

Permalink
feat: add slope height post app message
Browse files Browse the repository at this point in the history
  • Loading branch information
Najeong-Kim committed Dec 6, 2024
1 parent 25cf2e9 commit 784a354
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/features/slope/ui/slope-camera.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const SlopeCamera = ({
setOpenCamera();

if (!url) {
postAppMessage('선택한 웹캠은 아직 준비중 이에요', isWebview, (message) =>
postAppMessage('showToast', '선택한 웹캠은 아직 준비중 이에요', isWebview, (message) =>
toast(
<>
<NeutralFace /> {message}
Expand Down
16 changes: 12 additions & 4 deletions src/shared/lib/postAppMessage.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
type AppMessageMethod = 'showToast' | 'setHeight';

declare global {
interface Window {
Android: {
showToast: (message: string) => void;
setHeight: (message: string) => void;
};
webkit: {
messageHandlers: {
weski: {
postMessage: ({ method, message }: { method: "showToast", message: string }) => void;
postMessage: ({ method, message }: { method: AppMessageMethod, message: string }) => void;
};
};
};
}
}

const postAppMessage = (message: string, isWebview: boolean, showToast: (message: string) => void) => {
const postAppMessage = (method: AppMessageMethod, message: string, isWebview: boolean, showToast: (message: string) => void) => {
const userAgent = navigator.userAgent.toLowerCase();
const android = userAgent.match(/android/i);
const iphone = userAgent.match(/iphone/i);

if (isWebview) {
if (android !== null) {
return window.Android.showToast(message);
switch (method) {
case 'showToast':
return window.Android.showToast(message);
case 'setHeight':
return window.Android.setHeight(message);
}
} else if (iphone !== null) {
return window.webkit.messageHandlers.weski.postMessage({ method: "showToast", message: message });
return window.webkit.messageHandlers.weski.postMessage({ method: method, message: message });
}
}
return showToast(message);
Expand Down
11 changes: 9 additions & 2 deletions src/views/slope-status/ui/slope-status-page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { useQuery } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useLayoutEffect, useMemo, useRef } from 'react';
import SlopeStatusHeader from '@/widgets/header/ui/slope-status-header';
import SlopeStatusTime from '@/widgets/header/ui/slope-status-time';
import useMapPinch from '@/features/slope/hooks/useMapPinch';
Expand All @@ -12,9 +12,11 @@ import { slopeApi } from '@/entities/slope';
import type { Slope } from '@/entities/slope/model';
import { RESORT_DOMAIN } from '@/entities/slope/model';
import { cn } from '@/shared/lib';
import postAppMessage from '@/shared/lib/postAppMessage';

const SlopeStatusPage = ({ resortId }: { resortId: number }) => {
const { ref, style, containerRef } = useMapPinch();
const mainRef = useRef<HTMLElement>(null);

const { data: slopeRawData } = useQuery(slopeApi.slopeQueries.slope(resortId ?? 0));
const key = ResortData.find((resort) => resort.id === resortId)
Expand Down Expand Up @@ -49,10 +51,15 @@ const SlopeStatusPage = ({ resortId }: { resortId: number }) => {
[slopeRawData]
);

useLayoutEffect(() => {
if (!mainRef.current) return;
postAppMessage('setHeight', mainRef.current?.offsetHeight.toString(), true, () => {});
}, [slopeRawData]);

if (!slopes) return;

return (
<main className={cn('mb-3 w-full')}>
<main className={cn('mb-3 w-full')} ref={mainRef}>
<SlopeStatusHeader />
<section className={cn('relative mx-[20px] overflow-hidden')} ref={containerRef}>
<SlopeMap
Expand Down
11 changes: 9 additions & 2 deletions src/views/webcam/ui/webcam-mobile-map-page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { useQuery } from '@tanstack/react-query';
import React, { useCallback, useState } from 'react';
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { WebcamMap, WebcamSlopeList } from '@/widgets/webcam/ui';
import useMapPinch from '@/features/slope/hooks/useMapPinch';
import calculateWebcamPosition from '@/features/slope/lib/calculateWebcamPosition';
Expand All @@ -10,6 +10,7 @@ import { slopeApi } from '@/entities/slope';
import type { Slope, Webcam } from '@/entities/slope/model';
import { RESORT_DOMAIN, type Position } from '@/entities/slope/model';
import { cn } from '@/shared/lib';
import postAppMessage from '@/shared/lib/postAppMessage';

const WebCamMobileMapPage = ({ resortId }: { resortId?: number }) => {
const { data: slopeRawData } = useQuery(slopeApi.slopeQueries.slope(resortId ?? 0));
Expand All @@ -32,6 +33,7 @@ const WebCamMobileMapPage = ({ resortId }: { resortId?: number }) => {
...RESORT_DOMAIN[key].webcams.find((webcamConstant) => webcamConstant.id === webcam.number),
})) as Webcam[];

const mainRef = useRef<HTMLDivElement>(null);
const [cameraPositions, setCameraPositions] = useState<{
[key: number]: Position;
}>({});
Expand All @@ -55,10 +57,15 @@ const WebCamMobileMapPage = ({ resortId }: { resortId?: number }) => {
api.start({ scale: scale, x: boundedX, y: boundedY });
};

useEffect(() => {
if (!mainRef.current) return;
postAppMessage('setHeight', mainRef.current?.offsetHeight.toString(), true, () => {});
}, [slopeRawData]);

if (!slopes || !webcams) return;

return (
<main className={cn('w-full')}>
<main className={cn('w-full')} ref={mainRef}>
<WebcamMap
ref={ref}
style={style}
Expand Down
1 change: 0 additions & 1 deletion src/widgets/header/ui/slope-status-time.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ type SlopeStatusTimeProps = {
};

const SlopeStatusTime = ({ times }: SlopeStatusTimeProps) => {
console.log(times);
return (
<div
className={cn(
Expand Down

0 comments on commit 784a354

Please sign in to comment.