Open History to see the last commit
1import type {
2 EnvironmentId,
3 ProviderInstanceId,
4 ProviderRuntimeModeLimit,
5} from "@t3tools/contracts";
6import { createContext } from "react";
7
8import { useEnvironmentQuery } from "./query";
9import { serverEnvironment } from "./server";
10
11export interface RuntimeModeLimitsTarget {
12 readonly environmentId: EnvironmentId;
13 readonly instanceId: ProviderInstanceId;
14 readonly cwd: string;
15}
16
17const NO_LIMITS: ReadonlyArray<ProviderRuntimeModeLimit> = [];
18
19/** The folder and provider whose limits an open mode picker should check again. */
20export const RuntimeModeLimitsTargetContext = createContext<RuntimeModeLimitsTarget | null>(null);
21
22export function runtimeModeLimitsTarget(
23 environmentId: EnvironmentId | null,
24 instanceId: ProviderInstanceId | undefined,
25 cwd: string | null,
26): RuntimeModeLimitsTarget | null {
27 return environmentId && instanceId && cwd ? { environmentId, instanceId, cwd } : null;
28}
29
30/**
31 * Modes the provider offers that cannot run in `target`'s folder. Empty until the server
32 * answers and when it cannot, so a mode is only ever marked on a known reason. `mountScope`
33 * gives a second reader its own mount, which asks again when the answer is a few seconds old.
34 */
35export function useProviderRuntimeModeLimits(
36 target: RuntimeModeLimitsTarget | null,
37 mountScope?: "picker",
38): ReadonlyArray<ProviderRuntimeModeLimit> {
39 const { data } = useEnvironmentQuery(
40 target === null
41 ? null
42 : serverEnvironment.providerRuntimeModeLimits(
43 {
44 environmentId: target.environmentId,
45 input: { instanceId: target.instanceId, cwd: target.cwd },
46 },
47 mountScope,
48 ),
49 );
50 return data?.limits ?? NO_LIMITS;
51}