Skip to content

Commit

Permalink
EDITOR-#333 switch between editor and command (#334)
Browse files Browse the repository at this point in the history
* add a button

* remove button

* delete mode

* del mode

* fixed routes

* fix logo missing at /editor/command

* extract routing logic to a hook

---------

Co-authored-by: madmaxieee <[email protected]>
  • Loading branch information
seanfu56 and madmaxieee authored Feb 6, 2023
1 parent 94704f1 commit e84fdfe
Show file tree
Hide file tree
Showing 11 changed files with 3,031 additions and 231 deletions.
2,840 changes: 2,840 additions & 0 deletions editor-server/yarn.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions editor/components/ControlBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import WaveSurferApp from "../Wavesurfer/WaveSurferApp";
import VolumeSlider from "./VolumeSlider";
import ScaleSlider from "./ScaleSlider";
import FadeSwitch from "./FadeSwitch";
import useMode from "@/hooks/useMode";

import useRoute from "@/hooks/useRoute";

function ControlBar({ wavesurfer }: { wavesurfer: WaveSurferApp }) {
const mode = useMode();
const { page } = useRoute();

return (
<>
<PlayBackController wavesurfer={wavesurfer} />
<TimeController />
{mode === "editor" && <FadeSwitch />}
{page === "EDITOR" && <FadeSwitch />}
<VolumeSlider wavesurfer={wavesurfer} />
<ScaleSlider wavesurfer={wavesurfer} />
</>
Expand Down
34 changes: 26 additions & 8 deletions editor/components/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { useState } from "react";
import { AppBar, Toolbar, Box, Stack } from "@mui/material";

import AppBar from "@mui/material/AppBar";
import Box from "@mui/material/Box";
import Button from "@mui/material/Button";
import Toolbar from "@mui/material/Toolbar";
import Stack from "@mui/material/Stack";

import { Settings } from "../Settings";
import Tools from "components/Tools";
Expand All @@ -11,15 +16,13 @@ import StateIndicator from "./StateIndicator";

import { reactiveState } from "core/state";
import { useReactiveVar } from "@apollo/client";
import useMode from "@/hooks/useMode";

/**
* Top Bar, include title, timeController, upload/download btn
*/
import useRoute from "@/hooks/useRoute";

export default function Header() {
const [showSettings, setShowSettings] = useState<boolean>(false);
const editMode = useReactiveVar(reactiveState.editMode);
const mode = useMode();
const { page, navigate } = useRoute();

return (
<Stack direction="column">
Expand All @@ -28,11 +31,26 @@ export default function Header() {
<Toolbar style={{ minHeight: "6vh", width: "100%" }}>
<Box sx={{ height: "6vh", p: "1vh 1vw", mr: "3vw" }}>
<img
src="LDlogoWhite.png"
src="/LDlogoWhite.png"
alt="NTUEE Light Dance logo"
style={{ height: "100%" }}
/>
</Box>
<Button
variant="outlined"
onClick={() => {
if (page === "EDITOR") {
navigate.toCommandCenter();
} else if (page === "COMMAND_CENTER") {
navigate.toEditor();
} else {
navigate.toLogin();
}
window.location.reload();
}}
>
{page == "EDITOR" ? "command" : "editor"}
</Button>
<Box
sx={{
display: "flex",
Expand All @@ -41,7 +59,7 @@ export default function Header() {
gap: "1vw",
}}
>
{mode === "editor" && (
{page === "EDITOR" && (
<>
<EditButtons />
<EditorSelector />
Expand Down
4 changes: 3 additions & 1 deletion editor/components/RequireAuth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { useReactiveVar } from "@apollo/client";

import { reactiveState } from "@/core/state";

import { ROUTES } from "@/constants";

export default function RequireAuth({ children }: { children: JSX.Element }) {
const isLoggedIn = useReactiveVar(reactiveState.isLoggedIn);
const location = useLocation();

if (!isLoggedIn) {
return <Navigate to="/login" state={{ from: location }} replace />;
return <Navigate to={ROUTES.LOGIN} state={{ from: location }} replace />;
}

return children;
Expand Down
8 changes: 4 additions & 4 deletions editor/components/Settings/Preference/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { Box } from "@mui/material";

import { layoutMode } from "types/layout";
import MarkerSwitch from "./MarkerSwitch";
import { PreferenceSelector } from "./PreferenceSelector";
// import { PreferenceSelector } from "./PreferenceSelector";

function Preference() {
const [mode, setMode] = useState<layoutMode>("editor");
// const [mode, setMode] = useState<layoutMode>("editor");

return (
<Box sx={{ display: "flex", flexDirection: "column", gap: "1em" }}>
<PreferenceSelector
{/* <PreferenceSelector
label="mode"
value={mode}
Options={["editor", "command"]}
onChange={(e) => {
setMode(e.target.value as layoutMode);
}}
/>
/> */}
<MarkerSwitch />
</Box>
);
Expand Down
7 changes: 7 additions & 0 deletions editor/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ export const COMMANDS = {
RESTARTCONTROLLER,
NTHU_STOP,
};

export const ROUTES = {
ROOT: "/",
EDITOR: "/editor",
COMMAND_CENTER: "/editor/command",
LOGIN: "/login",
};
45 changes: 45 additions & 0 deletions editor/hooks/useRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { useCallback, useMemo } from "react";

import { useNavigate, useLocation } from "react-router-dom";

import { ROUTES } from "@/constants";

export type PageName = keyof typeof ROUTES | "UNKNOWN";

export default function useRoute() {
const location = useLocation();
const navigate = useNavigate();

const page: PageName = useMemo(() => {
const path = location.pathname;
if (path === ROUTES.EDITOR) {
return "EDITOR";
} else if (path === ROUTES.COMMAND_CENTER) {
return "COMMAND_CENTER";
} else if (path === ROUTES.LOGIN) {
return "LOGIN";
} else if (path === ROUTES.ROOT) {
return "ROOT";
} else {
return "UNKNOWN";
}
}, [location.pathname]);

const toEditor = useCallback(() => {
navigate(ROUTES.EDITOR);
}, [navigate]);

const toCommandCenter = useCallback(() => {
navigate(ROUTES.COMMAND_CENTER);
}, [navigate]);

const toLogin = useCallback(() => {
navigate(ROUTES.LOGIN);
}, [navigate]);

return {
location,
page,
navigate: { toEditor, toCommandCenter, toLogin },
};
}
4 changes: 3 additions & 1 deletion editor/pages/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import Typography from "@mui/material/Typography";
import { reactiveState } from "@/core/state";
import { login, checkToken } from "@/core/actions";

import { ROUTES } from "@/constants";

export default function LogIn() {
const isLoggedIn = useReactiveVar(reactiveState.isLoggedIn);
const location = useLocation();
Expand All @@ -24,7 +26,7 @@ export default function LogIn() {

if (isLoggedIn) {
const from = location.state?.from;
return <Navigate to={from?.pathname || "/"} />;
return <Navigate to={from?.pathname || ROUTES.ROOT} />;
}

const handleSubmit = (event: React.FormEvent<HTMLFormElement>) => {
Expand Down
14 changes: 10 additions & 4 deletions editor/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import RequireAuth from "@/components/RequireAuth";
import Login from "./Login";
import NotFound from "./NotFound";

import { ROUTES } from "@/constants";

const theme = createTheme({ palette: { mode: "dark" } });

const EditorWrapper = lazy(async () => await import("./App"));
Expand All @@ -26,11 +28,15 @@ export default function RootRouter() {
<Route
index
element={
<Navigate to="/editor" replace={true} state={{ from: "/" }} />
<Navigate
to={ROUTES.EDITOR}
replace={true}
state={{ from: ROUTES.ROOT }}
/>
}
/>
<Route
path="editor"
path={ROUTES.EDITOR}
element={
<RequireAuth>
<Suspense fallback={<Loading />}>
Expand All @@ -50,7 +56,7 @@ export default function RootRouter() {
}
/>
<Route
path="command"
path={ROUTES.COMMAND_CENTER}
element={
<RequireAuth>
<Suspense fallback={<Loading />}>
Expand All @@ -61,7 +67,7 @@ export default function RootRouter() {
/>
</Route>
<Route
path="login"
path={ROUTES.LOGIN}
element={
<Suspense fallback={<Loading />}>
<Login />
Expand Down
Loading

0 comments on commit e84fdfe

Please sign in to comment.