|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { QRCodeSVG } from 'qrcode.react'; |
| 4 | +import { useQuery } from '@tanstack/react-query'; |
| 5 | +import { useState, useRef, useEffect } from 'react'; |
| 6 | +import { COLORS, env, PATHS, SCREENS } from '@/constants'; |
| 7 | +import { useResponsive } from '@/hooks'; |
| 8 | +import { createQRToken } from '@/apis'; |
| 9 | +import { Modal as Layout } from '@/components'; |
| 10 | +import { formatTimeToMMSS } from '@/utils/dateUtil'; |
| 11 | +import { CopyButton } from './CopyButton'; |
| 12 | + |
| 13 | +const TIMER_DURATION = 5 * 60; // 5분 = 300초 |
| 14 | + |
| 15 | +export const QRCode = () => { |
| 16 | + const width = useResponsive(); |
| 17 | + const [timeLeft, setTimeLeft] = useState(TIMER_DURATION); |
| 18 | + |
| 19 | + const timerRef = useRef<NodeJS.Timeout | null>(null); |
| 20 | + const isExpired = timeLeft === 0; |
| 21 | + |
| 22 | + const { data, isLoading, refetch } = useQuery({ |
| 23 | + queryKey: [PATHS.QRLOGIN], |
| 24 | + queryFn: createQRToken, |
| 25 | + refetchOnMount: true, |
| 26 | + staleTime: 0, |
| 27 | + refetchOnWindowFocus: false, |
| 28 | + }); |
| 29 | + const url = `${env.BASE_URL}/api/qr-login?token=${data?.token}`; |
| 30 | + |
| 31 | + // 타이머 시작 |
| 32 | + useEffect(() => { |
| 33 | + if (!isLoading) { |
| 34 | + timerRef.current = setInterval(() => setTimeLeft((prev) => (prev <= 1 ? 0 : prev - 1)), 1000); |
| 35 | + } |
| 36 | + |
| 37 | + return () => { |
| 38 | + if (timerRef.current) clearInterval(timerRef.current); |
| 39 | + }; |
| 40 | + }, [isLoading]); |
| 41 | + |
| 42 | + return ( |
| 43 | + <Layout title="QR 로그인"> |
| 44 | + <div className="flex items-center justify-center gap-10"> |
| 45 | + <div |
| 46 | + className={ |
| 47 | + isExpired || isLoading |
| 48 | + ? `relative after:inset-0 after:absolute after:m-auto after:bg-BG-MAIN after:size-fit after:text-TEXT-MAIN after:px-3 after:py-1 after:rounded-lg after:font-medium ${isLoading ? 'after:content-["로딩중"]' : 'after:content-["만료됨"]'}` |
| 49 | + : '' |
| 50 | + } |
| 51 | + > |
| 52 | + <QRCodeSVG |
| 53 | + value={url} |
| 54 | + width={width < SCREENS.MBI ? 130 : 171} |
| 55 | + height={width < SCREENS.MBI ? 130 : 171} |
| 56 | + enableBackground={0} |
| 57 | + bgColor={COLORS.BG.SUB} |
| 58 | + fgColor={COLORS.TEXT.MAIN} |
| 59 | + className={`transition-all ${isExpired || isLoading ? 'blur-sm' : ''}`} |
| 60 | + /> |
| 61 | + </div> |
| 62 | + <div className="flex flex-col items-center gap-4"> |
| 63 | + <h3 className="text-T4 text-TEXT-ALT leading-none">만료까지</h3> |
| 64 | + <h2 |
| 65 | + className={`text-T2 leading-none min-w-[130px] text-center ${timeLeft <= 60 ? 'text-DESTRUCTIVE-SUB' : 'text-TEXT-MAIN'}`} |
| 66 | + > |
| 67 | + {formatTimeToMMSS(timeLeft)} |
| 68 | + </h2> |
| 69 | + {isExpired && !isLoading && ( |
| 70 | + <button |
| 71 | + className="text-I1 text-BG-MAIN bg-PRIMARY-MAIN px-5 py-1 rounded-sm" |
| 72 | + onClick={async () => { |
| 73 | + await refetch(); |
| 74 | + setTimeLeft(TIMER_DURATION); |
| 75 | + }} |
| 76 | + > |
| 77 | + 새로고침 |
| 78 | + </button> |
| 79 | + )} |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + |
| 83 | + <CopyButton url={url} disabled={isExpired || isLoading} /> |
| 84 | + </Layout> |
| 85 | + ); |
| 86 | +}; |
0 commit comments