Skip to content

Commit 000eeba

Browse files
committed
refactor(dashboard/menu): fix issues with team selection persistence
- Creating a new team should change the persisted team to the new created team - Visiting a team via URL should change the persisted team to the one in the URL - Deleting a team should remove the persisted team
1 parent 27c70e9 commit 000eeba

12 files changed

+92
-67
lines changed

components/dashboard/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function App() {
174174
const hash = getURLHash();
175175
const isRoot = window.location.pathname === "/" && hash === "";
176176
if (isRoot) {
177-
const teamSlug = getCurrentTeam(window.location, teams)?.slug;
177+
const teamSlug = getCurrentTeam(teams)?.slug;
178178
if (teamSlug) {
179179
history.push(`/t/${teamSlug}`);
180180
}

components/dashboard/src/Menu.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { countries } from "countries-list";
1313
import gitpodIcon from "./icons/gitpod.svg";
1414
import { getGitpodService, gitpodHostUrl } from "./service/service";
1515
import { UserContext } from "./user-context";
16-
import { TeamsContext, getCurrentTeam, setCurrentTeam } from "./teams/teams-context";
16+
import { TeamsContext, useCurrentTeam } from "./teams/teams-context";
1717
import getSettingsMenu from "./settings/settings-menu";
1818
import { adminMenu } from "./admin/admin-menu";
1919
import ContextMenu from "./components/ContextMenu";
@@ -38,7 +38,8 @@ export default function Menu() {
3838
const { user } = useContext(UserContext);
3939
const { teams } = useContext(TeamsContext);
4040
const location = useLocation();
41-
const team = getCurrentTeam(location, teams);
41+
const { team, setStoredTeamSlug } = useCurrentTeam();
42+
4243
const {
4344
showPaymentUI,
4445
setShowPaymentUI,
@@ -299,7 +300,7 @@ export default function Menu() {
299300
active: !team,
300301
separator: true,
301302
link: "/projects",
302-
onClick: () => setCurrentTeam(),
303+
onClick: () => setStoredTeamSlug(),
303304
},
304305
...(teams || [])
305306
.map((t) => ({
@@ -321,7 +322,7 @@ export default function Menu() {
321322
active: team && team.id === t.id,
322323
separator: true,
323324
link: `/t/${t.slug}`,
324-
onClick: () => setCurrentTeam(t),
325+
onClick: () => setStoredTeamSlug(t.slug),
325326
}))
326327
.sort((a, b) => (a.title.toLowerCase() > b.title.toLowerCase() ? 1 : -1)),
327328
{

components/dashboard/src/projects/Prebuild.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77
import moment from "moment";
88
import { PrebuildWithStatus } from "@gitpod/gitpod-protocol";
99
import { useContext, useEffect, useState } from "react";
10-
import { useHistory, useLocation, useRouteMatch } from "react-router";
10+
import { useHistory, useRouteMatch } from "react-router";
1111
import Header from "../components/Header";
1212
import PrebuildLogs from "../components/PrebuildLogs";
1313
import Spinner from "../icons/Spinner.svg";
1414
import { getGitpodService, gitpodHostUrl } from "../service/service";
15-
import { TeamsContext, getCurrentTeam } from "../teams/teams-context";
15+
import { TeamsContext, useCurrentTeam } from "../teams/teams-context";
1616
import { PrebuildStatus } from "./Prebuilds";
1717
import { shortCommitMessage } from "./render-utils";
1818

1919
export default function () {
2020
const history = useHistory();
21-
const location = useLocation();
2221

2322
const { teams } = useContext(TeamsContext);
24-
const team = getCurrentTeam(location, teams);
23+
const { team } = useCurrentTeam();
2524

2625
const match = useRouteMatch<{ team: string; project: string; prebuildId: string }>(
2726
"/(t/)?:team/:project/:prebuildId",

components/dashboard/src/projects/Prebuilds.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import moment from "moment";
88
import { PrebuildWithStatus, PrebuiltWorkspaceState, Project } from "@gitpod/gitpod-protocol";
99
import { useContext, useEffect, useState } from "react";
10-
import { useLocation, useRouteMatch } from "react-router";
10+
import { useRouteMatch } from "react-router";
1111
import Header from "../components/Header";
1212
import DropDown, { DropDownEntry } from "../components/DropDown";
1313
import { ItemsList, Item, ItemField } from "../components/ItemsList";
@@ -18,16 +18,14 @@ import StatusCanceled from "../icons/StatusCanceled.svg";
1818
import StatusPaused from "../icons/StatusPaused.svg";
1919
import StatusRunning from "../icons/StatusRunning.svg";
2020
import { getGitpodService } from "../service/service";
21-
import { TeamsContext, getCurrentTeam } from "../teams/teams-context";
21+
import { TeamsContext, useCurrentTeam } from "../teams/teams-context";
2222
import { shortCommitMessage } from "./render-utils";
2323
import { Link } from "react-router-dom";
2424
import { Disposable } from "vscode-jsonrpc";
2525

2626
export default function (props: { project?: Project; isAdminDashboard?: boolean }) {
27-
const location = useLocation();
28-
2927
const { teams } = useContext(TeamsContext);
30-
const team = getCurrentTeam(location, teams);
28+
const { team } = useCurrentTeam();
3129

3230
const match = useRouteMatch<{ team: string; resource: string }>("/(t/)?:team/:resource");
3331
const projectSlug = props.isAdminDashboard ? props.project?.slug : match?.params?.resource;

components/dashboard/src/projects/Project.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
import moment from "moment";
88
import { PrebuildWithStatus, Project } from "@gitpod/gitpod-protocol";
99
import { useContext, useEffect, useState } from "react";
10-
import { useHistory, useLocation, useRouteMatch } from "react-router";
10+
import { useHistory, useRouteMatch } from "react-router";
1111
import Header from "../components/Header";
1212
import { ItemsList, Item, ItemField, ItemFieldContextMenu } from "../components/ItemsList";
1313
import { getGitpodService, gitpodHostUrl } from "../service/service";
14-
import { TeamsContext, getCurrentTeam } from "../teams/teams-context";
14+
import { TeamsContext, useCurrentTeam } from "../teams/teams-context";
1515
import { prebuildStatusIcon, prebuildStatusLabel } from "./Prebuilds";
1616
import { shortCommitMessage, toRemoteURL } from "./render-utils";
1717
import Spinner from "../icons/Spinner.svg";
@@ -20,11 +20,10 @@ import { ErrorCodes } from "@gitpod/gitpod-protocol/lib/messaging/error";
2020
import { openAuthorizeWindow } from "../provider-utils";
2121

2222
export default function () {
23-
const location = useLocation();
2423
const history = useHistory();
2524

2625
const { teams } = useContext(TeamsContext);
27-
const team = getCurrentTeam(location, teams);
26+
const { team } = useCurrentTeam();
2827

2928
const match = useRouteMatch<{ team: string; resource: string }>("/(t/)?:team/:resource");
3029
const projectSlug = match?.params?.resource;

components/dashboard/src/projects/ProjectSettings.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
*/
66

77
import { useContext, useEffect, useState } from "react";
8-
import { useLocation } from "react-router";
98
import { Project, Team } from "@gitpod/gitpod-protocol";
109
import CheckBox from "../components/CheckBox";
1110
import { getGitpodService } from "../service/service";
12-
import { getCurrentTeam, TeamsContext } from "../teams/teams-context";
11+
import { useCurrentTeam } from "../teams/teams-context";
1312
import { PageWithSubMenu } from "../components/PageWithSubMenu";
1413
import PillLabel from "../components/PillLabel";
1514
import { ProjectContext } from "./project-context";
@@ -33,9 +32,7 @@ export function getProjectSettingsMenu(project?: Project, team?: Team) {
3332
}
3433

3534
export function ProjectSettingsPage(props: { project?: Project; children?: React.ReactNode }) {
36-
const location = useLocation();
37-
const { teams } = useContext(TeamsContext);
38-
const team = getCurrentTeam(location, teams);
35+
const { team } = useCurrentTeam();
3936

4037
return (
4138
<PageWithSubMenu

components/dashboard/src/projects/Projects.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import { Link } from "react-router-dom";
99
import Header from "../components/Header";
1010
import projectsEmpty from "../images/projects-empty.svg";
1111
import projectsEmptyDark from "../images/projects-empty-dark.svg";
12-
import { useHistory, useLocation } from "react-router";
12+
import { useHistory } from "react-router";
1313
import { useContext, useEffect, useState } from "react";
1414
import { getGitpodService } from "../service/service";
15-
import { getCurrentTeam, TeamsContext } from "../teams/teams-context";
15+
import { useCurrentTeam, TeamsContext } from "../teams/teams-context";
1616
import { ThemeContext } from "../theme-context";
1717
import { PrebuildWithStatus, Project } from "@gitpod/gitpod-protocol";
1818
import { toRemoteURL } from "./render-utils";
@@ -21,11 +21,10 @@ import ConfirmationModal from "../components/ConfirmationModal";
2121
import { prebuildStatusIcon } from "./Prebuilds";
2222

2323
export default function () {
24-
const location = useLocation();
2524
const history = useHistory();
2625

2726
const { teams } = useContext(TeamsContext);
28-
const team = getCurrentTeam(location, teams);
27+
const { team } = useCurrentTeam();
2928
const [projects, setProjects] = useState<Project[]>([]);
3029
const [lastPrebuilds, setLastPrebuilds] = useState<Map<string, PrebuildWithStatus>>(new Map());
3130

components/dashboard/src/teams/Members.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import { TeamMemberInfo, TeamMemberRole, TeamMembershipInvite } from "@gitpod/gitpod-protocol";
88
import moment from "moment";
99
import { useContext, useEffect, useState } from "react";
10-
import { useHistory, useLocation } from "react-router";
10+
import { useHistory } from "react-router";
1111
import Header from "../components/Header";
1212
import DropDown from "../components/DropDown";
1313
import { ItemsList, Item, ItemField, ItemFieldContextMenu } from "../components/ItemsList";
@@ -16,15 +16,14 @@ import Tooltip from "../components/Tooltip";
1616
import copy from "../images/copy.svg";
1717
import { getGitpodService } from "../service/service";
1818
import { UserContext } from "../user-context";
19-
import { TeamsContext, getCurrentTeam } from "./teams-context";
19+
import { TeamsContext, useCurrentTeam } from "./teams-context";
2020
import { trackEvent } from "../Analytics";
2121

2222
export default function () {
2323
const { user } = useContext(UserContext);
24-
const { teams, setTeams } = useContext(TeamsContext);
24+
const { setTeams } = useContext(TeamsContext);
2525
const history = useHistory();
26-
const location = useLocation();
27-
const team = getCurrentTeam(location, teams);
26+
const { team } = useCurrentTeam();
2827
const [members, setMembers] = useState<TeamMemberInfo[]>([]);
2928
const [genericInvite, setGenericInvite] = useState<TeamMembershipInvite>();
3029
const [showInviteModal, setShowInviteModal] = useState<boolean>(false);

components/dashboard/src/teams/TeamBilling.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import React, { useContext, useEffect, useState } from "react";
8-
import { useLocation } from "react-router";
98
import { TeamMemberInfo } from "@gitpod/gitpod-protocol";
109
import { Currency, Plan, Plans, PlanType } from "@gitpod/gitpod-protocol/lib/plans";
1110
import { TeamSubscription2 } from "@gitpod/gitpod-protocol/lib/team-subscription-protocol";
@@ -19,16 +18,14 @@ import { ReactComponent as CheckSvg } from "../images/check.svg";
1918
import { ReactComponent as Spinner } from "../icons/Spinner.svg";
2019
import { PaymentContext } from "../payment-context";
2120
import { getGitpodService } from "../service/service";
22-
import { getCurrentTeam, TeamsContext } from "./teams-context";
21+
import { useCurrentTeam } from "./teams-context";
2322
import { getTeamSettingsMenu } from "./TeamSettings";
2423
import TeamUsageBasedBilling from "./TeamUsageBasedBilling";
2524

2625
type PendingPlan = Plan & { pendingSince: number };
2726

2827
export default function TeamBilling() {
29-
const { teams } = useContext(TeamsContext);
30-
const location = useLocation();
31-
const team = getCurrentTeam(location, teams);
28+
const { team } = useCurrentTeam();
3229
const [members, setMembers] = useState<TeamMemberInfo[]>([]);
3330
const [teamSubscription, setTeamSubscription] = useState<TeamSubscription2 | undefined>();
3431
const { showPaymentUI, currency, setCurrency } = useContext(PaymentContext);

components/dashboard/src/teams/TeamSettings.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
import { Team } from "@gitpod/gitpod-protocol";
88
import { useContext, useEffect, useState } from "react";
9-
import { Redirect, useLocation } from "react-router";
9+
import { Redirect } from "react-router";
1010
import CodeText from "../components/CodeText";
1111
import ConfirmationModal from "../components/ConfirmationModal";
1212
import { PageWithSubMenu } from "../components/PageWithSubMenu";
1313
import { PaymentContext } from "../payment-context";
1414
import { getGitpodService, gitpodHostUrl } from "../service/service";
1515
import { UserContext } from "../user-context";
16-
import { getCurrentTeam, TeamsContext } from "./teams-context";
16+
import { useCurrentTeam } from "./teams-context";
1717

1818
export function getTeamSettingsMenu(params: { team?: Team; showPaymentUI?: boolean }) {
1919
const { team, showPaymentUI } = params;
@@ -37,10 +37,8 @@ export default function TeamSettings() {
3737
const [modal, setModal] = useState(false);
3838
const [teamSlug, setTeamSlug] = useState("");
3939
const [isUserOwner, setIsUserOwner] = useState(true);
40-
const { teams } = useContext(TeamsContext);
4140
const { user } = useContext(UserContext);
42-
const location = useLocation();
43-
const team = getCurrentTeam(location, teams);
41+
const { team, setStoredTeamSlug } = useCurrentTeam();
4442
const { showPaymentUI } = useContext(PaymentContext);
4543

4644
const close = () => setModal(false);
@@ -52,7 +50,7 @@ export default function TeamSettings() {
5250
const currentUserInTeam = members.find((member) => member.userId === user?.id);
5351
setIsUserOwner(currentUserInTeam?.role === "owner");
5452
})();
55-
}, []);
53+
}, [team, user?.id]);
5654

5755
if (!isUserOwner) {
5856
return <Redirect to="/" />;
@@ -63,6 +61,7 @@ export default function TeamSettings() {
6361
}
6462
await getGitpodService().server.deleteTeam(team.id, user.id);
6563
document.location.href = gitpodHostUrl.asDashboard().toString();
64+
setStoredTeamSlug();
6665
};
6766

6867
return (

components/dashboard/src/teams/TeamUsageBasedBilling.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, { useContext, useEffect, useState } from "react";
88
import { useLocation } from "react-router";
99
import { Appearance, loadStripe, Stripe } from "@stripe/stripe-js";
1010
import { Elements, PaymentElement, useElements, useStripe } from "@stripe/react-stripe-js";
11-
import { getCurrentTeam, TeamsContext } from "./teams-context";
11+
import { useCurrentTeam } from "./teams-context";
1212
import Modal from "../components/Modal";
1313
import { ReactComponent as Spinner } from "../icons/Spinner.svg";
1414
import { PaymentContext } from "../payment-context";
@@ -18,9 +18,8 @@ import { ThemeContext } from "../theme-context";
1818
type PendingStripeCustomer = { pendingSince: number };
1919

2020
export default function TeamUsageBasedBilling() {
21-
const { teams } = useContext(TeamsContext);
2221
const location = useLocation();
23-
const team = getCurrentTeam(location, teams);
22+
const { team } = useCurrentTeam();
2423
const { showUsageBasedUI } = useContext(PaymentContext);
2524
const [stripeCustomerId, setStripeCustomerId] = useState<string | undefined>();
2625
const [isLoading, setIsLoading] = useState<boolean>(true);

0 commit comments

Comments
 (0)