Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FE] 채팅 시 SSE에 의해 수신되는 내용이 늦게 반영되는 문제 해결 #960

Merged
Merged
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
22 changes: 16 additions & 6 deletions frontend/src/hooks/queries/useSSE.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useCallback, useEffect } from 'react';
import { type InfiniteData, useQueryClient } from '@tanstack/react-query';
import { useQueryClient } from '@tanstack/react-query';
import type { InfiniteData } from '@tanstack/react-query';
import { baseUrl } from '~/apis/http';
import { EventSourcePolyfill } from 'event-source-polyfill';
import { useToken } from '~/hooks/useToken';
import { useTeamPlace } from '~/hooks/useTeamPlace';
import { type ThreadsResponse } from '~/apis/feed';
import type { ThreadsResponse } from '~/apis/feed';

export const useSSE = () => {
const queryClient = useQueryClient();
Expand Down Expand Up @@ -34,11 +35,20 @@ export const useSSE = () => {

queryClient.setQueryData<InfiniteData<ThreadsResponse>>(
['threadData', teamPlaceId],
(old) => {
if (old) {
old.pages[0].threads = [newThread, ...old.pages[0].threads];
(oldData) => {
if (oldData) {
const newFirstPageThreads = {
threads: [newThread, ...oldData.pages[0].threads],
};
const newData = {
pageParams: oldData.pageParams,
pages:
oldData.pages.length === 1
? [newFirstPageThreads]
: [newFirstPageThreads, ...oldData.pages.slice(1)],
};

return old;
return newData;
}
},
);
Expand Down