Skip to content

Commit

Permalink
fix: content loss caused by dirty status override (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
lihebi authored Jun 28, 2023
1 parent 055e174 commit 0f1b143
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
6 changes: 5 additions & 1 deletion ui/src/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ function SyncStatus() {
let res: string[] = [];
if (state.repoLoaded) {
for (const id in state.pods) {
if (state.pods[id].dirty || state.pods[id].isSyncing) {
if (
state.pods[id].dirty ||
state.pods[id].dirtyPending ||
state.pods[id].isSyncing
) {
res.push(id);
}
}
Expand Down
3 changes: 3 additions & 0 deletions ui/src/lib/store/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export type Pod = {
type: "CODE" | "SCOPE" | "RICH";
content?: string;
dirty?: boolean;
// A temporary dirty status used during remote API syncing, so that new dirty
// status is not cleared by API returns.
dirtyPending?: boolean;
isSyncing?: boolean;
children: { id: string; type: string }[];
parent: string;
Expand Down
12 changes: 9 additions & 3 deletions ui/src/lib/store/repoStateSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export const createRepoStateSlice: StateCreator<
remoteUpdateAllPods: async (client) => {
// The pods that haven't been inserted to the database yet
const pendingPods = Object.values(get().pods).filter(
(pod) => pod.dirty && pod.pending
(pod) => (pod.dirty || pod.dirtyPending) && pod.pending
);

// First insert all pending pods and ignore their relationship for now
Expand All @@ -139,11 +139,17 @@ export const createRepoStateSlice: StateCreator<
if (!pod) return;
pod.children?.map(({ id }) => helper(id));
if (id !== "ROOT") {
if (pod.dirty && !pod.isSyncing) {
if ((pod.dirty || pod.dirtyPending) && !pod.isSyncing) {
set(
produce((state) => {
// FIXME when doRemoteUpdatePod fails, this will be stuck.
state.pods[id].isSyncing = true;
// Transfer the dirty status from dirty to dirtyPending. This is
// because pod may be updated during remote syncing, and the flag
// might be cleared by a successful return, causing unsaved
// content.
state.pods[id].dirty = false;
state.pods[id].dirtyPending = true;
})
);
try {
Expand All @@ -156,7 +162,7 @@ export const createRepoStateSlice: StateCreator<
state.pods[id].isSyncing = false;
// pod may be updated during remote syncing
// clear dirty flag only when remote update is successful
if (res) state.pods[id].dirty = false;
if (res) state.pods[id].dirtyPending = false;
})
);
} catch (e) {
Expand Down

0 comments on commit 0f1b143

Please sign in to comment.