Skip to content

Commit c194501

Browse files
authored
Fix: Not modify path on UI (#2317)
1 parent dcf440b commit c194501

File tree

11 files changed

+24
-150
lines changed

11 files changed

+24
-150
lines changed

web/client/src/context/project.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { create } from 'zustand'
22
import { ModelDirectory, type ModelFile } from '../models'
3-
import { type ModelArtifact } from '@models/artifact'
3+
import { ModelArtifact } from '@models/artifact'
44

55
export interface Tests {
66
ok: boolean
@@ -28,7 +28,6 @@ interface ProjectStore {
2828
selectedFile?: ModelFile
2929
setSelectedFile: (selectedFile?: ModelFile) => void
3030
findArtifactByPath: (path: string) => ModelArtifact | undefined
31-
findParentByPath: (path: string) => ModelDirectory | undefined
3231
refreshFiles: () => void
3332
inActiveRange: (artifact: ModelArtifact) => boolean
3433
}
@@ -82,12 +81,7 @@ export const useStoreProject = create<ProjectStore>((set, get) => ({
8281
findArtifactByPath(path) {
8382
const s = get()
8483

85-
return ModelDirectory.findArtifactByPath(s.project, path)
86-
},
87-
findParentByPath(path) {
88-
const s = get()
89-
90-
return ModelDirectory.findParentByPath(s.project, path)
84+
return ModelArtifact.findArtifactByPath(s.project, path)
9185
},
9286
refreshFiles() {
9387
const s = get()

web/client/src/library/components/documentation/Documentation.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React from 'react'
22
import { Disclosure } from '@headlessui/react'
33
import { MinusCircleIcon, PlusCircleIcon } from '@heroicons/react/24/solid'
44
import {
5-
PATH_SEPARATOR,
65
isArrayNotEmpty,
76
isFalse,
87
isString,
@@ -39,10 +38,7 @@ const Documentation = function Documentation({
3938
<ul className="w-full">
4039
<DetailsItem
4140
name="Path"
42-
value={model.path
43-
.split(PATH_SEPARATOR)
44-
.slice(0, -1)
45-
.join(PATH_SEPARATOR)}
41+
value={model.path}
4642
/>
4743
<DetailsItem
4844
name="Name"

web/client/src/library/components/fileExplorer/context.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
writeFileApiFilesPathPost,
88
} from '@api/client'
99
import { useStoreProject } from '@context/project'
10-
import { ModelArtifact } from '@models/artifact'
10+
import { type ModelArtifact } from '@models/artifact'
1111
import { ModelDirectory } from '@models/directory'
1212
import {
1313
isArrayNotEmpty,
@@ -116,7 +116,7 @@ export default function FileExplorerProvider({
116116

117117
const name = toUniqueName('new_directory')
118118

119-
writeDirectoryApiDirectoriesPathPost(`${parent.path}/${name}`, {})
119+
writeDirectoryApiDirectoriesPathPost([parent.path, name].join('/'), {})
120120
.then(created => {
121121
if (isNil(created)) return
122122

@@ -300,7 +300,7 @@ export default function FileExplorerProvider({
300300
artifact.rename(artifact.copyName())
301301
}
302302

303-
const new_path = ModelArtifact.toPath(target.path, artifact.name)
303+
const new_path = [target.path, artifact.name].join('/')
304304

305305
if (artifact instanceof ModelDirectory) {
306306
moveArtifactCallbacks.push(() => {

web/client/src/library/components/plan/help.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('getActionDisplayName', () => {
6767
const action = new ModelPlanAction({ value: EnumPlanAction.Run })
6868
const options = [EnumPlanAction.Run]
6969
const fallback = 'Start'
70-
const expected = 'Run'
70+
const expected = 'Plan'
7171

7272
const result = ModelPlanAction.getActionDisplayName(
7373
action,

web/client/src/models/artifact.posix.spec.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import { describe, test, expect, vi } from 'vitest'
1+
import { describe, test, expect } from 'vitest'
22
import { ModelArtifact } from './artifact'
33

4-
vi.mock('../utils/index', async () => {
5-
const actual: any = await vi.importActual('../utils/index')
6-
7-
return {
8-
...actual,
9-
PATH_SEPARATOR: '/',
10-
}
11-
})
12-
134
describe('Model Artifact', () => {
145
test('should create artifact with path', () => {
156
const artifact1 = new ModelArtifact({
167
name: 'test',
17-
path: '/Users/jsmith/Projects/sqlmesh/examples/sushi/',
8+
path: 'Users/jsmith/Projects/sqlmesh/examples/sushi',
189
})
1910

2011
expect(artifact1.path).toBeTruthy()

web/client/src/models/artifact.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PATH_SEPARATOR, isStringEmptyOrNil, toUniqueName } from '@utils/index'
1+
import { isStringEmptyOrNil, toUniqueName } from '@utils/index'
22
import { type ModelDirectory } from './directory'
33
import { ModelInitial } from './initial'
44

@@ -42,7 +42,7 @@ export class ModelArtifact<
4242
}
4343

4444
get path(): string {
45-
return this.toPath(this.name, this._path)
45+
return this._path
4646
}
4747

4848
get isUntitled(): boolean {
@@ -67,23 +67,18 @@ export class ModelArtifact<
6767

6868
rename(newName: string): void {
6969
if (this.isRemote) {
70-
this._path = this.toPath(newName, this._path.replace(this.name, newName))
70+
this._path = this._path.replace(this.name, newName)
7171
}
7272

7373
this._name = newName
7474
}
7575

76-
private toPath(name: string, fallback: string = ''): string {
77-
return ModelArtifact.toPath(
78-
this.withParent
79-
? `${this.parent?.path ?? ''}${PATH_SEPARATOR}${name}`
80-
: fallback,
81-
)
82-
}
83-
84-
static toPath(...paths: string[]): string {
85-
return paths
86-
.flatMap(path => path.split(PATH_SEPARATOR).filter(Boolean))
87-
.join(PATH_SEPARATOR)
76+
static findArtifactByPath(
77+
directory: ModelDirectory,
78+
path: string,
79+
): ModelArtifact | undefined {
80+
return directory.path === path
81+
? directory
82+
: directory.allArtifacts.find(artifact => artifact.path === path)
8883
}
8984
}

web/client/src/models/artifact.win.spec.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-
import { describe, test, expect, vi } from 'vitest'
1+
import { describe, test, expect } from 'vitest'
22
import { ModelArtifact } from './artifact'
33

4-
vi.mock('../utils/index', async () => {
5-
const actual: any = await vi.importActual('../utils/index')
6-
7-
return {
8-
...actual,
9-
PATH_SEPARATOR: '\\',
10-
}
11-
})
12-
134
describe('Model Artifact', () => {
145
test('should create artifact with path', () => {
156
const artifact1 = new ModelArtifact({
167
name: 'test',
17-
path: '\\Users\\jsmith\\Projects\\sqlmesh\\examples\\sushi\\',
8+
path: 'Users\\jsmith\\Projects\\sqlmesh\\examples\\sushi',
189
})
1910

2011
expect(artifact1.path).toBeTruthy()

web/client/src/models/directory.posix.spec.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { describe, test, expect, vi } from 'vitest'
1+
import { describe, test, expect } from 'vitest'
22
import { ModelDirectory } from './directory'
33

4-
vi.mock('../utils/index', async () => {
5-
const actual: any = await vi.importActual('../utils/index')
6-
7-
return {
8-
...actual,
9-
PATH_SEPARATOR: '/',
10-
}
11-
})
12-
134
describe('Model Directory', () => {
145
test('should create directory with nested atrifacts', () => {
156
const directory = new ModelDirectory(getFakePayloadDirectoryWithChildren())
@@ -38,22 +29,6 @@ describe('Model Directory', () => {
3829
expect(folder3).toBeTruthy()
3930
expect(folder3?.level).toBe(4)
4031
})
41-
42-
test('should find parent by path', () => {
43-
const directory = new ModelDirectory(getFakePayloadDirectoryWithChildren())
44-
45-
let found = ModelDirectory.findParentByPath(directory, '')
46-
47-
expect(found).toBe(directory)
48-
expect(found?.path).toBe('')
49-
expect(found?.level).toBe(0)
50-
51-
found = ModelDirectory.findParentByPath(directory, 'test/folder_1')
52-
53-
expect(found).toBe(directory.directories[0]?.directories[0])
54-
expect(found?.path).toBe('test/folder_1')
55-
expect(found?.level).toBe(2)
56-
})
5732
})
5833

5934
function getFakePayloadDirectoryWithChildren(): any {

web/client/src/models/directory.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
PATH_SEPARATOR,
3-
isFalse,
4-
isNotNil,
5-
isStringEmptyOrNil,
6-
} from '@utils/index'
1+
import { isFalse } from '@utils/index'
72
import type { Directory, File } from '../api/client'
83
import { type InitialArtifact, ModelArtifact } from './artifact'
94
import { ModelFile } from './file'
@@ -198,40 +193,4 @@ export class ModelDirectory extends ModelArtifact<InitialDirectory> {
198193
this.addFile(new ModelFile(f, this))
199194
})
200195
}
201-
202-
static findArtifactByPath(
203-
directory: ModelDirectory,
204-
path: string,
205-
): ModelArtifact | undefined {
206-
return directory.path === path
207-
? directory
208-
: directory.allArtifacts.find(artifact => artifact.path === path)
209-
}
210-
211-
static findParentByPath(
212-
directory: ModelDirectory,
213-
path: string,
214-
): ModelDirectory {
215-
const directories = directory.allDirectories
216-
const chain = path.split(PATH_SEPARATOR).reduce((acc: string[], path) => {
217-
if (acc.length === 0) return [path]
218-
219-
if (isStringEmptyOrNil(path)) return acc
220-
221-
const last = acc[acc.length - 1] ?? ''
222-
223-
acc.push(`${last}${PATH_SEPARATOR}${path}`)
224-
225-
return acc
226-
}, [])
227-
228-
while (chain.length > 0) {
229-
const path = chain.pop()
230-
const parent = directories.find(directory => directory.path === path)
231-
232-
if (isNotNil(parent)) return parent
233-
}
234-
235-
return directory
236-
}
237196
}

web/client/src/models/directory.win.spec.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { describe, test, expect, vi } from 'vitest'
1+
import { describe, test, expect } from 'vitest'
22
import { ModelDirectory } from './directory'
33

4-
vi.mock('../utils/index', async () => {
5-
const actual: any = await vi.importActual('../utils/index')
6-
7-
return {
8-
...actual,
9-
PATH_SEPARATOR: '\\',
10-
}
11-
})
12-
134
describe('Model Directory', () => {
145
test('should create directory with nested atrifacts', () => {
156
const directory = new ModelDirectory(getFakePayloadDirectoryWithChildren())
@@ -38,22 +29,6 @@ describe('Model Directory', () => {
3829
expect(folder3).toBeTruthy()
3930
expect(folder3?.level).toBe(4)
4031
})
41-
42-
test('should find parent by path', () => {
43-
const directory = new ModelDirectory(getFakePayloadDirectoryWithChildren())
44-
45-
let found = ModelDirectory.findParentByPath(directory, '')
46-
47-
expect(found).toBe(directory)
48-
expect(found?.path).toBe('')
49-
expect(found?.level).toBe(0)
50-
51-
found = ModelDirectory.findParentByPath(directory, 'test\\folder_1')
52-
53-
expect(found).toBe(directory.directories[0]?.directories[0])
54-
expect(found?.path).toBe('test\\folder_1')
55-
expect(found?.level).toBe(2)
56-
})
5732
})
5833

5934
function getFakePayloadDirectoryWithChildren(): any {

0 commit comments

Comments
 (0)