import { Feather } from "@expo/vector-icons"; import { useQuery, useQueryClient } from "@tanstack/react-query"; import DateTimePicker from "@react-native-community/datetimepicker"; import * as Haptics from "expo-haptics"; import { Image } from "expo-image"; import * as ImagePicker from "expo-image-picker"; import { fetch as expoFetch } from "expo/fetch"; import { useLocalSearchParams } from "expo-router"; import React, { useEffect, useState } from "react"; import { ActivityIndicator, Alert, Platform, ScrollView, StyleSheet, Switch, Text, TextInput, TouchableOpacity, View, } from "react-native"; import { KeyboardAwareScrollView } from "react-native-keyboard-controller"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { ChannelChip } from "@/components/ChannelChip"; import { PostizIntegration, PostizUploadResult, usePostiz } from "@/context/PostizContext"; import { useColors } from "@/hooks/useColors"; export default function ComposeScreen() { const colors = useColors(); const insets = useSafeAreaInsets(); const { client, isConfigured, apiKey, baseUrl } = usePostiz(); const queryClient = useQueryClient(); const { prefillContent, prefillIntegrationIds } = useLocalSearchParams<{ prefillContent?: string; prefillIntegrationIds?: string; }>(); const [content, setContent] = useState(""); const [selectedChannels, setSelectedChannels] = useState([]); const [postNow, setPostNow] = useState(false); const [scheduleDate, setScheduleDate] = useState( () => new Date(Date.now() + 60 * 60 * 1000) ); const [showDatePicker, setShowDatePicker] = useState(false); const [showTimePicker, setShowTimePicker] = useState(false); const [imageUri, setImageUri] = useState(null); const [uploading, setUploading] = useState(false); const [submitting, setSubmitting] = useState(false); useEffect(() => { if (prefillContent) setContent(String(prefillContent)); if (prefillIntegrationIds) { setSelectedChannels(String(prefillIntegrationIds).split(",").filter(Boolean)); } }, [prefillContent, prefillIntegrationIds]); const { data: integrations, isLoading: loadingIntegrations } = useQuery({ queryKey: ["integrations", !!client], queryFn: async () => { if (!client) return []; const res = await client.get("integrations"); return Array.isArray(res.data) ? res.data : res.data?.integrations ?? []; }, enabled: !!client, staleTime: 60000, }); const toggleChannel = (id: string) => { setSelectedChannels((prev) => prev.includes(id) ? prev.filter((c) => c !== id) : [...prev, id] ); }; const pickImage = async () => { const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync(); if (status !== "granted") { Alert.alert("Permission required", "Allow access to your photo library."); return; } const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ["images"], allowsEditing: false, quality: 0.85, }); if (!result.canceled && result.assets[0]) { setImageUri(result.assets[0].uri); } }; const removeImage = () => setImageUri(null); const uploadImage = async (): Promise => { setUploading(true); try { const formData = new FormData(); if (Platform.OS === "web") { const response = await expoFetch(imageUri!); const blob = await response.blob(); formData.append("file", blob, "upload.jpg"); } else { formData.append("file", { uri: imageUri!, name: "upload.jpg", type: "image/jpeg", } as unknown as Blob); } // eslint-disable-next-line no-undef const uploadRes = await globalThis.fetch(`${baseUrl}/upload`, { method: "POST", headers: { Authorization: apiKey }, body: formData, }); if (!uploadRes.ok) { const raw = await uploadRes.text().catch(() => uploadRes.statusText); throw new Error(`Upload HTTP ${uploadRes.status}: ${raw.slice(0, 200)}`); } return await uploadRes.json() as PostizUploadResult; } finally { setUploading(false); } }; const handleSubmit = async () => { if (!isConfigured) return; if (!content.trim()) { Alert.alert("Empty post", "Please write something before posting."); return; } if (selectedChannels.length === 0) { Alert.alert("No channel", "Please select at least one channel."); return; } Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium); setSubmitting(true); try { let media: Array<{ id: string; path: string }> = []; if (imageUri) { const uploaded = await uploadImage(); media = [{ id: uploaded.id, path: uploaded.path }]; } const payload = { type: postNow ? "now" : "schedule", date: postNow ? new Date().toISOString() : scheduleDate.toISOString(), shortLink: false, tags: [] as string[], posts: selectedChannels.map((integrationId) => { const valueItem: { content: string; image?: Array<{ id: string; path: string }> } = { content: content.trim(), }; if (media.length > 0) valueItem.image = media; return { integration: { id: integrationId }, value: [valueItem], }; }), }; const body = JSON.stringify(payload); console.log("[compose] POST", `${baseUrl}/posts`, body); // eslint-disable-next-line no-undef const res = await globalThis.fetch(`${baseUrl}/posts`, { method: "POST", headers: { Authorization: apiKey, "Content-Type": "application/json", }, body, }); if (!res.ok) { let detail = ""; try { const raw = await res.text(); console.log("[compose] 400 body:", raw); detail = raw.slice(0, 500); } catch { detail = res.statusText; } throw new Error(`HTTP ${res.status}: ${detail}`); } Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success); Alert.alert( "Posted!", postNow ? "Your post has been published." : "Post scheduled successfully.", [{ text: "OK", onPress: resetForm }] ); queryClient.invalidateQueries({ queryKey: ["posts"] }); queryClient.invalidateQueries({ queryKey: ["posts-list"] }); } catch (e: unknown) { Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error); const msg = e instanceof Error ? e.message : "Could not submit post."; Alert.alert("Failed", msg); } finally { setSubmitting(false); } }; const resetForm = () => { setContent(""); setSelectedChannels([]); setPostNow(false); setImageUri(null); setScheduleDate(new Date(Date.now() + 60 * 60 * 1000)); }; const formatDateLabel = (d: Date) => d.toLocaleDateString("en-US", { month: "short", day: "numeric", year: "numeric", }); const formatTimeLabel = (d: Date) => d.toLocaleTimeString("en-US", { hour: "2-digit", minute: "2-digit" }); if (!isConfigured) { return ( Not Configured Add your API key in Settings ); } return ( {content.length}/3000 {imageUri && ( )} {imageUri ? "Change image" : "Add image"} CHANNELS {loadingIntegrations ? ( ) : (integrations ?? []).length === 0 ? ( No channels found. Add integrations in your Postiz instance. ) : ( {(integrations ?? []).map((intg) => ( toggleChannel(intg.id)} /> ))} )} Post now {!postNow && ( { setShowTimePicker(false); setShowDatePicker((v) => !v); }} style={[ styles.dateBtn, { backgroundColor: colors.card, borderColor: colors.border }, ]} activeOpacity={0.7} > {formatDateLabel(scheduleDate)} { setShowDatePicker(false); setShowTimePicker((v) => !v); }} style={[ styles.dateBtn, { backgroundColor: colors.card, borderColor: colors.border }, ]} activeOpacity={0.7} > {formatTimeLabel(scheduleDate)} )} {showDatePicker && ( { if (Platform.OS !== "ios") setShowDatePicker(false); if (date) { const merged = new Date(scheduleDate); merged.setFullYear(date.getFullYear(), date.getMonth(), date.getDate()); setScheduleDate(merged); } }} /> )} {showTimePicker && ( { if (Platform.OS !== "ios") setShowTimePicker(false); if (date) { const merged = new Date(scheduleDate); merged.setHours(date.getHours(), date.getMinutes()); setScheduleDate(merged); } }} /> )} {submitting || uploading ? ( ) : ( <> {postNow ? "Publish Now" : "Schedule Post"} )} ); } const styles = StyleSheet.create({ container: { paddingHorizontal: 16, gap: 14, }, centered: { flex: 1, alignItems: "center", justifyContent: "center", gap: 10, }, textArea: { borderRadius: 14, borderWidth: 1, padding: 14, minHeight: 140, }, textInput: { fontSize: 15, fontFamily: "Inter_400Regular", lineHeight: 22, minHeight: 100, }, charCount: { fontSize: 11, fontFamily: "Inter_400Regular", alignSelf: "flex-end", marginTop: 4, }, imagePreviewWrap: { position: "relative", alignSelf: "flex-start", }, imagePreview: { width: 120, height: 120, borderRadius: 10, borderWidth: 1, }, removeImg: { position: "absolute", top: 4, right: 4, width: 20, height: 20, borderRadius: 10, alignItems: "center", justifyContent: "center", }, mediaBtn: { flexDirection: "row", alignItems: "center", gap: 8, paddingHorizontal: 14, paddingVertical: 10, borderRadius: 10, borderWidth: 1, alignSelf: "flex-start", }, mediaBtnText: { fontSize: 13, fontFamily: "Inter_500Medium", }, sectionLabel: { fontSize: 11, fontFamily: "Inter_600SemiBold", letterSpacing: 0.8, marginBottom: -6, }, sectionTitle: { fontSize: 18, fontFamily: "Inter_600SemiBold", }, hint: { fontSize: 13, fontFamily: "Inter_400Regular", textAlign: "center", }, channelList: { flexDirection: "row", gap: 8, flexWrap: "wrap", }, scheduleRow: { flexDirection: "row", alignItems: "center", justifyContent: "space-between", paddingHorizontal: 16, paddingVertical: 12, borderRadius: 12, borderWidth: 1, }, scheduleRowLeft: { flexDirection: "row", alignItems: "center", gap: 10, }, scheduleLabel: { fontSize: 15, fontFamily: "Inter_500Medium", }, dateTimeRow: { flexDirection: "row", gap: 10, }, dateBtn: { flex: 1, flexDirection: "row", alignItems: "center", gap: 8, paddingHorizontal: 12, paddingVertical: 10, borderRadius: 10, borderWidth: 1, }, dateBtnText: { fontSize: 13, fontFamily: "Inter_500Medium", }, submitBtn: { flexDirection: "row", alignItems: "center", justifyContent: "center", gap: 8, paddingVertical: 14, borderRadius: 14, marginTop: 4, }, submitText: { fontSize: 15, fontFamily: "Inter_600SemiBold", }, });