-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathadmin._index.tsx
More file actions
150 lines (141 loc) · 5.46 KB
/
admin._index.tsx
File metadata and controls
150 lines (141 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { MagnifyingGlassIcon } from "@heroicons/react/20/solid";
import { Form } from "@remix-run/react";
import type { LoaderFunctionArgs } from "@remix-run/server-runtime";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { z } from "zod";
import { Button, LinkButton } from "~/components/primitives/Buttons";
import { CopyableText } from "~/components/primitives/CopyableText";
import { Input } from "~/components/primitives/Input";
import { PaginationControls } from "~/components/primitives/Pagination";
import { Paragraph } from "~/components/primitives/Paragraph";
import {
Table,
TableBlankRow,
TableBody,
TableCell,
TableHeader,
TableHeaderCell,
TableRow,
} from "~/components/primitives/Table";
import { adminGetUsers } from "~/models/admin.server";
import { requireUserId } from "~/services/session.server";
import { createSearchParams } from "~/utils/searchParams";
export const SearchParams = z.object({
page: z.coerce.number().optional(),
search: z.string().optional(),
});
export type SearchParams = z.infer<typeof SearchParams>;
export const loader = async ({ request }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const searchParams = createSearchParams(request.url, SearchParams);
if (!searchParams.success) {
throw new Error(searchParams.error);
}
const result = await adminGetUsers(userId, searchParams.params.getAll());
return typedjson(result);
};
export default function AdminDashboardRoute() {
const { users, filters, page, pageCount } = useTypedLoaderData<typeof loader>();
return (
<main
aria-labelledby="primary-heading"
className="flex h-full min-w-0 flex-1 flex-col overflow-y-auto px-4 pb-4 lg:order-last"
>
<div className=" space-y-4">
<Form className="flex items-center gap-2">
<Input
placeholder="Search users or orgs"
variant="medium"
icon={MagnifyingGlassIcon}
fullWidth={true}
name="search"
defaultValue={filters.search}
autoFocus
/>
<Button type="submit" variant="secondary/medium">
Search
</Button>
</Form>
<Table>
<TableHeader>
<TableRow>
<TableHeaderCell>Email</TableHeaderCell>
<TableHeaderCell>Orgs</TableHeaderCell>
<TableHeaderCell>GitHub</TableHeaderCell>
<TableHeaderCell>id</TableHeaderCell>
<TableHeaderCell>Created</TableHeaderCell>
<TableHeaderCell>Admin?</TableHeaderCell>
<TableHeaderCell>Actions</TableHeaderCell>
</TableRow>
</TableHeader>
<TableBody>
{users.length === 0 ? (
<TableBlankRow colSpan={9}>
<Paragraph>No users found for search</Paragraph>
</TableBlankRow>
) : (
users.map((user) => {
return (
<TableRow key={user.id}>
<TableCell>
<CopyableText value={user.email} />
</TableCell>
<TableCell>
{user.orgMemberships.map((org) => (
<LinkButton
key={org.organization.slug}
variant="minimal/small"
to={`/admin/orgs?search=${encodeURIComponent(org.organization.slug)}`}
>
{org.organization.title} ({org.organization.slug})
{org.organization.deletedAt ? " (☠️)" : ""}
</LinkButton>
))}
</TableCell>
<TableCell>
<a
href={`https://github.com/${user.displayName}`}
target="_blank"
className="text-indigo-500 underline"
rel="noreferrer"
>
{user.displayName}
</a>
</TableCell>
<TableCell>
<CopyableText value={user.id} />
</TableCell>
<TableCell>
<CopyableText value={user.createdAt.toISOString()} />
</TableCell>
<TableCell>{user.admin ? "✅" : ""}</TableCell>
<TableCell isSticky={true}>
<Form method="post" action="/admin/impersonate" reloadDocument>
<input type="hidden" name="id" value={user.id} />
<Button
type="submit"
name="action"
value="impersonate"
className="mr-2"
variant="tertiary/small"
shortcut={
users.length === 1
? { modifiers: ["mod"], key: "enter", enabledOnInputElements: true }
: undefined
}
>
Impersonate
</Button>
</Form>
</TableCell>
</TableRow>
);
})
)}
</TableBody>
</Table>
<PaginationControls currentPage={page} totalPages={pageCount} />
</div>
</main>
);
}