import { useCallback, useEffect, useRef, useState } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { ArrowLeft, Mail, Shield, X } from "lucide-react";
import { toast } from "sonner";
import { Avatar, AvatarFallback } from "@/components/ui/avatar";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { ScrollArea } from "@/components/ui/scroll-area";
import { Separator } from "@/components/ui/separator";
import { Muted } from "@/components/ui/typography";
import { WindowControls } from "@/components/window-controls";
import { useNetworks } from "@/hooks/use-networks";
import {
useNetworkInvitations,
useInviteMembers,
useRevokeInvitation,
} from "@/hooks/use-invitations";
import { useAuthStore } from "@/stores/auth-store";
import { BillingSection } from "@/features/network-billing";
import type { Human } from "@/api/types";
function MemberRow({
human,
isAdmin,
}: {
human: Human;
isAdmin: boolean;
}) {
const initials = human.email_prefix.slice(0, 2).toUpperCase();
return (
{initials}
{human.email_prefix}
{human.email}
{isAdmin && (
Admin
)}
);
}
function InviteForm({ networkId }: { networkId: string }) {
const [email, setEmail] = useState("");
const inviteMembers = useInviteMembers(networkId);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
const trimmed = email.trim();
if (!trimmed) return;
inviteMembers.mutate([trimmed], {
onSuccess: () => {
toast.success(`Invitation sent to ${trimmed}`);
setEmail("");
},
onError: (err) => {
toast.error(err.message || "Failed to send invitation");
},
});
};
return (
);
}
function PendingInvitationRow({
email,
networkId,
}: {
email: string;
networkId: string;
}) {
const revokeInvitation = useRevokeInvitation(networkId);
const handleRevoke = () => {
revokeInvitation.mutate(email, {
onSuccess: () => {
toast.success(`Invitation to ${email} revoked`);
},
onError: (err) => {
toast.error(err.message || "Failed to revoke invitation");
},
});
};
return (
);
}
function SettingsGroup({
title,
children,
}: {
title: string;
children: React.ReactNode;
}) {
return (
);
}
export default function NetworkSettingsPage() {
const navigate = useNavigate();
const { networkId } = useParams<{ networkId: string }>();
const { data: networks } = useNetworks();
const network = networks?.find((n) => n.id === networkId);
const { data: invitations } = useNetworkInvitations(networkId!);
const currentUser = useAuthStore((s) => s.user);
const isAdmin = currentUser?.id === network?.admin_human.id;
const networkName = network?.name ?? "Network";
return (
{networkName}
{network?.humans.map((human, index) => (
{index < network.humans.length - 1 && (
)}
))}
{isAdmin && network && (
<>
{invitations && invitations.length > 0 ? (
invitations.map((inv, index) => (
{index < invitations.length - 1 && (
)}
))
) : (
No pending invitations
)}
>
)}
);
}