Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

LinkBranchPullRequestButton.tsx

54 lines1.7 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { Link2 } from "lucide-react";
3import { useState } from "react";
4import { usePullRequestLinking } from "~/hooks/usePullRequestLinking";
5import { Button } from "../ui/button";
6import { toastManager } from "../ui/toast";
7import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
8​
9/** Adopts a branch discovery as a durable link, even after the thread changes branches. */
10export function LinkBranchPullRequestButton({
11 threadRef,
12 url,
13}: {
14 threadRef: ScopedThreadRef;
15 url: string;
16}) {
17 const linking = usePullRequestLinking(threadRef.environmentId);
18 const [pending, setPending] = useState(false);
19 if (!linking.canLink(url)) return null;
20 return (
21 <Tooltip>
22 <TooltipTrigger
23 render={
24 <Button
25 size="icon-tiny"
26 variant="ghost-muted"
27 aria-label="Link this PR"
28 disabled={pending}
29 onPointerDown={(event) => event.stopPropagation()}
30 onClick={async (event) => {
31 event.preventDefault();
32 event.stopPropagation();
33 setPending(true);
34 try {
35 await linking.changeLink(threadRef, url, true);
36 } catch (error) {
37 toastManager.add({
38 type: "error",
39 title: "Could not link pull request",
40 description: error instanceof Error ? error.message : String(error),
41 });
42 } finally {
43 setPending(false);
44 }
45 }}
46 >
47 <Link2 className="size-3" />
48 </Button>
49 }
50 />
51 <TooltipPopup>Link this PR to keep it with this thread</TooltipPopup>
52 </Tooltip>
53 );