Skip to content

Commit

Permalink
fix(flat-pages): refresh rooms list on room not begin error (#2117)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious committed Feb 21, 2024
1 parent 282cef2 commit 5738311
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
16 changes: 9 additions & 7 deletions packages/flat-pages/src/HomePage/MainRoomListPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { useTranslate } from "@netless/flat-i18n";
import { RoomStore } from "@netless/flat-stores";
import { MainRoomList } from "./MainRoomList";

export type ActiveTabType = Exclude<ListRoomsType, ListRoomsType.History>;

interface MainRoomListPanelProps {
activeTab: "all" | "today" | "periodic";
setActiveTab: (activeTab: "all" | "today" | "periodic") => void;
activeTab: ActiveTabType;
setActiveTab: (activeTab: ActiveTabType) => void;
roomStore: RoomStore;
refreshRooms: () => Promise<void>;
}
Expand All @@ -20,18 +22,18 @@ export const MainRoomListPanel = observer<MainRoomListPanelProps>(function MainR
refreshRooms,
}) {
const t = useTranslate();
const filters = useMemo<Array<{ key: "all" | "today" | "periodic"; title: string }>>(
const filters = useMemo<Array<{ key: ActiveTabType; title: string }>>(
() => [
{
key: "all",
key: ListRoomsType.All,
title: t("all"),
},
{
key: "today",
key: ListRoomsType.Today,
title: t("today"),
},
{
key: "periodic",
key: ListRoomsType.Periodic,
title: t("periodic"),
},
],
Expand All @@ -46,7 +48,7 @@ export const MainRoomListPanel = observer<MainRoomListPanelProps>(function MainR
onTabActive={setActiveTab}
>
<MainRoomList
listRoomsType={activeTab as ListRoomsType}
listRoomsType={activeTab}
refreshRooms={refreshRooms}
roomStore={roomStore}
/>
Expand Down
13 changes: 10 additions & 3 deletions packages/flat-pages/src/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { observer } from "mobx-react-lite";
import { ListRoomsType } from "@netless/flat-server-api";
import { errorTips, useSafePromise } from "flat-components";
import { MainRoomMenu } from "./MainRoomMenu";
import { MainRoomListPanel } from "./MainRoomListPanel";
import { ActiveTabType, MainRoomListPanel } from "./MainRoomListPanel";
import { MainRoomHistoryPanel } from "./MainRoomHistoryPanel";
import { useLoginCheck } from "../utils/use-login-check";
import {
Expand All @@ -22,7 +22,7 @@ export const HomePage = observer(function HomePage() {
const roomStore = useContext(RoomStoreContext);
const globalStore = useContext(GlobalStoreContext);

const [activeTab, setActiveTab] = useState<"all" | "today" | "periodic">("all");
const [activeTab, setActiveTab] = useState<ActiveTabType>(ListRoomsType.All);

// eslint-disable-next-line react-hooks/exhaustive-deps
useEffect(() => pageStore.configure(), []);
Expand All @@ -33,7 +33,7 @@ export const HomePage = observer(function HomePage() {
async function refreshRooms() {
try {
await Promise.all([
sp(roomStore.listRooms(activeTab as ListRoomsType)),
sp(roomStore.listRooms(activeTab)),
sp(roomStore.listRooms(ListRoomsType.History)),
]);
} catch (e) {
Expand Down Expand Up @@ -61,6 +61,13 @@ export const HomePage = observer(function HomePage() {
};
}, [refreshRooms, isLogin]);

useEffect(() => {
if (isLogin && globalStore.requestRefreshRooms) {
void refreshRooms();
globalStore.updateRequestRefreshRooms(false);
}
}, [refreshRooms, isLogin, globalStore.requestRefreshRooms]);

return (
<div className="homepage-layout-horizontal-container">
<MainRoomMenu />
Expand Down
7 changes: 6 additions & 1 deletion packages/flat-pages/src/utils/join-room-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const joinRoomHandler = async (
}
} catch (e) {
// if room not found and is pmi room, show wait for teacher to enter
if (e.message.includes(RequestErrorCode.RoomNotFoundAndIsPmi)) {
if (e.errorCode === RequestErrorCode.RoomNotFoundAndIsPmi) {
void message.info(FlatI18n.t("wait-for-teacher-to-enter"));
return;
}
Expand All @@ -67,9 +67,14 @@ export const joinRoomHandler = async (
};
pushHistory(RouteNameType.HomePage);
globalStore.updateRoomNotBegin(roomNotBegin);
globalStore.updateRequestRefreshRooms(true);
return;
}

if (e.errorCode === RequestErrorCode.RoomNotBegin) {
globalStore.updateRequestRefreshRooms(true);
}

pushHistory(RouteNameType.HomePage);
errorTips(e);
}
Expand Down
5 changes: 5 additions & 0 deletions packages/flat-stores/src/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export class GlobalStore {
public pmiRoomList: PmiRoom[] | null = [];

public roomNotBegin: { title?: string; ownerName?: string } | null = null;
public requestRefreshRooms = false;

// login with password
public currentAccount: Account | null = null;
Expand Down Expand Up @@ -325,6 +326,10 @@ export class GlobalStore {
public updateRoomNotBegin(value: { title?: string; ownerName?: string } | null): void {
this.roomNotBegin = value;
}

public updateRequestRefreshRooms(value: boolean): void {
this.requestRefreshRooms = value;
}
}

export const globalStore = new GlobalStore();
Expand Down

0 comments on commit 5738311

Please sign in to comment.