Skip to content

Commit

Permalink
๐Ÿ› ์‚ฌํŒŒ๋ฆฌ ๋ธŒ๋ผ์šฐ์ € QR ๋ณต์‚ฌ ์‹คํŒจ ๊ด€๋ จ ์ฝ”๋“œ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
sen2y committed Aug 28, 2024
1 parent 2adf8cc commit 183b289
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/app/components/common/ShareModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Modal, Button } from 'antd'
import React, { useRef } from 'react'
import UrlInput from '@/app/components/accompany/UrlInput' // UrlInput ์ปดํฌ๋„ŒํŠธ ์ž„ํฌํŠธ
import React, { useRef, useState } from 'react'
import UrlInput from '@/app/components/accompany/UrlInput'
import { useCustomMessage } from '@/app/utils/alertUtils'
import { usePathname } from 'next/navigation'
import { QRCode } from 'antd'
Expand All @@ -17,34 +17,50 @@ const ShareModal: React.FC<ShareModalProps> = ({
isOpen,
onClose,
customUrl,
// customUrlQrPath,
}) => {
const { contextHolder, showSuccess } = useCustomMessage()
const { contextHolder, showSuccess, showWarning } = useCustomMessage()
const pathname = usePathname()
const defaultUrl = `${process.env.NEXT_PUBLIC_SELF_URL}${pathname}`
const qrCodeRef = useRef<HTMLDivElement>(null)
const [qrCodeBlob, setQrCodeBlob] = useState<Blob | null>(null)

const handleCopyQRCode = async () => {
// ๋ฏธ๋ฆฌ QR ์ฝ”๋“œ ์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ด๋†“์Šต๋‹ˆ๋‹ค.
const generateQRCodeBlob = async () => {
if (qrCodeRef.current) {
try {
const canvas = await html2canvas(qrCodeRef.current)
canvas.toBlob(async (blob) => {
canvas.toBlob((blob) => {
if (blob) {
try {
const item = new ClipboardItem({ 'image/png': blob })
await navigator.clipboard.write([item])
showSuccess('QR ์ฝ”๋“œ๊ฐ€ ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
} catch (error) {
showSuccess('ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค: ' + error)
}
setQrCodeBlob(blob)
}
})
} catch (error) {
showSuccess(`${error}, QR ์ฝ”๋“œ๋ฅผ ๋ณต์‚ฌํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.`)
showWarning('QR ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.')
}
}
}

// ํด๋ฆญ ์ด๋ฒคํŠธ์—์„œ ์ง์ ‘ ๋ณต์‚ฌํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
const handleCopyQRCode = async () => {
if (qrCodeBlob) {
try {
const item = new ClipboardItem({ 'image/png': qrCodeBlob })
await navigator.clipboard.write([item])
showSuccess('QR ์ฝ”๋“œ๊ฐ€ ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค!')
} catch (error) {
showWarning(
'Safari ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ํด๋ฆฝ๋ณด๋“œ ๋ณต์‚ฌ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. Chrome ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.'
)
}
} else {
showWarning('QR ์ฝ”๋“œ ์ด๋ฏธ์ง€๊ฐ€ ์•„์ง ์ค€๋น„๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.')
}
}

React.useEffect(() => {
generateQRCodeBlob()
}, [isOpen])

return (
<>
{contextHolder}
Expand All @@ -68,7 +84,6 @@ const ShareModal: React.FC<ShareModalProps> = ({
className='w-52 h-52 mx-auto flex items-center justify-center'
ref={qrCodeRef} // QR ์ฝ”๋“œ ์˜์—ญ์„ ์ฐธ์กฐ
>
{/* TODO : QR ๋‚ด๋ถ€์— ์šฐ๋ฆฌ ์•„์ด์ฝ˜ ๋„ฃ๊ธฐ */}
<QRCode errorLevel='H' value={defaultUrl} icon='' size={200} />
</div>
</div>
Expand Down

0 comments on commit 183b289

Please sign in to comment.