import React, { useMemo, useState } from 'react';
import { Pressable, StyleSheet, Text, TextInput, View } from 'react-native';
import {
errorCodes,
isErrorWithCode,
keepLocalCopy,
pick,
types,
} from '@react-native-documents/picker';
import { Camera, FileText, Images, Paperclip, Phone, Send, Video, Webcam } from 'lucide-react-native';
import { Asset, launchCamera, launchImageLibrary } from 'react-native-image-picker';
import { EnxChatTheme } from '../types';
/** WhatsApp-style send accent (brand green). */
const SEND_ICON_GREEN = '#25D366';
const SEND_ICON_MUTED = '#9ca3af';
const ATTACH_ICON_COLOR = '#374151';
const ATTACH_CIRCLE_BG = '#e8eaed';
const ICON_STROKE = 2;
const ICON_SIZE = 26;
type FilePayload = { base64: string; fileName: string; mimeType: string; localUri?: string; size?: number };
const payloadFromAsset = (asset: Asset): FilePayload | null => {
if (!asset.base64 || !asset.fileName) return null;
return {
base64: asset.base64,
fileName: asset.fileName,
mimeType: asset.type ?? 'application/octet-stream',
localUri: asset.uri,
size: asset.fileSize,
};
};
function AttachCircleButton({
onPress,
accessibilityLabel,
children,
}: {
onPress: () => void;
accessibilityLabel: string;
children: React.ReactNode;
}): React.JSX.Element {
return (
{children}
);
}
export const InputBar = ({
theme,
enabled,
showCallingOptions,
onSendText,
onSendFile,
onAudioCall,
onVideoCall,
}: {
theme: EnxChatTheme;
enabled: boolean;
showCallingOptions?: boolean;
onSendText?: (text: string) => void;
onSendFile?: (payload: FilePayload) => void;
onAudioCall?: () => void;
onVideoCall?: () => void;
}): React.JSX.Element => {
const [text, setText] = useState('');
const [showAttach, setShowAttach] = useState(false);
const canSend = useMemo(() => text.trim().length > 0, [text]);
const send = (): void => {
const clean = text.trim();
if (!clean) return;
setText('');
onSendText?.(clean);
};
const pickCamera = async (): Promise => {
setShowAttach(false);
const res = await launchCamera({ mediaType: 'photo', includeBase64: true, quality: 0.8 });
const payload = res.assets?.[0] ? payloadFromAsset(res.assets[0]) : null;
if (payload) onSendFile?.(payload);
};
const pickPhoto = async (): Promise => {
setShowAttach(false);
const res = await launchImageLibrary({ mediaType: 'photo', includeBase64: true, selectionLimit: 1, quality: 0.8 });
const payload = res.assets?.[0] ? payloadFromAsset(res.assets[0]) : null;
if (payload) onSendFile?.(payload);
};
const pickVideo = async (): Promise => {
setShowAttach(false);
const res = await launchImageLibrary({ mediaType: 'video', includeBase64: true, selectionLimit: 1 });
const payload = res.assets?.[0] ? payloadFromAsset(res.assets[0]) : null;
if (payload) onSendFile?.(payload);
};
const pickFile = async (): Promise => {
setShowAttach(false);
try {
const [file] = await pick({
type: types.allFiles,
allowMultiSelection: false,
mode: 'import',
});
if (!file?.name) return;
const [copy] = await keepLocalCopy({
destination: 'cachesDirectory',
files: [{ uri: file.uri, fileName: file.name }],
});
const localUri = copy.status === 'success' ? copy.localUri : file.uri;
onSendFile?.({
base64: '',
fileName: file.name,
mimeType: file.type ?? 'application/octet-stream',
localUri,
size: file.size ?? undefined,
});
} catch (e) {
if (isErrorWithCode(e) && e.code === errorCodes.OPERATION_CANCELED) return;
}
};
const iconProps = { color: ATTACH_ICON_COLOR, size: ICON_SIZE, strokeWidth: ICON_STROKE };
return (
{showAttach && (
{showCallingOptions && (
<>
{
setShowAttach(false);
onAudioCall?.();
}}
accessibilityLabel="Audio call"
>
{
setShowAttach(false);
onVideoCall?.();
}}
accessibilityLabel="Video call"
>
>
)}
)}
setShowAttach((v) => !v)}
style={styles.iconHit}
accessibilityLabel="Attach"
>
{showAttach ? (
✕
) : (
)}
);
};
const styles = StyleSheet.create({
wrap: { backgroundColor: '#fff', borderTopWidth: StyleSheet.hairlineWidth, borderTopColor: '#d1d5db' },
attachPanel: {
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#e5e7eb',
backgroundColor: '#fff',
paddingBottom: 4,
},
attachGrid: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'flex-start',
paddingHorizontal: 12,
paddingTop: 12,
},
attachItem: { width: '33.33%', alignItems: 'center', paddingVertical: 10 },
attachCircle: {
width: 56,
height: 56,
borderRadius: 28,
backgroundColor: ATTACH_CIRCLE_BG,
alignItems: 'center',
justifyContent: 'center',
},
row: { flexDirection: 'row', alignItems: 'flex-end', paddingHorizontal: 8, paddingVertical: 6 },
side: { paddingHorizontal: 10, paddingVertical: 10, fontWeight: '600' },
// Generous hit target around vector icons so they are easy to tap.
iconHit: { padding: 8 },
input: {
flex: 1,
maxHeight: 120,
backgroundColor: '#f3f4f6',
borderWidth: StyleSheet.hairlineWidth,
borderColor: '#d1d5db',
borderRadius: 22,
paddingHorizontal: 14,
paddingVertical: 9,
color: '#111827',
},
});