Skip to content

Commit b62a7cd

Browse files
committed
Fix edit button showing Add sheet on first click
sheet(isPresented:) captures closure values at build time, not at presentation time. The first click sets editingSnippetID but the sheet evaluates the old nil value, showing "Add" instead of "Edit". Switch to sheet(item:) with an Identifiable wrapper that carries the snippet and isEditing flag, guaranteeing the sheet is created fresh with the correct data each time. Made-with: Cursor
1 parent 85b1a59 commit b62a7cd

2 files changed

Lines changed: 34 additions & 38 deletions

File tree

voxtral_realtime/macos/VoxtralRealtime/Views/ReplacementManagementView.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
import SwiftUI
1010

11+
private struct ReplacementEditorItem: Identifiable {
12+
let id = UUID()
13+
let entry: ReplacementEntry
14+
let isEditing: Bool
15+
}
16+
1117
struct ReplacementManagementView: View {
1218
@Environment(ReplacementStore.self) private var replacementStore
1319
@State private var searchText = ""
14-
@State private var editingEntry = ReplacementEntry()
15-
@State private var editingEntryID: UUID?
16-
@State private var isPresentingEditor = false
20+
@State private var editorItem: ReplacementEditorItem?
1721

1822
var body: some View {
1923
VStack(alignment: .leading, spacing: 12) {
2024
HStack {
2125
TextField("Search terms", text: $searchText)
2226
.textFieldStyle(.roundedBorder)
2327
Button("Add") {
24-
editingEntry = ReplacementEntry()
25-
editingEntryID = nil
26-
isPresentingEditor = true
28+
editorItem = ReplacementEditorItem(entry: ReplacementEntry(), isEditing: false)
2729
}
2830
.buttonStyle(.borderedProminent)
2931
}
@@ -54,17 +56,13 @@ struct ReplacementManagementView: View {
5456
}
5557
Spacer()
5658
Button("Edit") {
57-
editingEntry = entry
58-
editingEntryID = entry.id
59-
isPresentingEditor = true
59+
editorItem = ReplacementEditorItem(entry: entry, isEditing: true)
6060
}
6161
.buttonStyle(.borderless)
6262
}
6363
.contextMenu {
6464
Button("Edit") {
65-
editingEntry = entry
66-
editingEntryID = entry.id
67-
isPresentingEditor = true
65+
editorItem = ReplacementEditorItem(entry: entry, isEditing: true)
6866
}
6967
Button(entry.isEnabled ? "Disable" : "Enable") {
7068
replacementStore.toggleEnabled(for: entry.id)
@@ -79,16 +77,16 @@ struct ReplacementManagementView: View {
7977
.listStyle(.inset)
8078
}
8179
}
82-
.sheet(isPresented: $isPresentingEditor) {
83-
ReplacementEntryEditor(entry: editingEntry, isEditing: editingEntryID != nil) { entry in
84-
if editingEntryID == nil {
85-
replacementStore.add(entry)
86-
} else {
80+
.sheet(item: $editorItem) { item in
81+
ReplacementEntryEditor(entry: item.entry, isEditing: item.isEditing) { entry in
82+
if item.isEditing {
8783
replacementStore.update(entry)
84+
} else {
85+
replacementStore.add(entry)
8886
}
89-
isPresentingEditor = false
87+
editorItem = nil
9088
} onCancel: {
91-
isPresentingEditor = false
89+
editorItem = nil
9290
}
9391
.frame(width: 420)
9492
}

voxtral_realtime/macos/VoxtralRealtime/Views/SnippetManagementView.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,24 @@
88

99
import SwiftUI
1010

11+
private struct SnippetEditorItem: Identifiable {
12+
let id = UUID()
13+
let snippet: Snippet
14+
let isEditing: Bool
15+
}
16+
1117
struct SnippetManagementView: View {
1218
@Environment(SnippetStore.self) private var snippetStore
1319
@State private var searchText = ""
14-
@State private var editingSnippet = Snippet()
15-
@State private var editingSnippetID: UUID?
16-
@State private var isPresentingEditor = false
20+
@State private var editorItem: SnippetEditorItem?
1721

1822
var body: some View {
1923
VStack(alignment: .leading, spacing: 12) {
2024
HStack {
2125
TextField("Search snippets", text: $searchText)
2226
.textFieldStyle(.roundedBorder)
2327
Button("Add") {
24-
editingSnippet = Snippet()
25-
editingSnippetID = nil
26-
isPresentingEditor = true
28+
editorItem = SnippetEditorItem(snippet: Snippet(), isEditing: false)
2729
}
2830
.buttonStyle(.borderedProminent)
2931
}
@@ -65,17 +67,13 @@ struct SnippetManagementView: View {
6567
}
6668
Spacer()
6769
Button("Edit") {
68-
editingSnippet = snippet
69-
editingSnippetID = snippet.id
70-
isPresentingEditor = true
70+
editorItem = SnippetEditorItem(snippet: snippet, isEditing: true)
7171
}
7272
.buttonStyle(.borderless)
7373
}
7474
.contextMenu {
7575
Button("Edit") {
76-
editingSnippet = snippet
77-
editingSnippetID = snippet.id
78-
isPresentingEditor = true
76+
editorItem = SnippetEditorItem(snippet: snippet, isEditing: true)
7977
}
8078
Button(snippet.isEnabled ? "Disable" : "Enable") {
8179
snippetStore.toggleEnabled(for: snippet.id)
@@ -90,16 +88,16 @@ struct SnippetManagementView: View {
9088
.listStyle(.inset)
9189
}
9290
}
93-
.sheet(isPresented: $isPresentingEditor) {
94-
SnippetEditor(snippet: editingSnippet, isEditing: editingSnippetID != nil) { snippet in
95-
if editingSnippetID == nil {
96-
snippetStore.add(snippet)
97-
} else {
91+
.sheet(item: $editorItem) { item in
92+
SnippetEditor(snippet: item.snippet, isEditing: item.isEditing) { snippet in
93+
if item.isEditing {
9894
snippetStore.update(snippet)
95+
} else {
96+
snippetStore.add(snippet)
9997
}
100-
isPresentingEditor = false
98+
editorItem = nil
10199
} onCancel: {
102-
isPresentingEditor = false
100+
editorItem = nil
103101
}
104102
.frame(width: 480, height: 460)
105103
}

0 commit comments

Comments
 (0)