import { useState } from 'react'; import { ActivityIndicator, StyleSheet, Text, TouchableOpacity, View, } from 'react-native'; import { useTranslation } from 'react-i18next'; import { apiFetch } from '../../lib/api'; import { useColors } from '../../lib/theme'; import { FormSheet } from '../FormSheet'; import { SheetFieldStack } from '../SheetFieldStack'; type Props = { visible: boolean; onClose: () => void; onCreated: (room: any) => void; }; export function CreateRoomSheet({ visible, onClose, onCreated }: Props) { const { t } = useTranslation(); const colors = useColors(); const styles = makeStyles(colors); const [name, setName] = useState(''); const [description, setDescription] = useState(''); const [isPublic, setIsPublic] = useState(true); const [joinMode, setJoinMode] = useState<'approval' | 'invite_only'>('approval'); const [creating, setCreating] = useState(false); const [fieldsDone, setFieldsDone] = useState(false); function reset() { setName(''); setDescription(''); setIsPublic(true); setJoinMode('approval'); setFieldsDone(false); } function handleClose() { reset(); onClose(); } async function create() { const trimmed = name.trim(); if (!trimmed || creating) return; setCreating(true); try { const room = await apiFetch('/api/chat/rooms', { method: 'POST', body: { name: trimmed, description: description.trim() || undefined, isPublic, joinMode: isPublic ? 'open' : joinMode, }, }); onCreated(room); reset(); onClose(); } catch { // ignore — Server-Error wird in einem späteren Pass mit Feedback versehen } finally { setCreating(false); } } return ( (v.trim().length === 0 ? ' ' : undefined), }, { key: 'description', label: t('chat.room_description'), placeholder: t('chat.room_description'), value: description, onChangeText: setDescription, autoCapitalize: 'sentences', }, ]} onComplete={() => setFieldsDone(true)} > {/* Public-Toggle */} setIsPublic((v) => !v)} > {t('chat.public_room')} {/* Join-Mode (nur bei privaten Gruppen) */} {!isPublic && ( {t('chat.join_mode')} {(['approval', 'invite_only'] as const).map((mode) => ( setJoinMode(mode)} > {t(`chat.join_mode_${mode === 'approval' ? 'approval' : 'invite'}`)} ))} )} {/* Action-Buttons — Abbrechen bleibt (kein Header-Button) */} {t('common.cancel')} {creating ? ( ) : ( {t('chat.create')} )} ); } function makeStyles(colors: ReturnType) { return StyleSheet.create({ toggleRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingVertical: 8, marginTop: 4, marginBottom: 4, }, toggleLabel: { fontSize: 14, fontFamily: 'Nunito_600SemiBold', color: colors.text, }, toggle: { width: 46, height: 28, borderRadius: 14, backgroundColor: colors.surfaceElevated, padding: 2, justifyContent: 'center', }, toggleOn: { backgroundColor: '#007AFF', }, toggleKnob: { width: 24, height: 24, borderRadius: 12, backgroundColor: colors.bg, shadowColor: '#000', shadowOpacity: 0.15, shadowRadius: 2, shadowOffset: { width: 0, height: 1 }, elevation: 2, }, toggleKnobOn: { transform: [{ translateX: 18 }], }, subLabel: { fontSize: 12, fontFamily: 'Nunito_600SemiBold', color: colors.textMuted, marginBottom: 6, }, modeRow: { flexDirection: 'row', }, modeBtn: { flex: 1, paddingVertical: 8, borderRadius: 10, borderWidth: 1, borderColor: colors.border, alignItems: 'center', marginRight: 6, }, modeBtnActive: { backgroundColor: colors.surface, borderColor: '#007AFF', }, modeBtnText: { fontSize: 12, fontFamily: 'Nunito_600SemiBold', color: colors.textMuted, }, modeBtnTextActive: { color: '#007AFF', }, actions: { flexDirection: 'row', marginTop: 12, marginBottom: 10, }, cancelBtn: { flex: 1, backgroundColor: colors.surfaceElevated, paddingVertical: 12, borderRadius: 12, alignItems: 'center', marginRight: 6, }, cancelText: { fontSize: 14, fontFamily: 'Nunito_600SemiBold', color: colors.text, }, createBtn: { flex: 1, backgroundColor: '#007AFF', paddingVertical: 12, borderRadius: 12, alignItems: 'center', marginLeft: 6, }, createText: { fontSize: 14, fontFamily: 'Nunito_700Bold', color: '#fff', }, }); }