18 lines
744 B
TypeScript
18 lines
744 B
TypeScript
"use client";
|
|
|
|
import { createContext, useCallback, useContext, type ReactNode } from "react";
|
|
import { commentRequest, type CommentLocale } from "./client";
|
|
|
|
const LocaleContext = createContext<CommentLocale>("th");
|
|
|
|
export function CommentLocaleProvider({ locale, children }: { locale: CommentLocale; children: ReactNode }) {
|
|
return <LocaleContext value={locale}>{children}</LocaleContext>;
|
|
}
|
|
|
|
export function useCommentLocale() {
|
|
const locale = useContext(LocaleContext);
|
|
const t = useCallback((thai: string, english: string) => locale === "en" ? english : thai, [locale]);
|
|
const request = useCallback(<T,>(url: string, options?: RequestInit) => commentRequest<T>(url, options, locale), [locale]);
|
|
return { locale, t, request };
|
|
}
|