Skip to content

Commit

Permalink
feat: show error when page is not responding
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Apr 12, 2024
1 parent 00d6418 commit 4a563a3
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
Binary file modified .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions src/content/contentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ messages.startRuntimeListening();
messages.listenRuntime('DETECT_TOLGEE', async () => {
if (configuration) {
messages.sendToPlugin('TOLGEE_CONFIG_LOADED', configuration);
} else {
messages.sendToPlugin('TOLGEE_CONFIG_NOT_LOADED');
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/popup/TolgeeDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export const TolgeeDetector = () => {
if (error) {
return (
<Box width={POPUP_WIDTH} p={1} color="red">
<Typography variant="body2">Error: {error}</Typography>
<Typography variant="body2" fontWeight="bold">
Error: {error}
</Typography>
</Box>
);
} else if (tolgeePresent === 'loading') {
Expand Down
13 changes: 11 additions & 2 deletions src/popup/sendMessage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
export const sendMessage = (type: string, data?: any) => {
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id as number, { type, data }, resolve);
chrome.tabs.sendMessage(
tabs[0].id as number,
{ type, data },
(response) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
}
resolve(response);
}
);
});
});
};
35 changes: 18 additions & 17 deletions src/popup/useDetectorForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Action =
type: 'CHANGE_LIB_CONFIG';
payload: { libData: LibConfig | null; frameId: number | null };
}
| { type: 'SET_ERROR'; payload: string }
| { type: 'SET_APPLIED_VALUES'; payload: Values | null }
| { type: 'SET_CREDENTIALS_CHECK'; payload: CredentialsCheck }
| { type: 'LOAD_STORED_VALUES'; payload: Values | null }
Expand Down Expand Up @@ -73,6 +74,12 @@ export const useDetectorForm = () => {
: 'present',
};
}
case 'SET_ERROR':
return {
...state,
tolgeePresent: 'not_present',
error: action.payload,
};
case 'SET_APPLIED_VALUES':
return {
...state,
Expand Down Expand Up @@ -152,25 +159,14 @@ export const useDetectorForm = () => {
}, [appliedValues]);

useEffect(() => {
sendMessage('DETECT_TOLGEE');
sendMessage('DETECT_TOLGEE').catch(() => {
dispatch({
type: 'SET_ERROR',
payload: 'No access to this page, try to refresh',
});
});
}, []);

// timeout when Tolgee is not detected
useEffect(() => {
if (!state.libConfig) {
const timer = setTimeout(
() =>
dispatch({
type: 'CHANGE_LIB_CONFIG',
payload: { frameId: null, libData: null },
}),
300
);
return () => clearTimeout(timer);
}
return undefined;
}, [state.libConfig]);

// after tolgee config is loaded
// get applied values and stored values
const onLibConfigChange = async () => {
Expand Down Expand Up @@ -203,6 +199,11 @@ export const useDetectorForm = () => {
type: 'CHANGE_LIB_CONFIG',
payload: { libData: data, frameId: frameId || null },
});
} else if (type === 'TOLGEE_CONFIG_NOT_LOADED') {
dispatch({
type: 'CHANGE_LIB_CONFIG',
payload: { libData: null, frameId: null },
});
}
};
chrome.runtime.onMessage.addListener(listener);
Expand Down

0 comments on commit 4a563a3

Please sign in to comment.