diff --git a/apps/frontend/app/add.tsx b/apps/frontend/app/add.tsx new file mode 100644 index 0000000..2cd8009 --- /dev/null +++ b/apps/frontend/app/add.tsx @@ -0,0 +1,212 @@ +import React, { useState } from 'react'; +import { + TextInput, + TouchableOpacity, + ScrollView, + Alert, + ActivityIndicator, + Platform, +} from 'react-native'; +import { useRouter } from 'expo-router'; +import * as ImagePicker from 'expo-image-picker'; +import { Text, View } from '@/components/Themed'; +import { savesApi } from '@/lib/api'; +import { useColorScheme } from '@/components/useColorScheme'; +import Colors from '@/constants/Colors'; + +export default function AddSaveScreen() { + const [mode, setMode] = useState<'upload' | 'url'>('upload'); + const [url, setUrl] = useState(''); + const [name, setName] = useState(''); + const [description, setDescription] = useState(''); + const [tags, setTags] = useState(''); + const [visibility, setVisibility] = useState<'public' | 'link'>('link'); + const [loading, setLoading] = useState(false); + const router = useRouter(); + const colorScheme = useColorScheme(); + const colors = Colors[colorScheme ?? 'light']; + + const handleUpload = async () => { + if (mode === 'url') { + if (!url) { + Alert.alert('Ошибка', 'Введите URL'); + return; + } + } + + setLoading(true); + try { + if (mode === 'url') { + const tagsArray = tags + ? tags.split(',').map((t) => t.trim()).filter(Boolean) + : undefined; + + await savesApi.createFromUrl({ + url, + name: name || undefined, + description: description || undefined, + tags: tagsArray, + visibility, + }); + } else { + // Загрузка файла + const result = await ImagePicker.launchImageLibraryAsync({ + mediaTypes: ImagePicker.MediaTypeOptions.All, + allowsEditing: false, + quality: 1, + }); + + if (result.canceled) { + setLoading(false); + return; + } + + const asset = result.assets[0]; + const file = { + uri: asset.uri, + type: asset.mimeType || 'image/jpeg', + name: asset.fileName || `image.${asset.uri.split('.').pop()}`, + }; + + const tagsArray = tags + ? tags.split(',').map((t) => t.trim()).filter(Boolean) + : undefined; + + await savesApi.uploadFile(file, { + name: name || undefined, + description: description || undefined, + tags: tagsArray, + visibility, + }); + } + + Alert.alert('Успех', 'Сейв успешно создан', [ + { text: 'OK', onPress: () => router.back() }, + ]); + } catch (error: any) { + Alert.alert('Ошибка', error.message || 'Не удалось создать сейв'); + } finally { + setLoading(false); + } + }; + + return ( + + + Добавить сейв + + + setMode('upload')} + > + + Загрузить файл + + + setMode('url')} + > + + По URL + + + + + {mode === 'url' && ( + + )} + + + + + + + + + Видимость: + setVisibility('public')} + > + + Публичный + + + setVisibility('link')} + > + + По ссылке + + + + + + {loading ? ( + + ) : ( + + {mode === 'url' ? 'Создать из URL' : 'Выбрать файл'} + + )} + + + + ); +}