feat: disciple link
This commit is contained in:
@@ -0,0 +1,246 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DiscipleLinkModal — links a platform account to one of Śrīla Prabhupāda's
|
||||||
|
* initiated disciples (or clears an existing link).
|
||||||
|
*
|
||||||
|
* The disciple directory lives in Mongo and the link column on the Postgres
|
||||||
|
* users row, so the picker searches server-side via /admin/disciples. Each
|
||||||
|
* option reports whether another account already claims it, letting us disable
|
||||||
|
* those rows up front instead of surfacing a 409 after the click.
|
||||||
|
*
|
||||||
|
* Clicking a free row links immediately — the action is one field, instantly
|
||||||
|
* reversible from the same modal, so a confirm step would only add friction.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useCallback, useEffect, useState } from "react";
|
||||||
|
import { Check, Link2Off, Search } from "lucide-react";
|
||||||
|
import { toast } from "sonner";
|
||||||
|
import { api, AdminError, type AdminDisciple, type AdminUser } from "@/lib/api";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Modal } from "@/components/ui/modal";
|
||||||
|
import { cn } from "@/lib/utils";
|
||||||
|
|
||||||
|
/** "Vṛndāvana · 1972" — whichever halves we know. */
|
||||||
|
function placeYear(d: AdminDisciple): string | null {
|
||||||
|
const year = d.initiation_date
|
||||||
|
? new Date(d.initiation_date).getFullYear()
|
||||||
|
: null;
|
||||||
|
const parts = [d.initiation_place, Number.isFinite(year) ? year : null].filter(
|
||||||
|
Boolean,
|
||||||
|
);
|
||||||
|
return parts.length ? parts.join(" · ") : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function DiscipleLinkModal({
|
||||||
|
user,
|
||||||
|
onClose,
|
||||||
|
onSaved,
|
||||||
|
}: {
|
||||||
|
user: AdminUser | null;
|
||||||
|
onClose: () => void;
|
||||||
|
onSaved: () => void;
|
||||||
|
}) {
|
||||||
|
const [q, setQ] = useState("");
|
||||||
|
const [options, setOptions] = useState<AdminDisciple[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [busyId, setBusyId] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Reset the query each time the modal opens for a different user, so a
|
||||||
|
// stale search doesn't carry over between rows.
|
||||||
|
useEffect(() => {
|
||||||
|
if (user) setQ("");
|
||||||
|
}, [user?.id]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
// Debounced search. Runs once on open (empty q → first page alphabetically)
|
||||||
|
// and then 250ms after the operator stops typing.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!user) return;
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
const t = setTimeout(async () => {
|
||||||
|
try {
|
||||||
|
const items = await api.disciples(q);
|
||||||
|
if (!cancelled) setOptions(items);
|
||||||
|
} catch (err) {
|
||||||
|
if (!cancelled) {
|
||||||
|
toast.error(
|
||||||
|
err instanceof AdminError ? err.message : "Could not load disciples.",
|
||||||
|
);
|
||||||
|
setOptions([]);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (!cancelled) setLoading(false);
|
||||||
|
}
|
||||||
|
}, 250);
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
clearTimeout(t);
|
||||||
|
};
|
||||||
|
}, [q, user?.id]); // eslint-disable-line react-hooks/exhaustive-deps
|
||||||
|
|
||||||
|
const link = useCallback(
|
||||||
|
async (d: AdminDisciple) => {
|
||||||
|
if (!user) return;
|
||||||
|
setBusyId(d.id);
|
||||||
|
try {
|
||||||
|
await api.linkDisciple(user.id, d.id);
|
||||||
|
toast.success(
|
||||||
|
`@${user.handle} linked to ${d.initiated_name} ${d.suffix}`.trim(),
|
||||||
|
);
|
||||||
|
onSaved();
|
||||||
|
onClose();
|
||||||
|
} catch (err) {
|
||||||
|
toast.error(
|
||||||
|
err instanceof AdminError ? err.message : "Could not link disciple.",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setBusyId(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[user, onSaved, onClose],
|
||||||
|
);
|
||||||
|
|
||||||
|
async function unlink() {
|
||||||
|
if (!user) return;
|
||||||
|
setBusyId("__unlink__");
|
||||||
|
try {
|
||||||
|
await api.unlinkDisciple(user.id);
|
||||||
|
toast.success(`Removed the disciple link from @${user.handle}`);
|
||||||
|
onSaved();
|
||||||
|
onClose();
|
||||||
|
} catch (err) {
|
||||||
|
toast.error(
|
||||||
|
err instanceof AdminError ? err.message : "Could not remove the link.",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setBusyId(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal
|
||||||
|
open={!!user}
|
||||||
|
onClose={onClose}
|
||||||
|
title={user ? `Direct disciple — @${user.handle}` : "Direct disciple"}
|
||||||
|
>
|
||||||
|
<div className="space-y-4">
|
||||||
|
<p className="text-sm text-muted-foreground">
|
||||||
|
Link this account to one of Śrīla Prabhupāda’s initiated
|
||||||
|
disciples. Linked accounts show a badge on their profile and are
|
||||||
|
highlighted in the disciples directory.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
{user?.disciple_id ? (
|
||||||
|
<div className="flex items-center justify-between gap-3 rounded-lg border border-border bg-muted/40 px-3 py-2.5">
|
||||||
|
<div className="min-w-0">
|
||||||
|
<div className="text-xs text-muted-foreground">
|
||||||
|
Currently linked to
|
||||||
|
</div>
|
||||||
|
<div className="truncate text-sm font-medium">
|
||||||
|
{user.disciple_name ?? (
|
||||||
|
<span className="text-muted-foreground">
|
||||||
|
unknown disciple ({user.disciple_id.slice(0, 8)}…)
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={unlink}
|
||||||
|
disabled={busyId !== null}
|
||||||
|
>
|
||||||
|
<Link2Off className="mr-1.5 size-3.5" />
|
||||||
|
{busyId === "__unlink__" ? "Removing…" : "Remove"}
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<div className="relative">
|
||||||
|
<Search className="pointer-events-none absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
||||||
|
<Input
|
||||||
|
value={q}
|
||||||
|
onChange={(e) => setQ(e.target.value)}
|
||||||
|
placeholder="Search initiated names…"
|
||||||
|
className="pl-9"
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="max-h-72 overflow-y-auto rounded-lg border border-border">
|
||||||
|
{loading && options.length === 0 ? (
|
||||||
|
<p className="px-3 py-6 text-center text-sm text-muted-foreground">
|
||||||
|
Searching…
|
||||||
|
</p>
|
||||||
|
) : options.length === 0 ? (
|
||||||
|
<p className="px-3 py-6 text-center text-sm text-muted-foreground">
|
||||||
|
No disciples match that search.
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<ul className="divide-y divide-border">
|
||||||
|
{options.map((d) => {
|
||||||
|
const isCurrent = d.id === user?.disciple_id;
|
||||||
|
// Claimed by somebody else — not linkable until that link goes.
|
||||||
|
const takenByOther = !!d.linked_handle && !isCurrent;
|
||||||
|
const sub = placeYear(d);
|
||||||
|
return (
|
||||||
|
<li key={d.id}>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
disabled={takenByOther || busyId !== null || isCurrent}
|
||||||
|
onClick={() => link(d)}
|
||||||
|
className={cn(
|
||||||
|
"flex w-full items-center justify-between gap-3 px-3 py-2.5 text-left transition-colors",
|
||||||
|
takenByOther || isCurrent
|
||||||
|
? "cursor-default"
|
||||||
|
: "hover:bg-muted",
|
||||||
|
takenByOther && "opacity-60",
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
<span className="min-w-0">
|
||||||
|
<span className="block truncate text-sm font-medium">
|
||||||
|
{d.initiated_name}{" "}
|
||||||
|
<span className="font-normal text-muted-foreground">
|
||||||
|
{d.suffix}
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
{sub ? (
|
||||||
|
<span className="block truncate text-xs text-muted-foreground">
|
||||||
|
{sub}
|
||||||
|
</span>
|
||||||
|
) : null}
|
||||||
|
</span>
|
||||||
|
<span className="shrink-0 text-xs">
|
||||||
|
{isCurrent ? (
|
||||||
|
<span className="inline-flex items-center gap-1 font-medium text-primary">
|
||||||
|
<Check className="size-3.5" /> Linked
|
||||||
|
</span>
|
||||||
|
) : takenByOther ? (
|
||||||
|
<span className="text-muted-foreground">
|
||||||
|
@{d.linked_handle}
|
||||||
|
</span>
|
||||||
|
) : busyId === d.id ? (
|
||||||
|
<span className="text-muted-foreground">Linking…</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-primary">Link</span>
|
||||||
|
)}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex justify-end">
|
||||||
|
<Button variant="ghost" onClick={onClose}>
|
||||||
|
Done
|
||||||
|
</Button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Modal>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ import { Avatar } from "@/components/ui/avatar";
|
|||||||
import { useConfirm } from "@/components/confirm";
|
import { useConfirm } from "@/components/confirm";
|
||||||
import { Input, Textarea } from "@/components/ui/input";
|
import { Input, Textarea } from "@/components/ui/input";
|
||||||
import { fmtDate } from "@/lib/utils";
|
import { fmtDate } from "@/lib/utils";
|
||||||
|
import { DiscipleLinkModal } from "./disciple-link-modal";
|
||||||
|
|
||||||
export default function UsersPage() {
|
export default function UsersPage() {
|
||||||
const [q, setQ] = useState("");
|
const [q, setQ] = useState("");
|
||||||
@@ -31,6 +32,7 @@ export default function UsersPage() {
|
|||||||
const confirm = useConfirm();
|
const confirm = useConfirm();
|
||||||
const [notify, setNotify] = useState<AdminUser | null>(null);
|
const [notify, setNotify] = useState<AdminUser | null>(null);
|
||||||
const [limitsUser, setLimitsUser] = useState<AdminUser | null>(null);
|
const [limitsUser, setLimitsUser] = useState<AdminUser | null>(null);
|
||||||
|
const [discipleUser, setDiscipleUser] = useState<AdminUser | null>(null);
|
||||||
|
|
||||||
async function toggleBan(u: AdminUser) {
|
async function toggleBan(u: AdminUser) {
|
||||||
const banning = !u.banned_at;
|
const banning = !u.banned_at;
|
||||||
@@ -54,11 +56,42 @@ export default function UsersPage() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function removeDiscipleLink(u: AdminUser) {
|
||||||
|
if (
|
||||||
|
!(await confirm({
|
||||||
|
title: `Remove disciple link from @${u.handle}?`,
|
||||||
|
message:
|
||||||
|
"Their profile badge and the highlight in the disciples directory will be removed. You can re-link them at any time.",
|
||||||
|
confirmLabel: "Remove link",
|
||||||
|
destructive: true,
|
||||||
|
}))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
await api.unlinkDisciple(u.id);
|
||||||
|
toast.success("Disciple link removed.");
|
||||||
|
list.refresh();
|
||||||
|
} catch (err) {
|
||||||
|
toast.error(err instanceof AdminError ? err.message : "Action failed.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function menuItems(u: AdminUser): MenuItem[] {
|
function menuItems(u: AdminUser): MenuItem[] {
|
||||||
const items: MenuItem[] = [
|
const items: MenuItem[] = [
|
||||||
{ label: "Send notification", onClick: () => setNotify(u) },
|
{ label: "Send notification", onClick: () => setNotify(u) },
|
||||||
{ label: "Edit limits", onClick: () => setLimitsUser(u) },
|
{ label: "Edit limits", onClick: () => setLimitsUser(u) },
|
||||||
|
{
|
||||||
|
label: u.disciple_id ? "Change disciple link" : "Link direct disciple",
|
||||||
|
onClick: () => setDiscipleUser(u),
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
if (u.disciple_id) {
|
||||||
|
items.push({
|
||||||
|
label: "Remove disciple link",
|
||||||
|
onClick: () => removeDiscipleLink(u),
|
||||||
|
});
|
||||||
|
}
|
||||||
// No ban option for fellow admins (avoid locking each other out).
|
// No ban option for fellow admins (avoid locking each other out).
|
||||||
if (u.role !== "admin") {
|
if (u.role !== "admin") {
|
||||||
items.push({
|
items.push({
|
||||||
@@ -109,6 +142,20 @@ export default function UsersPage() {
|
|||||||
<div className="text-xs text-muted-foreground">
|
<div className="text-xs text-muted-foreground">
|
||||||
@{u.handle}
|
@{u.handle}
|
||||||
</div>
|
</div>
|
||||||
|
{u.disciple_id ? (
|
||||||
|
<div
|
||||||
|
className="mt-1 inline-flex max-w-full items-center gap-1 rounded bg-primary/10 px-1.5 py-0.5 text-[10px] font-medium text-primary"
|
||||||
|
title={
|
||||||
|
u.disciple_name
|
||||||
|
? `Linked to ${u.disciple_name}`
|
||||||
|
: "Linked to an unknown disciple record"
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<span className="truncate">
|
||||||
|
{u.disciple_name ?? "Direct disciple"}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</TD>
|
</TD>
|
||||||
@@ -139,6 +186,11 @@ export default function UsersPage() {
|
|||||||
onClose={() => setLimitsUser(null)}
|
onClose={() => setLimitsUser(null)}
|
||||||
onSaved={list.refresh}
|
onSaved={list.refresh}
|
||||||
/>
|
/>
|
||||||
|
<DiscipleLinkModal
|
||||||
|
user={discipleUser}
|
||||||
|
onClose={() => setDiscipleUser(null)}
|
||||||
|
onSaved={list.refresh}
|
||||||
|
/>
|
||||||
</ListShell>
|
</ListShell>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+34
@@ -133,6 +133,22 @@ export type AdminUser = {
|
|||||||
bookmark_limit: number;
|
bookmark_limit: number;
|
||||||
public_collection_limit: number;
|
public_collection_limit: number;
|
||||||
private_collection_limit: number;
|
private_collection_limit: number;
|
||||||
|
/** Direct-disciple link: the Mongo disciple id this account is linked to,
|
||||||
|
* and that disciple's name (hydrated server-side). Both null when the
|
||||||
|
* account isn't one of Śrīla Prabhupāda's direct disciples. */
|
||||||
|
disciple_id: string | null;
|
||||||
|
disciple_name: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** One option in the direct-disciple picker. `linked_handle` is the @handle of
|
||||||
|
* the account already linked to this disciple — non-null means "taken". */
|
||||||
|
export type AdminDisciple = {
|
||||||
|
id: string;
|
||||||
|
initiated_name: string;
|
||||||
|
suffix: string;
|
||||||
|
initiation_date: string | null;
|
||||||
|
initiation_place: string | null;
|
||||||
|
linked_handle: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Per-user save-limit override payload. null = reset that limit to the plan
|
/** Per-user save-limit override payload. null = reset that limit to the plan
|
||||||
@@ -287,6 +303,24 @@ export const api = {
|
|||||||
body: JSON.stringify(limits),
|
body: JSON.stringify(limits),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
/** Search Śrīla Prabhupāda's disciples for the link picker. An empty query
|
||||||
|
* returns the first page alphabetically. */
|
||||||
|
async disciples(q: string, limit = 25): Promise<AdminDisciple[]> {
|
||||||
|
const { data } = await call<AdminDisciple[]>(
|
||||||
|
`/admin/disciples${qs({ q, limit })}`,
|
||||||
|
);
|
||||||
|
return data ?? [];
|
||||||
|
},
|
||||||
|
/** Mark a user as a direct disciple. Re-linking moves an existing link. */
|
||||||
|
linkDisciple(userId: string, discipleId: string) {
|
||||||
|
return call(`/admin/users/${userId}/disciple`, {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({ disciple_id: discipleId }),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
unlinkDisciple(userId: string) {
|
||||||
|
return call(`/admin/users/${userId}/disciple`, { method: "DELETE" });
|
||||||
|
},
|
||||||
posts(q: string, limit: number, offset: number) {
|
posts(q: string, limit: number, offset: number) {
|
||||||
return listed<AdminPost>(`/admin/posts${qs({ q, limit, offset })}`);
|
return listed<AdminPost>(`/admin/posts${qs({ q, limit, offset })}`);
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user