61 lines
5.6 KiB
TypeScript
61 lines
5.6 KiB
TypeScript
export type CommentLocale = "th" | "en";
|
|
|
|
export const COMMENT_ERRORS: Record<string, string> = {
|
|
"sign-in-required": "กรุณาเข้าสู่ระบบเพื่อแสดงความคิดเห็น",
|
|
"too-many-requests": "ทำรายการบ่อยเกินไป กรุณารอสักครู่แล้วลองใหม่",
|
|
"comment-abusive-language": "ความคิดเห็นมีคำหยาบหรือคำดูหมิ่น กรุณาแก้ไขข้อความก่อนส่ง",
|
|
"comment-spam": "ความคิดเห็นมีลิงก์หรือข้อความซ้ำมากเกินไป กรุณาแก้ไขข้อความก่อนส่ง",
|
|
"comment-duplicate": "คุณส่งข้อความนี้แล้ว กรุณารอ 5 นาทีก่อนส่งข้อความเดิมอีกครั้ง",
|
|
"uploads-busy": "ระบบอัปโหลดกำลังใช้งานมาก กรุณาลองใหม่อีกครั้ง",
|
|
"image-too-large": "รูปภาพแต่ละรูปต้องมีขนาดไม่เกิน 10 MiB",
|
|
"body-too-large": "แนบรูปภาพได้สูงสุด 5 รูป รูปละไม่เกิน 10 MiB",
|
|
"unsupported-image-type": "รองรับรูปภาพ JPEG, PNG และ WebP ไม่รองรับวิดีโอ",
|
|
"invalid-image": "ไม่สามารถอ่านรูปภาพได้ กรุณาเลือกไฟล์รูปภาพที่ถูกต้อง",
|
|
"comment-image-upload-failed": "อัปโหลดรูปภาพไม่สำเร็จ กรุณาลองใหม่ ข้อความร่างและรูปภาพของคุณยังอยู่",
|
|
"empty-comment": "เขียนความคิดเห็นหรือแนบรูปภาพ",
|
|
"invalid-comment": "เขียนได้สูงสุด 4,000 ตัวอักษร และแนบรูปภาพได้ 5 รูป",
|
|
"comment-edited-reload": "ความคิดเห็นนี้ถูกแก้ไขแล้ว กรุณาโหลดใหม่ก่อนบันทึก ข้อความร่างของคุณยังอยู่",
|
|
"comment-unavailable": "ไม่สามารถทำรายการกับความคิดเห็นนี้ได้แล้ว",
|
|
"comment-not-editable": "ไม่สามารถแก้ไขความคิดเห็นนี้ได้แล้ว",
|
|
"comment-author-banned": "บัญชีของคุณถูกระงับการแสดงความคิดเห็น",
|
|
"comment-author-ban-not-allowed": "ไม่สามารถแบนผู้เขียนความคิดเห็นนี้ได้",
|
|
"guide-trashed": "ไกด์นี้อยู่ในถังขยะ จึงไม่สามารถแสดงความคิดเห็นได้",
|
|
};
|
|
const ENGLISH_COMMENT_ERRORS: Record<keyof typeof COMMENT_ERRORS, string> = {
|
|
"sign-in-required": "Please sign in to comment",
|
|
"too-many-requests": "Too many requests. Please wait and try again.",
|
|
"comment-abusive-language": "Please remove abusive language before posting",
|
|
"comment-spam": "Please remove excessive links or repeated text before posting",
|
|
"comment-duplicate": "You already posted this comment. Wait 5 minutes before posting it again.",
|
|
"uploads-busy": "Uploads are busy. Please try again.",
|
|
"image-too-large": "Each image must be no larger than 10 MiB",
|
|
"body-too-large": "Attach up to 5 images, each no larger than 10 MiB",
|
|
"unsupported-image-type": "JPEG, PNG, and WebP images are supported. Videos are not supported.",
|
|
"invalid-image": "Could not read this image. Please choose a valid image file.",
|
|
"comment-image-upload-failed": "Image upload failed. Please try again. Your draft and images have been kept.",
|
|
"empty-comment": "Write a comment or attach an image",
|
|
"invalid-comment": "Comments can contain up to 4,000 characters and 5 images",
|
|
"comment-edited-reload": "This comment has changed. Reload before saving. Your draft has been kept.",
|
|
"comment-unavailable": "This comment is no longer available",
|
|
"comment-not-editable": "This comment can no longer be edited",
|
|
"comment-author-banned": "Your account has been banned from commenting",
|
|
"comment-author-ban-not-allowed": "This author cannot be banned",
|
|
"guide-trashed": "This guide is in the trash and cannot receive comments",
|
|
};
|
|
|
|
export function getCommentErrors(locale: CommentLocale = "th") {
|
|
return locale === "en" ? ENGLISH_COMMENT_ERRORS : COMMENT_ERRORS;
|
|
}
|
|
|
|
export async function commentRequest<T>(url: string, options?: RequestInit, locale: CommentLocale = "th"): Promise<T> {
|
|
const response = await fetch(url, { cache: "no-store", ...options });
|
|
if (!response.ok) {
|
|
const body = await response.json().catch(() => ({}));
|
|
throw new Error(getCommentErrors(locale)[body.error] ?? (locale === "en" ? "Could not complete this action. Please try again." : "ไม่สามารถทำรายการได้ กรุณาลองใหม่"));
|
|
}
|
|
return response.status === 204 ? undefined as T : await response.json() as T;
|
|
}
|
|
export function commentSignInHref() {
|
|
return `/auth/login?next=${encodeURIComponent(window.location.pathname + window.location.search + window.location.hash)}`;
|
|
}
|