Skip to content

Commit

Permalink
fix: show copy to clipboard failure (#2886)
Browse files Browse the repository at this point in the history
* fix for AppButtonCopy

* add some logging

* fix if statement

* refactor, use .then

* check for copied

* Fix recipe share link

* refactor AppButtonCopy

* update tooltip text

* update use-copy

* logging

* fix is supported check

* more fixes for use-copy.ts

---------

Co-authored-by: Michael Genson <[email protected]>
  • Loading branch information
Kuchenpirat and michael-genson authored Dec 29, 2023
1 parent f6f9a1c commit 6a71af9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
16 changes: 13 additions & 3 deletions frontend/components/Domain/Recipe/RecipeDialogShare.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export default defineComponent({
}
const { share, isSupported: shareIsSupported } = useShare();
const { copy } = useClipboard();
const { copy, copied, isSupported } = useClipboard();
function getRecipeText() {
return i18n.t("recipe.share-recipe-message", [props.name]);
Expand All @@ -154,8 +154,18 @@ export default defineComponent({
}
async function copyTokenLink(token: string) {
await copy(getTokenLink(token));
alert.success(i18n.t("recipe-share.recipe-link-copied-message") as string);
if (isSupported.value) {
await copy(getTokenLink(token));
if (copied.value) {
alert.success(i18n.t("recipe-share.recipe-link-copied-message") as string);
}
else {
alert.error(i18n.t("general.clipboard-copy-failure") as string);
}
}
else {
alert.error(i18n.t("general.clipboard-not-supported") as string);
}
}
async function shareRecipe(token: string) {
Expand Down
29 changes: 21 additions & 8 deletions frontend/components/global/AppButtonCopy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<v-tooltip
ref="copyToolTip"
v-model="show"
color="success lighten-1"
:color="copied? 'success lighten-1' : 'red lighten-1'"
top
:open-on-hover="false"
:open-on-click="true"
Expand All @@ -29,12 +29,14 @@
<v-icon left dark>
{{ $globals.icons.clipboardCheck }}
</v-icon>
<slot> {{ $t("general.copied_message") }} </slot>
<slot v-if="!isSupported"> {{ $t("general.your-browser-does-not-support-clipboard") }} </slot>
<slot v-else> {{ copied ? $t("general.copied_message") : $t("general.clipboard-copy-failure") }} </slot>
</span>
</v-tooltip>
</template>

<script lang="ts">
import { useClipboard } from "@vueuse/core"
import { defineComponent, ref } from "@nuxtjs/composition-api";
import { VTooltip } from "~/types/vuetify";
Expand All @@ -58,20 +60,29 @@ export default defineComponent({
},
},
setup(props) {
const { copy, copied, isSupported } = useClipboard()
const show = ref(false);
const copyToolTip = ref<VTooltip | null>(null);
function toggleBlur() {
copyToolTip.value?.deactivate();
}
function textToClipboard() {
async function textToClipboard() {
if (isSupported.value) {
await copy(props.copyText);
if (copied.value) {
console.log(`Copied\n${props.copyText}`)
}
else {
console.warn("Copy failed: ", copied.value);
}
}
else {
console.warn("Clipboard is currently not supported by your browser. Ensure you're on a secure (https) site.");
}
show.value = true;
const copyText = props.copyText;
navigator.clipboard.writeText(copyText).then(
() => console.log(`Copied\n${copyText}`),
() => console.log(`Copied Failed\n${copyText}`)
);
setTimeout(() => {
toggleBlur();
}, 500);
Expand All @@ -81,6 +92,8 @@ export default defineComponent({
show,
copyToolTip,
textToClipboard,
copied,
isSupported,
};
},
});
Expand Down
25 changes: 19 additions & 6 deletions frontend/composables/use-copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,30 @@ export function useCopy() {
const { i18n } = useContext();

function copyText(text: string) {
if (!isSupported) {
if (!isSupported.value) {
alert.error(i18n.tc("general.clipboard-not-supported"));
return;
}
copy(text);
alert.success(i18n.tc("general.copied-to-clipboard"));
copy(text).then(() => {
// Verify copy success as no error is thrown on failure.
if (copied.value) {
alert.success(i18n.tc("general.copied-to-clipboard"));
}
else {
alert.error(i18n.tc("general.clipboard-copy-failure"));
}
});
}

return { copyText, copied };
}

export function useCopyList() {
const { copy, isSupported } = useClipboard();
const { copy, isSupported, copied } = useClipboard();
const { i18n } = useContext();

function checkClipboard() {
if (!isSupported) {
if (!isSupported.value) {
alert.error(i18n.tc("general.your-browser-does-not-support-clipboard"));
return false;
}
Expand Down Expand Up @@ -54,7 +61,13 @@ export function useCopyList() {

function copyText(text: string, len: number) {
copy(text).then(() => {
alert.success(i18n.tc("general.copied-items-to-clipboard", len));
// Verify copy success as no error is thrown on failure.
if (copied.value) {
alert.success(i18n.tc("general.copied-items-to-clipboard", len));
}
else {
alert.error(i18n.tc("general.clipboard-copy-failure"));
}
});
}

Expand Down
3 changes: 2 additions & 1 deletion frontend/lang/messages/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@
"refresh": "Refresh",
"upload-file": "Upload File",
"created-on-date": "Created on: {0}",
"unsaved-changes": "You have unsaved changes. Do you want to save before leaving? Okay to save, Cancel to discard changes."
"unsaved-changes": "You have unsaved changes. Do you want to save before leaving? Okay to save, Cancel to discard changes.",
"clipboard-copy-failure": "Failed to copy to the clipboard."
},
"group": {
"are-you-sure-you-want-to-delete-the-group": "Are you sure you want to delete <b>{groupName}<b/>?",
Expand Down

0 comments on commit 6a71af9

Please sign in to comment.