Skip to content

Commit 0890475

Browse files
Jalilehesimkowitz
andauthored
Deleting current workspace switches to another instead of closing [ backend implementation ] (#1623)
I did not mean to close the previous pr, anyway i tried to implement what you suggested, the backend now does most of it and DeleteWorkspace will return an unclaimed id and avoid closing the window. ```go const moveToNewWorkspace = await WorkspaceService.DeleteWorkspace(workspaceId) console.log("delete-workspace done", workspaceId, ww?.waveWindowId); if (ww?.workspaceId == workspaceId){ if ( workspaceList?.length > 1 ) { await ww.switchWorkspace(moveToNewWorkspace) } else { console.log("delete-workspace closing window", workspaceId, ww?.waveWindowId); ww.destroy(); } } }); ``` ![unknown_2024 12 26-17 05](https://github.com/user-attachments/assets/9c8455e5-b71c-479d-a15c-ee5c99c7a909) ![unknown_2024 12 26-17 06](https://github.com/user-attachments/assets/5dbf63bc-1ffd-4088-abc0-7c02fac9af94) --------- Co-authored-by: Evan Simkowitz <[email protected]>
1 parent 477052e commit 0890475

File tree

5 files changed

+75
-26
lines changed

5 files changed

+75
-26
lines changed

emain/emain-window.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { delay, ensureBoundsAreVisible, waveKeyToElectronKey } from "./emain-uti
1818
import { log } from "./log";
1919
import { getElectronAppBasePath, unamePlatform } from "./platform";
2020
import { updater } from "./updater";
21+
2122
export type WindowOpts = {
2223
unamePlatform: string;
2324
};
@@ -303,7 +304,8 @@ export class WaveBrowserWindow extends BaseWindow {
303304
const workspaceList = await WorkspaceService.ListWorkspaces();
304305
if (!workspaceList?.find((wse) => wse.workspaceid === workspaceId)?.windowid) {
305306
const curWorkspace = await WorkspaceService.GetWorkspace(this.workspaceId);
306-
if (isNonEmptyUnsavedWorkspace(curWorkspace)) {
307+
308+
if (curWorkspace && isNonEmptyUnsavedWorkspace(curWorkspace)) {
307309
console.log(
308310
`existing unsaved workspace ${this.workspaceId} has content, opening workspace ${workspaceId} in new window`
309311
);
@@ -693,17 +695,22 @@ ipcMain.on("delete-workspace", (event, workspaceId) => {
693695
type: "question",
694696
buttons: ["Cancel", "Delete Workspace"],
695697
title: "Confirm",
696-
message: `Deleting workspace will also delete its contents.${workspaceHasWindow ? "\nWorkspace is open in a window, which will be closed." : ""}\n\nContinue?`,
698+
message: `Deleting workspace will also delete its contents.\n\nContinue?`,
697699
});
698700
if (choice === 0) {
699701
console.log("user cancelled workspace delete", workspaceId, ww?.waveWindowId);
700702
return;
701703
}
702-
await WorkspaceService.DeleteWorkspace(workspaceId);
704+
705+
const newWorkspaceId = await WorkspaceService.DeleteWorkspace(workspaceId);
703706
console.log("delete-workspace done", workspaceId, ww?.waveWindowId);
704707
if (ww?.workspaceId == workspaceId) {
705-
console.log("delete-workspace closing window", workspaceId, ww?.waveWindowId);
706-
ww.destroy();
708+
if (newWorkspaceId) {
709+
await ww.switchWorkspace(newWorkspaceId);
710+
} else {
711+
console.log("delete-workspace closing window", workspaceId, ww?.waveWindowId);
712+
ww.destroy();
713+
}
707714
}
708715
});
709716
});

frontend/app/store/services.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class WorkspaceServiceType {
189189
}
190190

191191
// @returns object updates
192-
DeleteWorkspace(workspaceId: string): Promise<void> {
192+
DeleteWorkspace(workspaceId: string): Promise<string> {
193193
return WOS.callBackendService("workspace", "DeleteWorkspace", Array.from(arguments))
194194
}
195195

pkg/service/workspaceservice/workspaceservice.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ func (svc *WorkspaceService) UpdateWorkspace(ctx context.Context, workspaceId st
5454
}
5555

5656
wps.Broker.Publish(wps.WaveEvent{
57-
Event: wps.Event_WorkspaceUpdate})
57+
Event: wps.Event_WorkspaceUpdate,
58+
})
5859

5960
updates := waveobj.ContextGetUpdatesRtn(ctx)
6061
go func() {
@@ -87,23 +88,26 @@ func (svc *WorkspaceService) DeleteWorkspace_Meta() tsgenmeta.MethodMeta {
8788
}
8889
}
8990

90-
func (svc *WorkspaceService) DeleteWorkspace(workspaceId string) (waveobj.UpdatesRtnType, error) {
91+
func (svc *WorkspaceService) DeleteWorkspace(workspaceId string) (waveobj.UpdatesRtnType, string, error) {
9192
ctx, cancelFn := context.WithTimeout(context.Background(), DefaultTimeout)
9293
defer cancelFn()
9394
ctx = waveobj.ContextWithUpdates(ctx)
94-
deleted, err := wcore.DeleteWorkspace(ctx, workspaceId, true)
95+
deleted, claimableWorkspace, err := wcore.DeleteWorkspace(ctx, workspaceId, true)
96+
if claimableWorkspace != "" {
97+
return nil, claimableWorkspace, nil
98+
}
9599
if err != nil {
96-
return nil, fmt.Errorf("error deleting workspace: %w", err)
100+
return nil, claimableWorkspace, fmt.Errorf("error deleting workspace: %w", err)
97101
}
98102
if !deleted {
99-
return nil, nil
103+
return nil, claimableWorkspace, nil
100104
}
101105
updates := waveobj.ContextGetUpdatesRtn(ctx)
102106
go func() {
103107
defer panichandler.PanicHandler("WorkspaceService:DeleteWorkspace:SendUpdateEvents")
104108
wps.Broker.SendUpdateEvents(updates)
105109
}()
106-
return updates, nil
110+
return updates, claimableWorkspace, nil
107111
}
108112

109113
func (svc *WorkspaceService) ListWorkspaces() (waveobj.WorkspaceList, error) {

pkg/wcore/window.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,13 @@ func SwitchWorkspace(ctx context.Context, windowId string, workspaceId string) (
5252
return nil, fmt.Errorf("error updating window: %w", err)
5353
}
5454

55-
deleted, err := DeleteWorkspace(ctx, curWsId, false)
56-
if err != nil {
57-
return nil, fmt.Errorf("error deleting current workspace: %w", err)
55+
deleted, _, err := DeleteWorkspace(ctx, curWsId, false)
56+
if err != nil && deleted {
57+
print(err.Error()) // @jalileh isolated the error for now, curwId/workspace was deleted when this occurs.
58+
} else if err != nil {
59+
return nil, fmt.Errorf("error deleting workspace: %w", err)
5860
}
61+
5962
if !deleted {
6063
log.Printf("current workspace %s was not deleted\n", curWsId)
6164
} else {
@@ -131,7 +134,7 @@ func CloseWindow(ctx context.Context, windowId string, fromElectron bool) error
131134
window, err := GetWindow(ctx, windowId)
132135
if err == nil {
133136
log.Printf("got window %s\n", windowId)
134-
deleted, err := DeleteWorkspace(ctx, window.WorkspaceId, false)
137+
deleted, _, err := DeleteWorkspace(ctx, window.WorkspaceId, false)
135138
if err != nil {
136139
log.Printf("error deleting workspace: %v\n", err)
137140
}

pkg/wcore/workspace.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ func CreateWorkspace(ctx context.Context, name string, icon string, color string
6666
}
6767

6868
wps.Broker.Publish(wps.WaveEvent{
69-
Event: wps.Event_WorkspaceUpdate})
69+
Event: wps.Event_WorkspaceUpdate,
70+
})
7071

7172
ws, _, err = UpdateWorkspace(ctx, ws.OID, name, icon, color, applyDefaults)
7273
return ws, err
@@ -114,40 +115,74 @@ func UpdateWorkspace(ctx context.Context, workspaceId string, name string, icon
114115
// If force is true, it will delete even if workspace is named.
115116
// If workspace is empty, it will be deleted, even if it is named.
116117
// Returns true if workspace was deleted, false if it was not deleted.
117-
func DeleteWorkspace(ctx context.Context, workspaceId string, force bool) (bool, error) {
118+
func DeleteWorkspace(ctx context.Context, workspaceId string, force bool) (bool, string, error) {
118119
log.Printf("DeleteWorkspace %s\n", workspaceId)
119120
workspace, err := wstore.DBMustGet[*waveobj.Workspace](ctx, workspaceId)
121+
if err != nil && wstore.ErrNotFound == err {
122+
return true, "", fmt.Errorf("workspace already deleted %w", err)
123+
}
124+
// @jalileh list needs to be saved early on i assume
125+
workspaces, err := ListWorkspaces(ctx)
126+
if err != nil {
127+
return false, "", fmt.Errorf("error retrieving workspaceList: %w", err)
128+
}
129+
120130
if err != nil {
121-
return false, fmt.Errorf("error getting workspace: %w", err)
131+
return false, "", fmt.Errorf("error getting workspace: %w", err)
122132
}
123133
if workspace.Name != "" && workspace.Icon != "" && !force && (len(workspace.TabIds) > 0 || len(workspace.PinnedTabIds) > 0) {
124134
log.Printf("Ignoring DeleteWorkspace for workspace %s as it is named\n", workspaceId)
125-
return false, nil
135+
return false, "", nil
126136
}
127137

128138
// delete all pinned and unpinned tabs
129139
for _, tabId := range append(workspace.TabIds, workspace.PinnedTabIds...) {
130140
log.Printf("deleting tab %s\n", tabId)
131141
_, err := DeleteTab(ctx, workspaceId, tabId, false)
132142
if err != nil {
133-
return false, fmt.Errorf("error closing tab: %w", err)
143+
return false, "", fmt.Errorf("error closing tab: %w", err)
134144
}
135145
}
136146
windowId, err := wstore.DBFindWindowForWorkspaceId(ctx, workspaceId)
137147
err = wstore.DBDelete(ctx, waveobj.OType_Workspace, workspaceId)
138148
if err != nil {
139-
return false, fmt.Errorf("error deleting workspace: %w", err)
149+
return false, "", fmt.Errorf("error deleting workspace: %w", err)
140150
}
141151
log.Printf("deleted workspace %s\n", workspaceId)
142152
wps.Broker.Publish(wps.WaveEvent{
143-
Event: wps.Event_WorkspaceUpdate})
153+
Event: wps.Event_WorkspaceUpdate,
154+
})
155+
144156
if windowId != "" {
145-
err = CloseWindow(ctx, windowId, false)
157+
158+
UnclaimedWorkspace, findAfter := "", false
159+
for _, ws := range workspaces {
160+
if ws.WorkspaceId == workspaceId {
161+
if UnclaimedWorkspace != "" {
162+
break
163+
}
164+
findAfter = true
165+
continue
166+
}
167+
if findAfter && ws.WindowId == "" {
168+
UnclaimedWorkspace = ws.WorkspaceId
169+
break
170+
} else if ws.WindowId == "" {
171+
UnclaimedWorkspace = ws.WorkspaceId
172+
}
173+
}
174+
175+
if UnclaimedWorkspace != "" {
176+
return true, UnclaimedWorkspace, nil
177+
} else {
178+
err = CloseWindow(ctx, windowId, false)
179+
}
180+
146181
if err != nil {
147-
return false, fmt.Errorf("error closing window: %w", err)
182+
return false, "", fmt.Errorf("error closing window: %w", err)
148183
}
149184
}
150-
return true, nil
185+
return true, "", nil
151186
}
152187

153188
func GetWorkspace(ctx context.Context, wsID string) (*waveobj.Workspace, error) {

0 commit comments

Comments
 (0)