@@ -3,6 +3,7 @@ import { Modal, Input, Button, Space, message, Typography, Divider, Alert, theme
33import { CopyOutlined , LinkOutlined , ReloadOutlined } from '@ant-design/icons' ;
44import { useIntl } from '@umijs/max' ;
55import { getWebhookURL } from '@/services/scripts' ;
6+ import { copyToClipboard } from '@/utils/clipboard' ;
67
78const { Text, Paragraph } = Typography ;
89
@@ -47,32 +48,11 @@ const WebhookModal: React.FC<WebhookModalProps> = ({
4748 }
4849 } ;
4950
50- // Copy to clipboard
51- const copyToClipboard = async ( text : string , label : string ) => {
52- try {
53- // 优先使用 Clipboard API
54- if ( navigator . clipboard && navigator . clipboard . writeText ) {
55- await navigator . clipboard . writeText ( text ) ;
56- message . success ( intl . formatMessage ( { id : 'scripts.webhook.copy_success' } , { label } ) ) ;
57- } else {
58- // 降级方案:使用传统的 execCommand
59- const textarea = document . createElement ( 'textarea' ) ;
60- textarea . value = text ;
61- textarea . style . position = 'fixed' ;
62- textarea . style . opacity = '0' ;
63- document . body . appendChild ( textarea ) ;
64- textarea . select ( ) ;
65- const successful = document . execCommand ( 'copy' ) ;
66- document . body . removeChild ( textarea ) ;
67-
68- if ( successful ) {
69- message . success ( intl . formatMessage ( { id : 'scripts.webhook.copy_success' } , { label } ) ) ;
70- } else {
71- throw new Error ( 'execCommand failed' ) ;
72- }
73- }
74- } catch ( error ) {
75- console . error ( intl . formatMessage ( { id : 'scripts.webhook.copy_failed' } ) , error ) ;
51+ const handleCopy = async ( text : string , label : string ) => {
52+ const success = await copyToClipboard ( text ) ;
53+ if ( success ) {
54+ message . success ( intl . formatMessage ( { id : 'scripts.webhook.copy_success' } , { label } ) ) ;
55+ } else {
7656 message . error ( intl . formatMessage ( { id : 'scripts.webhook.copy_failed' } ) ) ;
7757 }
7858 } ;
@@ -139,7 +119,7 @@ const WebhookModal: React.FC<WebhookModalProps> = ({
139119 />
140120 < Button
141121 icon = { < CopyOutlined /> }
142- onClick = { ( ) => copyToClipboard ( webhookInfo . webhook_url , intl . formatMessage ( { id : 'scripts.webhook.copy_url' } ) ) }
122+ onClick = { ( ) => handleCopy ( webhookInfo . webhook_url , intl . formatMessage ( { id : 'scripts.webhook.copy_url' } ) ) }
143123 />
144124 </ Input . Group >
145125 </ div >
@@ -156,7 +136,7 @@ const WebhookModal: React.FC<WebhookModalProps> = ({
156136 />
157137 < Button
158138 icon = { < CopyOutlined /> }
159- onClick = { ( ) => copyToClipboard ( webhookInfo . signature , intl . formatMessage ( { id : 'scripts.webhook.copy_signature' } ) ) }
139+ onClick = { ( ) => handleCopy ( webhookInfo . signature , intl . formatMessage ( { id : 'scripts.webhook.copy_signature' } ) ) }
160140 />
161141 </ Input . Group >
162142 </ div >
0 commit comments