mancala/apps/frontend/src/util/Util.ts

33 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-09-04 01:06:07 +03:00
import { Context } from "../context/context";
import { ConnectionState } from "@mancala/core";
2022-09-04 01:06:07 +03:00
import notyf from "./Notyf";
2022-07-14 08:53:26 +03:00
export default class Util {
public static range(size: number) {
2022-09-04 01:06:07 +03:00
var ans: number[] = [];
2022-07-14 08:53:26 +03:00
for (let i = 0; i < size; i++) {
ans.push(i);
}
return ans;
}
2022-09-04 01:06:07 +03:00
public static checkConnectionAndMaybeAlert(context: Context, t: (text: string) => string): boolean {
2022-09-04 01:06:07 +03:00
if (context.rtmt.connectionState !== "connected") {
notyf.error(t("ConnectionLost"));
2022-09-04 01:06:07 +03:00
return true;
}
return false;
}
2022-09-05 08:25:12 +03:00
public static getTextByConnectionState(context: Context, connectionState: ConnectionState, t: (text: string) => string): string {
2022-09-05 08:25:12 +03:00
const map: { [key: string]: string } = {
connecting: t("Connecting"),
connected: t("Connected"),
error: t("CannotConnect"),
closed: t("ConnectingAgain"),
reconnecting: t("ConnectingAgain"),
2022-09-05 08:25:12 +03:00
};
return map[connectionState];
}
2022-07-14 08:53:26 +03:00
}