Skip to content

Commit ec57d2a

Browse files
Copilotf
andcommitted
Add defensive null checks to shared utilities
Co-authored-by: f <[email protected]>
1 parent 1af5552 commit ec57d2a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

shared-utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44
function toggleDarkMode() {
55
const body = document.body;
66
const toggle = document.querySelector(".dark-mode-toggle");
7+
8+
// Handle case where toggle doesn't exist
9+
if (!toggle) {
10+
body.classList.toggle("dark-mode");
11+
const isDarkMode = body.classList.contains("dark-mode");
12+
localStorage.setItem("dark-mode", isDarkMode);
13+
return;
14+
}
15+
716
const sunIcon = toggle.querySelector(".sun-icon");
817
const moonIcon = toggle.querySelector(".moon-icon");
918

@@ -48,13 +57,15 @@ function parseCSV(csv) {
4857

4958
// Normalize text by removing extra whitespace and newlines
5059
function normalizeText(text) {
60+
if (!text) return "";
5161
return text
5262
.replace(/\s+/g, " ") // Normalize whitespace
5363
.replace(/[\n\r]/g, "") // Remove newlines
5464
.trim();
5565
}
5666

5767
// Scroll to a prompt card with highlight animation
68+
// Note: offset is set to -50 for consistent spacing across all pages
5869
function scrollToPromptCard(targetCard, isMobile, headerHeight) {
5970
if (!targetCard) return;
6071

@@ -80,6 +91,17 @@ function scrollToPromptCard(targetCard, isMobile, headerHeight) {
8091
} else {
8192
// On desktop, scroll the main-content container
8293
const mainContent = document.querySelector(".main-content");
94+
if (!mainContent) {
95+
// Fallback to window scroll if main-content doesn't exist
96+
const cardRect = targetCard.getBoundingClientRect();
97+
const scrollTop = window.pageYOffset + cardRect.top - headerHeight - 50;
98+
window.scrollTo({
99+
top: scrollTop,
100+
behavior: "smooth",
101+
});
102+
return;
103+
}
104+
83105
const cardRect = targetCard.getBoundingClientRect();
84106
const scrollTop = mainContent.scrollTop + cardRect.top - headerHeight - 50;
85107

0 commit comments

Comments
 (0)