Skip to content

Commit d22456e

Browse files
committed
fix: Address CodeRabbit review - guard location and clipboard API
- Move typeof location check before Dexie.debug to prevent ReferenceError in non-browser environments (Node/SSR) - Guard navigator.clipboard access with existence check - Check execCommand('copy') return value - only show 'Copied!' on success - Extract fallback copy logic into separate function
1 parent 3abf657 commit d22456e

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

addons/dexie-cloud/src/authentication/authenticate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ async function userAuthenticate(
251251
const isOffline = typeof navigator !== 'undefined' && !navigator.onLine;
252252
if (isOffline) {
253253
message = `You seem to be offline. Please connect to the internet and try again.`;
254-
} else if (Dexie.debug || (typeof location !== 'undefined' && (location.hostname === 'localhost' || location.hostname === '127.0.0.1'))) {
254+
} else if (typeof location !== 'undefined' && (Dexie.debug || location.hostname === 'localhost' || location.hostname === '127.0.0.1')) {
255255
// The audience is most likely the developer. Suggest to whitelist the localhost origin:
256256
const whitelistCommand = `npx dexie-cloud whitelist ${location.origin}`;
257257
message = `Could not connect to server. Please verify that your origin '${location.origin}' is whitelisted using \`npx dexie-cloud whitelist\``;

addons/dexie-cloud/src/default-ui/LoginDialog.tsx

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,13 @@ function CopyButton({ text }: { text: string }) {
206206
};
207207

208208
const handleClick = () => {
209-
navigator.clipboard.writeText(text).then(scheduleCopiedReset).catch(() => {
210-
// Fallback for older browsers
211-
const textarea = document.createElement('textarea');
212-
textarea.value = text;
213-
textarea.style.position = 'fixed';
214-
textarea.style.opacity = '0';
215-
document.body.appendChild(textarea);
216-
textarea.select();
217-
document.execCommand('copy');
218-
document.body.removeChild(textarea);
219-
scheduleCopiedReset();
220-
});
209+
if (typeof navigator !== 'undefined' && navigator.clipboard?.writeText) {
210+
navigator.clipboard.writeText(text).then(scheduleCopiedReset).catch(() => {
211+
fallbackCopy(text, scheduleCopiedReset);
212+
});
213+
} else {
214+
fallbackCopy(text, scheduleCopiedReset);
215+
}
221216
};
222217

223218
return (
@@ -231,3 +226,15 @@ function CopyButton({ text }: { text: string }) {
231226
</button>
232227
);
233228
}
229+
230+
function fallbackCopy(text: string, onSuccess: () => void) {
231+
const textarea = document.createElement('textarea');
232+
textarea.value = text;
233+
textarea.style.position = 'fixed';
234+
textarea.style.opacity = '0';
235+
document.body.appendChild(textarea);
236+
textarea.select();
237+
const success = document.execCommand('copy');
238+
document.body.removeChild(textarea);
239+
if (success) onSuccess();
240+
}

0 commit comments

Comments
 (0)