Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/app/(tabs)/profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { authApi } from '../../features/auth/api';
import { useSettings } from '../../features/settings/hooks';
import RegisterScreen from '../(auth)/register';
import LoginScreen from '../(auth)/login';
import { useNavigation } from '@react-navigation/native';
import { useNavigation, useRoute } from '@react-navigation/native';

// ---------- Unauthenticated view ----------
function GuestView() {
Expand Down Expand Up @@ -79,11 +79,11 @@ function UserProfile() {
const initials =
user?.username || user?.name
? (user.username || user.name)
.split(' ')
.map((w) => w[0])
.slice(0, 2)
.join('')
.toUpperCase()
.split(' ')
.map((w) => w[0])
.slice(0, 2)
.join('')
.toUpperCase()
: '?';

return (
Expand Down Expand Up @@ -311,6 +311,14 @@ function makeStyles(colors) {
export default function ProfileTab() {
const { isAuthenticated } = useAuth();
const { colors } = useSettings();
const navigation = useNavigation();
const route = useRoute();

React.useEffect(() => {
if (isAuthenticated && route.params?.goBack) {
navigation.goBack();
}
}, [isAuthenticated]);

return (
<SafeAreaView style={[{ flex: 1, backgroundColor: colors.background }]} edges={[]}>
Expand Down
35 changes: 34 additions & 1 deletion src/app/story/chapter/[id].jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, useRef, useMemo } from 'react';
import { useEffect, useState, useRef, useMemo } from 'react';
import {
View,
Text,
Expand All @@ -21,6 +21,7 @@ import { getChaptersByComic, getChapterById } from '../../../features/chapters/a
import { useRoute, useNavigation } from '@react-navigation/native';
import { updateReadingHistory } from '../../../features/bookmarks/api';
import { useAuth } from '../../../features/auth/hooks';
import { ReaderChatbot } from '../../../features/chapters/reader/ReaderChatbot';

const { width } = Dimensions.get('window');
const ITEM_HEIGHT = 64;
Expand Down Expand Up @@ -129,6 +130,24 @@ function makeStyles(colors) {
chapterItemText: { fontSize: 15, color: colors.textSecondary },
chapterItemTextActive: { color: colors.primary, fontWeight: 'bold' },
activeIndicator: { color: colors.primary, fontSize: 20 },
chatbotFab: {
position: 'absolute',
bottom: 90,
right: 20,
width: 48,
height: 48,
borderRadius: 24,
backgroundColor: colors.primary,
justifyContent: 'center',
alignItems: 'center',
zIndex: 20,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.3,
shadowRadius: 4,
elevation: 6,
},
chatbotFabText: { fontSize: 22 },
});
}

Expand All @@ -149,6 +168,7 @@ export default function ChapterDetail() {
const [chapterModal, setChapterModal] = useState(false);
const [sortOrder, setSortOrder] = useState('asc');
const [imageLoading, setImageLoading] = useState({});
const [chatbotVisible, setChatbotVisible] = useState(false);

const chapterListRef = useRef(null);
const uiAnim = useRef(new Animated.Value(1)).current;
Expand Down Expand Up @@ -371,6 +391,19 @@ export default function ChapterDetail() {
</View>
</Animated.View>

{/* CHATBOT FAB */}
<TouchableOpacity style={styles.chatbotFab} onPress={() => setChatbotVisible(true)}>
<Text style={styles.chatbotFabText}>🤖</Text>
</TouchableOpacity>

{/* CHATBOT MODAL */}
<ReaderChatbot
visible={chatbotVisible}
onClose={() => setChatbotVisible(false)}
comicId={comicId}
currentChapterNumber={chapter?.chapterNumber}
/>

{/* CHAPTER MODAL */}
<Modal visible={chapterModal} transparent animationType="fade">
<View style={styles.modalBackdrop}>
Expand Down
10 changes: 10 additions & 0 deletions src/features/chapters/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { axiosInstance } from '../../services/api/axios';
import { endpoints } from '../../services/api/endpoints';

export async function getChaptersByComic(comicId) {
const response = await axiosInstance.get(`/api/chapters/comic/${comicId}`);
Expand All @@ -9,3 +10,12 @@ export async function getChapterById(chapterId) {
const response = await axiosInstance.get(`/api/chapters/${chapterId}`);
return response.data;
}

export async function askReaderChatbot(comicId, message, currentChapterNumber) {
const { data } = await axiosInstance.post(
endpoints.readerChatbot(comicId),
{ message, currentChapterNumber },
{ requiresAuth: true }
);
return data;
}
260 changes: 260 additions & 0 deletions src/features/chapters/reader/ReaderChatbot.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
import { useState, useRef, useEffect } from 'react';
import {
View,
Text,
Modal,
TouchableOpacity,
TextInput,
ScrollView,
ActivityIndicator,
StyleSheet,
KeyboardAvoidingView,
Platform,
} from 'react-native';
import { useTranslation } from 'react-i18next';
import { useSettings } from '../../settings/hooks';
import { useAuth } from '../../auth/hooks';
import { askReaderChatbot } from '../api';
import { useNavigation } from '@react-navigation/native';

export function ReaderChatbot({ visible, onClose, comicId, currentChapterNumber }) {
const { colors, language } = useSettings();
const { isAuthenticated } = useAuth();
const { t } = useTranslation();
const styles = makeStyles(colors);
const navigation = useNavigation();

Comment on lines +20 to +26
const handleLoginPress = () => {
onClose();
navigation.navigate('Profile', { goBack: true });
};


const [input, setInput] = useState('');
const [messages, setMessages] = useState([]);
const [loading, setLoading] = useState(false);
const scrollRef = useRef(null);

useEffect(() => {
if (messages.length > 0) {
setTimeout(() => scrollRef.current?.scrollToEnd({ animated: true }), 100);
}
}, [messages]);

const sendMessage = async (text) => {
const question = text ?? input.trim();
if (!question || loading) return;
setInput('');
setLoading(true);
try {
const data = await askReaderChatbot(comicId, question, currentChapterNumber);
setMessages((prev) => [...prev, { question, response: data.responses }]);
} catch {
setMessages((prev) => [
...prev,
{
question,
response: {
vi: t('story.chatbot.errorMessage'),
en: t('story.chatbot.errorMessage'),
Comment on lines +58 to +59
},
},
]);
} finally {
setLoading(false);
}
};
const samples = t('story.chatbot.samples', { returnObjects: true });


return (
<Modal visible={visible} transparent animationType="slide" onRequestClose={onClose}>
<View style={styles.overlay}>
<TouchableOpacity style={styles.backdrop} onPress={onClose} activeOpacity={1} />
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
style={styles.sheet}
>
{/* Header */}
<View style={styles.header}>
<Text style={styles.headerTitle}>{t('story.chatbot.title')}</Text>
<TouchableOpacity onPress={onClose} hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}>
<Text style={styles.closeBtn}>✕</Text>
</TouchableOpacity>
</View>

{/* Messages / Quick questions */}
<ScrollView
ref={scrollRef}
style={styles.body}
contentContainerStyle={[
styles.bodyContent,
!isAuthenticated
]}
Comment on lines +90 to +93
keyboardShouldPersistTaps="handled"
>
{!isAuthenticated && (
<View style={styles.loginRequiredContainer}>
<Text style={styles.loginRequiredText}>
{t('home.loginRequiredPrefix')}
<Text style={styles.loginRequiredLink} onPress={handleLoginPress}>
{t('home.loginRequiredLink')}
</Text>
{t('home.loginRequiredSuffix')}
</Text>
</View>
)}

{isAuthenticated && messages.length === 0 && (
<View>
<Text style={styles.hint}>{t('story.chatbot.hint')}</Text>
{Array.isArray(samples) &&
samples.map((q, i) => (
<TouchableOpacity key={i} style={styles.sample} onPress={() => sendMessage(q)}>
<Text style={styles.sampleText}>{q}</Text>
</TouchableOpacity>
))}
</View>
)}

{isAuthenticated &&
messages.map((msg, i) => (
<View key={i} style={styles.messageGroup}>
<View style={styles.questionBubble}>
<Text style={styles.questionText}>{msg.question}</Text>
</View>
<View style={styles.responseBubble}>
<Text style={styles.responseText}>
{language === 'vi' ? msg.response.vi : msg.response.en}
</Text>
</View>
</View>
))}

{isAuthenticated && loading && (
<View style={styles.loadingRow}>
<ActivityIndicator size="small" color={colors.primary} />
<Text style={styles.loadingText}>{t('story.chatbot.thinking')}</Text>
</View>
)}
</ScrollView>

{/* Input row */}
{isAuthenticated && (
<View style={styles.inputRow}>
<TextInput
style={styles.input}
value={input}
onChangeText={setInput}
placeholder={t('story.chatbot.placeholder')}
placeholderTextColor={colors.textMuted}
multiline
maxLength={300}
/>
<TouchableOpacity
style={[styles.sendBtn, (!input.trim() || loading) && styles.sendBtnDisabled]}
onPress={() => sendMessage()}
disabled={!input.trim() || loading}
>
<Text style={styles.sendBtnText}>➤</Text>
</TouchableOpacity>
</View>
)}
</KeyboardAvoidingView>
</View>
</Modal>
);
}

function makeStyles(colors) {
return StyleSheet.create({
overlay: { flex: 1, justifyContent: 'flex-end' },
backdrop: { ...StyleSheet.absoluteFillObject, backgroundColor: 'rgba(0,0,0,0.5)' },
sheet: {
backgroundColor: colors.surface,
borderTopLeftRadius: 20,
borderTopRightRadius: 20,
maxHeight: '80%',
minHeight: '65%',
},
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
padding: 16,
borderBottomWidth: 1,
borderBottomColor: colors.border,
},
headerTitle: { fontSize: 17, fontWeight: '700', color: colors.text },
closeBtn: { fontSize: 18, color: colors.textSecondary, paddingHorizontal: 4 },
body: { flex: 1 },
bodyContent: { padding: 16, gap: 12 },
loginRequiredContainer: { alignItems: 'center', paddingHorizontal: 12 },
loginRequiredText: { color: colors.textSecondary, fontSize: 14, textAlign: 'center' },
loginRequiredLink: { color: colors.primary, fontWeight: '600' },
hint: { color: colors.textSecondary, fontSize: 14, marginBottom: 12 },
sample: {
backgroundColor: colors.card,
borderRadius: 12,
padding: 12,
marginBottom: 8,
borderWidth: 1,
borderColor: colors.border,
},
sampleText: { color: colors.primary, fontSize: 14 },
messageGroup: { gap: 8, marginBottom: 8 },
questionBubble: {
alignSelf: 'flex-end',
backgroundColor: colors.primary,
borderRadius: 14,
borderBottomRightRadius: 4,
padding: 10,
maxWidth: '80%',
},
questionText: { color: '#fff', fontSize: 14 },
responseBubble: {
alignSelf: 'flex-start',
backgroundColor: colors.card,
borderRadius: 14,
borderBottomLeftRadius: 4,
padding: 10,
maxWidth: '90%',
borderWidth: 1,
borderColor: colors.border,
},
responseText: { color: colors.text, fontSize: 14, lineHeight: 20 },
loadingRow: { flexDirection: 'row', alignItems: 'center', gap: 8 },
loadingText: { color: colors.textSecondary, fontSize: 14 },
inputRow: {
flexDirection: 'row',
alignItems: 'flex-end',
padding: 12,
borderTopWidth: 1,
borderTopColor: colors.border,
gap: 8,
marginBottom: 20,
},
input: {
flex: 1,
backgroundColor: colors.card,
borderRadius: 20,
paddingHorizontal: 14,
paddingVertical: 8,
color: colors.text,
fontSize: 14,
maxHeight: 100,
borderWidth: 1,
borderColor: colors.border,
},
sendBtn: {
backgroundColor: colors.primary,
width: 40,
height: 40,
borderRadius: 20,
justifyContent: 'center',
alignItems: 'center',
},
sendBtnDisabled: { backgroundColor: colors.border },
sendBtnText: { color: '#fff', fontSize: 16 },
});
}
Loading
Loading