Skip to content

Commit 0f1b143

Browse files
authored
fix: content loss caused by dirty status override (#344)
1 parent 055e174 commit 0f1b143

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

ui/src/components/Sidebar.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,11 @@ function SyncStatus() {
320320
let res: string[] = [];
321321
if (state.repoLoaded) {
322322
for (const id in state.pods) {
323-
if (state.pods[id].dirty || state.pods[id].isSyncing) {
323+
if (
324+
state.pods[id].dirty ||
325+
state.pods[id].dirtyPending ||
326+
state.pods[id].isSyncing
327+
) {
324328
res.push(id);
325329
}
326330
}

ui/src/lib/store/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ export type Pod = {
2020
type: "CODE" | "SCOPE" | "RICH";
2121
content?: string;
2222
dirty?: boolean;
23+
// A temporary dirty status used during remote API syncing, so that new dirty
24+
// status is not cleared by API returns.
25+
dirtyPending?: boolean;
2326
isSyncing?: boolean;
2427
children: { id: string; type: string }[];
2528
parent: string;

ui/src/lib/store/repoStateSlice.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export const createRepoStateSlice: StateCreator<
113113
remoteUpdateAllPods: async (client) => {
114114
// The pods that haven't been inserted to the database yet
115115
const pendingPods = Object.values(get().pods).filter(
116-
(pod) => pod.dirty && pod.pending
116+
(pod) => (pod.dirty || pod.dirtyPending) && pod.pending
117117
);
118118

119119
// First insert all pending pods and ignore their relationship for now
@@ -139,11 +139,17 @@ export const createRepoStateSlice: StateCreator<
139139
if (!pod) return;
140140
pod.children?.map(({ id }) => helper(id));
141141
if (id !== "ROOT") {
142-
if (pod.dirty && !pod.isSyncing) {
142+
if ((pod.dirty || pod.dirtyPending) && !pod.isSyncing) {
143143
set(
144144
produce((state) => {
145145
// FIXME when doRemoteUpdatePod fails, this will be stuck.
146146
state.pods[id].isSyncing = true;
147+
// Transfer the dirty status from dirty to dirtyPending. This is
148+
// because pod may be updated during remote syncing, and the flag
149+
// might be cleared by a successful return, causing unsaved
150+
// content.
151+
state.pods[id].dirty = false;
152+
state.pods[id].dirtyPending = true;
147153
})
148154
);
149155
try {
@@ -156,7 +162,7 @@ export const createRepoStateSlice: StateCreator<
156162
state.pods[id].isSyncing = false;
157163
// pod may be updated during remote syncing
158164
// clear dirty flag only when remote update is successful
159-
if (res) state.pods[id].dirty = false;
165+
if (res) state.pods[id].dirtyPending = false;
160166
})
161167
);
162168
} catch (e) {

0 commit comments

Comments
 (0)