@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 68 lines 2.01 KB
Raw
1 import type { ContextMenuItem } from "@t3tools/contracts";
2 import {
3 type SourceControlDefinition,
4 UNKNOWN_SOURCE_CONTROL_CLIENT,
5 } from "@t3tools/client-runtime/source-control-clients";
6
7 import { msg } from "gt-react";
8
9 import { writeTextToClipboard } from "~/hooks/useCopyToClipboard";
10 import { m, type MessageResolver } from "~/i18n/messages";
11 import { readLocalApi } from "~/localApi";
12
13 import { toastManager } from "../ui/toast";
14
15 export type PullRequestLinkContextMenuAction = "copy-link" | "open-external";
16
17 export const openOnHostLabel = (
18 host: SourceControlDefinition,
19 resolve: MessageResolver = m,
20 ): string =>
21 host === UNKNOWN_SOURCE_CONTROL_CLIENT
22 ? resolve(msg("Open on host"))
23 : resolve(msg("Open on {host}", { host: host.label }));
24
25 function pullRequestLinkContextMenuItems(
26 openLabel: string,
27 ): readonly ContextMenuItem<PullRequestLinkContextMenuAction>[] {
28 return [
29 { id: "copy-link", label: m(msg("Copy link")), icon: "copy" },
30 { id: "open-external", label: openLabel },
31 ];
32 }
33
34 export async function showPullRequestLinkContextMenu({
35 url,
36 openLabel,
37 position,
38 }: {
39 readonly url: string;
40 readonly openLabel: string;
41 readonly position: { readonly x: number; readonly y: number };
42 }): Promise<void> {
43 const api = readLocalApi();
44 if (!api) return;
45 let action: PullRequestLinkContextMenuAction | null = null;
46 try {
47 action = await api.contextMenu.show(pullRequestLinkContextMenuItems(openLabel), position);
48 } catch {
49 return;
50 }
51 try {
52 if (action === "copy-link")
53 await writeTextToClipboard(url, {
54 target: "link",
55 unavailableMessage: msg("Clipboard API is unavailable while copying link."),
56 failureMessage: msg("Failed to copy link to the clipboard."),
57 });
58 else if (action === "open-external") await api.shell.openExternal(url);
59 } catch {
60 toastManager.add({
61 type: "error",
62 title:
63 action === "copy-link"
64 ? m(msg("Could not copy the link"))
65 : m(msg("Could not open the link")),
66 });
67 }
68 }