refactor: update environment selector and API routes to utilize environmentId for service management; enhance UI with Badge component for production environments

This commit is contained in:
Mauricio Siu
2025-09-01 21:09:30 -06:00
parent e9322fc900
commit 59cbc8ee0d
9 changed files with 136 additions and 82 deletions
@@ -23,6 +23,7 @@ import { Label } from "@/components/ui/label";
import { Textarea } from "@/components/ui/textarea"; import { Textarea } from "@/components/ui/textarea";
import { toast } from "sonner"; import { toast } from "sonner";
import { ChevronDownIcon, PlusIcon, PencilIcon, TrashIcon } from "lucide-react"; import { ChevronDownIcon, PlusIcon, PencilIcon, TrashIcon } from "lucide-react";
import { Badge } from "@/components/ui/badge";
interface Environment { interface Environment {
environmentId: string; environmentId: string;
@@ -165,9 +166,9 @@ export const AdvancedEnvironmentSelector = ({
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span>{currentEnv?.name || "Select Environment"}</span> <span>{currentEnv?.name || "Select Environment"}</span>
{currentEnv?.name === "production" && ( {currentEnv?.name === "production" && (
<span className="px-1.5 py-0.5 text-xs bg-green-100 text-green-800 rounded"> <Badge >
Prod Prod
</span> </Badge>
)} )}
</div> </div>
<ChevronDownIcon className="h-4 w-4" /> <ChevronDownIcon className="h-4 w-4" />
@@ -189,9 +190,9 @@ export const AdvancedEnvironmentSelector = ({
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<span>{environment.name}</span> <span>{environment.name}</span>
{environment.name === "production" && ( {environment.name === "production" && (
<span className="px-1.5 py-0.5 text-xs bg-green-100 text-green-800 rounded"> <Badge >
Prod Prod
</span> </Badge>
)} )}
</div> </div>
{environment.environmentId === currentEnvironmentId && ( {environment.environmentId === currentEnvironmentId && (
@@ -241,7 +242,6 @@ export const AdvancedEnvironmentSelector = ({
</DropdownMenuContent> </DropdownMenuContent>
</DropdownMenu> </DropdownMenu>
{/* Create Environment Dialog */}
<Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}> <Dialog open={isCreateDialogOpen} onOpenChange={setIsCreateDialogOpen}>
<DialogContent> <DialogContent>
<DialogHeader> <DialogHeader>
@@ -285,9 +285,9 @@ export const AdvancedEnvironmentSelector = ({
</Button> </Button>
<Button <Button
onClick={handleCreateEnvironment} onClick={handleCreateEnvironment}
disabled={!name.trim() || createEnvironment.isPending} disabled={!name.trim() || createEnvironment.isLoading}
> >
{createEnvironment.isPending ? "Creating..." : "Create"} {createEnvironment.isLoading ? "Creating..." : "Create"}
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>
@@ -338,9 +338,9 @@ export const AdvancedEnvironmentSelector = ({
</Button> </Button>
<Button <Button
onClick={handleUpdateEnvironment} onClick={handleUpdateEnvironment}
disabled={!name.trim() || updateEnvironment.isPending} disabled={!name.trim() || updateEnvironment.isLoading}
> >
{updateEnvironment.isPending ? "Updating..." : "Update"} {updateEnvironment.isLoading ? "Updating..." : "Update"}
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>
@@ -370,9 +370,9 @@ export const AdvancedEnvironmentSelector = ({
<Button <Button
variant="destructive" variant="destructive"
onClick={handleDeleteEnvironment} onClick={handleDeleteEnvironment}
disabled={deleteEnvironment.isPending} disabled={deleteEnvironment.isLoading}
> >
{deleteEnvironment.isPending ? "Deleting..." : "Delete"} {deleteEnvironment.isLoading ? "Deleting..." : "Delete"}
</Button> </Button>
</DialogFooter> </DialogFooter>
</DialogContent> </DialogContent>
@@ -275,12 +275,19 @@ const EnvironmentPage = (
const { data: currentEnvironment } = api.environment.one.useQuery({ const { data: currentEnvironment } = api.environment.one.useQuery({
environmentId, environmentId,
}); });
const { data: allProjects } = api.project.all.useQuery();
const router = useRouter(); const router = useRouter();
const [isMoveDialogOpen, setIsMoveDialogOpen] = useState(false); const [isMoveDialogOpen, setIsMoveDialogOpen] = useState(false);
const [selectedTargetProject, setSelectedTargetProject] = const [selectedTargetProject, setSelectedTargetProject] =
useState<string>(""); useState<string>("");
const [selectedTargetEnvironment, setSelectedTargetEnvironment] =
useState<string>("");
const { data: selectedProjectEnvironments } = api.environment.byProjectId.useQuery(
{ projectId: selectedTargetProject },
{ enabled: !!selectedTargetProject }
);
const emptyServices = const emptyServices =
!currentEnvironment || !currentEnvironment ||
@@ -484,6 +491,10 @@ const EnvironmentPage = (
toast.error("Please select a target project"); toast.error("Please select a target project");
return; return;
} }
if (!selectedTargetEnvironment) {
toast.error("Please select a target environment");
return;
}
let success = 0; let success = 0;
setIsBulkActionLoading(true); setIsBulkActionLoading(true);
@@ -492,50 +503,54 @@ const EnvironmentPage = (
const service = filteredServices.find((s) => s.id === serviceId); const service = filteredServices.find((s) => s.id === serviceId);
if (!service) continue; if (!service) continue;
// TODO: Update move APIs to use targetEnvironmentId instead of targetProjectId
switch (service.type) { switch (service.type) {
case "application": case "application":
await applicationActions.move.mutateAsync({ await applicationActions.move.mutateAsync({
applicationId: serviceId, applicationId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "compose": case "compose":
await composeActions.move.mutateAsync({ await composeActions.move.mutateAsync({
composeId: serviceId, composeId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "postgres": case "postgres":
await postgresActions.move.mutateAsync({ await postgresActions.move.mutateAsync({
postgresId: serviceId, postgresId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "mysql": case "mysql":
await mysqlActions.move.mutateAsync({ await mysqlActions.move.mutateAsync({
mysqlId: serviceId, mysqlId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "mariadb": case "mariadb":
await mariadbActions.move.mutateAsync({ await mariadbActions.move.mutateAsync({
mariadbId: serviceId, mariadbId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "redis": case "redis":
await redisActions.move.mutateAsync({ await redisActions.move.mutateAsync({
redisId: serviceId, redisId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
case "mongo": case "mongo":
await mongoActions.move.mutateAsync({ await mongoActions.move.mutateAsync({
mongoId: serviceId, mongoId: serviceId,
targetProjectId: selectedTargetProject, targetEnvironmentId: selectedTargetEnvironment,
}); });
break; break;
} }
await utils.environment.one.invalidate({
environmentId,
});
success++; success++;
} catch (error) { } catch (error) {
toast.error( toast.error(
@@ -551,6 +566,9 @@ const EnvironmentPage = (
setIsDropdownOpen(false); setIsDropdownOpen(false);
setIsMoveDialogOpen(false); setIsMoveDialogOpen(false);
setIsBulkActionLoading(false); setIsBulkActionLoading(false);
// Reset move dialog state
setSelectedTargetProject("");
setSelectedTargetEnvironment("");
}; };
const handleBulkDelete = async (deleteVolumes = false) => { const handleBulkDelete = async (deleteVolumes = false) => {
@@ -964,14 +982,12 @@ const EnvironmentPage = (
<DialogHeader> <DialogHeader>
<DialogTitle>Move Services</DialogTitle> <DialogTitle>Move Services</DialogTitle>
<DialogDescription> <DialogDescription>
Select the target project to move{" "} Select the target project and environment to move{" "}
{selectedServices.length} services {selectedServices.length} services
</DialogDescription> </DialogDescription>
</DialogHeader> </DialogHeader>
<div className="flex flex-col gap-4"> <div className="flex flex-col gap-4">
{projectData?.environments?.filter( {allProjects?.filter((p) => p.projectId !== projectId).length === 0 ? (
(p) => p.projectId !== projectId,
).length === 0 ? (
<div className="flex flex-col items-center justify-center gap-2 py-4"> <div className="flex flex-col items-center justify-center gap-2 py-4">
<FolderInput className="h-8 w-8 text-muted-foreground" /> <FolderInput className="h-8 w-8 text-muted-foreground" />
<p className="text-sm text-muted-foreground text-center"> <p className="text-sm text-muted-foreground text-center">
@@ -980,32 +996,70 @@ const EnvironmentPage = (
</p> </p>
</div> </div>
) : ( ) : (
<Select <>
value={selectedTargetProject} {/* Step 1: Select Project */}
onValueChange={setSelectedTargetProject} <div className="flex flex-col gap-2">
> <label className="text-sm font-medium">Target Project</label>
<SelectTrigger> <Select
<SelectValue placeholder="Select target project" /> value={selectedTargetProject}
</SelectTrigger> onValueChange={(value) => {
<SelectContent> setSelectedTargetProject(value);
{allProjects setSelectedTargetEnvironment(""); // Reset environment when project changes
?.filter((p) => p.projectId !== projectId) }}
.map((project) => ( >
<SelectItem <SelectTrigger>
key={project.projectId} <SelectValue placeholder="Select target project" />
value={project.projectId} </SelectTrigger>
> <SelectContent>
{project.name} {allProjects
</SelectItem> ?.filter((p) => p.projectId !== projectId)
))} .map((project) => (
</SelectContent> <SelectItem
</Select> key={project.projectId}
value={project.projectId}
>
{project.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
{/* Step 2: Select Environment (only show if project is selected) */}
{selectedTargetProject && (
<div className="flex flex-col gap-2">
<label className="text-sm font-medium">Target Environment</label>
<Select
value={selectedTargetEnvironment}
onValueChange={setSelectedTargetEnvironment}
>
<SelectTrigger>
<SelectValue placeholder="Select target environment" />
</SelectTrigger>
<SelectContent>
{selectedProjectEnvironments?.map((env) => (
<SelectItem
key={env.environmentId}
value={env.environmentId}
>
{env.name}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
)}
</>
)} )}
</div> </div>
<DialogFooter> <DialogFooter>
<Button <Button
variant="outline" variant="outline"
onClick={() => setIsMoveDialogOpen(false)} onClick={() => {
setIsMoveDialogOpen(false);
setSelectedTargetProject("");
setSelectedTargetEnvironment("");
}}
> >
Cancel Cancel
</Button> </Button>
@@ -1013,9 +1067,9 @@ const EnvironmentPage = (
onClick={handleBulkMove} onClick={handleBulkMove}
isLoading={isBulkActionLoading} isLoading={isBulkActionLoading}
disabled={ disabled={
projectData?.environments?.filter( allProjects?.filter((p) => p.projectId !== projectId).length === 0 ||
(p) => p.projectId !== projectId, !selectedTargetProject ||
).length === 0 !selectedTargetEnvironment
} }
> >
Move Services Move Services
@@ -819,7 +819,7 @@ export const applicationRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
applicationId: z.string(), applicationId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -833,11 +833,11 @@ export const applicationRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -845,7 +845,7 @@ export const applicationRouter = createTRPCRouter({
const updatedApplication = await db const updatedApplication = await db
.update(applications) .update(applications)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(applications.applicationId, input.applicationId)) .where(eq(applications.applicationId, input.applicationId))
.returning() .returning()
+5 -5
View File
@@ -655,7 +655,7 @@ export const composeRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
composeId: z.string(), composeId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -667,18 +667,18 @@ export const composeRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
const updatedCompose = await db const updatedCompose = await db
.update(composeTable) .update(composeTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(composeTable.composeId, input.composeId)) .where(eq(composeTable.composeId, input.composeId))
.returning() .returning()
+5 -5
View File
@@ -337,7 +337,7 @@ export const mariadbRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mariadbId: z.string(), mariadbId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -349,11 +349,11 @@ export const mariadbRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -361,7 +361,7 @@ export const mariadbRouter = createTRPCRouter({
const updatedMariadb = await db const updatedMariadb = await db
.update(mariadbTable) .update(mariadbTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mariadbTable.mariadbId, input.mariadbId)) .where(eq(mariadbTable.mariadbId, input.mariadbId))
.returning() .returning()
+5 -5
View File
@@ -351,7 +351,7 @@ export const mongoRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mongoId: z.string(), mongoId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -363,11 +363,11 @@ export const mongoRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -375,7 +375,7 @@ export const mongoRouter = createTRPCRouter({
const updatedMongo = await db const updatedMongo = await db
.update(mongoTable) .update(mongoTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mongoTable.mongoId, input.mongoId)) .where(eq(mongoTable.mongoId, input.mongoId))
.returning() .returning()
+5 -5
View File
@@ -346,7 +346,7 @@ export const mysqlRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
mysqlId: z.string(), mysqlId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -358,11 +358,11 @@ export const mysqlRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -370,7 +370,7 @@ export const mysqlRouter = createTRPCRouter({
const updatedMysql = await db const updatedMysql = await db
.update(mysqlTable) .update(mysqlTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(mysqlTable.mysqlId, input.mysqlId)) .where(eq(mysqlTable.mysqlId, input.mysqlId))
.returning() .returning()
+5 -5
View File
@@ -367,7 +367,7 @@ export const postgresRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
postgresId: z.string(), postgresId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -381,11 +381,11 @@ export const postgresRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -393,7 +393,7 @@ export const postgresRouter = createTRPCRouter({
const updatedPostgres = await db const updatedPostgres = await db
.update(postgresTable) .update(postgresTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(postgresTable.postgresId, input.postgresId)) .where(eq(postgresTable.postgresId, input.postgresId))
.returning() .returning()
+5 -5
View File
@@ -330,7 +330,7 @@ export const redisRouter = createTRPCRouter({
.input( .input(
z.object({ z.object({
redisId: z.string(), redisId: z.string(),
targetProjectId: z.string(), targetEnvironmentId: z.string(),
}), }),
) )
.mutation(async ({ input, ctx }) => { .mutation(async ({ input, ctx }) => {
@@ -342,11 +342,11 @@ export const redisRouter = createTRPCRouter({
}); });
} }
const targetProject = await findProjectById(input.targetProjectId); const targetEnvironment = await findEnvironmentById(input.targetEnvironmentId);
if (targetProject.organizationId !== ctx.session.activeOrganizationId) { if (targetEnvironment.project.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ throw new TRPCError({
code: "UNAUTHORIZED", code: "UNAUTHORIZED",
message: "You are not authorized to move to this project", message: "You are not authorized to move to this environment",
}); });
} }
@@ -354,7 +354,7 @@ export const redisRouter = createTRPCRouter({
const updatedRedis = await db const updatedRedis = await db
.update(redisTable) .update(redisTable)
.set({ .set({
projectId: input.targetProjectId, environmentId: input.targetEnvironmentId,
}) })
.where(eq(redisTable.redisId, input.redisId)) .where(eq(redisTable.redisId, input.redisId))
.returning() .returning()