Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | 2x 63x 2x 2x 2x 68x 1x 136x 2x 46x 46x 46x 46x 46x 17x 46x 46x 46x 46x 46x 12x 46x 46x 46x 46x 46x 46x 46x 46x 46x 53x 46x 46x 4x 4x 46x 2x 2x 1x 46x 1x 1x 1x 46x 2x 2x 2x 2x 2x 46x 1x 1x 46x 46x 46x 56x 1x 1x 1x 1x 2x 2x 2x 2x 120x 1x 50x 3x 1x 1x | import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Link2, LogOut, Mail, Trash2, UserPlus, Users } from "lucide-react";
import MainPage from "@/components/page/MainPage/MainPage";
import {
ConfirmDialog,
CopyButton,
INVITE_STATUS,
SectionCard,
SkeletonLines,
StatusBadge,
Strong,
} from "@/components/detail";
import { useAccessCatalogue } from "@/hooks/useAccessCatalogue";
import { AccessEditor, AccessSummary } from "./AccessControls";
import {
AccessMap,
CreatedInvite,
MembershipApi,
MembershipKind,
ORGANISATION_ROLES,
OrganisationInvite,
OrganisationMember,
OrganisationRole,
ROLE_LABELS,
} from "@/objects/Organisation";
import { QUERY_KEYS } from "@/services/query_keys";
import { errorDetail } from "@/util/apiError";
import { inviteLink } from "@/util/invite";
import { parseServerDate } from "@/util/time";
const formatDate = (value: string | null) =>
value
? parseServerDate(value).toLocaleDateString(undefined, {
day: "numeric",
month: "short",
year: "numeric",
})
: "—";
const fieldClass =
"text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-700 text-gray-800 dark:text-gray-200 px-3 py-1.5 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500";
const inputClass = `w-full ${fieldClass}`;
const RoleSelect: React.FC<{
value: OrganisationRole;
onChange: (role: OrganisationRole) => void;
disabled?: boolean;
label: string;
}> = ({ value, onChange, disabled, label }) => (
<select
aria-label={label}
value={value}
disabled={disabled}
onChange={(e) => onChange(e.target.value as OrganisationRole)}
className={fieldClass}
>
{ORGANISATION_ROLES.map((role) => (
<option key={role} value={role}>
{ROLE_LABELS[role]}
</option>
))}
</select>
);
/**
* Who belongs to an organisation or project, and the invite links that let
* others in. Both share one implementation on the server, and this view.
*
* Everyone in it sees the members. Admins also change roles, remove people
* and make or withdraw invite links; the server enforces all of it,
* including that an organisation always keeps one admin, and its refusal is
* what this page shows.
*/
export const MembersView: React.FC<{
kind: MembershipKind;
/** Null when the route's id could not be read. */
id: string | null;
api: MembershipApi;
/** Where to go after leaving it. */
leaveTo: string;
/** Above the page, beside its title — the settings tabs it is one of. */
links?: React.ReactNode[];
}> = ({ kind, id: organisationId, api, leaveTo, links = [] }) => {
const queryClient = useQueryClient();
const navigate = useNavigate();
const membersKey = [QUERY_KEYS.GET.ORGANISATION_MEMBERS, kind, organisationId];
const invitesKey = [QUERY_KEYS.GET.ORGANISATION_INVITES, kind, organisationId];
const membersQuery = useQuery<OrganisationMember[]>({
queryKey: membersKey,
queryFn: () => api.members(organisationId!),
enabled: !!organisationId,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const members: OrganisationMember[] | undefined = membersQuery.data;
const isLoading = membersQuery.isLoading;
const you = members?.find((m) => m.is_you);
const isAdmin = you?.role === "admin";
const invitesQuery = useQuery<OrganisationInvite[]>({
queryKey: invitesKey,
queryFn: () => api.invites(organisationId!),
enabled: !!organisationId && isAdmin,
});
const invites: OrganisationInvite[] | undefined = invitesQuery.data;
const [error, setError] = useState<string | null>(null);
const [removing, setRemoving] = useState<OrganisationMember | null>(null);
const [email, setEmail] = useState("");
const [role, setRole] = useState<OrganisationRole>("member");
const [inviteAccess, setInviteAccess] = useState<AccessMap | null>(null);
const [created, setCreated] = useState<CreatedInvite | null>(null);
const { catalogue } = useAccessCatalogue();
// An invite starts from the Builder preset; the admin can adjust any area.
const presetAccess = (key: string): AccessMap | null =>
catalogue?.presets.find((p) => p.key === key)?.access ?? null;
const access: AccessMap | null = inviteAccess ?? presetAccess("builder");
const refresh = () => {
queryClient.invalidateQueries({ queryKey: membersKey });
queryClient.invalidateQueries({ queryKey: invitesKey });
};
const updateMember = useMutation({
mutationFn: (v: {
userId: string;
change: { role?: OrganisationRole; access?: AccessMap };
}) => api.updateMember(organisationId!, v.userId, v.change),
onMutate: () => setError(null),
onSuccess: refresh,
onError: (e) => setError(errorDetail(e, "Could not change their access.")),
});
const removeMember = useMutation({
mutationFn: (member: OrganisationMember) =>
api.removeMember(organisationId!, member.user_id),
onSuccess: (_, member) => {
setRemoving(null);
if (member.is_you) navigate(leaveTo);
else Erefresh();
},
});
const createInvite = useMutation({
mutationFn: () =>
api.createInvite(organisationId!, {
email,
role,
...(role === "member" && access ? { access } : {}),
}),
onMutate: () => setError(null),
onSuccess: (result) => {
setCreated(result);
setEmail("");
refresh();
},
onError: (e) => setError(errorDetail(e, "Could not create the invite.")),
});
const revokeInvite = useMutation({
mutationFn: (inviteId: string) => api.revokeInvite(organisationId!, inviteId),
onMutate: () => setError(null),
onSuccess: refresh,
onError: (e) => setError(errorDetail(e, "Could not withdraw the invite.")),
});
Iif (!organisationId)
return <div className="p-4 text-red-500">Invalid {kind} ID.</div>;
const link = created ? inviteLink(created.token) : null;
return (
<MainPage
title="Members"
description={`Who belongs to this ${kind}, and what they can do.`}
links={links}
>
<div className="max-w-3xl space-y-4">
{error && (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
{error}
</p>
)}
<SectionCard
variant="settings"
icon={Users}
title="Members"
subtitle={members ? String(members.length) : undefined}
description={`Admins manage members, invites and settings, and edit everything. Each member has a level in each area: readers use, writers also create and change their own, editors change anything.`}
>
{isLoading ? (
<div className="px-5 py-4">
<SkeletonLines count={3} />
</div>
) : (
(members ?? []).map((member) => (
<div
key={member.user_id}
className="flex flex-wrap items-center gap-3 px-5 py-3"
>
<div className="flex-1 min-w-0">
<div className="text-sm font-medium text-gray-800 dark:text-gray-200 truncate">
{member.name || member.email}
{member.is_you && (
<span className="ml-1.5 text-xs font-normal text-gray-400">
(you)
</span>
)}
</div>
<div className="text-xs text-gray-400 dark:text-gray-500 truncate">
{member.email} · joined {formatDate(member.joined_at)}
</div>
{member.role === "member" && !isAdmin && (
<div className="mt-1">
<AccessSummary access={member.access} />
</div>
)}
</div>
{member.via ? (
<span className="text-xs text-gray-500 dark:text-gray-400">
{member.via === "owner" ? "Owner" : `Admin (via ${member.via})`}
</span>
) : isAdmin ? (
<RoleSelect
label={`Role for ${member.email}`}
value={member.role}
disabled={updateMember.isPending}
onChange={(next) =>
updateMember.mutate({
userId: member.user_id,
change: { role: next },
})
}
/>
) : (
<span className="text-xs text-gray-500 dark:text-gray-400">
{ROLE_LABELS[member.role]}
</span>
)}
{!member.via && (member.is_you || isAdmin) && (
<button
type="button"
onClick={() => {
removeMember.reset();
setRemoving(member);
}}
className="inline-flex items-center gap-1.5 text-xs text-gray-500 hover:text-red-600 dark:text-gray-400 dark:hover:text-red-400"
>
{member.is_you ? (
<>
<LogOut className="size-3.5" /> Leave
</>
) : (
<>
<Trash2 className="size-3.5" /> Remove
</>
)}
</button>
)}
{isAdmin && member.role === "member" && (
<div className="basis-full">
<AccessEditor
labelFor={member.email}
value={member.access}
disabled={updateMember.isPending}
onChange={(next) =>
updateMember.mutate({
userId: member.user_id,
change: { access: next },
})
}
/>
</div>
)}
</div>
))
)}
</SectionCard>
{isAdmin && (
<SectionCard
variant="form"
icon={UserPlus}
title="Invite someone"
description="Make a link for one person and send it to them yourself. CHAIN-R doesn't send email."
>
<form
className="flex flex-wrap items-end gap-3"
onSubmit={(e) => {
e.preventDefault();
setCreated(null);
createInvite.mutate();
}}
>
<label className="flex-1 min-w-[14rem] text-xs text-gray-500 dark:text-gray-400">
Email address
<input
type="email"
required
value={email}
onChange={(e) => setEmail(e.target.value)}
placeholder="name@company.com"
className={`${inputClass} mt-1`}
/>
</label>
<label className="text-xs text-gray-500 dark:text-gray-400">
Role
<div className="mt-1">
<RoleSelect label="Role for the invite" value={role} onChange={setRole} />
</div>
</label>
{role === "member" && catalogue && access && (
<div className="basis-full min-w-0 space-y-2">
<label className="block text-xs text-gray-500 dark:text-gray-400">
Start from
<select
aria-label="Access preset"
className={`${fieldClass} mt-1 block w-full sm:w-auto max-w-full`}
value={
catalogue.presets.find((p) =>
catalogue.areas.every((a) => p.access[a.key] === access[a.key]),
)?.key ?? ""
}
onChange={(e) => setInviteAccess(presetAccess(e.target.value))}
>
<option value="" disabled>
Custom
</option>
{catalogue.presets.map((p) => (
<option key={p.key} value={p.key} title={p.description}>
{p.label} — {p.description}
</option>
))}
</select>
</label>
<AccessEditor
labelFor="the invite"
value={access}
onChange={setInviteAccess}
/>
</div>
)}
<button
type="submit"
disabled={createInvite.isPending || !email}
className="inline-flex items-center gap-2 px-3 py-1.5 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 disabled:opacity-50 transition-opacity"
>
<Link2 className="size-3.5" />
{createInvite.isPending ? "Creating…" : "Create invite link"}
</button>
</form>
{created && link && (
<div className="rounded-lg border border-amber-200 dark:border-amber-900/60 bg-amber-50 dark:bg-amber-950/20 p-3 space-y-2">
<p className="text-xs text-gray-600 dark:text-gray-300">
Send this link to <Strong>{created.invite.email}</Strong>. It
works once, only for that address, until{" "}
{formatDate(created.invite.expires_at)}.{" "}
<span className="font-medium">
It won't be shown again.
</span>
</p>
<div className="flex items-center gap-2">
<input
readOnly
aria-label="Invite link"
value={link}
onFocus={(e) => e.target.select()}
className={`${inputClass} font-mono text-xs`}
/>
<CopyButton value={link} label="Copy" />
</div>
</div>
)}
</SectionCard>
)}
{isAdmin && (invites?.length ?? 0) > 0 && (
<SectionCard variant="settings" icon={Mail} title="Invites">
{invites!.map((invite) => (
<div
key={invite.id}
className="flex flex-wrap items-center gap-3 px-5 py-3"
>
<div className="flex-1 min-w-0">
<div className="text-sm text-gray-800 dark:text-gray-200 truncate">
{invite.email}
</div>
<div className="text-xs text-gray-400 dark:text-gray-500">
{ROLE_LABELS[invite.role]} ·{" "}
{invite.status === "pending"
? `expires ${formatDate(invite.expires_at)}`
: `created ${formatDate(invite.created_at)}`}
</div>
{invite.role === "member" && (
<AccessSummary access={invite.access} />
)}
</div>
<StatusBadge status={invite.status} scale={INVITE_STATUS} size="xs" />
{invite.status === "pending" && (
<button
type="button"
disabled={revokeInvite.isPending}
onClick={() => revokeInvite.mutate(invite.id)}
className="text-xs text-gray-500 hover:text-red-600 dark:text-gray-400 dark:hover:text-red-400"
>
Withdraw
</button>
)}
</div>
))}
</SectionCard>
)}
</div>
{removing && (
<ConfirmDialog
title={removing.is_you ? `Leave this ${kind}?` : "Remove member?"}
icon={removing.is_you ? LogOut : Trash2}
destructive
body={
removing.is_you ? (
<>
You'll lose access to everything the {kind} owns until
an admin invites you again.
</>
) : (
<>
<Strong>{removing.name || removing.email}</Strong> will lose
access to everything the {kind} owns.
</>
)
}
confirmLabel={removing.is_you ? "Leave" : "Remove"}
loading={removeMember.isPending}
error={
removeMember.isError
? errorDetail(removeMember.error, "Could not remove the member.")
: null
}
onConfirm={() => removeMember.mutate(removing)}
onCancel={() => setRemoving(null)}
/>
)}
</MainPage>
);
};
export default MembersView;
|