"use client"; import { createContext, useContext, useState } from "react"; import { useTranslations } from "next-intl"; import type { GetResourcePolicyResponse } from "@server/routers/policy"; interface ResourcePolicyProviderProps { children: React.ReactNode; policy: GetResourcePolicyResponse; } export function ResourcePolicyProvider({ children, policy: serverPolicy }: ResourcePolicyProviderProps) { const [policy, setPolicy] = useState(serverPolicy); const t = useTranslations(); const updatePolicy = ( updatedPolicy: Partial ) => { if (!policy) { throw new Error(t("resourceErrorNoUpdate")); } setPolicy((prev) => { if (!prev) { return prev; } return { ...prev, ...updatedPolicy }; }); }; return ( {children} ); } export type ResourcePolicyContextType = { policy: GetResourcePolicyResponse; updatePolicy: (updatedPolicy: Partial) => void; }; export const ResourcePolicyContext = createContext< ResourcePolicyContextType | undefined >(undefined); export function useResourcePolicyContext() { const context = useContext(ResourcePolicyContext); if (context === undefined) { throw new Error( "useResourcePolicyContext must be used within a ResourcePolicyProvider" ); } return context; }