All files / lib/components/Game/Layer/ARLayer index.tsx

19.23% Statements 5/26
0% Branches 0/10
0% Functions 0/5
19.23% Lines 5/26

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                                                                                                                                                                            1x               1x                     1x                       1x                       1x                                
import { ARLayer as ARLayerModel } from '@uniquegood/realworld-web-interface/lib/models/game/Layer/AR';
import React from 'react';
import { createPortal } from 'react-dom';
import { useRunStatement } from '@lib/utils/Game/Statement';
import { Button } from '@lib/components/Button';
import { css } from '@emotion/react';
import qs from 'qs';
import { useGameStateContext } from '@lib/providers/GameState';
import { color } from '@uniquegood/realworld-studio-design';
 
export default function ARLayer({
  layer,
  onSuccess,
  onClose
}: {
  layer: ARLayerModel;
  onSuccess: () => unknown;
  onClose: () => unknown;
}) {
  const isFirstFoundRef = React.useRef(false);
  const [isFirstFound, setIsFirstFound] = React.useState(false);
  const { runStatement } = useRunStatement();
  const { render, saveOnServer } = useGameStateContext();
 
  function receiveMsgFromChild(e: { data: string }) {
    if (e.data === 'targetFound') {
      if (!isFirstFoundRef.current) {
        isFirstFoundRef.current = true;
        setIsFirstFound(true);
        (async () => {
          if (layer.actionMap.findARTarget) {
            for await (const eachStatement of layer.actionMap.findARTarget.statementList) {
              await runStatement({
                statement: eachStatement,
                withParameter: {
                  layerId: layer.id,
                  originActionType: 'findARTarget'
                }
              });
            }
            saveOnServer();
            render();
          }
        })();
      }
    }
  }
 
  React.useEffect(() => {
    // FIXME: 스크롤이 생겨서 추가
    document.body.style.overflow = 'hidden';
    window.addEventListener('message', receiveMsgFromChild);
 
    return () => {
      // FIXME: 스크롤이 생겨서 추가
      document.body.style.overflow = 'auto';
      window.removeEventListener('message', receiveMsgFromChild);
    };
  }, []);
 
  return createPortal(
    <div css={arContainer}>
      <iframe
        title="ar"
        css={arWrap}
        src={`/qr.html?${qs.stringify({
          targetImageMindARDataUrl: layer.targetImageMindARDataUrl,
          overlayImageUrl: layer.overlayImageUrl,
          guideImageUrl: layer.guideImageUrl
        })}`}
      />
      <div css={buttonGroup}>
        <Button css={closeButton} onClick={onClose}>
          닫기
        </Button>
        <Button css={successButton} onClick={onSuccess} isDisabled={!isFirstFound}>
          {isFirstFound
            ? layer.successButtonLabel
            : layer.beforeButtonLabel || layer.successButtonLabel}
        </Button>
      </div>
    </div>,
    document.body
  );
}
 
const arContainer = css`
  position: fixed;
  inset: 0;
  z-index: 1000;
  width: 100%;
  height: 100%;
`;
 
const arWrap = css`
  width: 100%;
  height: 100%;
  position: relative;
 
  .a-canvas {
    z-index: 1002;
    position: relative;
  }
`;
 
const buttonGroup = css`
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: min(calc(100% - 10px), calc(calc(100vh - 52px) * 0.5625));
  display: flex;
  flex-direction: row;
  z-index: 1003;
`;
 
const closeButton = css`
  width: 70px;
  height: 40px;
  border-radius: 10px;
  background-color: #000;
  color: #fff;
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
`;
 
const successButton = css`
  flex: 1;
  height: 40px;
  border-radius: 10px;
  background-color: ${color.palette_primary_purple_100};
  color: #fff;
  margin-right: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
 
  &:disabled {
    background-color: #ccc;
    color: #fff;
  }
`;