mirror of
https://github.com/langgenius/dify.git
synced 2026-05-13 08:57:28 +08:00
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: jyong <718720800@qq.com> Co-authored-by: Yansong Zhang <916125788@qq.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: hj24 <mambahj24@gmail.com> Co-authored-by: hj24 <huangjian@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Stephen Zhou <38493346+hyoban@users.noreply.github.com> Co-authored-by: CodingOnStar <hanxujiang@dify.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: 非法操作 <hjlarry@163.com> Co-authored-by: Ayush Baluni <73417844+aayushbaluni@users.noreply.github.com> Co-authored-by: yyh <92089059+lyzno1@users.noreply.github.com> Co-authored-by: jimcody1995 <jjimcody@gmail.com> Co-authored-by: James <63717587+jamesrayammons@users.noreply.github.com> Co-authored-by: Yunlu Wen <yunlu.wen@dify.ai> Co-authored-by: Stephen Zhou <hi@hyoban.cc> Co-authored-by: Coding On Star <447357187@qq.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: jerryzai <jerryzh8710@protonmail.com> Co-authored-by: NVIDIAN <speedy.hpc@hotmail.com> Co-authored-by: ai-hpc <ai-hpc@users.noreply.github.com> Co-authored-by: Asuka Minato <i@asukaminato.eu.org> Co-authored-by: Junghwan <70629228+shaun0927@users.noreply.github.com> Co-authored-by: HeYinKazune <70251095+HeYin-OS@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: yyh <yuanyouhuilyz@gmail.com> Co-authored-by: Jingyi <jingyi.qi@dify.ai> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com> Co-authored-by: sxxtony <166789813+sxxtony@users.noreply.github.com>
84 lines
2.9 KiB
TypeScript
84 lines
2.9 KiB
TypeScript
'use client'
|
|
import { QRCodeCanvas as QRCode } from 'qrcode.react'
|
|
import * as React from 'react'
|
|
import { useEffect, useRef, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import ActionButton from '@/app/components/base/action-button'
|
|
import Tooltip from '@/app/components/base/tooltip'
|
|
import { downloadUrl } from '@/utils/download'
|
|
|
|
type Props = {
|
|
content: string
|
|
}
|
|
|
|
const prefixEmbedded = 'overview.appInfo.qrcode.title'
|
|
|
|
const ShareQRCode = ({ content }: Props) => {
|
|
const { t } = useTranslation()
|
|
const [isShow, setIsShow] = useState<boolean>(false)
|
|
const qrCodeRef = useRef<HTMLDivElement>(null)
|
|
|
|
const toggleQRCode = (event: React.MouseEvent) => {
|
|
event.stopPropagation()
|
|
setIsShow(prev => !prev)
|
|
}
|
|
|
|
useEffect(() => {
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
/* v8 ignore next 2 -- this handler can fire during open/close transitions where the panel ref is temporarily null; guard is defensive. @preserve */
|
|
if (qrCodeRef.current && !qrCodeRef.current.contains(event.target as Node))
|
|
setIsShow(false)
|
|
}
|
|
|
|
if (isShow)
|
|
document.addEventListener('click', handleClickOutside)
|
|
|
|
return () => {
|
|
document.removeEventListener('click', handleClickOutside)
|
|
}
|
|
}, [isShow])
|
|
|
|
const downloadQR = () => {
|
|
const canvas = qrCodeRef.current?.querySelector('canvas')
|
|
if (!(canvas instanceof HTMLCanvasElement))
|
|
return
|
|
downloadUrl({ url: canvas.toDataURL(), fileName: 'qrcode.png' })
|
|
}
|
|
|
|
const handlePanelClick = (event: React.MouseEvent) => {
|
|
event.stopPropagation()
|
|
}
|
|
|
|
const tooltipText = t(`${prefixEmbedded}`, { ns: 'appOverview' })
|
|
/* v8 ignore next -- react-i18next returns a non-empty key/string in configured runtime; empty fallback protects against missing i18n payloads. @preserve */
|
|
const safeTooltipText = tooltipText || ''
|
|
|
|
return (
|
|
<Tooltip
|
|
popupContent={safeTooltipText}
|
|
>
|
|
<div className="relative h-6 w-6" onClick={toggleQRCode} data-testid="qrcode-container">
|
|
<ActionButton>
|
|
<span className="i-ri-qr-code-line h-4 w-4" />
|
|
</ActionButton>
|
|
{isShow && (
|
|
<div
|
|
ref={qrCodeRef}
|
|
className="absolute top-8 -right-8 z-10 flex w-[232px] flex-col items-center rounded-lg bg-components-panel-bg p-4 shadow-xs"
|
|
onClick={handlePanelClick}
|
|
>
|
|
<QRCode size={160} value={content} className="mb-2" />
|
|
<div className="flex items-center system-xs-regular">
|
|
<div className="text-text-tertiary">{t('overview.appInfo.qrcode.scan', { ns: 'appOverview' })}</div>
|
|
<div className="text-text-tertiary">·</div>
|
|
<div className="cursor-pointer text-text-accent-secondary" onClick={downloadQR}>{t('overview.appInfo.qrcode.download', { ns: 'appOverview' })}</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</Tooltip>
|
|
)
|
|
}
|
|
|
|
export default ShareQRCode
|