Skip to content

Open-source status page, incident response and uptime monitoring

chore: improve invitation flow (#828)

Maximilian Kaske4d865673c443
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/_components/info-banner.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/_components/info-banner.tsx
new file mode 100644
index 000000000..3028a98be
--- /dev/null
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/_components/info-banner.tsx
@@ -0,0 +1,27 @@
+"use client";
+
+import Link from "next/link";
+import { useParams } from "next/navigation";
+import { Info } from "lucide-react";
+
+import { Alert, AlertDescription, AlertTitle } from "@openstatus/ui";
+
+export function InfoBanner() {
+ const params = useParams<{ workspaceSlug: string }>();
+ return (
+ <Alert className="bg-muted/50">
+ <Info className="h-4 w-4" />
+ <AlertTitle>You&apos;re workspace name is empty</AlertTitle>
+ <AlertDescription>
+ To inform your team about the workspace name, please set it in the{" "}
+ <Link
+ href={`/app/${params.workspaceSlug}/settings/general`}
+ className="text-foreground inline-flex items-center font-medium underline underline-offset-4 hover:no-underline"
+ >
+ general
+ </Link>{" "}
+ settings.
+ </AlertDescription>
+ </Alert>
+ );
+}
diff --git a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/page.tsx b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/page.tsx
index 18e8abf2b..4fe294430 100644
--- a/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/page.tsx
+++ b/apps/web/src/app/app/[workspaceSlug]/(dashboard)/settings/team/page.tsx
@@ -4,6 +4,7 @@ import { DataTable as InvitationDataTable } from "@/components/data-table/invita
import { columns as userColumns } from "@/components/data-table/user/columns";
import { DataTable as UserDataTable } from "@/components/data-table/user/data-table";
import { api } from "@/trpc/server";
+import { InfoBanner } from "./_components/info-banner";
import { InviteButton } from "./_components/invite-button";
export default async function TeamPage() {
@@ -16,6 +17,7 @@ export default async function TeamPage() {
return (
<div className="flex flex-col gap-4">
{isFreePlan ? <ProFeatureAlert feature="Team members" /> : null}
+ {!isFreePlan && !workspace.name ? <InfoBanner /> : null}
{/* TODO: only show if isAdmin */}
<div className="flex justify-end">
<InviteButton disabled={isFreePlan} />
diff --git a/apps/web/src/app/app/invite/_components/link-cards.tsx b/apps/web/src/app/app/invite/_components/link-cards.tsx
new file mode 100644
index 000000000..207c46201
--- /dev/null
+++ b/apps/web/src/app/app/invite/_components/link-cards.tsx
@@ -0,0 +1,61 @@
+import Link from "next/link";
+import { ArrowRight, ArrowUpRight } from "lucide-react";
+
+import { Card, CardDescription, CardHeader, CardTitle } from "@openstatus/ui";
+
+const links = [
+ {
+ title: "Monitors",
+ description: "Create and manage monitors",
+ href: "/monitors",
+ },
+ {
+ title: "Status Pages",
+ description: "Create and manage pages",
+ href: "/status-pages",
+ },
+ {
+ title: "Status Reports",
+ description: "Create and manage reports",
+ href: "/status-reports",
+ },
+ {
+ title: "Documentation",
+ description: "Learn more about OpenStatus",
+ href: "https://docs.openstatus.dev",
+ },
+];
+
+export function LinkCards({ slug }: { slug: string }) {
+ return (
+ <div className="grid w-full grid-cols-1 gap-4 sm:grid-cols-2">
+ {links.map((link) => {
+ const isExternal = link.href.startsWith("http");
+ const href = isExternal ? link.href : `/app/${slug}${link.href}`;
+ const target = isExternal ? "_blank" : undefined;
+ return (
+ <Link
+ key={link.href}
+ href={href}
+ target={target}
+ className="group flex w-full flex-1"
+ >
+ <Card className="flex w-full flex-col">
+ <CardHeader className="flex-1">
+ <CardTitle>{link.title}</CardTitle>
+ <div className="flex flex-1 justify-between gap-2">
+ <CardDescription>{link.description}</CardDescription>
+ {isExternal ? (
+ <ArrowUpRight className="text-muted-foreground group-hover:text-foreground h-4 w-4 shrink-0 self-end" />
+ ) : (
+ <ArrowRight className="text-muted-foreground group-hover:text-foreground h-4 w-4 shrink-0 self-end" />
+ )}
+ </div>
+ </CardHeader>
+ </Card>
+ </Link>
+ );
+ })}
+ </div>
+ );
+}
diff --git a/apps/web/src/app/app/invite/page.tsx b/apps/web/src/app/app/invite/page.tsx
index 1560edf0c..abb34274c 100644
--- a/apps/web/src/app/app/invite/page.tsx
+++ b/apps/web/src/app/app/invite/page.tsx
@@ -1,9 +1,12 @@
-import Link from "next/link";
import { z } from "zod";
-import { Button } from "@openstatus/ui";
+import { Alert, AlertDescription, AlertTitle, Separator } from "@openstatus/ui";
+import { Icons } from "@/components/icons";
import { api } from "@/trpc/server";
+import { LinkCards } from "./_components/link-cards";
+
+const AlertTriangle = Icons["alert-triangle"];
/**
* allowed URL search params
@@ -18,17 +21,39 @@ export default async function InvitePage({
searchParams: { [key: string]: string | string[] | undefined };
}) {
const search = searchParamsSchema.safeParse(searchParams);
-
- const message = search.success
+ const { message, data } = search.success
? await api.invitation.acceptInvitation.mutate({ token: search.data.token })
- : "Invalid token";
+ : { message: "Unavailable invitation token.", data: undefined };
+
+ const workspace = await api.workspace.getWorkspace.query();
+
+ if (!data) {
+ return (
+ <div className="mx-auto flex h-full max-w-xl flex-1 flex-col items-center justify-center gap-4">
+ <h1 className="text-2xl font-semibold">Invitation</h1>
+ <Alert variant="destructive">
+ <AlertTriangle className="h-4 w-4" />
+ <AlertTitle>Something went wrong</AlertTitle>
+ <AlertDescription>{message}</AlertDescription>
+ </Alert>
+ <Separator className="my-4" />
+ <p className="text-muted-foreground">Quick Links</p>
+ <LinkCards slug={workspace.slug} />
+ </div>
+ );
+ }
return (
- <div className="flex h-full flex-1 flex-col items-center justify-center gap-4">
- <p className="text-lg text-muted-foreground">{message}</p>
- <Button>
- <Link href="/app">Dashboard</Link>
- </Button>
+ <div className="mx-auto flex h-full max-w-xl flex-1 flex-col items-center justify-center gap-4">
+ <h1 className="text-2xl font-semibold">Invitation</h1>
+ <Alert>
+ <Icons.check className="h-4 w-4" />
+ <AlertTitle>Ready to go</AlertTitle>
+ <AlertDescription>{message}</AlertDescription>
+ </Alert>
+ <Separator className="my-4" />
+ <p className="text-muted-foreground">Quick Links</p>
+ <LinkCards slug={data.slug} />
</div>
);
}
diff --git a/packages/analytics/src/type.ts b/packages/analytics/src/type.ts
index 90991aa4f..1859afc87 100644
--- a/packages/analytics/src/type.ts
+++ b/packages/analytics/src/type.ts
@@ -20,4 +20,5 @@ export type AnalyticsEvents =
| { event: "User Signed In" }
| { event: "User Vercel Beta" }
| { event: "Notification Created"; provider: string }
- | { event: "Subscribe to Status Page"; slug: string };
+ | { event: "Subscribe to Status Page"; slug: string }
+ | { event: "Invitation Created"; emailTo: string; workspaceId: number };
diff --git a/packages/api/src/analytics.ts b/packages/api/src/analytics.ts
index 527329a54..be88accba 100644
--- a/packages/api/src/analytics.ts
+++ b/packages/api/src/analytics.ts
@@ -46,4 +46,16 @@ export async function trackNewNotification(
export async function trackNewStatusReport() {}
-export async function trackNewInvitation() {}
+export async function trackNewInvitation(
+ user: User,
+ config: { emailTo: string; workspaceId: number },
+) {
+ await analytics.identify(user.id, {
+ userId: user.id,
+ email: user.email,
+ });
+ await trackAnalytics({
+ event: "Invitation Created",
+ ...config,
+ });
+}
diff --git a/packages/api/src/router/invitation.ts b/packages/api/src/router/invitation.ts
index 682f65589..96d26ae02 100644
--- a/packages/api/src/router/invitation.ts
+++ b/packages/api/src/router/invitation.ts
@@ -5,6 +5,7 @@ import { and, eq, gte, isNull } from "@openstatus/db";
import {
insertInvitationSchema,
invitation,
+ selectWorkspaceSchema,
user,
usersToWorkspaces,
} from "@openstatus/db/src/schema";
@@ -33,7 +34,7 @@ export const invitationRouter = createTRPCRouter({
where: and(
eq(invitation.workspaceId, opts.ctx.workspace.id),
gte(invitation.expiresAt, new Date()),
- isNull(invitation.acceptedAt),
+ isNull(invitation.acceptedAt)
),
})
).length;
@@ -51,20 +52,6 @@ export const invitationRouter = createTRPCRouter({
const token = crypto.randomUUID();
- await fetch("https://api.resend.com/emails", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
- },
- body: JSON.stringify({
- to: email,
- from: "Maximilian Kaske <[email protected]>",
- subject: "You have been invited to join OpenStatus.dev",
- html: `<p>Click here to join the workspace: <a href='https://openstatus.dev/app/invite?token=${token}'>accept invitation</a></p>`,
- }),
- });
-
const _invitation = await opts.ctx.db
.insert(invitation)
.values({ email, expiresAt, token, workspaceId: opts.ctx.workspace.id })
@@ -73,11 +60,32 @@ export const invitationRouter = createTRPCRouter({
if (process.env.NODE_ENV === "development") {
console.log(
- `>>>> Invitation token: http://localhost:3000/app/invite?token=${token} <<<< `,
+ `>>>> Invitation token: http://localhost:3000/app/invite?token=${token} <<<< `
);
+ } else {
+ await fetch("https://api.resend.com/emails", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${process.env.RESEND_API_KEY}`,
+ },
+ body: JSON.stringify({
+ to: email,
+ from: "OpenStatus <[email protected]>",
+ subject: `You have been invited to join OpenStatus.dev`,
+ html: `<p>You have been invited by ${opts.ctx.user.email} ${!!opts.ctx.workspace.name ? `to join the workspace '${opts.ctx.workspace.name}'.` : "to join a workspace."}</p>
+ <br>
+ <p>Click here to access the workspace: <a href='https://openstatus.dev/app/invite?token=${_invitation.token}'>accept invitation</a>.</p>
+ <p>If you don't have an account yet, it will require you to create one.</p>
+ `,
+ }),
+ });
}
- // TODO:
- await trackNewInvitation();
+
+ await trackNewInvitation(opts.ctx.user, {
+ emailTo: email,
+ workspaceId: opts.ctx.workspace.id,
+ });
return _invitation;
}),
@@ -90,8 +98,8 @@ export const invitationRouter = createTRPCRouter({
.where(
and(
eq(invitation.id, opts.input.id),
- eq(invitation.workspaceId, opts.ctx.workspace.id),
- ),
+ eq(invitation.workspaceId, opts.ctx.workspace.id)
+ )
)
.run();
}),
@@ -101,7 +109,7 @@ export const invitationRouter = createTRPCRouter({
where: and(
eq(invitation.workspaceId, opts.ctx.workspace.id),
gte(invitation.expiresAt, new Date()),
- isNull(invitation.acceptedAt),
+ isNull(invitation.acceptedAt)
),
});
return _invitations;
@@ -126,32 +134,38 @@ export const invitationRouter = createTRPCRouter({
*/
acceptInvitation: publicProcedure
.input(z.object({ token: z.string() }))
+ .output(
+ z.object({
+ message: z.string(),
+ data: selectWorkspaceSchema.optional(),
+ })
+ )
.mutation(async (opts) => {
const _invitation = await opts.ctx.db.query.invitation.findFirst({
where: and(
eq(invitation.token, opts.input.token),
- isNull(invitation.acceptedAt),
+ isNull(invitation.acceptedAt)
),
with: {
workspace: true,
},
});
- if (!opts.ctx.session?.user?.id) return "Missing user";
+ if (!opts.ctx.session?.user?.id) return { message: "Missing user." };
const _user = await opts.ctx.db.query.user.findFirst({
where: eq(user.id, Number(opts.ctx.session.user.id)),
});
- if (!_user) return "Invalid user";
+ if (!_user) return { message: "Invalid user." };
- if (!_invitation) return "Invalid invitation token";
+ if (!_invitation) return { message: "Invalid invitation token." };
if (_invitation.email !== _user.email)
- return "You are not invited to this workspace";
+ return { message: "You are not invited to this workspace." };
if (_invitation.expiresAt.getTime() < new Date().getTime()) {
- return "Invitation expired";
+ return { message: "Invitation expired." };
}
await opts.ctx.db
@@ -169,6 +183,9 @@ export const invitationRouter = createTRPCRouter({
})
.run();
- return "Invitation accepted";
+ return {
+ message: "Invitation accepted.",
+ data: _invitation.workspace,
+ };
}),
});