Open History to see the last commit
1import type { ScopedThreadRef, ThreadLinkedPullRequest } from "@t3tools/contracts";
2import { useCallback } from "react";
3
4import { threadEnvironment } from "../state/threads";
5import { useAtomCommand } from "../state/use-atom-command";
6
7/**
8 * Link and unlink, both through `thread.meta.update`. These are the only
9 * callers allowed to send `linkedPullRequest`: the command is multi-field, so
10 * any other caller spreading thread state would silently unlink.
11 */
12export function useThreadPullRequestLinkActions() {
13 const updateThreadMetadata = useAtomCommand(threadEnvironment.updateMetadata, {
14 reportFailure: false,
15 });
16 const linkPullRequest = useCallback(
17 (threadRef: ScopedThreadRef, link: ThreadLinkedPullRequest) =>
18 updateThreadMetadata({
19 environmentId: threadRef.environmentId,
20 input: { threadId: threadRef.threadId, linkedPullRequest: link },
21 }),
22 [updateThreadMetadata],
23 );
24 const unlinkPullRequest = useCallback(
25 (threadRef: ScopedThreadRef) =>
26 updateThreadMetadata({
27 environmentId: threadRef.environmentId,
28 input: { threadId: threadRef.threadId, linkedPullRequest: null },
29 }),
30 [updateThreadMetadata],
31 );
32 return { linkPullRequest, unlinkPullRequest };
33}