Files
triggerdotdev--trigger.dev/apps/webapp/app/models/message.server.ts
Matt Aitken bee59de3a0 Concurrency self serve (#2681)
* Don't use the organization max concurrency anymore

* Early draft of the concurrency page

* WIP adding a new stepper input component

* Move stepper to be alphabetical

* When max value is reached, disabled the + button

* Show placeholder if you delete all numbers

* Make all the html input values available to the component

* Adds size variants

* Move stepper into its own component

* Work on showing the extra concurrency

* The purchase form styling and functionality (minus actually purchasing)

* New style for outline input fields

* Concurrency purchasing working

* Purchasing concurrency and quota emails working

* Improvements to the modal

* Show cost breakdown in the modal

* Fix for allocated concurrency including DEV

* Improved types

* Allocating concurrency is working

* Live updates total env concurrency

* Implemented reset

* Fix for concurrency allocation editing across multiple projects

* Tabular numbers

* Added an error from allocating concurrency

* Fixes for allocating concurrency where it didn't calculate correctly

* "Increase limit" link to concurrency page

* Indent environments

* Added Preview limit when updating concurrency for an org

* Show error when changing plan fails

* Added maximumProjectCount column to Org

* Limit project count and display a rich error toast (with title and button now)

* Added title and button to toasts. Use it for new project error

* @trigger.dev/platform 1.0.20

* Allow submitting zero concurrency so you can downgrade back to nothing

* Use the server as the truth for omitted environments

* Updated the pricing panels

---------

Co-authored-by: James Ritchie <james@trigger.dev>
2025-11-19 11:22:23 +00:00

237 lines
5.4 KiB
TypeScript

import { json, createCookieSessionStorage, type Session } from "@remix-run/node";
import { redirect, typedjson } from "remix-typedjson";
import { ButtonVariant } from "~/components/primitives/Buttons";
import { env } from "~/env.server";
import { type FeedbackType } from "~/routes/resources.feedback";
export type ToastMessage = {
message: string;
type: "success" | "error";
options: Required<ToastMessageOptions>;
};
export type ToastMessageAction = {
label: string;
variant?: ButtonVariant;
action:
| {
type: "link";
path: string;
}
| {
type: "help";
feedbackType: FeedbackType;
};
};
export type ToastMessageOptions = {
title?: string;
/** Ephemeral means it disappears after a delay, defaults to true */
ephemeral?: boolean;
/** This display a button and make it not ephemeral, unless ephemeral is explicitlyset to false */
action?: ToastMessageAction;
};
const ONE_YEAR = 1000 * 60 * 60 * 24 * 365;
export const { commitSession, getSession } = createCookieSessionStorage({
cookie: {
name: "__message",
path: "/",
httpOnly: true,
sameSite: "lax",
secrets: [env.SESSION_SECRET],
secure: env.NODE_ENV === "production",
},
});
export function setSuccessMessage(
session: Session,
message: string,
options?: ToastMessageOptions
) {
session.flash("toastMessage", {
message,
type: "success",
options: {
...options,
ephemeral: options?.ephemeral ?? true,
},
} as ToastMessage);
}
export function setErrorMessage(session: Session, message: string, options?: ToastMessageOptions) {
session.flash("toastMessage", {
message,
type: "error",
options: {
...options,
ephemeral: options?.ephemeral ?? true,
},
} as ToastMessage);
}
export async function setRequestErrorMessage(
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setErrorMessage(session, message, options);
return session;
}
export async function setRequestSuccessMessage(
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setSuccessMessage(session, message, options);
return session;
}
export async function setToastMessageCookie(session: Session) {
return {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
};
}
export async function jsonWithSuccessMessage(
data: any,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setSuccessMessage(session, message, options);
return json(data, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function jsonWithErrorMessage(
data: any,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setErrorMessage(session, message, options);
return json(data, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function typedJsonWithSuccessMessage<T>(
data: T,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setSuccessMessage(session, message, options);
return typedjson(data, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function typedJsonWithErrorMessage<T>(
data: T,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setErrorMessage(session, message, options);
return typedjson(data, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function redirectWithSuccessMessage(
path: string,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setSuccessMessage(session, message, options);
return redirect(path, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function redirectWithErrorMessage(
path: string,
request: Request,
message: string,
options?: ToastMessageOptions
) {
const session = await getSession(request.headers.get("cookie"));
setErrorMessage(session, message, options);
return redirect(path, {
headers: {
"Set-Cookie": await commitSession(session, {
expires: new Date(Date.now() + ONE_YEAR),
}),
},
});
}
export async function redirectBackWithErrorMessage(
request: Request,
message: string,
options?: ToastMessageOptions
) {
const url = new URL(request.url);
return redirectWithErrorMessage(url.pathname, request, message, options);
}
export async function redirectBackWithSuccessMessage(
request: Request,
message: string,
options?: ToastMessageOptions
) {
const url = new URL(request.url);
return redirectWithSuccessMessage(url.pathname, request, message, options);
}