import { GROUP_MEMBER_MAX, GROUP_MEMBER_MIN } from "@rakazo/contracts"; import { botAvatarImageSrc } from "@rakazo/core"; import { Stack, useRouter } from "expo-router"; import { useEffect, useState } from "react"; import { Pressable, ScrollView, Text, TextInput } from "react-native"; import { BotAvatar } from "../components/bot-avatar"; import { type MobileBot, rpc } from "../lib/api"; export default function NewGroup() { const router = useRouter(); const [bots, setBots] = useState([]); const [name, setName] = useState(""); const [selected, setSelected] = useState([]); const [error, setError] = useState(null); const [pending, setPending] = useState(false); useEffect(() => { void rpc("bots/list") .then((nextBots) => setBots(nextBots.filter((bot) => !bot.archivedAt))) .catch(() => undefined); }, []); function toggle(botId: string) { setSelected((current) => { if (current.includes(botId)) return current.filter((id) => id !== botId); if (current.length >= GROUP_MEMBER_MAX) return current; return [...current, botId]; }); } async function create() { if ( !name.trim() || selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX || pending ) return; setPending(true); setError(null); try { const group = await rpc<{ id: string; name: string }>("groups/create", { name: name.trim(), botIds: selected, }); router.replace({ pathname: "/group-thread", params: { groupId: group.id, name: group.name }, }); } catch (err) { setError(err instanceof Error ? err.message : "Could not create group"); } finally { setPending(false); } } return ( <> Name Members ({GROUP_MEMBER_MIN}–{GROUP_MEMBER_MAX}) {bots.map((bot) => { const checked = selected.includes(bot.id); return ( toggle(bot.id)} style={{ flexDirection: "row", alignItems: "center", gap: 12, paddingVertical: 12, }} > {bot.name} {checked ? "✓" : ""} ); })} {error ? {error} : null} void create()} disabled={ !name.trim() || selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX || pending } style={{ marginTop: 24, backgroundColor: "#8B5CF6", opacity: !name.trim() || selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX || pending ? 0.5 : 1, borderRadius: 11, padding: 14, alignItems: "center", }} > {pending ? "Creating…" : "Create group"} ); }