The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects"; |
| 2 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 3 | import { AuthSourceControlWriteScope, resolveEnvironmentMachineKind } from "@t3tools/contracts"; |
| 4 | |
| 5 | import type { EnvironmentOption } from "../components/BranchToolbar.logic"; |
| 6 | import { useEnvironments } from "../state/environments"; |
| 7 | import { useEnvironmentsWithScope } from "../state/session"; |
| 8 | |
| 9 | export type AddProjectTarget = Pick<EnvironmentOption, "environmentId" | "label" | "machine">; |
| 10 | |
| 11 | /** |
| 12 | * Connected machines that do not have the project yet and can clone it. |
| 13 | * `projectEnvironments` is the project's current machine list, from |
| 14 | * `useLogicalProjectEnvironments`. Projects without a git remote get none. |
| 15 | */ |
| 16 | export function useAddProjectTargets( |
| 17 | project: EnvironmentProject | null, |
| 18 | projectEnvironments: readonly Pick<EnvironmentOption, "environmentId">[] | undefined, |
| 19 | ): readonly AddProjectTarget[] { |
| 20 | const { environments } = useEnvironments(); |
| 21 | const canClone = useEnvironmentsWithScope(environments, AuthSourceControlWriteScope); |
| 22 | if (!project?.repositoryIdentity?.locator.remoteUrl) return []; |
| 23 | const hasProject = new Set([ |
| 24 | project.environmentId, |
| 25 | ...(projectEnvironments ?? []).map((env) => env.environmentId), |
| 26 | ]); |
| 27 | return environments |
| 28 | .filter( |
| 29 | (environment) => |
| 30 | !hasProject.has(environment.environmentId) && |
| 31 | canClone.has(environment.environmentId) && |
| 32 | canCreateProjectInEnvironment(environment.connection.phase) && |
| 33 | environment.serverConfig !== null, |
| 34 | ) |
| 35 | .map((environment) => ({ |
| 36 | environmentId: environment.environmentId, |
| 37 | label: environment.label, |
| 38 | machine: resolveEnvironmentMachineKind(environment.serverConfig), |
| 39 | })) |
| 40 | .toSorted((a, b) => a.label.localeCompare(b.label)); |
| 41 | } |