fix(clipboard): defensive for navigator clipboard (#462)

* fix: defensive for navigator clipboard

* add dot in en.ts

---------

Co-authored-by: Guy Ben-Aharon <baguy3@gmail.com>
This commit is contained in:
Huy Bui
2024-12-02 18:27:13 +07:00
committed by GitHub
parent 807cd22e0c
commit 5fc10a7e64
21 changed files with 252 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ import React, { lazy, Suspense, useCallback, useEffect } from 'react';
import { Spinner } from '../spinner/spinner';
import { useTheme } from '@/hooks/use-theme';
import { useMonaco } from '@monaco-editor/react';
import { useToast } from '@/components/toast/use-toast';
import { Button } from '../button/button';
import { Copy, CopyCheck } from 'lucide-react';
import { Tooltip, TooltipContent, TooltipTrigger } from '../tooltip/tooltip';
@@ -38,6 +39,7 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
const { t } = useTranslation();
const monaco = useMonaco();
const { effectiveTheme } = useTheme();
const { toast } = useToast();
const [isCopied, setIsCopied] = React.useState(false);
const [tooltipOpen, setTooltipOpen] = React.useState(false);
@@ -66,10 +68,32 @@ export const CodeSnippet: React.FC<CodeSnippetProps> = React.memo(
}
}, [code, monaco, autoScroll]);
const copyToClipboard = useCallback(() => {
navigator.clipboard.writeText(code);
setIsCopied(true);
}, [code]);
const copyToClipboard = useCallback(async () => {
if (!navigator?.clipboard) {
toast({
title: t('copy_to_clipboard_toast.unsupported.title'),
variant: 'destructive',
description: t(
'copy_to_clipboard_toast.unsupported.description'
),
});
return;
}
try {
await navigator.clipboard.writeText(code);
setIsCopied(true);
} catch (error) {
setIsCopied(false);
toast({
title: t('copy_to_clipboard_toast.failed.title'),
variant: 'destructive',
description: t(
'copy_to_clipboard_toast.failed.description'
),
});
}
}, [code, t, toast]);
return (
<div

View File

@@ -78,6 +78,17 @@ export const bn: LanguageTranslation = {
none: 'কিছুই না',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'কপি ব্যর্থ হয়েছে',
description: 'ক্লিপবোর্ড সমর্থিত নয়',
},
failed: {
title: 'কপি ব্যর্থ হয়েছে',
description: 'কিছু ভুল হয়েছে। অনুগ্রহ করে আবার চেষ্টা করুন।',
},
},
theme: {
system: 'সিস্টেম',
light: 'হালকা',

View File

@@ -78,6 +78,18 @@ export const de: LanguageTranslation = {
none: 'Keine',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Kopieren fehlgeschlagen',
description: 'Zwischenablage nicht unterstützt',
},
failed: {
title: 'Kopieren fehlgeschlagen',
description:
'Etwas ist schiefgelaufen. Bitte versuchen Sie es erneut.',
},
},
theme: {
system: 'System',
light: 'Hell',

View File

@@ -77,6 +77,17 @@ export const en = {
none: 'none',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Copy failed',
description: 'Clipboard not supported.',
},
failed: {
title: 'Copy failed',
description: 'Something went wrong. Please try again.',
},
},
theme: {
system: 'System',
light: 'Light',

View File

@@ -69,6 +69,17 @@ export const es: LanguageTranslation = {
cancel: 'Cancelar',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Copia fallida',
description: 'Portapapeles no soportado',
},
failed: {
title: 'Copia fallida',
description: 'Algo salió mal. Por favor, inténtelo de nuevo.',
},
},
theme: {
system: 'Sistema',
light: 'Claro',

View File

@@ -68,6 +68,17 @@ export const fr: LanguageTranslation = {
cancel: 'Annuler',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Échec de la copie',
description: 'Presse-papiers non pris en charge',
},
failed: {
title: 'Échec de la copie',
description: 'Quelque chose a mal tourné. Veuillez réessayer.',
},
},
theme: {
system: 'Système',
light: 'Clair',

View File

@@ -78,6 +78,17 @@ export const gu: LanguageTranslation = {
none: 'કઈ નહીં',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'નકલ નિષ્ફળ',
description: 'ક્લિપબોર્ડ આધારિત નથી',
},
failed: {
title: 'નકલ નિષ્ફળ',
description: 'કંઈક ખોટું થયું છે. કૃપા કરીને ફરી પ્રયાસ કરો.',
},
},
theme: {
system: 'સિસ્ટમ',
light: 'હલકો',

View File

@@ -78,6 +78,17 @@ export const hi: LanguageTranslation = {
none: 'कोई नहीं',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'कॉपी असफल',
description: 'क्लिपबोर्ड समर्थित नहीं है',
},
failed: {
title: 'कॉपी असफल',
description: 'कुछ गलत हो गया। कृपया पुनः प्रयास करें।',
},
},
theme: {
system: 'सिस्टम',
light: 'हल्का',

View File

@@ -78,6 +78,17 @@ export const id_ID: LanguageTranslation = {
none: 'Tidak ada',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Gagal menyalin',
description: 'Clipboard tidak didukung',
},
failed: {
title: 'Gagal menyalin',
description: 'Ada yang salah. Silakan coba lagi.',
},
},
theme: {
system: 'Sistem',
light: 'Terang',

View File

@@ -79,6 +79,18 @@ export const ja: LanguageTranslation = {
none: 'なし',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'コピー失敗',
description: 'クリップボードがサポートされていません',
},
failed: {
title: 'コピー失敗',
description:
'何かがうまくいきませんでした。もう一度お試しください。',
},
},
theme: {
system: 'システム',
light: 'ライト',

View File

@@ -77,6 +77,17 @@ export const ko_KR: LanguageTranslation = {
none: '없음',
},
copy_to_clipboard_toast: {
unsupported: {
title: '복사 실패',
description: '클립보드가 지원되지 않습니다"',
},
failed: {
title: '복사 실패',
description: '문제가 발생했습니다. 다시 시도해주세요.',
},
},
theme: {
system: '시스템 설정에 따름',
light: '밝게',

View File

@@ -78,6 +78,17 @@ export const mr: LanguageTranslation = {
none: 'काहीही नाही',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'कॉपी अयशस्वी',
description: 'क्लिपबोर्ड समर्थित नाही',
},
failed: {
title: 'कॉपी अयशस्वी',
description: 'काहीतरी चूक झाली. कृपया पुन्हा प्रयत्न करा.',
},
},
theme: {
system: 'सिस्टम',
light: 'लाईट',

View File

@@ -77,6 +77,17 @@ export const ne: LanguageTranslation = {
none: 'कुनै पनि छैन',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'प्रतिलिपि असफल',
description: 'क्लिपबोर्ड समर्थित छैन',
},
failed: {
title: 'प्रतिलिपि असफल',
description: 'केही गडबड भयो। कृपया फेरि प्रयास गर्नुहोस्।',
},
},
theme: {
system: 'सिस्टम',
light: 'लाइट',

View File

@@ -78,6 +78,17 @@ export const pt_BR: LanguageTranslation = {
none: 'nenhum',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Falha na cópia',
description: 'Área de transferência não suportada',
},
failed: {
title: 'Falha na cópia',
description: 'Algo deu errado. Por favor, tente novamente.',
},
},
theme: {
system: 'Sistema',
light: 'Claro',

View File

@@ -77,6 +77,18 @@ export const ru: LanguageTranslation = {
none: 'никто',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Ошибка копирования',
description: 'Буфер обмена не поддерживается',
},
failed: {
title: 'Ошибка копирования',
description:
'Что-то пошло не так. Пожалуйста, попробуйте еще раз.',
},
},
theme: {
system: 'Системная',
light: 'Светлая',

View File

@@ -78,6 +78,17 @@ export const te: LanguageTranslation = {
none: 'ఎదరికాదు',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'కాపీ విఫలమైంది',
description: 'క్లిప్‌బోర్డ్ మద్దతు ఇవ్వదు',
},
failed: {
title: 'కాపీ విఫలమైంది',
description: 'ఏదో తప్పు జరిగింది. దయచేసి మళ్లీ ప్రయత్నించండి.',
},
},
theme: {
system: 'సిస్టమ్',
light: 'హালకా',

View File

@@ -78,6 +78,17 @@ export const tr: LanguageTranslation = {
none: 'yok',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Kopyalama başarısız',
description: 'Panoya desteklenmiyor',
},
failed: {
title: 'Kopyalama başarısız',
description: 'Bir şeyler ters gitti. Lütfen tekrar deneyin.',
},
},
theme: {
system: 'Sistem',
light: 'Açık',

View File

@@ -78,6 +78,17 @@ export const uk: LanguageTranslation = {
none: 'немає',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Помилка копіювання',
description: 'Буфер обміну не підтримується',
},
failed: {
title: 'Помилка копіювання',
description: 'Щось пішло не так. Будь ласка, спробуйте ще раз.',
},
},
theme: {
system: 'система',
light: 'світлий',

View File

@@ -77,6 +77,17 @@ export const vi: LanguageTranslation = {
none: 'không có',
},
copy_to_clipboard_toast: {
unsupported: {
title: 'Sao chép thất bại',
description: 'Không hỗ trợ bảng tạm',
},
failed: {
title: 'Sao chép thất bại',
description: 'Đã xảy ra lỗi. Vui lòng thử lại.',
},
},
theme: {
system: 'Hệ thống',
light: 'Sáng',
@@ -97,7 +108,7 @@ export const vi: LanguageTranslation = {
clear: 'Xóa',
show_more: 'Hiển thị thêm',
show_less: 'Hiển thị ít hơn',
copy_to_clipboard: 'Sao chép vào Clipboard',
copy_to_clipboard: 'Sao chép vào bảng tạm',
copied: 'Đã sao chép!',
side_panel: {

View File

@@ -74,6 +74,17 @@ export const zh_CN: LanguageTranslation = {
none: '无',
},
copy_to_clipboard_toast: {
unsupported: {
title: '复制失败',
description: '不支持剪贴板',
},
failed: {
title: '复制失败',
description: '出现问题。请再试一次。',
},
},
theme: {
system: '系统',
light: '浅色',

View File

@@ -74,6 +74,17 @@ export const zh_TW: LanguageTranslation = {
none: '無',
},
copy_to_clipboard_toast: {
unsupported: {
title: '複製失敗',
description: '不支援剪貼簿',
},
failed: {
title: '複製失敗',
description: '出現問題。請再試一次。',
},
},
theme: {
system: '系統',
light: '淺色',