Skip to content

Commit bb0ce5a

Browse files
committed
Merge branch '657-ambiguous-branches-create-clone' into 'master'
fix: show dataset names in the branch dropdown and use them when fetching snapshots (#657) See merge request postgres-ai/database-lab!1080
2 parents 4df2616 + 1ac1b1b commit bb0ce5a

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

ui/packages/ce/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@postgres.ai/ce",
3-
"version": "4.0.2",
3+
"version": "4.0.3",
44
"private": true,
55
"dependencies": {
66
"@craco/craco": "^6.4.3",

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

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from '@postgres.ai/shared/helpers/getEntropy'
2424

2525
import { Snapshot } from '@postgres.ai/shared/types/api/entities/snapshot'
26+
import { Branch } from '@postgres.ai/shared/types/api/endpoints/getBranches'
2627
import { useCreatedStores, MainStoreApi } from './useCreatedStores'
2728
import { useForm, FormValues } from './useForm'
2829
import { getCliCloneStatus, getCliCreateCloneCommand } from './utils'
@@ -48,9 +49,10 @@ export const CreateClone = observer((props: Props) => {
4849
const history = useHistory()
4950
const stores = useCreatedStores(props.api)
5051
const timer = useTimer()
51-
const [branchesList, setBranchesList] = useState<string[]>([])
52+
const [branchesList, setBranchesList] = useState<Branch[]>([])
5253
const [snapshots, setSnapshots] = useState([] as Snapshot[])
5354
const [isLoadingSnapshots, setIsLoadingSnapshots] = useState(false)
55+
const [selectedBranchKey, setSelectedBranchKey] = useState<string>('')
5456

5557
// Form.
5658
const onSubmit = async (values: FormValues) => {
@@ -74,8 +76,8 @@ export const CreateClone = observer((props: Props) => {
7476
}
7577
}
7678

77-
const fetchBranchSnapshotsData = async (branchName: string, initialSnapshotId?: string) => {
78-
const snapshotsRes = (await stores.main.getSnapshots(props.instanceId, branchName)) ?? []
79+
const fetchBranchSnapshotsData = async (branchName: string, dataset?: string, initialSnapshotId?: string) => {
80+
const snapshotsRes = (await stores.main.getSnapshots(props.instanceId, branchName, dataset)) ?? []
7981
setSnapshots(snapshotsRes)
8082

8183
const selectedSnapshot = snapshotsRes.find(s => s.id === initialSnapshotId) || snapshotsRes[0]
@@ -86,11 +88,15 @@ export const CreateClone = observer((props: Props) => {
8688
const handleSelectBranch = async (
8789
e: React.ChangeEvent<{ value: string }>,
8890
) => {
89-
const selectedBranch = e.target.value
90-
formik.setFieldValue('branch', selectedBranch)
91+
const compositeKey = e.target.value
92+
const [branchName, dataset] = compositeKey.split('|')
93+
94+
setSelectedBranchKey(compositeKey)
95+
formik.setFieldValue('branch', branchName)
96+
formik.setFieldValue('dataset', dataset)
9197

9298
if (props.api.getSnapshots) {
93-
await fetchBranchSnapshotsData(selectedBranch)
99+
await fetchBranchSnapshotsData(branchName, dataset)
94100
}
95101
}
96102

@@ -103,18 +109,27 @@ export const CreateClone = observer((props: Props) => {
103109

104110
const branches = (await stores.main.getBranches(props.instanceId)) ?? []
105111

106-
let initiallySelectedBranch = branches[0]?.name;
112+
let initiallySelectedBranch = branches[0];
107113

108-
if (initialBranch && branches.find((branch) => branch.name === initialBranch)) {
109-
initiallySelectedBranch = initialBranch;
114+
if (initialBranch) {
115+
const foundBranch = branches.find((branch) => branch.name === initialBranch)
116+
if (foundBranch) {
117+
initiallySelectedBranch = foundBranch
118+
}
110119
}
111120

112-
setBranchesList(branches.map((branch) => branch.name))
113-
formik.setFieldValue('branch', initiallySelectedBranch)
121+
setBranchesList(branches)
122+
formik.setFieldValue('branch', initiallySelectedBranch?.name ?? '')
123+
formik.setFieldValue('dataset', initiallySelectedBranch?.dataset ?? '')
124+
125+
if (initiallySelectedBranch) {
126+
const compositeKey = `${initiallySelectedBranch.name}|${initiallySelectedBranch.dataset}`
127+
setSelectedBranchKey(compositeKey)
128+
}
114129

115-
if (props.api.getSnapshots) {
116-
await fetchBranchSnapshotsData(initiallySelectedBranch, initialSnapshotId)
117-
} else {
130+
if (props.api.getSnapshots && initiallySelectedBranch) {
131+
await fetchBranchSnapshotsData(initiallySelectedBranch.name, initiallySelectedBranch.dataset, initialSnapshotId)
132+
} else if (!props.api.getSnapshots) {
118133
const allSnapshots = stores.main?.snapshots?.data ?? []
119134
const sortedSnapshots = allSnapshots.slice().sort(compareSnapshotsDesc)
120135
setSnapshots(sortedSnapshots)
@@ -212,15 +227,15 @@ export const CreateClone = observer((props: Props) => {
212227
<Select
213228
fullWidth
214229
label="Branch"
215-
value={formik.values.branch}
230+
value={selectedBranchKey}
216231
disabled={!branchesList || isCreatingClone}
217232
onChange={handleSelectBranch}
218233
error={Boolean(formik.errors.branch)}
219234
items={
220-
branchesList?.map((snapshot) => {
235+
branchesList?.map((branch) => {
221236
return {
222-
value: snapshot,
223-
children: snapshot,
237+
value: `${branch.name}|${branch.dataset}`,
238+
children: `${branch.name} (${branch.dataset})`,
224239
}
225240
}) ?? []
226241
}

ui/packages/shared/pages/CreateClone/stores/Main.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ export class MainStore {
102102
return response
103103
}
104104

105-
getSnapshots = async (instanceId: string, branchName?: string) => {
105+
getSnapshots = async (instanceId: string, branchName?: string, dataset?: string) => {
106106
if (!this.api.getSnapshots) return
107107
const { response, error } = await this.api.getSnapshots({
108108
instanceId,
109109
branchName,
110+
dataset,
110111
})
111112

112113
if (error) this.getSnapshotsError = await error.json().then((err) => err)

ui/packages/shared/pages/CreateClone/useForm.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Yup from 'yup'
33

44
export type FormValues = {
55
branch: string
6+
dataset: string
67
cloneId: string
78
snapshotId: string
89
dbUser: string
@@ -33,6 +34,7 @@ export const useForm = (onSubmit: (values: FormValues) => void) => {
3334
const formik = useFormik<FormValues>({
3435
initialValues: {
3536
branch: '',
37+
dataset: '',
3638
cloneId: '',
3739
snapshotId: '',
3840
dbUser: '',

0 commit comments

Comments
 (0)