Skip to content

Commit

Permalink
fix: only create a new file if parent dir exists
Browse files Browse the repository at this point in the history
This also fixes a bug where in a non-directory file, the back button
moves the user back two directories.
  • Loading branch information
oneirocosm committed Oct 5, 2024
1 parent 43861c9 commit e1dfff3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
16 changes: 16 additions & 0 deletions cmd/wsh/cmd/wshcmd-view.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package cmd

import (
"io/fs"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -56,6 +58,20 @@ func viewRun(cmd *cobra.Command, args []string) {
WriteStderr("[error] getting absolute path: %v\n", err)
return
}
absParent, err := filepath.Abs(filepath.Dir(fileArg))
if err != nil {
WriteStderr("[error] getting absolute path of parent dir: %v\n", err)
return
}
_, err = os.Stat(absParent)
if err == fs.ErrNotExist {
WriteStderr("[error] parent directory does not exist: %q\n", absParent)
return
}
if err != nil {
WriteStderr("[error] getting file info: %v\n", err)
return
}
wshCmd = &wshrpc.CommandCreateBlockData{
BlockDef: &waveobj.BlockDef{
Meta: map[string]interface{}{
Expand Down
19 changes: 18 additions & 1 deletion frontend/app/view/preview/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,12 @@ export class PreviewModel implements ViewModel {
const fileInfo = await getFn(this.statFile);
const fileName = await getFn(this.statFilePath);
const editMode = getFn(this.editMode);
const parentFileInfo = await this.getParentInfo(fileInfo);
console.log(parentFileInfo);

if (parentFileInfo?.notfound ?? false) {
return { errorStr: `Parent Directory Not Found: ${fileInfo.path}` };
}
if (fileInfo?.notfound) {
return { specializedView: "codeedit" };
}
Expand Down Expand Up @@ -492,6 +497,18 @@ export class PreviewModel implements ViewModel {
services.ObjectService.UpdateObjectMeta(blockOref, updateMeta);
}

async getParentInfo(fileInfo: FileInfo): Promise<FileInfo | undefined> {
const conn = globalStore.get(this.connection);
try {
const parentFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.path, ".."], {
route: makeConnRoute(conn),
});
return parentFileInfo;
} catch {
return undefined;
}
}

async goParentDirectory() {
const fileInfo = await globalStore.get(this.statFile);
if (fileInfo == null) {
Expand All @@ -500,7 +517,7 @@ export class PreviewModel implements ViewModel {
}
const conn = globalStore.get(this.connection);
try {
const newFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.dir, ".."], {
const newFileInfo = await RpcApi.RemoteFileJoinCommand(WindowRpcClient, [fileInfo.path, ".."], {
route: makeConnRoute(conn),
});
console.log(newFileInfo.path);
Expand Down

0 comments on commit e1dfff3

Please sign in to comment.