Skip to content

Commit 24cd881

Browse files
committed
feat: refresh page on cloudflare timeout
1 parent f566de7 commit 24cd881

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

src/components/QueryBox/index.tsx

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
import { LanguageModel } from '@/types/ModelSelector';
2020
import pplxApi from '@/utils/pplx-api';
2121
import { ui } from '@/utils/ui';
22-
import { useQuery } from '@tanstack/react-query';
22+
import {
23+
UndefinedInitialDataOptions,
24+
useQuery,
25+
} from '@tanstack/react-query';
2326

2427
import useQueryBoxObserver from '../hooks/useQueryBoxObserver';
2528
import { Separator } from '../ui/separator';
@@ -39,15 +42,48 @@ export default function QueryBox() {
3942
userSettings: false,
4043
});
4144

45+
const autoRefreshSessionTimeout = usePopupSettingsStore(
46+
(state) => state.qolTweaks.autoRefreshSessionTimeout
47+
);
48+
49+
const queryOptions: Pick<
50+
UndefinedInitialDataOptions,
51+
'refetchIntervalInBackground' | 'retry'
52+
> = autoRefreshSessionTimeout
53+
? {
54+
refetchIntervalInBackground: true,
55+
retry: false,
56+
}
57+
: {};
58+
4259
const {
4360
data: userSettings,
4461
isLoading: isLoadingUserSettings,
4562
refetch: refetchUserSettings,
63+
error: userSettingsError,
4664
} = useQuery({
4765
queryKey: ['userSettings'],
4866
queryFn: pplxApi.fetchUserSettings,
67+
refetchInterval: 10000,
68+
...queryOptions,
4969
});
5070

71+
useEffect(() => {
72+
if (!autoRefreshSessionTimeout) return;
73+
74+
if (userSettingsError?.message === 'Cloudflare timeout') {
75+
toast({
76+
title: '⚠️ Cloudflare timeout!',
77+
description: 'Refreshing the page...',
78+
timeout: 3000,
79+
});
80+
81+
setTimeout(() => {
82+
window.location.reload();
83+
}, 3000);
84+
}
85+
}, [userSettingsError, autoRefreshSessionTimeout, toast]);
86+
5187
useQuery({
5288
queryKey: ['userProfileSettings'],
5389
queryFn: pplxApi.fetchUserProfileSettings,

src/content-script/session-store/popup-settings.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type PopupSettingsState = {
1515
quickQueryCommander: boolean;
1616
threadMessageStickyToolbar: boolean;
1717
codeBlockEnhancedToolbar: boolean;
18+
autoRefreshSessionTimeout: boolean;
1819
};
1920
visualTweaks: {
2021
collapseEmptyThreadVisualColumns: boolean;
@@ -35,6 +36,7 @@ const usePopupSettingsStore = create<PopupSettingsState>()(
3536
quickQueryCommander: false,
3637
threadMessageStickyToolbar: false,
3738
codeBlockEnhancedToolbar: false,
39+
autoRefreshSessionTimeout: false,
3840
},
3941
visualTweaks: {
4042
collapseEmptyThreadVisualColumns: false,
@@ -62,6 +64,7 @@ const popupSettingsStore = usePopupSettingsStore;
6264
quickQueryCommander: false,
6365
threadMessageStickyToolbar: false,
6466
codeBlockEnhancedToolbar: false,
67+
autoRefreshSessionTimeout: false,
6568
},
6669
visualTweaks: {
6770
collapseEmptyThreadVisualColumns: false,

src/popup/settings.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ const qolTweaks: PopupSetting<
4646
label: 'Inline query params invocation',
4747
storeKey: 'quickQueryCommander',
4848
},
49+
{
50+
id: 'code-block-enhanced-toolbar',
51+
label: 'Code block enhanced toolbar',
52+
storeKey: 'codeBlockEnhancedToolbar',
53+
},
4954
{
5055
id: 'thread-message-sticky-toolbar',
5156
label: 'Thread message sticky toolbar',
@@ -54,9 +59,11 @@ const qolTweaks: PopupSetting<
5459
versionRelease: '0.0.0.12',
5560
},
5661
{
57-
id: 'code-block-enhanced-toolbar',
58-
label: 'Code block enhanced toolbar',
59-
storeKey: 'codeBlockEnhancedToolbar',
62+
id: 'auto-refresh-session-timeout',
63+
label: 'Auto-refresh Cloudflare session timeout',
64+
storeKey: 'autoRefreshSessionTimeout',
65+
experimental: true,
66+
versionRelease: '0.0.0.12',
6067
},
6168
];
6269

src/types/ChromeStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type ChromeStore = {
2222
quickQueryCommander: boolean;
2323
threadMessageStickyToolbar: boolean;
2424
codeBlockEnhancedToolbar: boolean;
25+
autoRefreshSessionTimeout: boolean;
2526
};
2627
visualTweaks: {
2728
collapseEmptyThreadVisualColumns: boolean;

src/utils/pplx-api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ async function fetchUserSettings(): Promise<UserSettingsApiResponse> {
2323
'https://www.perplexity.ai/p/api/v1/user/settings'
2424
);
2525

26+
if (
27+
resp.startsWith(
28+
'<!DOCTYPE html><html lang="en-US"><head><title>Just a moment...'
29+
)
30+
)
31+
throw new Error('Cloudflare timeout');
32+
2633
return jsonUtils.safeParse(resp);
2734
}
2835

0 commit comments

Comments
 (0)