"use client";

import { useEffect, useState, memo } from "react";
import useRequest from "@/lib/hooks/useRequest";
import { ACTIVITY_LOG } from "@/core/utils/routes";

const ActivityCodeLog = memo(({ activity_code }) => {
    const requestServer = useRequest({ notificationShow: false });
    const [hasLogged, setHasLogged] = useState(false);

    useEffect(() => {
        if (!activity_code || hasLogged) return;

        const controller = new AbortController();

        const fetchActivityLog = async () => {
            try {
                await requestServer(ACTIVITY_LOG, "post", {
                    data: { activityCode: activity_code },
                    signal: controller.signal,
                });
                setHasLogged(true);
            } catch (error) {}
        };

        fetchActivityLog();

        return () => {
            controller.abort();
        };
    }, [activity_code, hasLogged, requestServer]);

    return null;
});

export default ActivityCodeLog;
