From 63290c0144e63446c2f220f994ebe9c0ee7d3ff5 Mon Sep 17 00:00:00 2001 From: Chris Maltby Date: Thu, 12 Sep 2024 18:22:26 +0100 Subject: [PATCH] Fix issue where adding a new song wouldn't warn about unsaved changes in current song --- CHANGELOG.md | 7 +++++++ src/components/music/NavigatorSongs.tsx | 4 ++-- src/main.ts | 9 +-------- .../trackerDocument/trackerDocumentMiddleware.ts | 15 +++++++++++++-- .../trackerDocument/trackerDocumentState.ts | 5 +++++ 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0c085009..6e48de569 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed + +- Fix issue where adding a new song wouldn't warn about unsaved changes in current song +- Fix issue where adding a song with an already existing name wouldn't auto select the newly created song + ## [4.1.2] - 2024-09-09 ### Added diff --git a/src/components/music/NavigatorSongs.tsx b/src/components/music/NavigatorSongs.tsx index eb5e47b18..215ca027c 100644 --- a/src/components/music/NavigatorSongs.tsx +++ b/src/components/music/NavigatorSongs.tsx @@ -18,7 +18,7 @@ import useSplitPane from "ui/hooks/use-split-pane"; import styled from "styled-components"; import { SplitPaneVerticalDivider } from "ui/splitpane/SplitPaneDivider"; import { NoSongsMessage } from "./NoSongsMessage"; -import { addNewSongFile } from "store/features/trackerDocument/trackerDocumentState"; +import { requestAddNewSongFile } from "store/features/trackerDocument/trackerDocumentState"; import trackerActions from "store/features/tracker/trackerActions"; import API from "renderer/lib/api"; import { useAppDispatch, useAppSelector } from "store/hooks"; @@ -503,7 +503,7 @@ export const NavigatorSongs = ({ const path = assetPath("music", { filename: `${stripInvalidPathCharacters(filename)}.uge`, }); - dispatch(addNewSongFile(path)); + dispatch(requestAddNewSongFile(path)); } setAddSongMode(false); }} diff --git a/src/main.ts b/src/main.ts index e74ca5404..8118eebd6 100755 --- a/src/main.ts +++ b/src/main.ts @@ -1583,14 +1583,7 @@ ipcMain.handle( newFilename = filename.replace(/\d+$/, `${number}`); } const newPath = `${newFilename}.uge`; - await copy2(oPath, newPath); - - const data = await loadMusicData(projectRoot)(newPath); - if (!data) { - console.error(`Unable to load asset ${filename}`); - } - - return data; + return await copy2(oPath, newPath); } } catch (e) { console.error(e); diff --git a/src/store/features/trackerDocument/trackerDocumentMiddleware.ts b/src/store/features/trackerDocument/trackerDocumentMiddleware.ts index 4ce29b1b5..00ef6db63 100644 --- a/src/store/features/trackerDocument/trackerDocumentMiddleware.ts +++ b/src/store/features/trackerDocument/trackerDocumentMiddleware.ts @@ -3,7 +3,11 @@ import { RootState } from "store/configureStore"; import editorActions from "store/features/editor/editorActions"; import { musicSelectors } from "store/features/entities/entitiesState"; import navigationActions from "store/features/navigation/navigationActions"; -import { saveSongFile } from "./trackerDocumentState"; +import { + addNewSongFile, + requestAddNewSongFile, + saveSongFile, +} from "./trackerDocumentState"; import trackerDocumentActions from "./trackerDocumentActions"; import electronActions from "store/features/electron/electronActions"; import l10n from "shared/lib/lang/l10n"; @@ -18,7 +22,8 @@ const trackerMiddleware: ThunkMiddleware = (navigationActions.setSection.match(action) && action.payload !== "music") || (editorActions.setSelectedSongId.match(action) && - action.payload !== state.editor.selectedSongId) + action.payload !== state.editor.selectedSongId) || + requestAddNewSongFile.match(action) ) { if (state.trackerDocument.present.modified) { // Display confirmation and stop action if @@ -43,6 +48,12 @@ const trackerMiddleware: ThunkMiddleware = } } + // Delay creation until confirmUnsavedChangesTrackerDialog has + // had a chance to ask about unsaved changes + if (requestAddNewSongFile.match(action)) { + store.dispatch(addNewSongFile(action.payload)); + } + if ( projectActions.saveProject.pending.match(action) && state.trackerDocument.present.modified diff --git a/src/store/features/trackerDocument/trackerDocumentState.ts b/src/store/features/trackerDocument/trackerDocumentState.ts index ac65bbc7a..939465c49 100644 --- a/src/store/features/trackerDocument/trackerDocumentState.ts +++ b/src/store/features/trackerDocument/trackerDocumentState.ts @@ -4,6 +4,7 @@ import { createAsyncThunk, createSlice, PayloadAction, + createAction, } from "@reduxjs/toolkit"; import cloneDeep from "lodash/cloneDeep"; import { PatternCell } from "shared/lib/uge/song/PatternCell"; @@ -32,6 +33,10 @@ export const initialState: TrackerDocumentState = { modified: false, }; +export const requestAddNewSongFile = createAction( + "tracker/requestAddNewSong" +); + export const addNewSongFile = createAsyncThunk< { data: MusicAssetData }, string