Skip to content

Commit

Permalink
refactor(flat-pages): update messages on join rooms not begin (#2105)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Jan 23, 2024
1 parent a55a901 commit 4fcb5aa
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 64 deletions.
58 changes: 0 additions & 58 deletions desktop/renderer-app/src/pages/utils/join-room-handler.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useContext } from "react";
import { matchPath, useLocation } from "react-router-dom";
import { GlobalStoreContext } from "@netless/flat-pages/src/components/StoreProvider";
import { joinRoomHandler } from "@netless/flat-pages/src/utils/join-room-handler";
import { urlProtocolStore } from "../../stores/url-protocol-store";
import { joinRoomHandler } from "../../pages/utils/join-room-handler";
import { useAutoRun } from "../mobx";
import { usePushHistory, RouteNameType, RouteParams } from "../routes";
import { ClassRouteName, routeConfig } from "../../route-config";
Expand Down
1 change: 1 addition & 0 deletions packages/flat-i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@
"the-room-is-full": "The room is full, please try again later or contact the owner",
"the-room-is-expired": "The room has ended",
"the-room-is-not-started-yet": "The room has not started yet, please try again later",
"your-room-is-not-started-yet": "Your room has not started yet, please try again later",
"time-limit-tip": "This {{roomType}} room has a limit of {{minutes}} minutes",
"vip-level": {
"0": "free",
Expand Down
1 change: 1 addition & 0 deletions packages/flat-i18n/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@
"the-room-is-full": "免费版房间人数已达上限,暂时无法加入,请联系老师解决",
"the-room-is-expired": "房间已结束",
"the-room-is-not-started-yet": "房间未开始,开课前 5 分钟可进入,已将该房间添加到房间列表",
"your-room-is-not-started-yet": "房间未开始,开课前 5 分钟可进入",
"time-limit-tip": "你已加入{{roomType}} {{minutes}} 分钟限时房间",
"vip-level": {
"0": "免费版",
Expand Down
22 changes: 19 additions & 3 deletions packages/flat-pages/src/utils/join-room-handler.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { RouteNameType, usePushHistory } from "../utils/routes";
import { RouteNameType } from "../utils/routes";
import { roomStore, globalStore } from "@netless/flat-stores";
import { RequestErrorCode, RoomType } from "@netless/flat-server-api";
import { RequestErrorCode, RoomType, ServerRequestError } from "@netless/flat-server-api";
import { errorTips, message } from "flat-components";
import { FlatI18n } from "@netless/flat-i18n";

export const joinRoomHandler = async (
roomUUID: string,
pushHistory: ReturnType<typeof usePushHistory>,
// The 'pushHistory()' signature is different in electron and in web.
// Use 'any' here since we only use the same part between them.
pushHistory: (routeName: any, data?: any) => void,
): Promise<void> => {
const formatRoomUUID = roomUUID.replace(/\s+/g, "");

Expand Down Expand Up @@ -53,6 +55,20 @@ export const joinRoomHandler = async (
void message.info(FlatI18n.t("wait-for-teacher-to-enter"));
return;
}

// if room not started, show different message according to owner
if (e.errorCode === RequestErrorCode.RoomNotBegin && e.detail) {
const { ownerUUID } = e.detail as {
uuid: string;
beginTime: number;
ownerUUID: string;
};
if (globalStore.userUUID === ownerUUID) {
(e as ServerRequestError).errorMessage = "your-room-is-not-started-yet";
}
// show it in error tips
}

pushHistory(RouteNameType.HomePage);
errorTips(e);

Expand Down
4 changes: 3 additions & 1 deletion packages/flat-server-api/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ export class ServerRequestError extends Error {
public errorCode: RequestErrorCode;
public errorMessage: string;
public serverMessage?: string;
public detail?: unknown;

public constructor(errorCode: RequestErrorCode, message?: string) {
public constructor(errorCode: RequestErrorCode, message?: string, detail?: unknown) {
super(`request failed: ${errorCode} (${RequestErrorCode[errorCode]})`);
this.name = this.constructor.name;
this.errorCode = errorCode;
this.errorMessage = RequestErrorMessage[errorCode];
this.serverMessage = message;
this.detail = detail;
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/flat-server-api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type FlatServerRawResponseData<T> =
status: Status.Failed;
code: RequestErrorCode;
message?: string;
detail?: unknown;
};

export function setFlatAuthToken(token: string): void {
Expand Down Expand Up @@ -123,7 +124,7 @@ export async function requestFlatServer<TPayload, TResult>(
const data: FlatServerRawResponseData<TResult> = await response.json();

if (data.status !== Status.Success && data.status !== Status.Process) {
throw new ServerRequestError(data.code, data.message);
throw new ServerRequestError(data.code, data.message, data.detail);
}

return data;
Expand Down

0 comments on commit 4fcb5aa

Please sign in to comment.