Skip to content

Commit

Permalink
Add context menu entry to move an environment file (#1065)
Browse files Browse the repository at this point in the history
Closes #1062
  • Loading branch information
255kb committed Jul 4, 2023
1 parent 0cdad3e commit 94b78f9
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 9 deletions.
4 changes: 3 additions & 1 deletion packages/desktop/src/main/libs/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ export const initIPCListeners = (mainWindow: BrowserWindow) => {
* This IPC channel must be mocked when running e2e tests
*/
ipcMain.handle('APP_SHOW_SAVE_DIALOG', async (event, options) => {
options.defaultPath = getDialogDefaultPath();
if (!options.defaultPath) {
options.defaultPath = getDialogDefaultPath();
}

if (IS_TESTING) {
return { filePath: dialogMocks.save.pop() };
Expand Down
7 changes: 7 additions & 0 deletions packages/desktop/src/renderer/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,13 @@ export class AppComponent extends Logger implements OnInit, AfterViewInit {
);
}
break;
case 'move':
if (payload.subject === 'environment') {
this.environmentsService
.moveEnvironmentFileToFolder(payload.subjectUUID)
.subscribe();
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const EnvironmentsContextMenu = (
},
label: 'Copy configuration to clipboard (JSON)',
icon: 'assignment',
separator: true,
separator: false,
disabled: false
},
{
Expand All @@ -31,9 +31,19 @@ export const EnvironmentsContextMenu = (
action: 'showInFolder',
subjectUUID: environmentUUID
},
label: 'Show in folder',
label: 'Show data file in explorer/finder',
icon: 'folder',
separator: true,
disabled: false
},
{
payload: {
subject: 'environment',
action: 'move',
subjectUUID: environmentUUID
},
label: 'Move data file to folder',
icon: 'folder_move',
separator: false,
disabled: false
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,16 @@
d="M20 6h-8l-2-2H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2zm0 12H4V8h16v10z"
></path>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
*ngSwitchCase="'folder_move'"
>
<path d="M0 0h24v24H0V0z" fill="none" />
<path
d="M20 6h-8l-2-2H4c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V8c0-1.1-.9-2-2-2zm-6 12v-3h-4v-4h4V8l5 5-5 5z"
/>
</svg>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export class SvgComponent {
| 'note_add'
| 'folder'
| 'folder_open'
| 'folder_move'
| 'history'
| 'https'
| 'info'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,11 @@ export const DesktopMessages: {
log: true,
toastType: 'success',
showToast: true
}),
ENVIRONMENT_MOVED: (messageParams) => ({
message: `Environment ${messageParams.environmentUUID} was moved to the new location.`,
log: true,
toastType: 'success',
showToast: true
})
};
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ContextMenuItemPayload = {
| 'env_settings'
| 'env_logs'
| 'showInFolder'
| 'move'
| 'copyJSON'
| 'copyFullPath'
| 'copyDatabucketID'
Expand Down
4 changes: 2 additions & 2 deletions packages/desktop/src/renderer/app/models/messages.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type MessageParams = {
error?: Error & { code?: string };
};
export type MessageCodes =
// TODO maybe remove servererrorcodes if we don't report them
| keyof typeof ServerErrorCodes
| 'INITIALIZING_APP'
| 'UNKNOWN_ERROR'
Expand Down Expand Up @@ -35,4 +34,5 @@ export type MessageCodes =
| 'STORAGE_SAVE_ERROR'
| 'MIGRATING_ENVIRONMENT'
| 'LOGIN_ERROR'
| 'LOGIN_SUCCESS';
| 'LOGIN_SUCCESS'
| 'ENVIRONMENT_MOVED';
6 changes: 4 additions & 2 deletions packages/desktop/src/renderer/app/services/dialogs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export class DialogsService {
*/
public showSaveDialog(
title: string,
saveWorkingDir = true
saveWorkingDir = true,
defaultPath?: string
): Observable<string | null> {
return from(
MainAPI.invoke('APP_SHOW_SAVE_DIALOG', {
filters: this.filters.json,
title
title,
defaultPath
})
).pipe(
// Get the directory
Expand Down
67 changes: 67 additions & 0 deletions packages/desktop/src/renderer/app/services/environments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import {
duplicateRouteToAnotherEnvironmentAction,
logRequestAction,
navigateEnvironmentsAction,
refreshEnvironmentAction,
reloadEnvironmentAction,
removeDatabucketAction,
removeEnvironmentAction,
Expand Down Expand Up @@ -1226,6 +1227,72 @@ export class EnvironmentsService extends Logger {
MainAPI.send('APP_SHOW_FILE', environmentPath);
}

/**
* Move an environment file to a folder
*
* @param environmentUUID
*/
public moveEnvironmentFileToFolder(environmentUUID: string) {
const settings = this.store.get('settings');
const environmentInfo = settings.environments.find(
(environment) => environment.uuid === environmentUUID
);

// prefill dialog with current environment folder and filename
return this.dialogsService
.showSaveDialog('Choose a folder', true, environmentInfo.path)
.pipe(
switchMap((filePath) => {
if (!filePath) {
return EMPTY;
}

if (
this.store
.get('settings')
.environments.find(
(environmentItem) => environmentItem.path === filePath
) !== undefined
) {
return throwError(() => 'ENVIRONMENT_FILE_IN_USE');
}

return zip(
of(filePath),
from(MainAPI.invoke('APP_GET_FILENAME', filePath))
);
}),
catchError((errorCode) => {
this.logMessage('error', errorCode as MessageCodes);

return EMPTY;
}),
tap(([filePath, filename]) => {
this.store.update(
updateSettingsAction({
environments: settings.environments.map((environment) => {
if (environment.uuid === environmentUUID) {
return {
uuid: environmentUUID,
path: filePath
};
}

return environment;
})
})
);

// trigger a save to update the environment file, otherwise file will be created only during next modification
this.store.update(refreshEnvironmentAction(environmentUUID));

this.logMessage('info', 'ENVIRONMENT_MOVED', {
environmentUUID
});
})
);
}

/**
* Copy an environment JSON to the clipboard
*
Expand Down
13 changes: 13 additions & 0 deletions packages/desktop/src/renderer/app/stores/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const enum ActionTypes {
REMOVE_ENVIRONMENT = 'REMOVE_ENVIRONMENT',
UPDATE_ENVIRONMENT = 'UPDATE_ENVIRONMENT',
RELOAD_ENVIRONMENT = 'RELOAD_ENVIRONMENT',
REFRESH_ENVIRONMENT = 'REFRESH_ENVIRONMENT',
UPDATE_ENVIRONMENT_STATUS = 'UPDATE_ENVIRONMENT_STATUS',
UPDATE_FILTER = 'UPDATE_FILTER',
SET_ACTIVE_ROUTE = 'SET_ACTIVE_ROUTE',
Expand Down Expand Up @@ -262,6 +263,17 @@ export const reloadEnvironmentAction = (
newEnvironment
} as const);

/**
* Trigger a save of an environment without modyfing its state
*
* @param environmentUUID
*/
export const refreshEnvironmentAction = (environmentUUID: string) =>
({
type: ActionTypes.REFRESH_ENVIRONMENT,
environmentUUID
} as const);

/**
* Update an environment status
*
Expand Down Expand Up @@ -645,6 +657,7 @@ export type Actions =
| ReturnType<typeof removeEnvironmentAction>
| ReturnType<typeof updateEnvironmentAction>
| ReturnType<typeof reloadEnvironmentAction>
| ReturnType<typeof refreshEnvironmentAction>
| ReturnType<typeof updateEnvironmentStatusAction>
| ReturnType<typeof updateFilterAction>
| ReturnType<typeof setActiveRouteAction>
Expand Down
16 changes: 16 additions & 0 deletions packages/desktop/src/renderer/app/stores/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,22 @@ export const environmentReducer = (
break;
}

case ActionTypes.REFRESH_ENVIRONMENT: {
const environments = state.environments.map((environment) => {
if (environment.uuid === action.environmentUUID) {
return { ...environment };
}

return environment;
});

newState = {
...state,
environments
};
break;
}

case ActionTypes.UPDATE_ENVIRONMENT_STATUS: {
const newEnvironmentsStatus: EnvironmentsStatuses = {
...state.environmentsStatus
Expand Down
3 changes: 2 additions & 1 deletion packages/desktop/test/libs/context-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export enum ContextMenuEnvironmentActions {
DUPLICATE = 1,
COPY_JSON = 2,
SHOW_FOLDER = 3,
CLOSE = 4
MOVE_FOLDER = 4,
CLOSE = 5
}

export enum ContextMenuDatabucketActions {
Expand Down

0 comments on commit 94b78f9

Please sign in to comment.