|
| 1 | +import { MetaFunction } from "@remix-run/react"; |
| 2 | +import { type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/server-runtime"; |
| 3 | +import { tryCatch } from "@trigger.dev/core"; |
| 4 | +import { typedjson, useTypedLoaderData } from "remix-typedjson"; |
| 5 | +import { z } from "zod"; |
| 6 | +import { AdminDebugTooltip } from "~/components/admin/debugTooltip"; |
| 7 | +import { EnvironmentCombo } from "~/components/environments/EnvironmentLabel"; |
| 8 | +import { |
| 9 | + MainCenteredContainer, |
| 10 | + MainHorizontallyCenteredContainer, |
| 11 | + PageBody, |
| 12 | + PageContainer, |
| 13 | +} from "~/components/layout/AppLayout"; |
| 14 | +import { Header2 } from "~/components/primitives/Headers"; |
| 15 | +import { InputGroup } from "~/components/primitives/InputGroup"; |
| 16 | +import { Label } from "~/components/primitives/Label"; |
| 17 | +import { NavBar, PageAccessories, PageTitle } from "~/components/primitives/PageHeader"; |
| 18 | +import { Paragraph } from "~/components/primitives/Paragraph"; |
| 19 | +import * as Property from "~/components/primitives/PropertyTable"; |
| 20 | +import { |
| 21 | + Table, |
| 22 | + TableBody, |
| 23 | + TableCell, |
| 24 | + TableHeader, |
| 25 | + TableHeaderCell, |
| 26 | + TableRow, |
| 27 | +} from "~/components/primitives/Table"; |
| 28 | +import { useOrganization } from "~/hooks/useOrganizations"; |
| 29 | +import { redirectWithErrorMessage, redirectWithSuccessMessage } from "~/models/message.server"; |
| 30 | +import { findProjectBySlug } from "~/models/project.server"; |
| 31 | +import { |
| 32 | + EnvironmentWithConcurrency, |
| 33 | + ManageConcurrencyPresenter, |
| 34 | +} from "~/presenters/v3/ManageConcurrencyPresenter.server"; |
| 35 | +import { requireUser, requireUserId } from "~/services/session.server"; |
| 36 | +import { cn } from "~/utils/cn"; |
| 37 | +import { EnvironmentParamSchema, regionsPath, v3BillingPath } from "~/utils/pathBuilder"; |
| 38 | +import { SetDefaultRegionService } from "~/v3/services/setDefaultRegion.server"; |
| 39 | +import { useCurrentPlan } from "../_app.orgs.$organizationSlug/route"; |
| 40 | +import { useFeatures } from "~/hooks/useFeatures"; |
| 41 | +import { LinkButton } from "~/components/primitives/Buttons"; |
| 42 | + |
| 43 | +export const meta: MetaFunction = () => { |
| 44 | + return [ |
| 45 | + { |
| 46 | + title: `Concurrency | Trigger.dev`, |
| 47 | + }, |
| 48 | + ]; |
| 49 | +}; |
| 50 | + |
| 51 | +export const loader = async ({ request, params }: LoaderFunctionArgs) => { |
| 52 | + const userId = await requireUserId(request); |
| 53 | + const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params); |
| 54 | + |
| 55 | + const project = await findProjectBySlug(organizationSlug, projectParam, userId); |
| 56 | + if (!project) { |
| 57 | + throw new Response(undefined, { |
| 58 | + status: 404, |
| 59 | + statusText: "Project not found", |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + const presenter = new ManageConcurrencyPresenter(); |
| 64 | + const [error, result] = await tryCatch( |
| 65 | + presenter.call({ |
| 66 | + userId: userId, |
| 67 | + projectId: project.id, |
| 68 | + organizationId: project.organizationId, |
| 69 | + }) |
| 70 | + ); |
| 71 | + |
| 72 | + if (error) { |
| 73 | + throw new Response(undefined, { |
| 74 | + status: 400, |
| 75 | + statusText: error.message, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return typedjson(result); |
| 80 | +}; |
| 81 | + |
| 82 | +const FormSchema = z.object({ |
| 83 | + regionId: z.string(), |
| 84 | +}); |
| 85 | + |
| 86 | +export const action = async ({ request, params }: ActionFunctionArgs) => { |
| 87 | + const user = await requireUser(request); |
| 88 | + const { organizationSlug, projectParam, envParam } = EnvironmentParamSchema.parse(params); |
| 89 | + |
| 90 | + const project = await findProjectBySlug(organizationSlug, projectParam, user.id); |
| 91 | + |
| 92 | + const redirectPath = regionsPath( |
| 93 | + { slug: organizationSlug }, |
| 94 | + { slug: projectParam }, |
| 95 | + { slug: envParam } |
| 96 | + ); |
| 97 | + |
| 98 | + if (!project) { |
| 99 | + throw redirectWithErrorMessage(redirectPath, request, "Project not found"); |
| 100 | + } |
| 101 | + |
| 102 | + const formData = await request.formData(); |
| 103 | + const parsedFormData = FormSchema.safeParse(Object.fromEntries(formData)); |
| 104 | + |
| 105 | + if (!parsedFormData.success) { |
| 106 | + throw redirectWithErrorMessage(redirectPath, request, "No region specified"); |
| 107 | + } |
| 108 | + |
| 109 | + const service = new SetDefaultRegionService(); |
| 110 | + const [error, result] = await tryCatch( |
| 111 | + service.call({ |
| 112 | + projectId: project.id, |
| 113 | + regionId: parsedFormData.data.regionId, |
| 114 | + isAdmin: user.admin || user.isImpersonating, |
| 115 | + }) |
| 116 | + ); |
| 117 | + |
| 118 | + if (error) { |
| 119 | + return redirectWithErrorMessage(redirectPath, request, error.message); |
| 120 | + } |
| 121 | + |
| 122 | + return redirectWithSuccessMessage(redirectPath, request, `Set ${result.name} as default`); |
| 123 | +}; |
| 124 | + |
| 125 | +export default function Page() { |
| 126 | + const { canAddConcurrency, environments } = useTypedLoaderData<typeof loader>(); |
| 127 | + const organization = useOrganization(); |
| 128 | + |
| 129 | + return ( |
| 130 | + <PageContainer> |
| 131 | + <NavBar> |
| 132 | + <PageTitle title="Concurrency" /> |
| 133 | + <PageAccessories> |
| 134 | + <AdminDebugTooltip> |
| 135 | + <Property.Table> |
| 136 | + {environments.map((environment) => ( |
| 137 | + <Property.Item key={environment.id}> |
| 138 | + <Property.Label> |
| 139 | + {environment.type}{" "} |
| 140 | + {environment.branchName ? ` (${environment.branchName})` : ""} |
| 141 | + </Property.Label> |
| 142 | + <Property.Value>{environment.id}</Property.Value> |
| 143 | + </Property.Item> |
| 144 | + ))} |
| 145 | + </Property.Table> |
| 146 | + </AdminDebugTooltip> |
| 147 | + </PageAccessories> |
| 148 | + </NavBar> |
| 149 | + <PageBody scrollable={false}> |
| 150 | + <MainHorizontallyCenteredContainer> |
| 151 | + {canAddConcurrency ? ( |
| 152 | + <div> |
| 153 | + <div className="mb-3 border-b border-grid-dimmed pb-1"> |
| 154 | + <Header2>Manage your concurrency</Header2> |
| 155 | + </div> |
| 156 | + <div className="flex flex-col gap-6"> |
| 157 | + <InputGroup fullWidth> |
| 158 | + <div className="flex w-full items-center justify-between"> |
| 159 | + <Label>Secret key</Label> |
| 160 | + </div> |
| 161 | + </InputGroup> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + ) : ( |
| 165 | + <NotUpgradable environments={environments} /> |
| 166 | + )} |
| 167 | + </MainHorizontallyCenteredContainer> |
| 168 | + </PageBody> |
| 169 | + </PageContainer> |
| 170 | + ); |
| 171 | +} |
| 172 | + |
| 173 | +function NotUpgradable({ environments }: { environments: EnvironmentWithConcurrency[] }) { |
| 174 | + const { isManagedCloud } = useFeatures(); |
| 175 | + const plan = useCurrentPlan(); |
| 176 | + const organization = useOrganization(); |
| 177 | + |
| 178 | + return ( |
| 179 | + <div className="flex flex-col gap-3"> |
| 180 | + <div className="border-b border-grid-dimmed pb-1"> |
| 181 | + <Header2>Your concurrency</Header2> |
| 182 | + </div> |
| 183 | + {isManagedCloud ? ( |
| 184 | + <> |
| 185 | + <Paragraph variant="small"> |
| 186 | + Concurrency limits determine how many runs you can execute at the same time. You can |
| 187 | + upgrade your plan to get more concurrency. You are currently on the{" "} |
| 188 | + {plan?.v3Subscription?.plan?.title ?? "Free"} plan. |
| 189 | + </Paragraph> |
| 190 | + <LinkButton variant="primary/small" to={v3BillingPath(organization)}> |
| 191 | + Upgrade for more concurrency |
| 192 | + </LinkButton> |
| 193 | + </> |
| 194 | + ) : null} |
| 195 | + <div className="mt-3 flex flex-col gap-3"> |
| 196 | + <Table> |
| 197 | + <TableHeader> |
| 198 | + <TableRow> |
| 199 | + <TableHeaderCell className="pl-0">Environment</TableHeaderCell> |
| 200 | + <TableHeaderCell alignment="right">Concurrency limit</TableHeaderCell> |
| 201 | + </TableRow> |
| 202 | + </TableHeader> |
| 203 | + <TableBody> |
| 204 | + {environments.map((environment) => ( |
| 205 | + <TableRow key={environment.id}> |
| 206 | + <TableCell className="pl-0"> |
| 207 | + <EnvironmentCombo environment={environment} /> |
| 208 | + </TableCell> |
| 209 | + <TableCell alignment="right">{environment.maximumConcurrencyLimit}</TableCell> |
| 210 | + </TableRow> |
| 211 | + ))} |
| 212 | + </TableBody> |
| 213 | + </Table> |
| 214 | + </div> |
| 215 | + </div> |
| 216 | + ); |
| 217 | +} |
0 commit comments