Skip to content

Commit

Permalink
Merge pull request #7551 from H1ghBre4k3r/move-error-handling-to-writ…
Browse files Browse the repository at this point in the history
…eClipboard

feat(clipboard): move the toast-handling into writeClipboard function
  • Loading branch information
estib-vega authored Mar 9, 2025
2 parents 13cfeec + 402fabe commit ae0c528
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 40 deletions.
27 changes: 8 additions & 19 deletions apps/desktop/src/components/FileContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,18 @@
<ContextMenuItem
label="Copy Path"
onclick={async () => {
try {
if (!project) return;
const absPath = await join(project.path, item.files[0].path);
writeClipboard(absPath);
contextMenu.close();
// dismiss();
} catch (err) {
console.error('Failed to copy path', err);
toasts.error('Failed to copy path');
}
if (!project) return;
const absPath = await join(project.path, item.files[0].path);
await writeClipboard(absPath, 'Failed to copy path');
contextMenu.close();
}}
/>
<ContextMenuItem
label="Copy Relative Path"
onclick={() => {
try {
if (!project) return;
writeClipboard(item.files[0].path);
contextMenu.close();
} catch (err) {
console.error('Failed to copy relative path', err);
toasts.error('Failed to copy relative path');
}
onclick={async () => {
if (!project) return;
await writeClipboard(item.files[0].path, 'Failed to copy relative path');
contextMenu.close();
}}
/>
{/if}
Expand Down
28 changes: 9 additions & 19 deletions apps/desktop/src/components/v3/FileContextMenu.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,19 @@
<ContextMenuItem
label="Copy Path"
onclick={async () => {
try {
if (!project) return;
const absPath = await join(project.path, changes[0]!.path);
writeClipboard(absPath);
contextMenu.close();
// dismiss();
} catch (err) {
console.error('Failed to copy path', err);
toasts.error('Failed to copy path');
}
if (!project) return;
const absPath = await join(project.path, changes[0]!.path);
await writeClipboard(absPath, 'Failed to copy path');
contextMenu.close();
// dismiss();
}}
/>
<ContextMenuItem
label="Copy Relative Path"
onclick={() => {
try {
if (!project) return;
writeClipboard(changes[0]!.path);
contextMenu.close();
} catch (err) {
console.error('Failed to copy relative path', err);
toasts.error('Failed to copy relative path');
}
onclick={async () => {
if (!project) return;
await writeClipboard(changes[0]!.path, 'Failed to copy relative path');
contextMenu.close();
}}
/>
{/if}
Expand Down
20 changes: 18 additions & 2 deletions apps/desktop/src/lib/backend/clipboard.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import * as toasts from '@gitbutler/ui/toasts';
import { writeText, readText } from '@tauri-apps/plugin-clipboard-manager';

export async function writeClipboard(text: string) {
await writeText(text);
/**
* Copy the provided text into the the system clipboard. Upon completion, a toast will be displayed which contains
* information about the success of this operation.
*
* @param text text to be copied into the system clipboard.
* @param errorMessage optional custom error message which will be displayed if the operation failes. If this is
* not provided, a default generic message will be used.
*/
export async function writeClipboard(text: string, errorMessage = 'Failed to copy') {
await writeText(text)
.then(() => {
toasts.success('Copied to clipboard');
})
.catch((err) => {
toasts.error(errorMessage);
console.error(errorMessage, err);
});
}

export async function readClipboard() {
Expand Down

0 comments on commit ae0c528

Please sign in to comment.