Skip to content

Commit 94dfb3b

Browse files
author
Bogdan Tsechoev
committed
Merge branch 'prefill-data' into 'dle-4-0'
fix(UI): Pre-filling branch and snapshot when creating a clone from a branch or snapshot See merge request postgres-ai/database-lab!1017
2 parents 76b2db0 + 8406a58 commit 94dfb3b

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

ui/packages/ce/src/App/Instance/Branches/Branch/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const Branch = () => {
4949
branches: () => ROUTES.INSTANCE.BRANCHES.BRANCHES.path,
5050
snapshot: (snapshotId: string) =>
5151
ROUTES.INSTANCE.SNAPSHOTS.SNAPSHOT.createPath(snapshotId),
52-
createClone: () => ROUTES.INSTANCE.CLONES.CREATE.path,
52+
createClone: (branchId: string) => ROUTES.INSTANCE.CLONES.CREATE.createPath(branchId),
5353
}}
5454
/>
5555
</PageContainer>

ui/packages/ce/src/App/Instance/Snapshots/Snapshot/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const Snapshot = () => {
5050
ROUTES.INSTANCE.BRANCHES.BRANCH.createPath(branchName),
5151
clone: (cloneId: string) =>
5252
ROUTES.INSTANCE.CLONES.CLONE.createPath(cloneId),
53-
createClone: () => ROUTES.INSTANCE.CLONES.CREATE.path,
53+
createClone: (branchId: string, snapshotId: string) => ROUTES.INSTANCE.CLONES.CREATE.createPath(branchId, snapshotId),
5454
}}
5555
api={api}
5656
elements={elements}

ui/packages/ce/src/config/routes.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ export const ROUTES = {
7373
CREATE: {
7474
name: 'Create clone',
7575
path: `/instance/clones/create`,
76+
createPath: (branchId?: string, snapshotId?: string) => {
77+
const params = new URLSearchParams();
78+
79+
if (branchId) params.set('branch_id', branchId);
80+
if (snapshotId) params.set('snapshot_id', snapshotId);
81+
82+
const query = params.toString();
83+
return `/instance/clones/create${query ? `?${query}` : ''}`;
84+
}
7685
},
7786

7887
CLONES: {

ui/packages/shared/pages/Branches/Branch/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type Host = {
1010
branch: () => string
1111
branches: () => string
1212
snapshot: (snapshotId: string) => string
13-
createClone: () => string
13+
createClone: (branchId: string) => string
1414
}
1515
api: Api
1616
elements: {

ui/packages/shared/pages/Branches/Branch/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export const BranchesPage = observer((props: Props) => {
217217
<Button
218218
variant="contained"
219219
color="primary"
220-
onClick={() => history.push(props.routes.createClone())}
220+
onClick={() => history.push(props.routes.createClone(props.branchId))}
221221
disabled={isReloading}
222222
title={'Create clone'}
223223
className={classes.actionButton}

ui/packages/shared/pages/CreateClone/index.tsx

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ export const CreateClone = observer((props: Props) => {
7373
}
7474
}
7575

76-
const fetchBranchSnapshotsData = async (branchName: string) => {
77-
const snapshotsRes =
78-
(await stores.main.getSnapshots(props.instanceId, branchName)) ?? []
76+
const fetchBranchSnapshotsData = async (branchName: string, initialSnapshotId?: string) => {
77+
const snapshotsRes = (await stores.main.getSnapshots(props.instanceId, branchName)) ?? []
7978
setSnapshots(snapshotsRes)
80-
formik.setFieldValue('snapshotId', snapshotsRes[0]?.id)
79+
80+
const selectedSnapshot = snapshotsRes.find(s => s.id === initialSnapshotId) || snapshotsRes[0]
81+
82+
formik.setFieldValue('snapshotId', selectedSnapshot?.id)
8183
}
8284

8385
const handleSelectBranch = async (
@@ -93,24 +95,30 @@ export const CreateClone = observer((props: Props) => {
9395

9496
const formik = useForm(onSubmit)
9597

96-
const fetchData = async () => {
98+
const fetchData = async (initialBranch?: string, initialSnapshotId?: string) => {
9799
try {
98100
setIsLoadingSnapshots(true)
99101
await stores.main.load(props.instanceId)
100102

101103
const branches = (await stores.main.getBranches(props.instanceId)) ?? []
102-
const initiallySelectedBranch = branches[0]?.name
104+
105+
let initiallySelectedBranch = branches[0]?.name;
106+
107+
if (initialBranch && branches.find((branch) => branch.name === initialBranch)) {
108+
initiallySelectedBranch = initialBranch;
109+
}
110+
103111
setBranchesList(branches.map((branch) => branch.name))
104112
formik.setFieldValue('branch', initiallySelectedBranch)
105113

106114
if (props.api.getSnapshots) {
107-
await fetchBranchSnapshotsData(initiallySelectedBranch)
115+
await fetchBranchSnapshotsData(initiallySelectedBranch, initialSnapshotId)
108116
} else {
109117
const allSnapshots = stores.main?.snapshots?.data ?? []
110118
const sortedSnapshots = allSnapshots.slice().sort(compareSnapshotsDesc)
111119
setSnapshots(sortedSnapshots)
112-
const [firstSnapshot] = allSnapshots ?? []
113-
formik.setFieldValue('snapshotId', firstSnapshot?.id)
120+
let selectedSnapshot = allSnapshots.find(s => s.id === initialSnapshotId) || allSnapshots[0]
121+
formik.setFieldValue('snapshotId', selectedSnapshot?.id)
114122
}
115123
} catch (error) {
116124
console.error('Error fetching data:', error)
@@ -121,8 +129,13 @@ export const CreateClone = observer((props: Props) => {
121129

122130
// Initial loading data.
123131
useEffect(() => {
124-
fetchData()
125-
}, [])
132+
const queryString = history.location.search.split('?')[1]
133+
const params = new URLSearchParams(queryString)
134+
const branchId = params.get('branch_id') ?? undefined
135+
const snapshotId = params.get('snapshot_id') ?? undefined
136+
137+
fetchData(branchId, snapshotId)
138+
}, [history.location.search, formik.initialValues])
126139

127140
// Redirect when clone is created and stable.
128141
useEffect(() => {

ui/packages/shared/pages/Snapshots/Snapshot/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type Host = {
1111
snapshots: () => string
1212
branch: (branchName: string) => string
1313
clone: (cloneId: string) => string
14-
createClone: () => string
14+
createClone: (branchId: string, snapshotId: string) => string
1515
}
1616
api: Api
1717
elements: {

ui/packages/shared/pages/Snapshots/Snapshot/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export const SnapshotPage = observer((props: Props) => {
208208
<Button
209209
variant="contained"
210210
color="primary"
211-
onClick={() => history.push(props.routes.createClone())}
211+
onClick={() => history.push(props.routes.createClone(snapshot?.branch as string, snapshot?.id as string))}
212212
title={'Create clone'}
213213
className={classes.actionButton}
214214
>

0 commit comments

Comments
 (0)