Skip to content

Commit 8a970a4

Browse files
lihebili-xin-yi
authored andcommitted
fix: content loss caused by dirty status override (#344)
1 parent 1c51cf4 commit 8a970a4

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
@@ -389,7 +389,11 @@ function SyncStatus() {
389389
let res: string[] = [];
390390
if (state.repoLoaded) {
391391
for (const id in state.pods) {
392-
if (state.pods[id].dirty || state.pods[id].isSyncing) {
392+
if (
393+
state.pods[id].dirty ||
394+
state.pods[id].dirtyPending ||
395+
state.pods[id].isSyncing
396+
) {
393397
res.push(id);
394398
}
395399
}

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
@@ -156,7 +156,7 @@ export const createRepoStateSlice: StateCreator<
156156
remoteUpdateAllPods: async (client) => {
157157
// The pods that haven't been inserted to the database yet
158158
const pendingPods = Object.values(get().pods).filter(
159-
(pod) => pod.dirty && pod.pending
159+
(pod) => (pod.dirty || pod.dirtyPending) && pod.pending
160160
);
161161

162162
// First insert all pending pods and ignore their relationship for now
@@ -182,11 +182,17 @@ export const createRepoStateSlice: StateCreator<
182182
if (!pod) return;
183183
pod.children?.map(({ id }) => helper(id));
184184
if (id !== "ROOT") {
185-
if (pod.dirty && !pod.isSyncing) {
185+
if ((pod.dirty || pod.dirtyPending) && !pod.isSyncing) {
186186
set(
187187
produce((state) => {
188188
// FIXME when doRemoteUpdatePod fails, this will be stuck.
189189
state.pods[id].isSyncing = true;
190+
// Transfer the dirty status from dirty to dirtyPending. This is
191+
// because pod may be updated during remote syncing, and the flag
192+
// might be cleared by a successful return, causing unsaved
193+
// content.
194+
state.pods[id].dirty = false;
195+
state.pods[id].dirtyPending = true;
190196
})
191197
);
192198
try {
@@ -199,7 +205,7 @@ export const createRepoStateSlice: StateCreator<
199205
state.pods[id].isSyncing = false;
200206
// pod may be updated during remote syncing
201207
// clear dirty flag only when remote update is successful
202-
if (res) state.pods[id].dirty = false;
208+
if (res) state.pods[id].dirtyPending = false;
203209
})
204210
);
205211
} catch (e) {

0 commit comments

Comments
 (0)