Skip to content

Commit 495d8ed

Browse files
committed
[TOOL-3757] Dashboard: Prefill last used project in contract deployment page (#6534)
1 parent 88c213b commit 495d8ed

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use client";
2+
3+
import { useEffect } from "react";
4+
5+
export const LAST_USED_PROJECT_ID = "last-used-project-id";
6+
export const LAST_USED_TEAM_ID = "last-used-team-id";
7+
8+
export function SaveLastUsedProject(props: {
9+
projectId: string;
10+
teamId: string;
11+
}) {
12+
// eslint-disable-next-line no-restricted-syntax
13+
useEffect(() => {
14+
try {
15+
localStorage.setItem(LAST_USED_PROJECT_ID, props.projectId);
16+
localStorage.setItem(LAST_USED_TEAM_ID, props.teamId);
17+
} catch {
18+
// ignore localStorage errors
19+
}
20+
}, [props.projectId, props.teamId]);
21+
22+
return null;
23+
}

apps/dashboard/src/app/team/[team_slug]/[project_slug]/layout.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { redirect } from "next/navigation";
44
import { getValidAccount } from "../../../account/settings/getAccount";
55
import { getAuthTokenWalletAddress } from "../../../api/lib/getAuthToken";
66
import { TeamHeaderLoggedIn } from "../../components/TeamHeader/team-header-logged-in.client";
7+
import { SaveLastUsedProject } from "./components/SaveLastUsedProject";
78
import { ProjectTabs } from "./tabs";
89

910
export default async function TeamLayout(props: {
@@ -61,6 +62,7 @@ export default async function TeamLayout(props: {
6162
/>
6263
</div>
6364
<div className="flex grow flex-col">{props.children}</div>
65+
<SaveLastUsedProject projectId={project.id} teamId={team.id} />
6466
</div>
6567
);
6668
}

apps/dashboard/src/components/contract-components/contract-deploy-form/custom-contract.tsx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,16 @@ import { useActiveAccount, useActiveWalletChain } from "thirdweb/react";
4444
import { upload } from "thirdweb/storage";
4545
import { isZkSyncChain } from "thirdweb/utils";
4646
import { FormHelperText, FormLabel, Text } from "tw-components";
47+
import {
48+
LAST_USED_PROJECT_ID,
49+
LAST_USED_TEAM_ID,
50+
} from "../../../app/team/[team_slug]/[project_slug]/components/SaveLastUsedProject";
4751
import { useAddContractToProject } from "../../../app/team/[team_slug]/[project_slug]/hooks/project-contracts";
4852
import { useCustomFactoryAbi, useFunctionParamsFromABI } from "../hooks";
4953
import {
5054
AddToProjectCardUI,
55+
type MinimalProject,
56+
type MinimalTeam,
5157
type MinimalTeamsAndProjects,
5258
} from "./add-to-project-card";
5359
import { Fieldset } from "./common";
@@ -151,9 +157,42 @@ export const CustomContractForm: React.FC<CustomContractFormProps> = ({
151157
const thirdwebClient = useThirdwebClient(jwt);
152158

153159
const [isImportEnabled, setIsImportEnabled] = useState(true);
154-
const [importSelection, setImportSelection] = useState({
155-
team: teamsAndProjects[0]?.team,
156-
project: teamsAndProjects[0]?.projects[0],
160+
161+
const [importSelection, setImportSelection] = useState<{
162+
team: MinimalTeam | undefined;
163+
project: MinimalProject | undefined;
164+
}>(() => {
165+
const defaultSelection = {
166+
team: teamsAndProjects[0]?.team,
167+
project: teamsAndProjects[0]?.projects[0],
168+
};
169+
170+
try {
171+
const lastUsedTeamSlug = localStorage.getItem(LAST_USED_TEAM_ID);
172+
const lastUsedProjectSlug = localStorage.getItem(LAST_USED_PROJECT_ID);
173+
174+
if (!lastUsedTeamSlug || !lastUsedProjectSlug) {
175+
return defaultSelection;
176+
}
177+
178+
const teamWithProjects = teamsAndProjects.find(
179+
(t) => t.team.id === lastUsedTeamSlug,
180+
);
181+
const project = teamWithProjects?.projects.find(
182+
(p) => p.id === lastUsedProjectSlug,
183+
);
184+
185+
if (teamWithProjects && project) {
186+
return {
187+
team: teamWithProjects.team,
188+
project,
189+
};
190+
}
191+
} catch {
192+
// ignore localStorage errors
193+
}
194+
195+
return defaultSelection;
157196
});
158197

159198
const activeAccount = useActiveAccount();

0 commit comments

Comments
 (0)