Skip to content

Commit

Permalink
fix: sync popup firing multiple times (hoppscotch#3063)
Browse files Browse the repository at this point in the history
  • Loading branch information
nivedin authored May 31, 2023
1 parent 385cabc commit f0a1fc3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 27 deletions.
40 changes: 40 additions & 0 deletions packages/hoppscotch-common/src/newstore/syncing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { distinctUntilChanged, pluck } from "rxjs"
import DispatchingStore, { defineDispatchers } from "./DispatchingStore"

type SyncState = {
isInitialSync: boolean
shouldSync: boolean
}

type CurrentSyncingState = {
currentSyncingItem: SyncState
}

const initialState: CurrentSyncingState = {
currentSyncingItem: {
isInitialSync: false,
shouldSync: false,
},
}

const dispatchers = defineDispatchers({
changeCurrentSyncStatus(_, { syncItem }: { syncItem: SyncState }) {
return {
currentSyncingItem: syncItem,
}
},
})

export const currentSyncStore = new DispatchingStore(initialState, dispatchers)

export const currentSyncingStatus$ = currentSyncStore.subject$.pipe(
pluck("currentSyncingItem"),
distinctUntilChanged()
)

export function changeCurrentSyncStatus(syncItem: SyncState) {
currentSyncStore.dispatch({
dispatcher: "changeCurrentSyncStatus",
payload: { syncItem },
})
}
74 changes: 47 additions & 27 deletions packages/hoppscotch-common/src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
</template>

<script lang="ts" setup>
import { ref, onMounted, onBeforeUnmount, watch, onBeforeMount } from "vue"
import { ref, onMounted, onBeforeUnmount, onBeforeMount } from "vue"
import { safelyExtractRESTRequest } from "@hoppscotch/data"
import { translateExtURLParams } from "~/helpers/RESTExtURLParams"
import { useRoute } from "vue-router"
Expand Down Expand Up @@ -123,6 +123,11 @@ import { useToast } from "~/composables/toast"
import { PersistableRESTTabState } from "~/helpers/rest/tab"
import { watchDebounced } from "@vueuse/core"
import { oauthRedirect } from "~/helpers/oauth"
import { useReadonlyStream } from "~/composables/stream"
import {
changeCurrentSyncStatus,
currentSyncingStatus$,
} from "~/newstore/syncing"
const savingRequest = ref(false)
const confirmingCloseForTabID = ref<string | null>(null)
Expand All @@ -134,7 +139,10 @@ const toast = useToast()
const tabs = getActiveTabs()
const confirmSync = ref(false)
const confirmSync = useReadonlyStream(currentSyncingStatus$, {
isInitialSync: false,
shouldSync: true,
})
const tabStateForSync = ref<PersistableRESTTabState | null>(null)
function bindRequestToURLParams() {
Expand Down Expand Up @@ -229,29 +237,6 @@ const onSaveModalClose = () => {
}
}
watch(confirmSync, (newValue) => {
if (newValue) {
toast.show(t("confirm.sync"), {
duration: 0,
action: [
{
text: `${t("action.yes")}`,
onClick: (_, toastObject) => {
syncTabState()
toastObject.goAway(0)
},
},
{
text: `${t("action.no")}`,
onClick: (_, toastObject) => {
toastObject.goAway(0)
},
},
],
})
}
})
const syncTabState = () => {
if (tabStateForSync.value) loadTabsFromPersistedState(tabStateForSync.value)
}
Expand Down Expand Up @@ -290,6 +275,35 @@ function startTabStateSync(): Subscription {
return sub
}
const showSyncToast = () => {
toast.show(t("confirm.sync"), {
duration: 0,
action: [
{
text: `${t("action.yes")}`,
onClick: (_, toastObject) => {
syncTabState()
changeCurrentSyncStatus({
isInitialSync: true,
shouldSync: true,
})
toastObject.goAway(0)
},
},
{
text: `${t("action.no")}`,
onClick: (_, toastObject) => {
changeCurrentSyncStatus({
isInitialSync: true,
shouldSync: false,
})
toastObject.goAway(0)
},
},
],
})
}
function setupTabStateSync() {
const route = useRoute()
Expand All @@ -305,9 +319,15 @@ function setupTabStateSync() {
const tabStateFromSync =
await platform.sync.tabState.loadTabStateFromSync()
if (tabStateFromSync) {
if (tabStateFromSync && !confirmSync.value.isInitialSync) {
tabStateForSync.value = tabStateFromSync
confirmSync.value = true
showSyncToast()
// Have to set isInitialSync to true here because the toast is shown
// and the user does not click on any of the actions
changeCurrentSyncStatus({
isInitialSync: true,
shouldSync: false,
})
}
}
Expand Down

0 comments on commit f0a1fc3

Please sign in to comment.