Skip to content

Commit

Permalink
Merge pull request #17 from fabriciopgl/feature/1
Browse files Browse the repository at this point in the history
✨ Adiciona logger de acesso
  • Loading branch information
fabriciopgl authored Dec 3, 2024
2 parents c090d36 + 7ed9223 commit 5403a17
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions .env.development
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion app/providers.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -31,7 +32,9 @@ const Providers: React.FC<ProvidersProps> = ({ children, themeProps }) => {
<NextThemesProvider disableTransitionOnChange {...themeProps}>
<AuthProvider>
<FavoriteProvider>
<PlacesProvider>{children}</PlacesProvider>
<PlacesProvider>
<LoggerProvider>{children}</LoggerProvider>
</PlacesProvider>
</FavoriteProvider>
</AuthProvider>
</NextThemesProvider>
Expand Down
80 changes: 80 additions & 0 deletions contexts/LogContext.tsx
Original file line number Diff line number Diff line change
@@ -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<LoggerContextProps | undefined>(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 (
<LoggerContext.Provider value={{ logHomeVisit }}>
{children}
</LoggerContext.Provider>
);
};

export const useLogger = (): LoggerContextProps => {
const context = useContext(LoggerContext);
if (!context) {
throw new Error("useLogger must be used within a LoggerProvider");
}
return context;
};

0 comments on commit 5403a17

Please sign in to comment.