Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(web): revert functionality to remember last workspace opened #523

Merged
merged 2 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function had two functionalities.

a. Handle redirect
b. Set user id if currentUserId !== data?.me?.id

This modification looks like losing b, is it merely unnecessary code?

It might be necessary when switching accounts.

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