From 7ed9223eb3ad1a94481057d8a88b3ebce2735127 Mon Sep 17 00:00:00 2001 From: Fabricio Pagliarini Date: Mon, 2 Dec 2024 21:52:45 -0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Adiciona=20logger=20de=20acesso?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 1 + .github/workflows/deploy.yml | 1 + app/providers.tsx | 5 ++- contexts/LogContext.tsx | 80 ++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 contexts/LogContext.tsx diff --git a/.env.development b/.env.development index 8c1afbc..72d60dd 100644 --- a/.env.development +++ b/.env.development @@ -1,5 +1,6 @@ NEXT_PUBLIC_GOOGLE_CLIENT_ID=xxxx NEXT_PUBLIC_GOOGLE_USER_SCRIPT_URL=xxxx NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL=xxxx +NEXT_PUBLIC_GOOGLE_ACCESS_LOGS_SCRIPT_URL=xxxx NEXT_PUBLIC_GOOGLE_PLACE_DETAILS_SCRIPT_URL=xxxx NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL=xxxx \ No newline at end of file diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ab7123d..002d1fb 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -30,6 +30,7 @@ jobs: NEXT_PUBLIC_GOOGLE_PLACES_SCRIPT_URL: ${{ secrets.GOOGLE_PLACES_SCRIPT_URL }} NEXT_PUBLIC_GOOGLE_FAVORITE_PLACES_SCRIPT_URL: ${{ secrets.GOOGLE_FAVORITE_PLACES_SCRIPT_URL }} NEXT_PUBLIC_GOOGLE_PLACE_DETAILS_SCRIPT_URL: ${{ secrets.GOOGLE_PLACE_DETAILS_SCRIPT_URL }} + NEXT_PUBLIC_GOOGLE_ACCESS_LOGS_SCRIPT_URL: ${{ secrets.GOOGLE_ACCESS_LOGS_SCRIPT_URL }} run: yarn build - name: Deploy to GitHub Pages diff --git a/app/providers.tsx b/app/providers.tsx index 45cd133..e010660 100644 --- a/app/providers.tsx +++ b/app/providers.tsx @@ -1,6 +1,7 @@ "use client"; import { AuthProvider } from "@/contexts/AuthContext"; import { FavoriteProvider } from "@/contexts/FavoritePlacesContext"; +import { LoggerProvider } from "@/contexts/LogContext"; import { PlacesProvider } from "@/contexts/PlacesContext"; import { NextUIProvider } from "@nextui-org/system"; import { ThemeProvider as NextThemesProvider } from "next-themes"; @@ -31,7 +32,9 @@ const Providers: React.FC = ({ children, themeProps }) => { - {children} + + {children} + diff --git a/contexts/LogContext.tsx b/contexts/LogContext.tsx new file mode 100644 index 0000000..71d4369 --- /dev/null +++ b/contexts/LogContext.tsx @@ -0,0 +1,80 @@ +"use client"; + +import React, { createContext, useContext, useEffect, useState } from "react"; +import axios from "axios"; +import Cookies from "js-cookie"; + +interface LoggerContextProps { + logHomeVisit: () => void; +} + +const LoggerContext = createContext(undefined); + +const COOKIE_NAME = "site_visited"; // Nome do cookie +const COOKIE_EXPIRATION_HOURS = 3; // Tempo de expiração do cookie em horas +const SCRIPT_URL = process.env.NEXT_PUBLIC_GOOGLE_ACCESS_LOGS_SCRIPT_URL!; + +export const LoggerProvider: React.FC<{ children: React.ReactNode }> = ({ + children, +}) => { + const [logSent, setLogSent] = useState(false); + + const logHomeVisit = async () => { + if (logSent) return; + + const hasVisited = Cookies.get(COOKIE_NAME); + if (hasVisited) { + console.log("Log já enviado recentemente. Evitando novo envio."); + return; + } + + try { + const ipInfoResponse = await axios.get( + "https://ipinfo.io/json?token=6cf47125441753" + ); + + const logData = { + ...ipInfoResponse.data, + userAgent: navigator.userAgent, + referrer: document.referrer || "Direto", + screenResolution: `${window.screen.width}x${window.screen.height}`, + timestamp: new Date().toISOString(), + }; + + console.log("Log de acesso enviado:", logData); + + // Envia os dados para sua API + await axios.post(SCRIPT_URL, logData, { + headers: { + "Content-Type": "text/plain;charset=utf-8", + }, + }); + + Cookies.set(COOKIE_NAME, "true", { + expires: COOKIE_EXPIRATION_HOURS / 24, + }); + setLogSent(true); + } catch (error) { + console.error("Erro ao enviar log:", error); + } + }; + + useEffect(() => { + logHomeVisit(); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + + return ( + + {children} + + ); +}; + +export const useLogger = (): LoggerContextProps => { + const context = useContext(LoggerContext); + if (!context) { + throw new Error("useLogger must be used within a LoggerProvider"); + } + return context; +};