import { GROUP_MEMBER_MAX, GROUP_MEMBER_MIN } from "@rakazo/contracts"; import { Stack, useRouter } from "expo-router"; import { useEffect, useState } from "react"; import { Pressable, ScrollView, Text, TextInput } from "react-native"; import { BotMemberPicker } from "../components/bot-member-picker"; import { type MobileBot, rpc } from "../lib/api"; import { useI18n } from "../lib/i18n"; import { useMobileTokens } from "../lib/native"; export default function NewGroup() { const { t } = useI18n(); const tokens = useMobileTokens(); 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); }, []); 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 : t("Could not create group")); } finally { setPending(false); } } return ( <> {t("Name")} {t("Members")} {error ? {error} : null} void create()} disabled={ !name.trim() || selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX || pending } style={{ marginTop: 24, backgroundColor: tokens.primary, opacity: !name.trim() || selected.length < GROUP_MEMBER_MIN || selected.length > GROUP_MEMBER_MAX || pending ? 0.5 : 1, borderRadius: 11, padding: 14, alignItems: "center", }} > {pending ? t("Creating…") : t("Create group")} ); }