-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
1145 lines (967 loc) · 34.4 KB
/
script.js
File metadata and controls
1145 lines (967 loc) · 34.4 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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// API endpoints para cotação do dólar (com fallback)
const API_URLS = [
"https://economia.awesomeapi.com.br/json/last/USD-BRL?token=76191ef05baf52e0267a57136587ed3a5304792b31346a1d2fb53e805f03327d",
"https://api.exchangerate-api.com/v4/latest/USD",
];
let currentApiIndex = 0;
const _API_URL = API_URLS[0];
// Configurações
const UPDATE_INTERVAL = 30000; // 30 segundos (recomendado pela API)
const UPDATE_INTERVAL_BACKGROUND = 60000; // 60 segundos quando em background
// Estado da aplicação
let currentQuote = null;
let _previousQuote = null;
let alerts = [];
let updateInterval = null;
let isFetching = false; // Previne requisições sobrepostas
const activeNotifications = new Set(); // Rastreia notificações ativas
let conversionValue = null; // Valor em dólares para conversão
let serviceWorkerRegistration = null;
const quoteHistory = []; // Histórico de cotações para o gráfico
const MAX_HISTORY_LENGTH = 50; // Máximo de pontos no gráfico
let chartPoints = []; // Armazenar posições dos pontos do gráfico para hover
let hoveredPointIndex = -1; // Índice do ponto sendo hovered
// Passcode gate - sessionStorage key for authenticated state
const PASSCODE_SESSION_KEY = "passcode_authenticated";
// Initialize app (runs after passcode verification)
function initApp() {
// Mostrar estado de carregamento inicial
const quoteElement = document.getElementById("quoteValue");
if (quoteElement) {
quoteElement.textContent = "Carregando...";
quoteElement.style.color = "#666";
}
loadAlerts();
loadConversionValue();
requestNotificationPermission();
registerServiceWorker();
// Inicializar gráfico
let resizeTimeout;
function resizeCanvas() {
const canvas = document.getElementById("quoteChart");
if (canvas) {
// Limpar timeout anterior
clearTimeout(resizeTimeout);
// Usar timeout para evitar múltiplas chamadas durante o resize
resizeTimeout = setTimeout(() => {
// Forçar recalculo do layout
const container = canvas.parentElement;
if (container) {
// Garantir que o container tenha largura definida
const containerWidth = container.clientWidth || container.offsetWidth;
const containerHeight = container.clientHeight || 200;
// Ajustar tamanho do canvas para alta resolução
const dpr = window.devicePixelRatio || 1;
const availableWidth = Math.max(containerWidth - 32, 200); // Subtrair padding
const availableHeight = Math.max(containerHeight - 100, 180); // Subtrair padding e título
canvas.width = availableWidth * dpr;
canvas.height = availableHeight * dpr;
const ctx = canvas.getContext("2d");
ctx.scale(dpr, dpr);
canvas.style.width = `${availableWidth}px`;
canvas.style.height = `${availableHeight}px`;
// Redesenhar gráfico se houver dados
if (quoteHistory.length > 0) {
hoveredPointIndex = -1; // Resetar hover ao redimensionar
updateChart();
}
}
}, 100);
}
}
// Aguardar um frame para garantir que o layout está pronto
requestAnimationFrame(() => {
resizeCanvas();
});
// Também redimensionar quando a página estiver totalmente carregada
if (document.readyState === "complete") {
setTimeout(resizeCanvas, 100);
} else {
window.addEventListener("load", () => {
setTimeout(resizeCanvas, 100);
});
}
window.addEventListener("resize", resizeCanvas);
// Também escutar mudanças de orientação
window.addEventListener("orientationchange", () => {
setTimeout(resizeCanvas, 300);
});
// Adicionar interatividade ao gráfico (hover)
setupChartInteractivity();
// Aguardar um pouco antes da primeira requisição para garantir que tudo está pronto
setTimeout(() => {
fetchQuote();
// Redimensionar canvas após buscar dados
setTimeout(resizeCanvas, 200);
}, 500);
// Atualizar periodicamente
startUpdateInterval();
// Configurar botão de adicionar alerta
document.getElementById("addAlertBtn").addEventListener("click", addAlert);
// Configurar botão de testar notificação
document.getElementById("testNotificationBtn").addEventListener("click", testNotification);
// Configurar botão de salvar valor de conversão
document.getElementById("saveConversionBtn").addEventListener("click", saveConversionValue);
// Otimizar quando a aba fica em background
setupVisibilityHandling();
// Cleanup quando a página é fechada
window.addEventListener("beforeunload", cleanup);
}
// Passcode gate - check authentication on load
document.addEventListener("DOMContentLoaded", () => {
const passcodeScreen = document.getElementById("passcodeScreen");
const appContainer = document.getElementById("appContainer");
const passcodeForm = document.getElementById("passcodeForm");
const passcodeInput = document.getElementById("passcodeInput");
const passcodeError = document.getElementById("passcodeError");
const passcodeSubmitBtn = document.getElementById("passcodeSubmitBtn");
// Already authenticated (same session)
if (sessionStorage.getItem(PASSCODE_SESSION_KEY) === "true") {
passcodeScreen.style.display = "none";
appContainer.style.display = "";
initApp();
return;
}
// Show passcode screen, hide app
passcodeScreen.style.display = "flex";
appContainer.style.display = "none";
passcodeForm.addEventListener("submit", async (e) => {
e.preventDefault();
const passcode = passcodeInput.value.trim();
if (!passcode) return;
passcodeError.style.display = "none";
passcodeSubmitBtn.disabled = true;
passcodeSubmitBtn.textContent = "Verificando...";
try {
const res = await fetch("/api/verify-passcode", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ passcode }),
});
const data = await res.json();
if (data.success) {
sessionStorage.setItem(PASSCODE_SESSION_KEY, "true");
passcodeScreen.style.display = "none";
appContainer.style.display = "";
initApp();
} else {
passcodeError.style.display = "block";
passcodeInput.value = "";
passcodeInput.focus();
}
} catch (_err) {
passcodeError.textContent = "Erro ao verificar. Tente novamente.";
passcodeError.style.display = "block";
} finally {
passcodeSubmitBtn.disabled = false;
passcodeSubmitBtn.textContent = "Acessar";
}
});
passcodeInput.focus();
});
// Registrar Service Worker
async function registerServiceWorker() {
if ("serviceWorker" in navigator) {
try {
const registration = await navigator.serviceWorker.register("/service-worker.js");
serviceWorkerRegistration = registration;
console.log("[Service Worker] Registrado com sucesso:", registration.scope);
// Aguardar o service worker estar pronto
if (registration.installing) {
console.log("[Service Worker] Instalando...");
} else if (registration.waiting) {
console.log("[Service Worker] Aguardando...");
} else if (registration.active) {
console.log("[Service Worker] Ativo");
syncDataWithServiceWorker();
}
// Escutar atualizações do service worker
registration.addEventListener("updatefound", () => {
const newWorker = registration.installing;
newWorker.addEventListener("statechange", () => {
if (newWorker.state === "activated") {
console.log("[Service Worker] Nova versão ativada");
syncDataWithServiceWorker();
}
});
});
// Escutar mensagens do service worker
navigator.serviceWorker.addEventListener("message", (event) => {
const { type, data } = event.data;
if (type === "quote-updated") {
// Atualizar UI se a página estiver visível
if (!document.hidden && data) {
currentQuote = {
value: data.value,
timestamp: new Date(data.timestamp),
change: data.change,
};
updateUI();
}
}
});
} catch (error) {
console.error("[Service Worker] Erro ao registrar:", error);
}
} else {
console.log("[Service Worker] Não suportado neste navegador");
}
}
// Sincronizar dados com o service worker
function syncDataWithServiceWorker() {
if (!serviceWorkerRegistration || !serviceWorkerRegistration.active) {
return;
}
// Sincronizar alertas
serviceWorkerRegistration.active.postMessage({
type: "sync-alerts",
data: alerts,
});
// Sincronizar valor de conversão
serviceWorkerRegistration.active.postMessage({
type: "sync-conversion",
data: conversionValue,
});
}
// Solicitar permissão para notificações
async function requestNotificationPermission() {
if ("Notification" in window && Notification.permission === "default") {
await Notification.requestPermission();
}
}
// Testar notificação
async function testNotification() {
if (!("Notification" in window)) {
alert("Este navegador não suporta notificações");
return;
}
if (Notification.permission === "denied") {
alert(
"Permissão de notificação foi negada. Por favor, permita notificações nas configurações do navegador."
);
return;
}
if (Notification.permission === "default") {
const permission = await Notification.requestPermission();
if (permission !== "granted") {
alert("Permissão de notificação é necessária para receber alertas.");
return;
}
}
// Criar notificação de teste
const testValue = currentQuote
? currentQuote.value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 3,
maximumFractionDigits: 3,
})
: "R$ 5,000";
let testMessage = `Esta é uma notificação de teste! A cotação atual é ${testValue}.`;
// Adicionar valor convertido se existir
if (conversionValue && currentQuote) {
// Calcular valor em reais (valor em dólares * cotação)
const realAmount = conversionValue * (currentQuote.value - 0.02);
const formattedReal = realAmount.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});
testMessage += ` | Valor convertido: ${formattedReal}`;
}
const notification = new Notification("💵 Teste de Notificação", {
body: testMessage,
icon: 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">💵</text></svg>',
badge:
'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><text y=".9em" font-size="90">💵</text></svg>',
tag: "test-notification",
silent: false,
requireInteraction: false,
});
activeNotifications.add(notification);
notification.onclick = () => {
window.focus();
notification.close();
activeNotifications.delete(notification);
};
// Fechar automaticamente após 5 segundos
setTimeout(() => {
try {
notification.close();
activeNotifications.delete(notification);
} catch (_e) {
// Ignorar erros
}
}, 5000);
}
// Buscar cotação do dólar
async function fetchQuote() {
// Prevenir requisições sobrepostas
if (isFetching) {
return;
}
isFetching = true;
try {
const apiUrl = API_URLS[currentApiIndex];
const response = await fetch(apiUrl, {
method: "GET",
mode: "cors",
cache: "no-cache",
headers: {
Accept: "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status} - ${response.statusText}`);
}
const data = await response.json();
if (!data) {
throw new Error("Resposta da API está vazia");
}
let quoteValue = null;
let changeValue = 0;
// Processar resposta da AwesomeAPI (formato principal)
if (data.USDBRL) {
const usdData = data.USDBRL;
quoteValue = Number.parseFloat(usdData.bid || usdData.ask || usdData.high || usdData.low);
changeValue = usdData.pctChange ? Number.parseFloat(usdData.pctChange) : 0;
}
// Processar resposta do ExchangeRate-API (fallback)
else if (data.rates?.BRL) {
quoteValue = Number.parseFloat(data.rates.BRL);
changeValue = 0; // Esta API não fornece variação percentual
}
// Tentar outros formatos
else if (data.USD) {
const usdData = data.USD;
quoteValue = Number.parseFloat(usdData.bid || usdData.ask || usdData.value);
changeValue = usdData.pctChange ? Number.parseFloat(usdData.pctChange) : 0;
} else if (data.value) {
quoteValue = Number.parseFloat(data.value);
}
if (!quoteValue || Number.isNaN(quoteValue)) {
console.warn("Estrutura da API recebida:", data);
throw new Error("Valor da cotação não encontrado ou inválido");
}
_previousQuote = currentQuote;
currentQuote = {
value: quoteValue,
timestamp: new Date(),
change: changeValue,
};
// Adicionar à lista de histórico
quoteHistory.push({
value: quoteValue,
timestamp: currentQuote.timestamp,
});
// Limitar o tamanho do histórico
if (quoteHistory.length > MAX_HISTORY_LENGTH) {
quoteHistory.shift(); // Remove o mais antigo
}
updateUI();
updateChart();
checkAlerts();
// Resetar índice da API em caso de sucesso
currentApiIndex = 0;
} catch (error) {
console.error("Erro ao buscar cotação:", error);
console.error("URL tentada:", API_URLS[currentApiIndex]);
// Tentar API alternativa se disponível
if (currentApiIndex < API_URLS.length - 1) {
currentApiIndex++;
console.log(`Tentando API alternativa: ${API_URLS[currentApiIndex]}`);
isFetching = false;
setTimeout(() => fetchQuote(), 1000);
return;
}
const quoteElement = document.getElementById("quoteValue");
if (quoteElement) {
quoteElement.textContent = "Erro ao carregar";
quoteElement.style.color = "var(--error, #ef4444)";
}
// Resetar índice e tentar novamente após 5 segundos
currentApiIndex = 0;
setTimeout(() => {
if (!currentQuote) {
fetchQuote();
}
}, 5000);
} finally {
isFetching = false;
}
}
// Atualizar interface
function updateUI() {
if (!currentQuote) return;
const quoteElement = document.getElementById("quoteValue");
if (!quoteElement) return;
// Atualizar valor
const formattedValue = currentQuote.value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 3,
maximumFractionDigits: 3,
});
quoteElement.textContent = formattedValue;
quoteElement.style.color = ""; // Resetar cor em caso de erro anterior
// Atualizar variação
const changeElement = document.getElementById("changeValue");
const changeValue = currentQuote.change || 0;
changeElement.textContent = `${changeValue >= 0 ? "+" : ""}${changeValue.toFixed(3)}%`;
changeElement.className = `change-value ${changeValue >= 0 ? "positive" : "negative"}`;
// Atualizar hora
const timeString = currentQuote.timestamp.toLocaleTimeString("pt-BR");
document.getElementById("updateTime").textContent = timeString;
// Atualizar valor convertido
updateConvertedValue();
}
// Atualizar gráfico de cotações
function updateChart() {
const canvas = document.getElementById("quoteChart");
if (!canvas || quoteHistory.length === 0) return;
// Garantir que o canvas tenha tamanho correto antes de desenhar
const rect = canvas.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
// Se o canvas não tem tamanho, tentar redimensionar
resizeCanvas();
return;
}
// Limpar pontos anteriores
chartPoints = [];
const ctx = canvas.getContext("2d");
const dpr = window.devicePixelRatio || 1;
// O canvas já está dimensionado com dpr, então precisamos usar o tamanho CSS
const width = rect.width || canvas.offsetWidth || 400;
const height = rect.height || canvas.offsetHeight || 200;
// Limpar canvas (usar dimensões físicas que já incluem dpr)
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (quoteHistory.length < 2) {
// Se há apenas um ponto, não há linha para desenhar
return;
}
// Calcular valores mínimo e máximo para escala
const values = quoteHistory.map((q) => q.value);
const minValue = Math.min(...values);
const maxValue = Math.max(...values);
const range = maxValue - minValue || 1; // Evitar divisão por zero
// Padding para o gráfico (ajustado para mobile)
const isMobile = window.innerWidth <= 640;
const padding = {
top: 20,
right: isMobile ? 10 : 20,
bottom: isMobile ? 25 : 30,
left: isMobile ? 35 : 50,
};
// Garantir que temos espaço suficiente para o gráfico
const chartWidth = Math.max(width - padding.left - padding.right, 100);
const chartHeight = Math.max(height - padding.top - padding.bottom, 80);
// Desenhar eixos
ctx.strokeStyle = "rgba(0, 0, 0, 0.12)";
ctx.lineWidth = 1;
// Linha horizontal (eixo X)
ctx.beginPath();
ctx.moveTo(padding.left, height - padding.bottom);
ctx.lineTo(width - padding.right, height - padding.bottom);
ctx.stroke();
// Linha vertical (eixo Y)
ctx.beginPath();
ctx.moveTo(padding.left, padding.top);
ctx.lineTo(padding.left, height - padding.bottom);
ctx.stroke();
// Garantir que não desenhamos fora dos limites
const maxX = Math.min(width - padding.right, canvas.width / dpr);
const maxY = Math.min(height - padding.bottom, canvas.height / dpr);
// Desenhar linhas de referência (grid)
ctx.strokeStyle = "rgba(0, 0, 0, 0.08)";
ctx.lineWidth = 0.5 / dpr; // Ajustar para dpr
for (let i = 0; i <= 4; i++) {
const y = padding.top + (chartHeight / 4) * i;
ctx.beginPath();
ctx.moveTo(padding.left, y);
ctx.lineTo(maxX, y);
ctx.stroke();
}
// Desenhar valores no eixo Y
ctx.fillStyle = "#475569";
const fontSize = isMobile ? 9 : 10;
ctx.font = `${fontSize}px "SF Mono", "Monaco", "Inconsolata", "Fira Code", "Droid Sans Mono", "Source Code Pro", monospace`;
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (let i = 0; i <= 4; i++) {
const value = maxValue - (range / 4) * i;
const y = padding.top + (chartHeight / 4) * i;
const xPosition = padding.left - (isMobile ? 5 : 10);
ctx.fillText(value.toFixed(3), xPosition, y);
}
// Desenhar linha do gráfico
ctx.strokeStyle = "#3b82f6";
ctx.lineWidth = 1.5 / dpr; // Ajustar para dpr
ctx.beginPath();
quoteHistory.forEach((quote, index) => {
const x = padding.left + (chartWidth / (quoteHistory.length - 1)) * index;
const normalizedValue = (quote.value - minValue) / range;
const y = padding.top + chartHeight - normalizedValue * chartHeight;
// Garantir que os pontos estão dentro dos limites
const clampedX = Math.min(Math.max(x, padding.left), maxX);
const clampedY = Math.min(Math.max(y, padding.top), maxY);
if (index === 0) {
ctx.moveTo(clampedX, clampedY);
} else {
ctx.lineTo(clampedX, clampedY);
}
});
ctx.stroke();
// Armazenar posições dos pontos para hover e desenhar pontos
chartPoints = [];
const pointRadius = isMobile ? 4 : 5;
const hoverRadius = isMobile ? 8 : 10;
quoteHistory.forEach((quote, index) => {
const x = padding.left + (chartWidth / (quoteHistory.length - 1)) * index;
const normalizedValue = (quote.value - minValue) / range;
const y = padding.top + chartHeight - normalizedValue * chartHeight;
// Garantir que os pontos estão dentro dos limites
const clampedX = Math.min(Math.max(x, padding.left), maxX);
const clampedY = Math.min(Math.max(y, padding.top), maxY);
// Armazenar posição do ponto
chartPoints.push({
x: clampedX,
y: clampedY,
value: quote.value,
timestamp: quote.timestamp,
index: index,
});
// Desenhar ponto com destaque se estiver em hover
const isHovered = hoveredPointIndex === index;
const currentRadius = isHovered ? hoverRadius : pointRadius;
// Círculo externo (branco) para destaque
if (isHovered) {
ctx.fillStyle = "rgba(255, 255, 255, 0.8)";
ctx.beginPath();
ctx.arc(clampedX, clampedY, currentRadius / dpr, 0, Math.PI * 2);
ctx.fill();
}
// Ponto principal
ctx.fillStyle = isHovered ? "#2563eb" : "#3b82f6";
ctx.beginPath();
ctx.arc(clampedX, clampedY, pointRadius / dpr, 0, Math.PI * 2);
ctx.fill();
// Borda branca para melhor visibilidade
ctx.strokeStyle = "#ffffff";
ctx.lineWidth = 1.5 / dpr;
ctx.stroke();
});
// Desenhar área preenchida abaixo da linha
if (quoteHistory.length >= 2) {
const gradient = ctx.createLinearGradient(0, padding.top, 0, height - padding.bottom);
gradient.addColorStop(0, "rgba(59, 130, 246, 0.15)");
gradient.addColorStop(1, "rgba(59, 130, 246, 0.02)");
ctx.fillStyle = gradient;
ctx.beginPath();
ctx.moveTo(padding.left, height - padding.bottom);
quoteHistory.forEach((quote, index) => {
const x = padding.left + (chartWidth / (quoteHistory.length - 1)) * index;
const normalizedValue = (quote.value - minValue) / range;
const y = padding.top + chartHeight - normalizedValue * chartHeight;
// Garantir que os pontos estão dentro dos limites
const clampedX = Math.min(Math.max(x, padding.left), maxX);
const clampedY = Math.min(Math.max(y, padding.top), maxY);
ctx.lineTo(clampedX, clampedY);
});
ctx.lineTo(maxX, height - padding.bottom);
ctx.closePath();
ctx.fill();
}
// Esconder tooltip se não houver hover ativo
if (hoveredPointIndex === -1) {
const tooltip = document.getElementById("chartTooltip");
if (tooltip) {
tooltip.style.display = "none";
}
}
}
// Configurar interatividade do gráfico (hover)
let chartInteractivitySetup = false;
function setupChartInteractivity() {
const canvas = document.getElementById("quoteChart");
const tooltip = document.getElementById("chartTooltip");
if (!canvas || !tooltip || chartInteractivitySetup) return;
const chartWrapper = canvas.parentElement;
if (!chartWrapper) return;
chartInteractivitySetup = true;
let currentHoverIndex = -1;
function getPointAtPosition(mouseX, mouseY) {
const rect = canvas.getBoundingClientRect();
const x = mouseX - rect.left;
const y = mouseY - rect.top;
const _dpr = window.devicePixelRatio || 1;
const isMobile = window.innerWidth <= 640;
const hitRadius = isMobile ? 12 : 15;
// Encontrar o ponto mais próximo
let closestIndex = -1;
let minDistance = Number.POSITIVE_INFINITY;
chartPoints.forEach((point, index) => {
const distance = Math.sqrt((point.x - x) ** 2 + (point.y - y) ** 2);
if (distance < hitRadius && distance < minDistance) {
minDistance = distance;
closestIndex = index;
}
});
return closestIndex;
}
function showTooltip(index, _mouseX, _mouseY) {
if (index === -1 || !chartPoints[index]) {
tooltip.style.display = "none";
return;
}
const point = chartPoints[index];
const date = new Date(point.timestamp);
const dateStr = date.toLocaleDateString("pt-BR", {
day: "2-digit",
month: "2-digit",
year: "numeric",
});
const timeStr = date.toLocaleTimeString("pt-BR", {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
const valueStr = point.value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 3,
maximumFractionDigits: 3,
});
tooltip.innerHTML = `
<div style="font-weight: 600; margin-bottom: 2px;">${valueStr}</div>
<div style="font-size: 10px; opacity: 0.9;">${dateStr}</div>
<div style="font-size: 10px; opacity: 0.9;">${timeStr}</div>
`;
const rect = canvas.getBoundingClientRect();
const wrapperRect = chartWrapper.getBoundingClientRect();
// Posicionar tooltip acima do ponto
let left = rect.left + point.x - wrapperRect.left;
let top = rect.top + point.y - wrapperRect.top - 10;
// Ajustar para não sair da tela
const tooltipWidth = tooltip.offsetWidth || 120;
const tooltipHeight = tooltip.offsetHeight || 60;
if (left - tooltipWidth / 2 < 0) {
left = tooltipWidth / 2;
} else if (left + tooltipWidth / 2 > wrapperRect.width) {
left = wrapperRect.width - tooltipWidth / 2;
}
if (top - tooltipHeight < 0) {
top = rect.top + point.y - wrapperRect.top + 20;
}
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top}px`;
tooltip.style.display = "block";
}
function hideTooltip() {
tooltip.style.display = "none";
currentHoverIndex = -1;
hoveredPointIndex = -1;
// Redesenhar gráfico para remover destaque do ponto
if (quoteHistory.length > 0) {
updateChart();
}
}
// Event listeners
canvas.addEventListener("mousemove", (e) => {
const pointIndex = getPointAtPosition(e.clientX, e.clientY);
if (pointIndex !== currentHoverIndex) {
currentHoverIndex = pointIndex;
hoveredPointIndex = pointIndex;
if (pointIndex !== -1) {
showTooltip(pointIndex, e.clientX, e.clientY);
updateChart(); // Redesenhar para destacar o ponto
} else {
hideTooltip();
}
} else if (pointIndex !== -1) {
// Atualizar posição do tooltip se o mouse se mover
showTooltip(pointIndex, e.clientX, e.clientY);
}
});
canvas.addEventListener("mouseleave", () => {
hideTooltip();
});
// Para mobile/touch
canvas.addEventListener("touchstart", (e) => {
e.preventDefault();
const touch = e.touches[0];
const pointIndex = getPointAtPosition(touch.clientX, touch.clientY);
if (pointIndex !== -1) {
hoveredPointIndex = pointIndex;
showTooltip(pointIndex, touch.clientX, touch.clientY);
updateChart();
}
});
canvas.addEventListener("touchend", () => {
setTimeout(() => {
hideTooltip();
}, 2000); // Manter tooltip por 2 segundos no touch
});
}
// Adicionar alerta
function addAlert() {
const type = document.getElementById("alertType").value;
const value = Number.parseFloat(document.getElementById("alertValue").value);
if (!value || value <= 0) {
window.alert("Por favor, insira um valor válido");
return;
}
const alert = {
id: Date.now(),
type: type,
value: value,
triggered: false,
};
alerts.push(alert);
saveAlerts();
renderAlerts();
syncDataWithServiceWorker(); // Sincronizar com service worker
// Limpar formulário
document.getElementById("alertValue").value = "";
}
// Remover alerta
function removeAlert(id) {
alerts = alerts.filter((alert) => alert.id !== id);
saveAlerts();
renderAlerts();
syncDataWithServiceWorker(); // Sincronizar com service worker
}
// Renderizar lista de alertas
function renderAlerts() {
const alertsList = document.getElementById("alertsList");
if (alerts.length === 0) {
alertsList.innerHTML = '<p class="no-alerts">Nenhum alerta configurado</p>';
return;
}
alertsList.innerHTML = alerts
.map(
(alert) => `
<div class="alert-item">
<div class="alert-info">
<div class="alert-type ${alert.type}">
${alert.type === "above" ? "Acima de" : "Abaixo de"}
</div>
<div class="alert-value">
${alert.value.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 3,
maximumFractionDigits: 3,
})}
</div>
</div>
<button class="delete-btn" onclick="removeAlert(${alert.id})">Remover</button>
</div>
`
)
.join("");
}
// Verificar alertas
function checkAlerts() {
if (!currentQuote || alerts.length === 0) return;
const currentValue = currentQuote.value;
for (const alert of alerts) {
// if (alert.triggered) return;
let shouldTrigger = false;
if (alert.type === "above" && currentValue >= alert.value) {
shouldTrigger = true;
} else if (alert.type === "below" && currentValue <= alert.value) {
shouldTrigger = true;
}
if (shouldTrigger) {
triggerAlert(alert, currentValue);
// alert.triggered = true;
saveAlerts();
}
}
}
// Salvar alertas no localStorage
function saveAlerts() {
localStorage.setItem("dollarAlerts", JSON.stringify(alerts));
}
// Carregar alertas do localStorage
function loadAlerts() {
const saved = localStorage.getItem("dollarAlerts");
if (saved) {
alerts = JSON.parse(saved);
renderAlerts();
}
// Sincronizar após carregar
setTimeout(() => syncDataWithServiceWorker(), 1000);
}
// Salvar valor de conversão no localStorage
function saveConversionValue() {
const value = Number.parseFloat(document.getElementById("conversionValue").value);
if (!value || value <= 0) {
alert("Por favor, insira um valor válido maior que zero");
return;
}
conversionValue = value;
localStorage.setItem("dollarConversionValue", JSON.stringify(conversionValue));
syncDataWithServiceWorker(); // Sincronizar com service worker
// Atualizar exibição do valor convertido
updateConvertedValue();
// Limpar campo
document.getElementById("conversionValue").value = "";
}
// Carregar valor de conversão do localStorage
function loadConversionValue() {
const saved = localStorage.getItem("dollarConversionValue");
if (saved) {
conversionValue = JSON.parse(saved);
// Preencher campo com o valor salvo
document.getElementById("conversionValue").value = conversionValue;
}
// Sincronizar após carregar
setTimeout(() => syncDataWithServiceWorker(), 1000);
}
// Atualizar exibição do valor convertido
function updateConvertedValue() {
const container = document.getElementById("convertedValueContainer");
const amountElement = document.getElementById("convertedAmount");
if (!conversionValue || !currentQuote) {
container.style.display = "none";
return;
}
// Calcular valor em reais (valor em dólares * cotação)
const realAmount = conversionValue * (currentQuote.value - 0.02);
// Formatar valor em reais
const formattedReal = realAmount.toLocaleString("pt-BR", {
style: "currency",
currency: "BRL",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
});