import Text from "@/components/Text";
import { regexOnlySpacesOrEnter } from "@/constants/OnlySpaceOrEnter";
import Styles from "@/constants/Styles";
import { useTheme } from "@/providers/ThemeProvider";
import { Feather, Ionicons, MaterialCommunityIcons, MaterialIcons } from "@expo/vector-icons";
import * as DocumentPicker from "expo-document-picker";
import { Platform, Pressable, ScrollView, TextInput, View } from "react-native";
import Toast from "react-native-toast-message";
type Props = {
mode: 'new' | 'edit' | 'locked'
lockedReason?: string
value: string
onChange: (val: string) => void
loading: boolean
onSend: () => void
onCancelEdit?: () => void
files?: { uri: string; name: string }[]
onAddFile?: (files: { uri: string; name: string }[]) => void
onRemoveFile?: (index: number) => void
existingFiles?: { id: string; name: string; extension: string }[]
onRemoveExistingFile?: (id: string) => void
canSend: boolean
}
export default function DiscussionCommentInput({
mode, lockedReason, value, onChange, loading, onSend,
onCancelEdit, files = [], onAddFile, onRemoveFile,
existingFiles = [], onRemoveExistingFile, canSend
}: Props) {
const { colors } = useTheme()
async function pickFiles() {
const result = await DocumentPicker.getDocumentAsync({ type: ['*/*'], multiple: true })
if (!result.canceled && onAddFile) {
let skipped = 0
const newFiles: { uri: string; name: string }[] = []
for (const asset of result.assets) {
if (!asset.uri) continue
if (files.some(f => f.name === asset.name)) { skipped++; continue }
newFiles.push({ uri: asset.uri, name: asset.name })
}
if (skipped > 0) Toast.show({ type: 'small', text1: 'Beberapa file sudah ditambahkan' })
if (newFiles.length > 0) onAddFile(newFiles)
}
}
if (mode === 'locked') {
return (
{lockedReason}
)
}
const sendDisabled = loading || value.trim() === '' || regexOnlySpacesOrEnter.test(value) || !canSend
return (
{mode === 'edit' && (
Edit Komentar
)}
{(existingFiles.length > 0 || files.length > 0) && (
{existingFiles.map((f) => (
onRemoveExistingFile?.(f.id)}
style={{
flexDirection: 'row', alignItems: 'center', gap: 6,
paddingHorizontal: 10, paddingVertical: 6,
borderRadius: 20, borderWidth: 1,
backgroundColor: colors.card, borderColor: colors.icon + '18',
marginRight: 8
}}
>
{f.name}.{f.extension}
))}
{files.map((f, idx) => (
onRemoveFile?.(idx)}
style={{
flexDirection: 'row', alignItems: 'center', gap: 6,
paddingHorizontal: 10, paddingVertical: 6,
borderRadius: 20, borderWidth: 1,
backgroundColor: colors.card, borderColor: colors.icon + '18',
marginRight: 8
}}
>
{f.name}
))}
)}
{mode === 'new' && (
0 ? colors.tabActive : colors.dimmed}
/>
)}
!sendDisabled && onSend()}
style={{
width: 40, height: 40, borderRadius: 20,
backgroundColor: sendDisabled ? colors.dimmed + '40' : colors.tint,
justifyContent: 'center', alignItems: 'center',
marginBottom: 0
}}
>
)
}