Skip to content

Fix edge cases with deleting last org/project #892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions apps/webapp/app/components/navigation/SideMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,31 @@ function ProjectSelector({
<Fragment key={organization.id}>
<PopoverSectionHeader title={organization.title} />
<div className="flex flex-col gap-1 p-1">
{organization.projects.map((p) => {
const isSelected = p.id === project.id;
return (
<PopoverMenuItem
key={p.id}
to={projectPath(organization, p)}
title={
<div className="flex w-full items-center justify-between text-bright">
<span className="grow truncate text-left">{p.name}</span>
<MenuCount count={p.jobCount} />
</div>
}
isSelected={isSelected}
icon="folder"
/>
);
})}
{organization.projects.length > 0 ? (
organization.projects.map((p) => {
const isSelected = p.id === project.id;
return (
<PopoverMenuItem
key={p.id}
to={projectPath(organization, p)}
title={
<div className="flex w-full items-center justify-between text-bright">
<span className="grow truncate text-left">{p.name}</span>
<MenuCount count={p.jobCount} />
</div>
}
isSelected={isSelected}
icon="folder"
/>
);
})
) : (
<PopoverMenuItem
to={newProjectPath(organization)}
title="New project"
icon="plus"
Comment on lines +379 to +382
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you delete the last project in an org and then navigate somehow to a different org you end up with an org you can't get to in the nav. This allows you to create a project for that org.

/>
)}
</div>
</Fragment>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ export class NewOrganizationPresenter {

public async call({ userId }: { userId: User["id"] }) {
const organizations = await this.#prismaClient.organization.findMany({
select: {
projects: {
where: { deletedAt: null },
},
},
where: { members: { some: { userId } } },
});

return {
hasOrganizations: organizations.length > 0,
hasOrganizations: organizations.filter((o) => o.projects.length > 0).length > 0,
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class SelectBestProjectPresenter {
const projectId = await getCurrentProjectId(request);
if (projectId) {
const project = await this.#prismaClient.project.findUnique({
where: { id: projectId, organization: { members: { some: { userId } } } },
where: { id: projectId, deletedAt: null, organization: { members: { some: { userId } } } },
include: { organization: true },
});
if (project) {
Expand All @@ -28,6 +28,7 @@ export class SelectBestProjectPresenter {
organization: true,
},
where: {
deletedAt: null,
organization: {
members: { some: { userId } },
},
Expand Down
15 changes: 12 additions & 3 deletions apps/webapp/app/routes/_app.orgs.new/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { parse } from "@conform-to/zod";
import { RadioGroup } from "@radix-ui/react-radio-group";
import type { ActionFunction, LoaderFunctionArgs } from "@remix-run/node";
import { json, redirect } from "@remix-run/node";
import { Form, useActionData } from "@remix-run/react";
import { Form, useActionData, useNavigation } from "@remix-run/react";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { z } from "zod";
import { MainCenteredContainer } from "~/components/layout/AppLayout";
Expand All @@ -23,7 +23,7 @@ import { createOrganization } from "~/models/organization.server";
import { NewOrganizationPresenter } from "~/presenters/NewOrganizationPresenter.server";
import { commitCurrentProjectSession, setCurrentProjectId } from "~/services/currentProject.server";
import { requireUserId } from "~/services/session.server";
import { plansPath, projectPath, rootPath, selectPlanPath } from "~/utils/pathBuilder";
import { projectPath, rootPath, selectPlanPath } from "~/utils/pathBuilder";

const schema = z.object({
orgName: z.string().min(3).max(50),
Expand Down Expand Up @@ -86,6 +86,7 @@ export default function NewOrganizationPage() {
const { hasOrganizations } = useTypedLoaderData<typeof loader>();
const lastSubmission = useActionData();
const { isManagedCloud } = useFeatures();
const navigation = useNavigation();

const [form, { orgName, projectName }] = useForm({
id: "create-organization",
Expand All @@ -95,8 +96,11 @@ export default function NewOrganizationPage() {
return parse(formData, { schema });
},
shouldRevalidate: "onSubmit",
shouldValidate: "onSubmit",
});

const isLoading = navigation.state === "submitting" || navigation.state === "loading";

return (
<MainCenteredContainer className="max-w-[22rem]">
<FormTitle LeadingIcon="organization" title="Create an Organization" />
Expand Down Expand Up @@ -161,7 +165,12 @@ export default function NewOrganizationPage() {

<FormButtons
confirmButton={
<Button type="submit" variant={"primary/small"} TrailingIcon="arrow-right">
<Button
type="submit"
variant={"primary/small"}
TrailingIcon="arrow-right"
disabled={isLoading}
>
Create
</Button>
}
Expand Down
10 changes: 2 additions & 8 deletions apps/webapp/app/services/deleteOrganization.server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { DateFormatter } from "@internationalized/date";
import { PrismaClient } from "@trigger.dev/database";
import { prisma } from "~/db.server";
import { DisableJobService } from "./jobs/disableJob.server";
import { AuthenticatedEnvironment } from "./apiAuth.server";
import { DeleteJobService } from "./jobs/deleteJob.server";
import { DeleteEndpointService } from "./endpoints/deleteEndpointService";
import { logger } from "./logger.server";
import { DisableScheduleSourceService } from "./schedules/disableScheduleSource.server";
import { featuresForRequest } from "~/features.server";
import { DeleteProjectService } from "./deleteProject.server";
import { BillingService } from "./billing.server";
import { DateFormatter } from "@internationalized/date";
import { DeleteProjectService } from "./deleteProject.server";

export class DeleteOrganizationService {
#prismaClient: PrismaClient;
Expand Down
9 changes: 4 additions & 5 deletions apps/webapp/app/services/deleteProject.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { PrismaClient } from "@trigger.dev/database";
import { prisma } from "~/db.server";
import { DisableJobService } from "./jobs/disableJob.server";
import { AuthenticatedEnvironment } from "./apiAuth.server";
import { DeleteJobService } from "./jobs/deleteJob.server";
import { DeleteEndpointService } from "./endpoints/deleteEndpointService";
import { logger } from "./logger.server";
import { DisableScheduleSourceService } from "./schedules/disableScheduleSource.server";

type Options = { projectId: string; userId: string } | { projectSlug: string; userId: string };
type Options = ({ projectId: string } | { projectSlug: string }) & {
userId: string;
};

export class DeleteProjectService {
#prismaClient: PrismaClient;
Expand Down Expand Up @@ -52,7 +51,7 @@ export class DeleteProjectService {
}

if (project.deletedAt) {
throw new Error("Project already deleted");
return;
}

//disable and delete all jobs
Expand Down