Commit 2de10d37 by Xianquan Committed by GitHub

fix: clear chat cache when switching teams (#6956)

parent 0591d183
...@@ -8,6 +8,7 @@ import { useRequest } from '@fastgpt/web/hooks/useRequest'; ...@@ -8,6 +8,7 @@ import { useRequest } from '@fastgpt/web/hooks/useRequest';
import MySelect from '@fastgpt/web/components/common/MySelect'; import MySelect from '@fastgpt/web/components/common/MySelect';
import { useSystemStore } from '@/web/common/system/useSystemStore'; import { useSystemStore } from '@/web/common/system/useSystemStore';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { useChatStore } from '@/web/core/chat/context/useChatStore';
const TeamSelector = ({ const TeamSelector = ({
showManage, showManage,
...@@ -23,6 +24,7 @@ const TeamSelector = ({ ...@@ -23,6 +24,7 @@ const TeamSelector = ({
const router = useRouter(); const router = useRouter();
const { userInfo } = useUserStore(); const { userInfo } = useUserStore();
const { setLoading } = useSystemStore(); const { setLoading } = useSystemStore();
const { resetChatCache } = useChatStore();
const { data: myTeams = [] } = useRequest(() => getTeamList(TeamMemberStatusEnum.active), { const { data: myTeams = [] } = useRequest(() => getTeamList(TeamMemberStatusEnum.active), {
manual: false, manual: false,
...@@ -33,6 +35,7 @@ const TeamSelector = ({ ...@@ -33,6 +35,7 @@ const TeamSelector = ({
async (teamId: string) => { async (teamId: string) => {
setLoading(true); setLoading(true);
await putSwitchTeam(teamId); await putSwitchTeam(teamId);
resetChatCache();
}, },
{ {
onFinally: () => { onFinally: () => {
......
...@@ -14,6 +14,7 @@ import type { TeamTmbItemType, TeamMemberItemType } from '@fastgpt/global/suppor ...@@ -14,6 +14,7 @@ import type { TeamTmbItemType, TeamMemberItemType } from '@fastgpt/global/suppor
import { useRequest } from '@fastgpt/web/hooks/useRequest'; import { useRequest } from '@fastgpt/web/hooks/useRequest';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { useChatStore } from '@/web/core/chat/context/useChatStore';
const EditInfoModal = dynamic(() => import('./EditInfoModal')); const EditInfoModal = dynamic(() => import('./EditInfoModal'));
...@@ -52,6 +53,7 @@ export const TeamModalContextProvider = ({ children }: { children: ReactNode }) ...@@ -52,6 +53,7 @@ export const TeamModalContextProvider = ({ children }: { children: ReactNode })
const [editTeamData, setEditTeamData] = useState<EditTeamFormDataType>(); const [editTeamData, setEditTeamData] = useState<EditTeamFormDataType>();
const { userInfo, initUserInfo } = useUserStore(); const { userInfo, initUserInfo } = useUserStore();
const { resetChatCache } = useChatStore();
const { const {
data: myTeams = [], data: myTeams = [],
...@@ -70,6 +72,7 @@ export const TeamModalContextProvider = ({ children }: { children: ReactNode }) ...@@ -70,6 +72,7 @@ export const TeamModalContextProvider = ({ children }: { children: ReactNode })
const { runAsync: onSwitchTeam, loading: isSwitchingTeam } = useRequest( const { runAsync: onSwitchTeam, loading: isSwitchingTeam } = useRequest(
async (teamId: string) => { async (teamId: string) => {
await putSwitchTeam(teamId); await putSwitchTeam(teamId);
resetChatCache();
return initUserInfo(); return initUserInfo();
}, },
{ {
......
...@@ -31,18 +31,10 @@ const ipDetectURL = 'https://qifu-api.baidubce.com/ip/local/geo/v1/district'; ...@@ -31,18 +31,10 @@ const ipDetectURL = 'https://qifu-api.baidubce.com/ip/local/geo/v1/district';
// Cookies Modal Component // Cookies Modal Component
const CookiesModal = () => { const CookiesModal = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const [isOpen, setIsOpen] = useState(false);
const cookieVersion = '1'; const cookieVersion = '1';
const [localCookieVersion, setLocalCookieVersion] = const [localCookieVersion, setLocalCookieVersion] =
useLocalStorageState<string>('localCookieVersion'); useLocalStorageState<string>('localCookieVersion');
const [isOpen, setIsOpen] = useState(() => localCookieVersion !== cookieVersion);
useEffect(() => {
// Check if user has agreed to current cookie version
if (localCookieVersion !== cookieVersion) {
setIsOpen(true);
}
}, [localCookieVersion, cookieVersion]);
const handleAgree = () => { const handleAgree = () => {
setLocalCookieVersion(cookieVersion); setLocalCookieVersion(cookieVersion);
...@@ -102,7 +94,11 @@ const ChineseRedirectModal = () => { ...@@ -102,7 +94,11 @@ const ChineseRedirectModal = () => {
}); });
// IP detection without cache // IP detection without cache
const checkIpInChina = useCallback(async () => { useEffect(() => {
// Only check IP if redirect URL is provided and user hasn't disabled it
if (!chineseRedirectUrl || !showRedirect) return;
const checkIpInChina = async () => {
try { try {
const res = await GET<any>(ipDetectURL); const res = await GET<any>(ipDetectURL);
const country = res?.country; const country = res?.country;
...@@ -118,14 +114,10 @@ const ChineseRedirectModal = () => { ...@@ -118,14 +114,10 @@ const ChineseRedirectModal = () => {
} catch (error) { } catch (error) {
console.log('IP detection failed:', error); console.log('IP detection failed:', error);
} }
}, []); };
useEffect(() => {
// Only check IP if redirect URL is provided and user hasn't disabled it
if (chineseRedirectUrl && showRedirect) {
checkIpInChina(); checkIpInChina();
} }, [chineseRedirectUrl, showRedirect]);
}, [chineseRedirectUrl, showRedirect, checkIpInChina]);
const handleRedirect = () => { const handleRedirect = () => {
if (chineseRedirectUrl) { if (chineseRedirectUrl) {
...@@ -188,7 +180,7 @@ export const LoginContainer = ({ ...@@ -188,7 +180,7 @@ export const LoginContainer = ({
}) => { }) => {
const { t } = useTranslation(); const { t } = useTranslation();
const { feConfigs } = useSystemStore(); const { feConfigs } = useSystemStore();
const { setLastChatAppId } = useChatStore(); const { resetChatCache } = useChatStore();
const [pageType, setPageType] = useState<`${LoginPageTypeEnum}`>(LoginPageTypeEnum.passwordLogin); const [pageType, setPageType] = useState<`${LoginPageTypeEnum}`>(LoginPageTypeEnum.passwordLogin);
const [showCommunityModal, setShowCommunityModal] = useState(false); const [showCommunityModal, setShowCommunityModal] = useState(false);
...@@ -204,8 +196,8 @@ export const LoginContainer = ({ ...@@ -204,8 +196,8 @@ export const LoginContainer = ({
// initialization logic // initialization logic
useEffect(() => { useEffect(() => {
// reset chat state // reset chat state
setLastChatAppId(''); resetChatCache();
}, [feConfigs?.oauth?.wechat, setLastChatAppId]); }, [feConfigs?.oauth?.wechat, resetChatCache]);
// dynamic component based on page type // dynamic component based on page type
const DynamicComponent = useMemo(() => { const DynamicComponent = useMemo(() => {
......
...@@ -22,6 +22,8 @@ type State = { ...@@ -22,6 +22,8 @@ type State = {
outLinkAuthData: OutLinkChatAuthProps; outLinkAuthData: OutLinkChatAuthProps;
setOutLinkAuthData: (e: OutLinkChatAuthProps) => any; setOutLinkAuthData: (e: OutLinkChatAuthProps) => any;
resetChatCache: () => any;
}; };
const createCustomStorage = () => { const createCustomStorage = () => {
...@@ -85,10 +87,6 @@ export const useChatStore = create<State>()( ...@@ -85,10 +87,6 @@ export const useChatStore = create<State>()(
state.chatId = getNanoid(24); state.chatId = getNanoid(24);
} }
if (!state.appId && state.lastChatAppId) {
state.appId = state.lastChatAppId;
}
state.source = e; state.source = e;
}); });
}, },
...@@ -127,6 +125,17 @@ export const useChatStore = create<State>()( ...@@ -127,6 +125,17 @@ export const useChatStore = create<State>()(
set((state) => { set((state) => {
state.outLinkAuthData = e; state.outLinkAuthData = e;
}); });
},
resetChatCache() {
set((state) => {
state.source = undefined;
state.appId = '';
state.lastChatAppId = '';
state.chatId = '';
state.lastChatId = '';
state.lastPane = ChatSidebarPaneEnum.HOME;
state.outLinkAuthData = {};
});
} }
})), })),
{ {
......
...@@ -155,7 +155,7 @@ describe('useChatStore', () => { ...@@ -155,7 +155,7 @@ describe('useChatStore', () => {
expect(useChatStore.getState().lastChatId).toBe(`${ChatSourceEnum.api}-test-generated-id`); expect(useChatStore.getState().lastChatId).toBe(`${ChatSourceEnum.api}-test-generated-id`);
}); });
it('should set appId from lastChatAppId if not set', () => { it('should not set appId from lastChatAppId when source changes', () => {
useChatStore.setState({ useChatStore.setState({
appId: '', appId: '',
lastChatAppId: 'last-app-id', lastChatAppId: 'last-app-id',
...@@ -167,7 +167,36 @@ describe('useChatStore', () => { ...@@ -167,7 +167,36 @@ describe('useChatStore', () => {
}); });
const store = useChatStore.getState(); const store = useChatStore.getState();
store.setSource(ChatSourceEnum.api); store.setSource(ChatSourceEnum.api);
expect(useChatStore.getState().appId).toBe('last-app-id'); expect(useChatStore.getState().appId).toBe('');
expect(useChatStore.getState().lastChatAppId).toBe('last-app-id');
});
it('should reset chat cache', () => {
const store = useChatStore.getState();
useChatStore.setState({
source: ChatSourceEnum.share,
appId: 'app-id',
lastChatAppId: 'last-app-id',
chatId: 'chat-id',
lastChatId: `${ChatSourceEnum.share}-chat-id`,
lastPane: ChatSidebarPaneEnum.RECENTLY_USED_APPS,
outLinkAuthData: {
shareId: 'share-id',
outLinkUid: 'outlink-uid'
}
});
store.resetChatCache();
expect(useChatStore.getState()).toMatchObject({
source: undefined,
appId: '',
lastChatAppId: '',
chatId: '',
lastChatId: '',
lastPane: ChatSidebarPaneEnum.HOME,
outLinkAuthData: {}
});
}); });
it('should set lastPane to undefined by default', () => { it('should set lastPane to undefined by default', () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment