fix(web): persistent WS connection, auto-refetch on ready, and direct selector binding with scroll fix
This commit is contained in:
@@ -288,9 +288,7 @@ export function ChatArea() {
|
|||||||
const activeChannelId = rawActiveChannelId ? rawActiveChannelId.toLowerCase() : null;
|
const activeChannelId = rawActiveChannelId ? rawActiveChannelId.toLowerCase() : null;
|
||||||
const channelsByServer = useChannelStore((s) => s.channelsByServer);
|
const channelsByServer = useChannelStore((s) => s.channelsByServer);
|
||||||
const activeServerId = useServerStore((s) => s.activeServerId);
|
const activeServerId = useServerStore((s) => s.activeServerId);
|
||||||
const messages = useMessageStore(
|
const messages = useMessageStore((s) => (activeChannelId ? s.messagesByChannel[activeChannelId] || [] : []));
|
||||||
useCallback((s) => (activeChannelId ? s.messagesByChannel[activeChannelId] || [] : []), [activeChannelId])
|
|
||||||
);
|
|
||||||
const isLoading = useMessageStore((s) => s.isLoading);
|
const isLoading = useMessageStore((s) => s.isLoading);
|
||||||
const isLoadingOlder = useMessageStore((s) => s.isLoadingOlder);
|
const isLoadingOlder = useMessageStore((s) => s.isLoadingOlder);
|
||||||
const hasMore = useMessageStore((s) => activeChannelId ? s.hasMoreByChannel[activeChannelId] !== false : true);
|
const hasMore = useMessageStore((s) => activeChannelId ? s.hasMoreByChannel[activeChannelId] !== false : true);
|
||||||
@@ -461,6 +459,9 @@ export function ChatArea() {
|
|||||||
}, [activeChannelId, isLoadingOlder, hasMore, fetchOlderMessages]);
|
}, [activeChannelId, isLoadingOlder, hasMore, fetchOlderMessages]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (scrollContainerRef.current) {
|
||||||
|
scrollContainerRef.current.scrollTop = scrollContainerRef.current.scrollHeight;
|
||||||
|
}
|
||||||
bottomRef.current?.scrollIntoView({ behavior: "auto" });
|
bottomRef.current?.scrollIntoView({ behavior: "auto" });
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
|
|||||||
@@ -176,9 +176,14 @@ export function DMChat() {
|
|||||||
const id = rawId ? rawId.toLowerCase() : undefined;
|
const id = rawId ? rawId.toLowerCase() : undefined;
|
||||||
const conversation = conversations.find((c) => c.id.toLowerCase() === id);
|
const conversation = conversations.find((c) => c.id.toLowerCase() === id);
|
||||||
const hasMore = useConversationStore((s) => id ? s.hasMoreByConversation[id] !== false : true);
|
const hasMore = useConversationStore((s) => id ? s.hasMoreByConversation[id] !== false : true);
|
||||||
const messages = useConversationStore(
|
const messages = useConversationStore((s) => (id ? s.messagesByConversation[id] || [] : []));
|
||||||
useCallback((s) => (id ? s.messagesByConversation[id] || [] : []), [id])
|
|
||||||
);
|
useEffect(() => {
|
||||||
|
if (scrollContainerRef.current) {
|
||||||
|
scrollContainerRef.current.scrollTop = scrollContainerRef.current.scrollHeight;
|
||||||
|
}
|
||||||
|
bottomRef.current?.scrollIntoView({ behavior: "auto" });
|
||||||
|
}, [messages]);
|
||||||
|
|
||||||
const handleAddReaction = useCallback(async (messageId: string, emoji: string) => {
|
const handleAddReaction = useCallback(async (messageId: string, emoji: string) => {
|
||||||
if (!id) return;
|
if (!id) return;
|
||||||
@@ -216,9 +221,7 @@ export function DMChat() {
|
|||||||
}
|
}
|
||||||
}, [id, setActive, fetchMessages]);
|
}, [id, setActive, fetchMessages]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
bottomRef.current?.scrollIntoView({ behavior: "auto" });
|
|
||||||
}, [messages]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!id || messages.length === 0 || isLoading) return;
|
if (!id || messages.length === 0 || isLoading) return;
|
||||||
|
|||||||
@@ -35,8 +35,6 @@ export function Layout() {
|
|||||||
const user = useAuthStore((state) => state.user);
|
const user = useAuthStore((state) => state.user);
|
||||||
const logout = useAuthStore((state) => state.logout);
|
const logout = useAuthStore((state) => state.logout);
|
||||||
const updateProfile = useAuthStore((state) => state.updateProfile);
|
const updateProfile = useAuthStore((state) => state.updateProfile);
|
||||||
const wsConnect = useWebSocketStore((s) => s.connect);
|
|
||||||
const wsDisconnect = useWebSocketStore((s) => s.disconnect);
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const [showStatusMenu, setShowStatusMenu] = useState(false);
|
const [showStatusMenu, setShowStatusMenu] = useState(false);
|
||||||
@@ -63,9 +61,8 @@ export function Layout() {
|
|||||||
}, [currentVoiceRoom]);
|
}, [currentVoiceRoom]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
wsConnect();
|
useWebSocketStore.getState().connect();
|
||||||
return () => { wsDisconnect(); };
|
}, []);
|
||||||
}, [wsConnect, wsDisconnect]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isLoading && !isAuthenticated && location.pathname !== '/login') {
|
if (!isLoading && !isAuthenticated && location.pathname !== '/login') {
|
||||||
|
|||||||
@@ -110,6 +110,10 @@ export const useWebSocketStore = create<WebSocketState>((set, get) => ({
|
|||||||
if (data.type === 'ready') {
|
if (data.type === 'ready') {
|
||||||
set({ connected: true });
|
set({ connected: true });
|
||||||
reconnectDelay = 1000;
|
reconnectDelay = 1000;
|
||||||
|
const activeChan = useChannelStore.getState().activeChannelId;
|
||||||
|
if (activeChan) useMessageStore.getState().fetchMessages(activeChan);
|
||||||
|
const activeConv = useConversationStore.getState().activeConversationId;
|
||||||
|
if (activeConv) useConversationStore.getState().fetchMessages(activeConv);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user