Skip to content

Commit

Permalink
fix(web): revert functionality to remember last workspace opened (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
KaWaite authored Jun 28, 2023
1 parent 4b79e9d commit 05e32d1
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 321 deletions.
25 changes: 6 additions & 19 deletions web/src/beta/features/Navbar/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useGetProjectBySceneQuery,
useGetTeamsQuery,
} from "@reearth/services/gql";
import { useProject, useSessionWorkspace, useWorkspace } from "@reearth/services/state";
import { useProject, useWorkspace } from "@reearth/services/state";

type User = {
name: string;
Expand All @@ -16,9 +16,8 @@ type User = {
export default (sceneId: string) => {
const { logout: handleLogout } = useAuth();

const [currentWorkspace, setWorkspace] = useSessionWorkspace();
const [currentWorkspace, setCurrentWorkspace] = useWorkspace();
const [currentProject, setProject] = useProject();
const [lastWorkspace, setLastWorkspace] = useWorkspace();

const [workspaceModalVisible, setWorkspaceModalVisible] = useState(false);

Expand Down Expand Up @@ -58,16 +57,6 @@ export default (sceneId: string) => {
const handleWorkspaceModalOpen = useCallback(() => setWorkspaceModalVisible(true), []);
const handleWorkspaceModalClose = useCallback(() => setWorkspaceModalVisible(false), []);

useEffect(() => {
if (!currentWorkspace && lastWorkspace && workspaceId == lastWorkspace.id)
setWorkspace(lastWorkspace);
else {
const workspace = workspaces?.find(workspace => workspace.id === workspaceId);
setWorkspace(workspace);
setLastWorkspace(workspace);
}
}, [currentWorkspace, lastWorkspace, setLastWorkspace, setWorkspace, workspaceId, workspaces]);

useEffect(() => {
setProject(p =>
p?.id !== project?.id
Expand All @@ -87,13 +76,12 @@ export default (sceneId: string) => {
(workspaceId: string) => {
const workspace = workspaces?.find(team => team.id === workspaceId);
if (workspace && workspaceId !== currentWorkspace?.id) {
setWorkspace(workspace);
setLastWorkspace(currentWorkspace);
setCurrentWorkspace(workspace);

navigate(`/dashboard/${workspaceId}`);
}
},
[workspaces, currentWorkspace, setWorkspace, setLastWorkspace, navigate],
[workspaces, currentWorkspace, setCurrentWorkspace, navigate],
);

const [createTeamMutation] = useCreateTeamMutation();
Expand All @@ -105,13 +93,12 @@ export default (sceneId: string) => {
refetchQueries: ["GetTeams"],
});
if (results.data?.createTeam) {
setWorkspace(results.data.createTeam.team);
setLastWorkspace(results.data.createTeam.team);
setCurrentWorkspace(results.data.createTeam.team);

navigate(`/dashboard/${results.data.createTeam.team.id}`);
}
},
[createTeamMutation, setWorkspace, setLastWorkspace, navigate],
[createTeamMutation, setCurrentWorkspace, navigate],
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { useNavigate } from "react-router-dom";
import { useAuth, useCleanUrl } from "@reearth/services/auth";
import { useGetTeamsQuery } from "@reearth/services/gql";
import { useT } from "@reearth/services/i18n";
import {
useWorkspace,
useNotification,
useUserId,
useSessionWorkspace,
} from "@reearth/services/state";
import { useWorkspace, useNotification, useUserId } from "@reearth/services/state";

export type Mode = "layer" | "widget";

Expand All @@ -19,54 +14,20 @@ export default () => {
const navigate = useNavigate();
const { isAuthenticated, isLoading, error: authError, login, logout } = useAuth();
const [error, isErrorChecked] = useCleanUrl();
const [currentWorkspace, setWorkspace] = useSessionWorkspace();
const [currentWorkspace, setCurrentWorkspace] = useWorkspace();
const [currentUserId, setCurrentUserId] = useUserId();
const [, setNotification] = useNotification();
const [lastWorkspace, setLastWorkspace] = useWorkspace();

const { data, loading } = useGetTeamsQuery({ skip: !isAuthenticated });

if (isAuthenticated && !currentUserId) {
setCurrentUserId(data?.me?.id);
}
useEffect(() => {
if (!currentWorkspace && lastWorkspace) setWorkspace(lastWorkspace);
}, [currentWorkspace, lastWorkspace, setWorkspace]);

const workspaceId = useMemo(() => {
return currentWorkspace?.id || data?.me?.myTeam.id;
}, [currentWorkspace?.id, data?.me?.myTeam.id]);

const handleRedirect = useCallback(() => {
if (currentUserId === data?.me?.id) {
setWorkspace(
workspaceId
? data?.me?.teams.find(t => t.id === workspaceId) ?? data?.me?.myTeam
: undefined,
);
setLastWorkspace(currentWorkspace);

navigate(`/dashboard/${workspaceId}`);
} else {
setCurrentUserId(data?.me?.id);
setWorkspace(data?.me?.myTeam);
setLastWorkspace(currentWorkspace);

navigate(`/dashboard/${data?.me?.myTeam.id}`);
}
}, [
currentUserId,
data?.me?.id,
data?.me?.teams,
data?.me?.myTeam,
setWorkspace,
workspaceId,
setLastWorkspace,
currentWorkspace,
navigate,
setCurrentUserId,
]);

const verifySignup = useCallback(
async (token: string) => {
const res = await axios.post(
Expand Down Expand Up @@ -103,21 +64,22 @@ export default () => {
} else if (!isAuthenticated && !isLoading) {
login();
} else {
if (!data || !workspaceId) return;
handleRedirect();
if (currentWorkspace || !data || !workspaceId) return;
setCurrentWorkspace(data.me?.myTeam);
navigate(`/dashboard/${workspaceId}`);
}
}, [
isAuthenticated,
login,
isLoading,
verifySignup,
navigate,
currentWorkspace,
handleRedirect,
data,
isErrorChecked,
error,
workspaceId,
login,
verifySignup,
navigate,
setCurrentWorkspace,
]);

useEffect(() => {
Expand Down
39 changes: 5 additions & 34 deletions web/src/classic/components/organisms/Dashboard/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import {
useProject,
useUnselectProject,
useNotification,
useSessionWorkspace,
} from "@reearth/services/state";
import { ProjectType } from "@reearth/types";

Expand All @@ -29,12 +28,10 @@ export type ProjectNodes = NonNullable<GetProjectsQuery["projects"]["nodes"][num
const projectsPerPage = 9;

export default (workspaceId?: string) => {
const [currentWorkspace, setCurrentWorkspace] = useSessionWorkspace();

const [currentProject] = useProject();
const unselectProject = useUnselectProject();
const [, setNotification] = useNotification();
const [lastWorkspace, setLastWorkspace] = useWorkspace();
const [currentWorkspace, setCurrentWorkspace] = useWorkspace();

const { data, refetch } = useGetMeQuery();
const [modalShown, setModalShown] = useState(false);
Expand All @@ -60,40 +57,23 @@ export default (workspaceId?: string) => {
const gqlCache = useApolloClient().cache;

useEffect(() => {
if (!currentWorkspace && lastWorkspace && lastWorkspace.id === workspaceId)
setCurrentWorkspace(lastWorkspace);
else if (workspace) {
if (workspace?.id && workspace.id !== currentWorkspace?.id) {
setCurrentWorkspace({
personal,
...workspace,
});
setLastWorkspace({
personal,
...workspace,
});
}
}, [
currentWorkspace,
workspace,
setCurrentWorkspace,
personal,
setLastWorkspace,
lastWorkspace,
workspaceId,
workspaces,
]);
}, [currentWorkspace, workspace, setCurrentWorkspace, personal]);

const handleWorkspaceChange = useCallback(
(workspaceId: string) => {
const workspace = workspaces?.find(workspace => workspace.id === workspaceId);
if (workspace) {
setCurrentWorkspace(workspace);
setLastWorkspace(workspace);

navigate(`/dashboard/${workspaceId}`);
}
},
[workspaces, setCurrentWorkspace, setLastWorkspace, navigate],
[workspaces, setCurrentWorkspace, navigate],
);

const [createTeamMutation] = useCreateTeamMutation();
Expand All @@ -109,20 +89,11 @@ export default (workspaceId?: string) => {
text: t("Successfully created workspace!"),
});
setCurrentWorkspace(results.data.createTeam.team);
setLastWorkspace(results.data.createTeam.team);
navigate(`/dashboard/${results.data.createTeam.team.id}`);
}
refetch();
},
[
createTeamMutation,
refetch,
setNotification,
t,
setCurrentWorkspace,
setLastWorkspace,
navigate,
],
[createTeamMutation, setCurrentWorkspace, refetch, navigate, t, setNotification],
);

useEffect(() => {
Expand Down
41 changes: 10 additions & 31 deletions web/src/classic/components/organisms/EarthEditor/Header/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ import {
useCreateTeamMutation,
} from "@reearth/services/gql";
import { useT } from "@reearth/services/i18n";
import {
useSceneId,
useWorkspace,
useProject,
useNotification,
useSessionWorkspace,
} from "@reearth/services/state";
import { useSceneId, useWorkspace, useProject, useNotification } from "@reearth/services/state";

export default () => {
const url = window.REEARTH_CONFIG?.published?.split("{}");
Expand All @@ -29,9 +23,8 @@ export default () => {

const [, setNotification] = useNotification();
const [sceneId] = useSceneId();
const [currentWorkspace, setWorkspace] = useSessionWorkspace();
const [currentProject, setProject] = useProject();
const [lastWorkspace, setLastWorkspace] = useWorkspace();
const [currentWorkspace, setCurrentWorkspace] = useWorkspace();

const navigate = useNavigate();

Expand Down Expand Up @@ -82,16 +75,6 @@ export default () => {
[checkProjectAliasQuery, project],
);

useEffect(() => {
if (!currentWorkspace && lastWorkspace && workspaceId == lastWorkspace.id)
setWorkspace(lastWorkspace);
else {
const workspace = workspaces?.find(workspace => workspace.id === workspaceId);
setWorkspace(workspace);
setLastWorkspace(workspace);
}
}, [currentWorkspace, lastWorkspace, setLastWorkspace, setWorkspace, workspaceId, workspaces]);

useEffect(() => {
setValidAlias(
!validatingAlias &&
Expand Down Expand Up @@ -171,16 +154,14 @@ export default () => {
);

const handleTeamChange = useCallback(
(workspaceId: string) => {
const workspace = workspaces?.find(workspace => workspace.id === workspaceId);
if (workspace && workspaceId !== currentWorkspace?.id) {
setWorkspace(workspace);
setLastWorkspace(currentWorkspace);

navigate(`/dashboard/${workspaceId}`);
(id: string) => {
const workspace = workspaces?.find(workspace => workspace.id === id);
if (workspace && id !== currentWorkspace?.id) {
setCurrentWorkspace(workspace);
navigate(`/dashboard/${id}`);
}
},
[workspaces, currentWorkspace, setWorkspace, setLastWorkspace, navigate],
[currentWorkspace?.id, setCurrentWorkspace, workspaces, navigate],
);

const [createTeamMutation] = useCreateTeamMutation();
Expand All @@ -191,13 +172,11 @@ export default () => {
refetchQueries: ["GetTeams"],
});
if (results.data?.createTeam) {
setWorkspace(results.data.createTeam.team);
setLastWorkspace(results.data.createTeam.team);

setCurrentWorkspace(results.data.createTeam.team);
navigate(`/dashboard/${results.data.createTeam.team.id}`);
}
},
[createTeamMutation, setWorkspace, setLastWorkspace, navigate],
[createTeamMutation, setCurrentWorkspace, navigate],
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
useSelectedBlock,
useWidgetAlignEditorActivated,
useSelectedWidgetArea,
useSessionWorkspace,
} from "@reearth/services/state";

import useQueries, { Mode as RawMode } from "./hooks-queries";
Expand Down Expand Up @@ -71,8 +70,7 @@ export default (mode: Mode) => {
const [isCapturing, onIsCapturingChange] = useIsCapturing();
const [camera, setCamera] = useCamera();
const [sceneMode] = useSceneMode();
const [workspace] = useSessionWorkspace();
const [lastWorkspace] = useWorkspace();
const [workspace] = useWorkspace();

const [sceneId] = useSceneId();
const [widgetAlignEditorActivated, setWidgetAlignEditorActivated] =
Expand Down Expand Up @@ -367,7 +365,7 @@ export default (mode: Mode) => {

return {
pane,
workspaceId: workspace?.id ?? lastWorkspace?.id,
workspaceId: workspace?.id,
error,
loading,
isLayerGroup,
Expand Down
12 changes: 3 additions & 9 deletions web/src/classic/components/organisms/Settings/Account/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ import { useCallback } from "react";

import { useUpdateMeMutation, useGetProfileQuery, Theme as GQLTheme } from "@reearth/services/gql";
import { useT } from "@reearth/services/i18n";
import {
useWorkspace,
useProject,
useNotification,
useSessionWorkspace,
} from "@reearth/services/state";
import { useWorkspace, useProject, useNotification } from "@reearth/services/state";

const enumTypeMapper: Partial<Record<GQLTheme, string>> = {
[GQLTheme.Default]: "default",
Expand All @@ -27,9 +22,8 @@ export default () => {
const t = useT();
const [, setNotification] = useNotification();
const client = useApolloClient();
const [currentWorkspace] = useSessionWorkspace();
const [currentWorkspace] = useWorkspace();
const [currentProject] = useProject();
const [lastWorkspace] = useWorkspace();

const { data: profileData } = useGetProfileQuery();
const me = profileData?.me;
Expand Down Expand Up @@ -101,7 +95,7 @@ export default () => {
);

return {
currentWorkspace: currentWorkspace ?? lastWorkspace,
currentWorkspace,
currentProject,
me,
hasPassword,
Expand Down
Loading

0 comments on commit 05e32d1

Please sign in to comment.