fix tab switching state issues

- Add useFocusEffect to calendar for automatic event reload on tab focus
- Create ChatStore (Zustand) for persistent chat messages across tab switches
- Replace local useState with store in chat screen
This commit is contained in:
2026-01-04 17:40:40 +01:00
parent 1532acab78
commit 7c081787fe
5 changed files with 87 additions and 48 deletions

View File

@@ -5,6 +5,7 @@ import Header from "../../components/Header";
import BaseBackground from "../../components/BaseBackground";
import { FlashList } from "@shopify/flash-list";
import { ChatService } from "../../services";
import { useChatStore, MessageData } from "../../stores";
import { ProposedEventChange } from "@caldav/shared";
import { ProposedEventCard } from "../../components/ProposedEventCard";
@@ -24,17 +25,12 @@ type ChatMessageProps = {
onReject?: () => void;
};
type MessageData = ChatMessageProps & {
id: string;
conversationId?: string;
};
type ChatInputProps = {
onSend: (text: string) => void;
};
const Chat = () => {
const [messages, setMessages] = useState<MessageData[]>([]);
const { messages, addMessage, updateMessage } = useChatStore();
const handleEventResponse = async (
action: "confirm" | "reject",
@@ -43,11 +39,7 @@ const Chat = () => {
proposedChange?: ProposedEventChange,
) => {
// Mark message as responded (optimistic update)
setMessages((prev) =>
prev.map((msg) =>
msg.id === messageId ? { ...msg, respondedAction: action } : msg,
),
);
updateMessage(messageId, { respondedAction: action });
try {
const response =
@@ -68,15 +60,11 @@ const Chat = () => {
content: response.message.content,
conversationId: response.conversationId,
};
setMessages((prev) => [...prev, botMessage]);
addMessage(botMessage);
} catch (error) {
console.error(`Failed to ${action} event:`, error);
// Revert on error
setMessages((prev) =>
prev.map((msg) =>
msg.id === messageId ? { ...msg, respondedAction: undefined } : msg,
),
);
updateMessage(messageId, { respondedAction: undefined });
}
};
@@ -87,7 +75,7 @@ const Chat = () => {
side: "right",
content: text,
};
setMessages((prev) => [...prev, userMessage]);
addMessage(userMessage);
try {
// Fetch server response
@@ -101,7 +89,7 @@ const Chat = () => {
proposedChange: response.message.proposedChange,
conversationId: response.conversationId,
};
setMessages((prev) => [...prev, botMessage]);
addMessage(botMessage);
} catch (error) {
console.error("Failed to send message:", error);
}