Skip to content

Open-source status page, incident response and uptime monitoring

feat: remove user if owner (#515)

Maximilian Kaskea2bdd7057ec5
diff --git a/apps/web/src/components/data-table/user/columns.tsx b/apps/web/src/components/data-table/user/columns.tsx
index 747f472a7..8ce7d85a2 100644
--- a/apps/web/src/components/data-table/user/columns.tsx
+++ b/apps/web/src/components/data-table/user/columns.tsx
@@ -44,14 +44,16 @@ export const columns: ColumnDef<User & { role: WorkspaceRole }>[] = [
return <span>{formatDate(row.getValue("createdAt"))}</span>;
},
},
- // {
- // id: "actions",
- // cell: ({ row }) => {
- // return (
- // <div className="text-right">
- // <DataTableRowActions row={row} />
- // </div>
- // );
- // },
- // },
+ {
+ id: "actions",
+ cell: ({ row }) => {
+ const role = row.original.role;
+ if (role === "owner") return null;
+ return (
+ <div className="text-right">
+ <DataTableRowActions row={row} />
+ </div>
+ );
+ },
+ },
];
diff --git a/apps/web/src/components/data-table/user/data-table-row-actions.tsx b/apps/web/src/components/data-table/user/data-table-row-actions.tsx
index f2a50ca5d..7abbd673a 100644
--- a/apps/web/src/components/data-table/user/data-table-row-actions.tsx
+++ b/apps/web/src/components/data-table/user/data-table-row-actions.tsx
@@ -1,14 +1,11 @@
-// TODO: once TeamM=ember is released, use that
-
"use client";
import * as React from "react";
-import Link from "next/link";
import { useRouter } from "next/navigation";
import type { Row } from "@tanstack/react-table";
import { MoreHorizontal } from "lucide-react";
-import { selectPageSchema } from "@openstatus/db/src/schema";
+import { selectUserSchema } from "@openstatus/db/src/schema";
import {
AlertDialog,
AlertDialogAction,
@@ -37,7 +34,7 @@ interface DataTableRowActionsProps<TData> {
export function DataTableRowActions<TData>({
row,
}: DataTableRowActionsProps<TData>) {
- const page = selectPageSchema.parse(row.original);
+ const user = selectUserSchema.parse(row.original);
const router = useRouter();
const { toast } = useToastAction();
const [alertOpen, setAlertOpen] = React.useState(false);
@@ -46,9 +43,9 @@ export function DataTableRowActions<TData>({
async function onDelete() {
startTransition(async () => {
try {
- if (!page.id) return;
- await api.page.delete.mutate({ id: page.id });
- toast("deleted");
+ if (!user.id) return;
+ await api.workspace.removeWorkspaceUser.mutate({ id: user.id });
+ toast("removed");
router.refresh();
setAlertOpen(false);
} catch {
@@ -70,22 +67,9 @@ export function DataTableRowActions<TData>({
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
- <Link href={`./status-pages/edit?id=${page.id}`}>
- <DropdownMenuItem>Edit</DropdownMenuItem>
- </Link>
- <Link
- href={
- process.env.NODE_ENV === "production"
- ? `https://${page.slug}.openstatus.dev`
- : `/status-page/${page.slug}`
- }
- target="_blank"
- >
- <DropdownMenuItem>Visit</DropdownMenuItem>
- </Link>
<AlertDialogTrigger asChild>
<DropdownMenuItem className="text-destructive focus:bg-destructive focus:text-background">
- Delete
+ Remove
</DropdownMenuItem>
</AlertDialogTrigger>
</DropdownMenuContent>
@@ -94,8 +78,8 @@ export function DataTableRowActions<TData>({
<AlertDialogHeader>
<AlertDialogTitle>Are you absolutely sure?</AlertDialogTitle>
<AlertDialogDescription>
- This action cannot be undone. This will permanently delete the
- monitor.
+ This action cannot be undone. This will remove the user from your
+ team.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
@@ -108,7 +92,7 @@ export function DataTableRowActions<TData>({
disabled={isPending}
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
>
- {!isPending ? "Delete" : <LoadingAnimation />}
+ {!isPending ? "Remove" : <LoadingAnimation />}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
diff --git a/apps/web/src/hooks/use-toast-action.tsx b/apps/web/src/hooks/use-toast-action.tsx
index bccd0de63..b59bb514d 100644
--- a/apps/web/src/hooks/use-toast-action.tsx
+++ b/apps/web/src/hooks/use-toast-action.tsx
@@ -21,6 +21,7 @@ const config = {
},
success: { title: "Success" },
deleted: { title: "Deleted successfully" }, // TODO: we are not informing the user besides the visual changes when an entry has been deleted
+ removed: { title: "Removed successfully" },
saved: { title: "Saved successfully" },
"test-error": {
title: "Connection Failed",
diff --git a/packages/api/src/router/workspace.ts b/packages/api/src/router/workspace.ts
index 0dffe74ba..58facb3fa 100644
--- a/packages/api/src/router/workspace.ts
+++ b/packages/api/src/router/workspace.ts
@@ -1,7 +1,7 @@
import { generateSlug } from "random-word-slugs";
import { z } from "zod";
-import { eq } from "@openstatus/db";
+import { and, eq } from "@openstatus/db";
import {
selectWorkspaceSchema,
user,
@@ -64,6 +64,36 @@ export const workspaceRouter = createTRPCRouter({
return result;
}),
+ removeWorkspaceUser: protectedProcedure
+ .input(z.object({ id: z.number() }))
+ .mutation(async (opts) => {
+ const _userToWorkspace =
+ await opts.ctx.db.query.usersToWorkspaces.findFirst({
+ where: and(
+ eq(usersToWorkspaces.userId, opts.ctx.user.id),
+ eq(usersToWorkspaces.workspaceId, opts.ctx.workspace.id),
+ ),
+ });
+
+ if (!_userToWorkspace) throw new Error("No user to workspace found");
+
+ if (!["owner"].includes(_userToWorkspace.role))
+ throw new Error("Not authorized to remove user from workspace");
+
+ if (opts.input.id === opts.ctx.user.id)
+ throw new Error("Cannot remove yourself from workspace");
+
+ await opts.ctx.db
+ .delete(usersToWorkspaces)
+ .where(
+ and(
+ eq(usersToWorkspaces.userId, opts.input.id),
+ eq(usersToWorkspaces.workspaceId, opts.ctx.workspace.id),
+ ),
+ )
+ .run();
+ }),
+
createWorkspace: protectedProcedure
.input(z.object({ name: z.string() }))
.mutation(async (opts) => {