Implement typesafe-i18n

This commit is contained in:
Alexis Faizeau 2022-01-21 17:06:03 +01:00
parent 0be77164ec
commit 446b4639c7
97 changed files with 1162 additions and 1341 deletions

View file

@ -3,6 +3,7 @@ import Timeout = NodeJS.Timeout;
import { userIsAdminStore } from "./GameStore";
import { CONTACT_URL } from "../Enum/EnvironmentVariable";
import { analyticsClient } from "../Administration/AnalyticsClient";
import type { Translation } from "../i18n/i18n-types";
export const menuIconVisiblilityStore = writable(false);
export const menuVisiblilityStore = writable(false);
@ -32,37 +33,95 @@ function createWarningContainerStore() {
export const warningContainerStore = createWarningContainerStore();
export enum SubMenusInterface {
settings = "Settings",
profile = "Profile",
invite = "Invite",
aboutRoom = "Credit",
globalMessages = "Global Messages",
contact = "Contact",
settings = "settings",
profile = "profile",
invite = "invite",
aboutRoom = "credit",
globalMessages = "globalMessages",
contact = "contact",
}
type MenuKeys = keyof Translation["menu"]["sub"];
interface TranslatedMenu {
type: "translated";
key: MenuKeys;
}
/**
* A menu item from the scripting API
*/
interface ScriptingMenu {
type: "scripting";
label: string;
}
export type MenuItem = TranslatedMenu | ScriptingMenu;
function createSubMenusStore() {
const { subscribe, update } = writable<string[]>([
SubMenusInterface.profile,
SubMenusInterface.globalMessages,
SubMenusInterface.contact,
SubMenusInterface.settings,
SubMenusInterface.invite,
SubMenusInterface.aboutRoom,
const { subscribe, update } = writable<MenuItem[]>([
{
type: "translated",
key: SubMenusInterface.profile,
},
{
type: "translated",
key: SubMenusInterface.globalMessages,
},
{
type: "translated",
key: SubMenusInterface.contact,
},
{
type: "translated",
key: SubMenusInterface.settings,
},
{
type: "translated",
key: SubMenusInterface.invite,
},
{
type: "translated",
key: SubMenusInterface.aboutRoom,
},
]);
return {
subscribe,
addMenu(menuCommand: string) {
update((menuList: string[]) => {
if (!menuList.find((menu) => menu === menuCommand)) {
menuList.push(menuCommand);
addTranslatedMenu(menuCommand: MenuKeys) {
update((menuList) => {
if (!menuList.find((menu) => menu.type === "translated" && menu.key === menuCommand)) {
menuList.push({
type: "translated",
key: menuCommand,
});
}
return menuList;
});
},
removeMenu(menuCommand: string) {
update((menuList: string[]) => {
const index = menuList.findIndex((menu) => menu === menuCommand);
removeTranslatedMenu(menuCommand: MenuKeys) {
update((menuList) => {
const index = menuList.findIndex((menu) => menu.type === "translated" && menu.key === menuCommand);
if (index !== -1) {
menuList.splice(index, 1);
}
return menuList;
});
},
addScriptingMenu(menuCommand: string) {
update((menuList) => {
if (!menuList.find((menu) => menu.type === "scripting" && menu.label === menuCommand)) {
menuList.push({
type: "scripting",
label: menuCommand,
});
}
return menuList;
});
},
removeScriptingMenu(menuCommand: string) {
update((menuList) => {
const index = menuList.findIndex((menu) => menu.type === "scripting" && menu.label === menuCommand);
if (index !== -1) {
menuList.splice(index, 1);
}
@ -77,15 +136,15 @@ export const subMenusStore = createSubMenusStore();
export const contactPageStore = writable<string | undefined>(CONTACT_URL);
export function checkSubMenuToShow() {
subMenusStore.removeMenu(SubMenusInterface.globalMessages);
subMenusStore.removeMenu(SubMenusInterface.contact);
subMenusStore.removeTranslatedMenu(SubMenusInterface.globalMessages);
subMenusStore.removeTranslatedMenu(SubMenusInterface.contact);
if (get(userIsAdminStore)) {
subMenusStore.addMenu(SubMenusInterface.globalMessages);
subMenusStore.addTranslatedMenu(SubMenusInterface.globalMessages);
}
if (get(contactPageStore) !== undefined) {
subMenusStore.addMenu(SubMenusInterface.contact);
subMenusStore.addTranslatedMenu(SubMenusInterface.contact);
}
}
@ -97,12 +156,12 @@ export function handleMenuRegistrationEvent(
source: string | undefined = undefined,
options: { allowApi: boolean }
) {
if (get(subMenusStore).includes(menuName)) {
if (get(subMenusStore).find((item) => item.type === "scripting" && item.label === menuName)) {
console.warn("The menu " + menuName + " already exist.");
return;
}
subMenusStore.addMenu(menuName);
subMenusStore.addScriptingMenu(menuName);
if (iframeUrl !== undefined) {
const url = new URL(iframeUrl, source);
@ -111,12 +170,6 @@ export function handleMenuRegistrationEvent(
}
export function handleMenuUnregisterEvent(menuName: string) {
const subMenuGeneral: string[] = Object.values(SubMenusInterface);
if (subMenuGeneral.includes(menuName)) {
console.warn("The menu " + menuName + " is a mandatory menu. It can't be remove");
return;
}
subMenusStore.removeMenu(menuName);
subMenusStore.removeScriptingMenu(menuName);
customMenuIframe.delete(menuName);
}