-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdashboard.html
More file actions
930 lines (786 loc) · 33.2 KB
/
dashboard.html
File metadata and controls
930 lines (786 loc) · 33.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Notion Dashboard Widget</title>
<script src="https://unpkg.com/@phosphor-icons/web"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600&display=swap');
:root {
/* Permanent Dark Mode */
--text-primary: #E3E3E3;
--text-secondary: #A5A5A5;
--accent-color: #63b3ed;
--divider-color: rgba(255, 255, 255, 0.15);
}
body {
font-family: 'Outfit', sans-serif;
height: 100vh;
display: flex;
/* Use margin:auto on child to center, allowing scroll if overflow */
overflow: auto;
margin: 0;
background-color: #191919;
/* Notion Dark */
color: var(--text-primary);
/* Notion Dark */
color: var(--text-primary);
/* overflow: hidden; <-- Removed to allow scroll on small screens */
transition: background-color 0.8s ease, color 0.8s ease;
}
.widget-container {
width: 95%;
max-width: 1000px;
min-width: 240px;
min-height: 120px;
height: auto;
/* Transparent / Minimalist */
background: transparent;
color: var(--text-primary);
position: relative;
z-index: 1;
/* No border or shadow for purely native look, or very subtle */
border: none;
box-shadow: none;
/* FLEXBOX for better fluidity */
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 32px;
transition: all 0.5s ease;
margin: auto;
/* Safe Centering */
}
/* Section Styling */
.section {
display: flex;
flex-direction: column;
justify-content: center;
/* Vertical centering */
align-items: center;
/* Horizontal centering default */
height: 100%;
position: relative;
flex: 1;
min-width: 0;
padding: 0 10px;
}
/* Dividers */
.section:not(:last-child)::after {
content: '';
position: absolute;
right: 0;
top: 25%;
height: 50%;
width: 1px;
background: var(--divider-color);
transition: background 0.8s ease;
}
/* 1. Greeting & Time */
/* 1. Greeting & Time */
.greeting-box {
align-items: center;
/* Center horizontally now that text is gone on small screens, or left for large */
text-align: center;
}
@media(min-width: 850px) {
.greeting-box {
align-items: flex-start;
text-align: left;
}
}
.greeting-box h1 {
font-size: 2rem;
font-weight: 600;
line-height: 1;
margin-top: 0;
margin-bottom: 4px;
white-space: nowrap;
}
.greeting-box p {
font-size: 1.1rem;
opacity: 0.9;
color: var(--text-secondary);
white-space: nowrap;
display: none;
/* Hide greeting text */
}
/* 2. Weather */
/* 2. Weather */
.weather-box {
align-items: center;
text-align: center;
}
.weather-main {
display: flex;
align-items: center;
gap: 8px;
justify-content: center;
}
.weather-icon i {
font-size: 2.2rem;
}
.temp {
font-size: 2rem;
font-weight: 600;
}
.weather-tip {
font-size: 0.85rem;
margin-top: 4px;
opacity: 0.9;
/* Subtle pill background adapted to theme */
background: var(--divider-color);
padding: 4px 10px;
border-radius: 10px;
white-space: normal;
line-height: 1.2;
display: -webkit-box;
-webkit-line-clamp: 2;
line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
max-width: 100%;
}
/* Hide weather tip when empty */
.weather-tip:empty {
display: none;
margin: 0;
padding: 0;
}
/* 3. Prayer Times */
.prayer-box {
align-items: center;
transition: transform 0.2s;
text-align: center;
}
.prayer-box:hover {
transform: scale(1.02);
}
.prayer-header {
font-size: 0.8rem;
text-transform: uppercase;
letter-spacing: 1px;
opacity: 0.8;
margin-bottom: 4px;
color: var(--text-secondary);
}
.prayer-time {
font-size: 2rem;
font-weight: 600;
color: var(--accent-color);
line-height: 1.1;
}
.prayer-name {
font-size: 1rem;
font-weight: 500;
}
/* 4. Quote */
.quote-box {
text-align: center;
font-style: italic;
padding-left: 20px;
display: flex;
}
.quote-text {
font-size: 0.95rem;
line-height: 1.3;
margin-bottom: 6px;
display: -webkit-box;
-webkit-line-clamp: 3;
line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
.quote-source {
font-size: 0.8rem;
opacity: 0.8;
font-weight: 500;
color: var(--text-secondary);
}
.city-display {
font-size: 0.8rem;
opacity: 0.7;
cursor: pointer;
display: inline-flex;
align-items: center;
gap: 4px;
margin-top: 4px;
transition: opacity 0.2s;
}
.city-display:hover {
opacity: 1;
text-decoration: underline;
}
#city-input {
font-family: inherit;
font-size: 0.8rem;
background: rgba(255, 255, 255, 0.2);
border: 1px solid var(--divider-color);
border-radius: 4px;
color: var(--text-primary);
padding: 2px 6px;
width: 120px;
display: none;
/* Hidden by default */
}
body.dark-mode #city-input {
background: rgba(0, 0, 0, 0.2);
}
/* Setup / Blank Slate */
#setup-container {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
z-index: 100;
background: rgba(0, 0, 0, 0.4);
/* Dim background */
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
opacity: 0;
pointer-events: none;
transition: opacity 0.3s ease;
}
#setup-container.active {
opacity: 1;
pointer-events: all;
}
#city-input-large {
background: rgba(25, 25, 25, 0.9);
border: 1px solid var(--accent-color);
color: var(--text-primary);
font-size: 1.5rem;
padding: 16px 32px;
border-radius: 100px;
width: 300px;
max-width: 80vw;
/* Responsive for small screens */
text-align: center;
outline: none;
font-family: inherit;
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
transform: scale(0.9);
transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
#setup-container.active #city-input-large {
transform: scale(1);
}
#city-input-large::placeholder {
color: var(--text-secondary);
opacity: 0.5;
}
/* Dashboard Wrapper */
#dashboard-content {
width: 100%;
height: 100%;
/* Fill container */
display: flex;
justify-content: space-between;
align-items: center;
transition: opacity 0.5s ease, filter 0.3s ease;
cursor: pointer;
/* Click to edit */
/* Hidden by default (Blank Slate) */
opacity: 0;
pointer-events: none;
}
/* Only show when active class is added */
body.dashboard-active #dashboard-content {
opacity: 1;
pointer-events: all;
}
/* When setup is open (re-editing), we can either hide or blur.
User asked for "Blank" initially. For re-edit, let's keep it clean:
Just show input. So removing .dashboard-active will hide it again.
*/
</style>
<style>
/* --- JS-Driven Responsive Classes --- */
/* Level 1: Hide Quote (< 850px) */
body.resp-hide-quote .quote-box {
display: none !important;
}
body.resp-hide-quote .section:nth-child(3)::after {
display: none !important;
}
body.resp-hide-quote .widget-container {
padding: 0 20px;
}
/* Level 2: Hide Greeting Text (< 550px) */
body.resp-hide-greeting .greeting-box p {
display: none !important;
}
/* Compact typography when greeting hidden */
body.resp-hide-greeting .greeting-box h1 {
font-size: 2rem;
margin-bottom: 0;
}
body.resp-hide-greeting .weather-icon i {
font-size: 1.8rem;
}
body.resp-hide-greeting .temp {
font-size: 1.6rem;
}
body.resp-hide-greeting .prayer-time {
font-size: 2rem;
}
body.resp-hide-greeting .prayer-name {
font-size: 0.9rem;
}
body.resp-hide-greeting .widget-container {
padding: 0 16px;
}
/* Level 3: Hide Weather Tip (< 450px) */
body.resp-hide-tip .weather-tip {
display: none !important;
}
body.resp-hide-tip .section {
padding: 0 4px;
}
body.resp-hide-tip .section::after {
display: none !important;
}
/* Level 4: Hide Prayer Header (< 340px) */
body.resp-hide-prayer-label .prayer-header {
display: none !important;
}
/* Fix alignment when prayer label is hidden */
body.resp-hide-prayer-label .greeting-box h1,
body.resp-hide-prayer-label .prayer-time {
margin: 0 !important;
padding: 0 !important;
line-height: 1;
}
body.resp-hide-prayer-label .section {
align-items: center !important;
justify-content: center !important;
}
/* Level 5: Hide Weather Section (< 300px) - The "Minimal" Mode */
body.resp-hide-weather .weather-box {
display: none !important;
}
body.resp-hide-weather .section {
min-width: auto;
}
/* Fix Vertical Alignment: Make Prayer side-by-side so it balances with Time */
body.resp-hide-weather .prayer-box {
flex-direction: row !important;
gap: 8px;
align-items: baseline;
}
/* Typography Alignment: Equalize sizes for perfect balance */
body.resp-hide-weather .greeting-box h1,
body.resp-hide-weather .prayer-time {
font-size: 2rem !important;
/* Unified Large Size */
line-height: 1;
margin: 0 !important;
/* Critical: Remove default h1 margins */
padding: 0 !important;
}
/* Ensure parent boxes don't add unequal height */
body.resp-hide-weather .section {
align-items: center !important;
justify-content: center !important;
}
body.resp-hide-weather .prayer-name {
font-size: 0.9rem !important;
/* Smaller Name */
opacity: 0.8;
}
</style>
</head>
<body>
<div class="widget-container">
<!-- Setup Overlay (Blank Slate) -->
<div id="setup-container">
<input type="text" id="city-input-large" placeholder="Enter City..." onkeydown="handleSetupInput(event)"
onclick="event.stopPropagation()">
</div>
<!-- Main Dashboard Content -->
<div id="dashboard-content" onclick="openSetup()">
<!-- Time -->
<div class="section greeting-box">
<h1 id="time-display">--:--</h1>
<p id="greeting-text">...</p>
</div>
<!-- Prayer -->
<div class="section prayer-box">
<div class="prayer-header">Next Prayer</div>
<div class="prayer-time">--:--</div>
<div class="prayer-name">...</div>
</div>
<!-- Quote -->
<div class="section quote-box">
<div class="quote-text">...</div>
<div class="quote-source"></div>
</div>
<!-- Weather -->
<div class="section weather-box">
<div class="weather-main">
<div class="weather-icon"><i class="ph-fill ph-dots-three-circle"></i></div>
<div class="temp">--°</div>
</div>
<div class="weather-tip">...</div>
</div>
</div>
<!-- Prayer Modal Removed -->
<script>
// --- State ---
const state = {
lat: null,
lon: null,
timezone: null,
weather: null,
prayerTimes: null,
city: null, // Start null to ensure blank slate
};
// --- Quotes Collection (Expanded) ---
const quotes = [
{ text: "Verily, with hardship comes ease.", source: "Quran 94:6" },
{ text: "Allah does not burden a soul beyond that it can bear.", source: "Quran 2:286" },
{ text: "And He found you lost and guided you.", source: "Quran 93:7" },
{ text: "So remember Me; I will remember you.", source: "Quran 2:152" },
{ text: "Call upon Me, I will respond to you.", source: "Quran 40:60" },
{ text: "Indeed, good deeds do away with misdeeds.", source: "Quran 11:114" },
{ text: "And keep up prayer; surely prayer keeps (one) away from indecency and evil.", source: "Quran 29:45" },
{ text: "My Mercy encompasses all things.", source: "Quran 7:156" },
{ text: "If you are grateful, I will surely increase you [in favor].", source: "Quran 14:7" },
{ text: "Allah is with the patient.", source: "Quran 2:153" },
{ text: "Indeed, Allah is with those who fear Him and those who are doers of good.", source: "Quran 16:128" },
{ text: "Our Lord, forgive us our sins and the excess [committed] in our affairs.", source: "Quran 3:147" },
{ text: "And whoever puts his trust in Allah, then He will suffice him.", source: "Quran 65:3" },
{ text: "Do not despair of the mercy of Allah.", source: "Quran 39:53" },
{ text: "Speak good to people.", source: "Quran 2:83" },
{ text: "Show forgiveness, enjoin what is good, and turn away from the ignorant.", source: "Quran 7:199" }
];
// --- UI Initialization ---
function init() {
// Set random quote
const randomQuote = quotes[Math.floor(Math.random() * quotes.length)];
document.querySelector('.quote-text').innerText = `"${randomQuote.text}"`;
document.querySelector('.quote-source').innerText = randomQuote.source;
// Start Clock
updateClock();
setInterval(updateClock, 1000);
// Get Location & Data
getLocation();
// Init Responsive Logic
updateResponsiveLayout();
window.addEventListener('resize', updateResponsiveLayout);
}
function updateResponsiveLayout() {
const w = window.innerWidth;
const b = document.body.classList;
// Priority 4: Quotes
w < 850 ? b.add('resp-hide-quote') : b.remove('resp-hide-quote');
// Priority 6: Greeting
w < 550 ? b.add('resp-hide-greeting') : b.remove('resp-hide-greeting');
// Priority 5: Weather Tip
w < 450 ? b.add('resp-hide-tip') : b.remove('resp-hide-tip');
// Cleanup: Prayer Header
w < 340 ? b.add('resp-hide-prayer-label') : b.remove('resp-hide-prayer-label');
// Priority 3: Weather Box (The User's Request: Hide as it gets smaller)
// At < 300px, 240px min-width, we drop Weather to keep Time + Prayer clean.
w < 300 ? b.add('resp-hide-weather') : b.remove('resp-hide-weather');
}
// --- Location & APIs ---
function getLocation() {
// 1. Try Local Storage first (User Override)
try {
const saved = localStorage.getItem('widget_location');
if (saved) {
const data = JSON.parse(saved);
state.lat = data.lat;
state.lon = data.lon;
state.city = data.name;
state.timezone = data.timezone; // Restore timezone
// Show Dashboard immediately since we have data
document.body.classList.add('dashboard-active');
fetchData();
return;
}
} catch (e) {
console.warn("Local Storage access denied or failed", e);
}
// 2. Try Geolocation (Only if no saved state, but usually we wait for input now)
// For Click-to-Configure, we default to Setup Mode if no storage.
// But we can still try to auto-locate to pre-fill or auto-start.
// Let's stick to "User Choose" philosophy: If no storage, show Setup.
openSetup();
}
function useFallbackLocation() {
// Not used in Blank Slate mode usually, but keep as safe fallback
state.lat = 51.5074;
state.lon = -0.1278;
state.city = "London";
state.timezone = "Europe/London";
fetchData();
}
// --- Interaction Logic (Setup Mode) ---
function openSetup() {
document.body.classList.add('setup-mode');
document.body.classList.remove('dashboard-active'); // Hide dashboard
document.getElementById('setup-container').classList.add('active');
const input = document.getElementById('city-input-large');
input.value = (state.city === "Loading..." || !state.city) ? "" : state.city;
setTimeout(() => input.focus(), 100);
}
function closeSetup() {
document.body.classList.remove('setup-mode');
const setup = document.getElementById('setup-container');
setup.classList.remove('active');
// Show Dashboard
document.body.classList.add('dashboard-active');
}
function handleSetupInput(event) {
if (event.key === 'Enter') {
const city = event.target.value.trim();
if (city) {
searchCity(city);
}
}
}
async function searchCity(input) {
// No visual loading state on text, maybe assume user waits or add spinner later
// For now, keep it fast.
try {
// Open-Meteo Geocoding API
const url = `https://geocoding-api.open-meteo.com/v1/search?name=${encodeURIComponent(input)}&count=1&language=en&format=json`;
const res = await fetch(url);
const data = await res.json();
if (data.results && data.results.length > 0) {
const place = data.results[0];
state.lat = place.latitude;
state.lon = place.longitude;
state.city = place.name;
state.timezone = place.timezone;
// Save to Storage
try {
localStorage.setItem('widget_location', JSON.stringify({
lat: state.lat,
lon: state.lon,
name: state.city,
timezone: state.timezone
}));
} catch (e) {
console.warn("Could not save to Local Storage", e);
}
closeSetup(); // Success: Adds dashboard-active
fetchData();
} else {
alert("City not found.");
}
} catch (e) {
console.error("Geocoding failed", e);
alert("Error finding city.");
}
}
async function fetchData() {
await Promise.all([fetchWeather(), fetchPrayerTimes()]);
// Re-run updates once we have data
updateUI();
updateGreetingAndTheme(new Date());
}
async function fetchWeather() {
try {
// Fetch 2 days of hourly/daily data to handle late-night forecasts
const url = `https://api.open-meteo.com/v1/forecast?latitude=${state.lat}&longitude=${state.lon}¤t=temperature_2m,weather_code,is_day&hourly=weather_code&daily=temperature_2m_max,temperature_2m_min,weather_code&forecast_days=2&timezone=auto`;
const res = await fetch(url);
const data = await res.json();
state.timezone = data.timezone; // Force update timezone from API source of truth
state.weather = data; // Store entire response
} catch (e) {
console.error("Weather fetch failed", e);
}
}
async function fetchPrayerTimes() {
try {
const date = new Date();
// adhan api params: 3 = Muslim World League
const url = `https://api.aladhan.com/v1/timings/${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}?latitude=${state.lat}&longitude=${state.lon}&method=3`;
const res = await fetch(url);
const data = await res.json();
state.prayerTimes = data.data.timings;
} catch (e) {
console.error("Prayer fetch failed", e);
}
}
// --- Updates & Logic ---
function updateClock() {
const now = new Date();
// Use target timezone if available, else browser local
const timeOptions = { hour: '2-digit', minute: '2-digit' };
if (state.timezone) {
timeOptions.timeZone = state.timezone;
}
const timeString = now.toLocaleTimeString([], timeOptions);
document.getElementById('time-display').innerText = timeString;
// Update greeting every minute
if (now.getSeconds() === 0) updateGreetingAndTheme(now);
}
function updateGreetingAndTheme(now) {
let greeting = 'Good Morning';
// Use Prayer Times for dynamic sun-based events if available
if (state.prayerTimes) {
const currentMinutes = now.getHours() * 60 + now.getMinutes();
const fajr = timeToMinutes(state.prayerTimes.Fajr);
const sunrise = timeToMinutes(state.prayerTimes.Sunrise);
const dhuhr = timeToMinutes(state.prayerTimes.Dhuhr);
const maghrib = timeToMinutes(state.prayerTimes.Maghrib);
const isha = timeToMinutes(state.prayerTimes.Isha);
// Greeting Logic
if (currentMinutes >= fajr && currentMinutes < sunrise) greeting = 'Blessed Fajr';
else if (currentMinutes >= sunrise && currentMinutes < dhuhr) greeting = 'Good Morning';
else if (currentMinutes >= dhuhr && currentMinutes < maghrib) greeting = 'Good Afternoon';
else if (currentMinutes >= maghrib && currentMinutes < isha) greeting = 'Good Evening';
else greeting = 'Good Night';
} else {
// Flashback Greeting logic
const hour = now.getHours();
if (hour >= 5 && hour < 12) greeting = 'Good Morning';
else if (hour >= 12 && hour < 17) greeting = 'Good Afternoon';
else if (hour >= 17 && hour < 20) greeting = 'Good Evening';
else greeting = 'Good Night';
}
document.getElementById('greeting-text').innerText = `${greeting}`;
// Permanent Dark Mode: No dynamic background switching needed.
}
function timeToMinutes(timeStr) {
const [h, m] = timeStr.split(':').map(Number);
return h * 60 + m;
}
function updateUI() {
// Note: City Name display is removed from dashboard;
// the dashboard itself is the display of that city's data.
if (state.weather && state.weather.current) {
const current = state.weather.current;
const temp = Math.round(current.temperature_2m);
const code = current.weather_code;
document.querySelector('.temp').innerText = `${temp}°`;
// Weather Icons & Tips
const { icon, tip } = getWeatherDetails(state.weather);
document.querySelector('.weather-icon').innerHTML = `<i class="${icon}"></i>`;
document.querySelector('.weather-tip').innerText = tip;
}
if (state.prayerTimes) {
const next = getNextPrayer(state.prayerTimes);
if (next) {
document.querySelector('.prayer-time').innerText = next.time;
document.querySelector('.prayer-name').innerText = next.name;
document.querySelector('.prayer-header').innerText = "Next Prayer";
}
}
}
function getWeatherDetails(weatherData) {
// Codes: 0=Clear, 1-3=Cloudy, 45-48=Fog, 51-67=Rain/Drizzle, 71-77=Snow, 80-82=Showers, 95+=Thunderstorm
const current = weatherData.current;
const daily = weatherData.daily;
const hourly = weatherData.hourly;
const code = current.weather_code;
const temp = current.temperature_2m;
let icon = 'ph-fill ph-sun';
let tip = 'Good Day';
// 1. Icon Selection
if (code <= 1) icon = 'ph-fill ph-sun';
else if (code <= 3) icon = 'ph-fill ph-cloud';
else if (code <= 67) icon = 'ph-fill ph-cloud-rain';
else if (code <= 77) icon = 'ph-fill ph-snowflake';
else if (code > 80) icon = 'ph-fill ph-cloud-lightning';
// 2. Smart Tip Logic (Priority-based)
const now = new Date();
// Critical: Get the hour in the TARGET timezone, not browser's
let nowHour = now.getHours();
if (state.timezone) {
try {
// Create date object for target timezone to extract hour
const targetTime = new Date(now.toLocaleString("en-US", { timeZone: state.timezone }));
nowHour = targetTime.getHours();
} catch (e) {
console.warn("Invalid timezone, falling back to local time", e);
}
}
let forecastMsg = "";
let foundEvent = false;
// A. Check for weather CHANGE in next 2-3 hours only
// Only show if conditions are getting worse or significantly different
const checkHours = 3; // Focus on next 2-3 hours
for (let i = nowHour + 1; i <= nowHour + checkHours; i++) {
if (i >= hourly.weather_code.length) break;
const futureCode = hourly.weather_code[i];
// Define "weather change":
// - Currently clear/cloudy (code < 51) but rain/snow/storm coming
// - Currently rainy but snow/storm coming
const isSignificantChange =
(code < 51 && futureCode >= 51) || // Clear → Precip
(code >= 51 && code < 71 && futureCode >= 71); // Rain → Snow/Storm
if (isSignificantChange) {
const displayHour = i % 24;
const ampm = displayHour >= 12 ? 'pm' : 'am';
const shortHour = displayHour % 12 || 12;
const shortTime = `${shortHour}${ampm}`;
if (futureCode >= 95) forecastMsg = `Storm @ ${shortTime}`;
else if ((futureCode >= 71 && futureCode <= 77) || futureCode === 85 || futureCode === 86) forecastMsg = `Snow @ ${shortTime}`;
else forecastMsg = `Rain @ ${shortTime}`;
foundEvent = true;
break;
}
}
// B. Check for significant temperature change in next 2-3 hours
if (!foundEvent && hourly && hourly.temperature_2m) {
for (let i = nowHour + 1; i <= nowHour + checkHours; i++) {
if (i >= hourly.temperature_2m.length) break;
const futureTemp = hourly.temperature_2m[i];
const tempDiff = Math.abs(futureTemp - temp);
// Only alert if temp changes by 5°+ in next few hours
if (tempDiff >= 5) {
const displayHour = i % 24;
const ampm = displayHour >= 12 ? 'pm' : 'am';
const shortHour = displayHour % 12 || 12;
const shortTime = `${shortHour}${ampm}`;
if (futureTemp > temp) {
forecastMsg = `Warming to ${Math.round(futureTemp)}° @ ${shortTime}`;
} else {
forecastMsg = `Cooling to ${Math.round(futureTemp)}° @ ${shortTime}`;
}
foundEvent = true;
break;
}
}
}
// C. If no change detected in next 2-3 hours, show nothing (stable conditions)
if (foundEvent) {
tip = forecastMsg;
} else {
// Hide forecast tip entirely when stable
tip = "";
}
return { icon, tip };
}
function getNextPrayer(timings) {
const now = new Date();
const currentMinutes = now.getHours() * 60 + now.getMinutes();
const prayerOrder = ['Fajr', 'Dhuhr', 'Asr', 'Maghrib', 'Isha'];
for (let name of prayerOrder) {
const time = timings[name]; // "HH:MM"
const [h, m] = time.split(':').map(Number);
const prayerMinutes = h * 60 + m;
if (prayerMinutes > currentMinutes) {
return { name, time };
}
}
// If none matched, next is Fajr tomorrow
return { name: 'Fajr', time: timings['Fajr'] };
}
// Initialize
init();
// Initial call for immediate theme check
updateGreetingAndTheme(new Date());
</script>
</body>
</html>