"use client"; import { useI18n } from "@/components/I18nProvider"; import { useCallback, useEffect, useState } from "react"; import { api } from "./StartGame"; export type MapAdmin = { id: string; user_id: string; state: string; created_at: string; name: string; username: string | null; }; export function MapAdmins({ id, access }: { id: string; access: string }) { const { t } = useI18n(); const [admins, setAdmins] = useState([]); const [caller, setCaller] = useState(""); const [name, setName] = useState(""); const [error, setError] = useState(""); const [message, setMessage] = useState(""); const [busy, setBusy] = useState(false); const load = useCallback(async () => { try { const result = await api<{ admins: MapAdmin[]; caller: string }>( `datasets/${id}/admins`, ); setAdmins(result.admins); setCaller(result.caller); setError(""); } catch (e) { setError((e as Error).message); } }, [id]); useEffect(() => { load(); const timer = setInterval(load, 10000); return () => clearInterval(timer); }, [load]); async function invite(event: React.FormEvent) { event.preventDefault(); setBusy(true); setError(""); setMessage(""); try { await api(`datasets/${id}/admins`, { name }); setName(""); setMessage( "Invite sent. Your friend can accept it from their dashboard.", ); await load(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } async function remove(userId: string) { setBusy(true); setError(""); try { await api(`datasets/${id}/admins/${userId}`, undefined, "DELETE"); await load(); } catch (e) { setError((e as Error).message); } finally { setBusy(false); } } return (

{t("Map admins")}

{t( "Invite friends to manage this map with you. They can edit details, choose locations, refresh the map and handle contributions after accepting your invite.", )}

{admins.map((a) => (
{a.name} {a.username ? @{a.username} : null}{" "} {a.state === "accepted" ? t("· admin") : t("· invite pending")} {(access === "owner" || a.user_id === caller) && ( )}
))} {access === "owner" && (
)} {error && (

{t(error)}

)} {message && (

{t(message)}

)}
); }