"use client";
import { updateUser } from "(app)/settings/actions";
import { Changelog } from "@repo/db";
import { ChangelogModalBtn } from "@repo/shared-components";
import { useSession } from "next-auth/react";
import toast from "react-hot-toast";

export const ChangelogWrapper = ({ latestChangelog }: { latestChangelog: Changelog | null }) => {
	const { data: session } = useSession();
	const autoOpen = !session?.user.changelogAck && !!latestChangelog;

	if (!latestChangelog) return null;

	return (
		<ChangelogModalBtn
			latestChangelog={latestChangelog}
			autoOpen={autoOpen}
			onClose={async () => {
				await updateUser({ changelogAck: true });
				if (!session?.user.changelogAck) {
					toast.success("Changelog als gelesen markiert");
				}
			}}
		/>
	);
};
