Skip to content
Source
Sign inSign up

sourceControlAppAccessToast.ts

38 lines1.6 KB

1import type { EnvironmentId } from "@t3tools/contracts";
2import { appSignInTargetForRemote } from "@t3tools/client-runtime/source-control-clients";
3import { runAtomCommand } from "@t3tools/client-runtime/state/runtime";
4import { appAtomRegistry } from "../rpc/atomRegistry";
5import { sourceControlEnvironment } from "../state/sourceControl";
6import { toastManager } from "./ui/toast";
7​
8/**
9 * After a clone, suggests installing the forge app on the repository's owner when the server is
10 * signed in with it but the app does not reach the repository.
11 */
12export async function suggestAppAccessForRemote(
13 environmentId: EnvironmentId,
14 remoteUrl: string,
15): Promise<void> {
16 const target = appSignInTargetForRemote(remoteUrl);
17 if (target === null) return;
18 const result = await runAtomCommand(
19 appAtomRegistry,
20 sourceControlEnvironment.checkAppAccess,
21 { environmentId, input: target.input },
22 { label: "check app access", reportFailure: false, reportDefect: false },
23 );
24 if (result._tag !== "Success" || result.value.status !== "not-covered") return;
25 const owner = target.input.repository.split("/")[0] ?? target.input.repository;
26 const id = toastManager.add({
27 type: "info",
28 title: `Give ${target.appSignIn.appName} access to ${owner}`,
29 description: `It isn't installed there, so GitHub requests for ${target.input.repository} use your own login and its lower rate limit.`,
30 actionProps: {
31 children: "Install",
32 onClick: () => {
33 toastManager.close(id);
34 window.open(target.appSignIn.installUrl, "_blank", "noopener,noreferrer");
35 },
36 },
37 });