| 1 | import { Link } from "react-router"; |
| 2 | |
| 3 | import type { Route } from "./+types/invitations"; |
| 4 | import { InvitationList } from "../components/invitation-list"; |
| 5 | import { answerInvitation, loadInvitations } from "../lib/invitations.server"; |
| 6 | import { declinedLine } from "../lib/invitations"; |
| 7 | import { page } from "../lib/meta"; |
| 8 | import { assertSameOrigin, nextPath, requireUser } from "../lib/session.server"; |
| 9 | |
| 10 | export function meta(args: Route.MetaArgs) { |
| 11 | return page(args, { title: "Invitations · g1t" }); |
| 12 | } |
| 13 | |
| 14 | /** |
| 15 | * The workspace invitations waiting for the signed-in person's answer. |
| 16 | * Nobody joins a workspace without saying yes here (or on the invite's own |
| 17 | * page). Linked from the inbox item and the invitation email. |
| 18 | */ |
| 19 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 20 | const user = requireUser(context, request); |
| 21 | const next = nextPath(request); |
| 22 | return { |
| 23 | invitations: await loadInvitations(user), |
| 24 | hasWorkspace: (user.workspaces ?? []).length > 0, |
| 25 | next: next === "/" ? null : next, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | export async function action({ request, context }: Route.ActionArgs) { |
| 30 | assertSameOrigin(request); |
| 31 | const user = requireUser(context, request); |
| 32 | return (await answerInvitation(user, await request.formData(), request)) ?? null; |
| 33 | } |
| 34 | |
| 35 | export default function Invitations({ loaderData, actionData }: Route.ComponentProps) { |
| 36 | const { invitations, hasWorkspace, next } = loaderData; |
| 37 | return ( |
| 38 | <main className="mx-auto max-w-2xl px-4 py-12"> |
| 39 | <h1 className="text-xl font-semibold">Invitations</h1> |
| 40 | <p className="mt-2 text-sm text-muted"> |
| 41 | Workspaces you are invited to join. You join one only when you accept; declining tells whoever invited you. |
| 42 | </p> |
| 43 | {actionData?.declined !== undefined && ( |
| 44 | <p className="mt-4 text-sm text-muted" role="status"> |
| 45 | {declinedLine(actionData.declined)} |
| 46 | </p> |
| 47 | )} |
| 48 | <div className="mt-6"> |
| 49 | {invitations.length > 0 ? ( |
| 50 | <InvitationList |
| 51 | invitations={invitations} |
| 52 | error={actionData?.invitationError} |
| 53 | errorFor={actionData?.invitationId} |
| 54 | next={next} |
| 55 | /> |
| 56 | ) : ( |
| 57 | <div className="rounded-xl border border-dashed border-line px-6 py-10 text-center"> |
| 58 | <p className="text-sm font-medium">No invitations waiting</p> |
| 59 | <p className="mt-1 text-sm text-muted"> |
| 60 | {hasWorkspace ? ( |
| 61 | <> |
| 62 | When someone invites you to a workspace, it shows here and in your{" "} |
| 63 | <Link to="/inbox" className="underline underline-offset-4 hover:text-fg"> |
| 64 | inbox |
| 65 | </Link> |
| 66 | . |
| 67 | </> |
| 68 | ) : ( |
| 69 | <> |
| 70 | <Link to="/workspaces/new" className="underline underline-offset-4 hover:text-fg"> |
| 71 | Create your workspace |
| 72 | </Link> |
| 73 | , or ask a workspace's owners to invite you by your username. |
| 74 | </> |
| 75 | )} |
| 76 | </p> |
| 77 | </div> |
| 78 | )} |
| 79 | </div> |
| 80 | </main> |
| 81 | ); |
| 82 | } |