feat: desktop app
Co-authored-by: Vivek R <123vivekr@gmail.com> Co-authored-by: Liyas Thomas <liyascthomas@gmail.com>
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import {
|
||||
runGQLQuery,
|
||||
runGQLSubscription,
|
||||
runMutation,
|
||||
} from "@hoppscotch/common/helpers/backend/GQLClient"
|
||||
import {
|
||||
CreateUserSettingsDocument,
|
||||
CreateUserSettingsMutation,
|
||||
CreateUserSettingsMutationVariables,
|
||||
GetUserSettingsDocument,
|
||||
GetUserSettingsQuery,
|
||||
GetUserSettingsQueryVariables,
|
||||
UpdateUserSettingsDocument,
|
||||
UpdateUserSettingsMutation,
|
||||
UpdateUserSettingsMutationVariables,
|
||||
UserSettingsUpdatedDocument,
|
||||
} from "../../api/generated/graphql"
|
||||
|
||||
export const getUserSettings = () =>
|
||||
runGQLQuery<
|
||||
GetUserSettingsQuery,
|
||||
GetUserSettingsQueryVariables,
|
||||
"user_settings/not_found"
|
||||
>({
|
||||
query: GetUserSettingsDocument,
|
||||
variables: {},
|
||||
})
|
||||
|
||||
export const createUserSettings = (properties: string) =>
|
||||
runMutation<
|
||||
CreateUserSettingsMutation,
|
||||
CreateUserSettingsMutationVariables,
|
||||
""
|
||||
>(CreateUserSettingsDocument, {
|
||||
properties,
|
||||
})()
|
||||
|
||||
export const updateUserSettings = (properties: string) =>
|
||||
runMutation<
|
||||
UpdateUserSettingsMutation,
|
||||
UpdateUserSettingsMutationVariables,
|
||||
""
|
||||
>(UpdateUserSettingsDocument, {
|
||||
properties,
|
||||
})()
|
||||
|
||||
export const runUserSettingsUpdatedSubscription = () =>
|
||||
runGQLSubscription({
|
||||
query: UserSettingsUpdatedDocument,
|
||||
variables: {},
|
||||
})
|
||||
@@ -0,0 +1,88 @@
|
||||
import { SettingsPlatformDef } from "@hoppscotch/common/platform/settings"
|
||||
import { settingsSyncer } from "./settings.sync"
|
||||
import { authEvents$, def as platformAuth } from "@platform/auth"
|
||||
import {
|
||||
createUserSettings,
|
||||
getUserSettings,
|
||||
runUserSettingsUpdatedSubscription,
|
||||
} from "./settings.api"
|
||||
import * as E from "fp-ts/Either"
|
||||
import { runGQLSubscription } from "@hoppscotch/common/helpers/backend/GQLClient"
|
||||
import {
|
||||
bulkApplySettings,
|
||||
getDefaultSettings,
|
||||
} from "@hoppscotch/common/newstore/settings"
|
||||
import { runDispatchWithOutSyncing } from "@lib/sync"
|
||||
|
||||
function initSettingsSync() {
|
||||
const currentUser$ = platformAuth.getCurrentUserStream()
|
||||
settingsSyncer.startStoreSync()
|
||||
settingsSyncer.setupSubscriptions(setupSubscriptions)
|
||||
|
||||
// load the settings
|
||||
|
||||
loadUserSettings()
|
||||
|
||||
currentUser$.subscribe(async (user) => {
|
||||
if (user) {
|
||||
// load the settings
|
||||
loadUserSettings()
|
||||
}
|
||||
})
|
||||
|
||||
authEvents$.subscribe((event) => {
|
||||
if (event.event == "login" || event.event == "token_refresh") {
|
||||
settingsSyncer.startListeningToSubscriptions()
|
||||
}
|
||||
|
||||
if (event.event == "logout") {
|
||||
settingsSyncer.stopListeningToSubscriptions()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function loadUserSettings() {
|
||||
const res = await getUserSettings()
|
||||
|
||||
// create user settings if it doesn't exist
|
||||
E.isLeft(res) &&
|
||||
res.left.error == "user_settings/not_found" &&
|
||||
(await createUserSettings(JSON.stringify(getDefaultSettings())))
|
||||
|
||||
if (E.isRight(res)) {
|
||||
runDispatchWithOutSyncing(() => {
|
||||
bulkApplySettings(JSON.parse(res.right.me.settings.properties))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function setupSubscriptions() {
|
||||
let subs: ReturnType<typeof runGQLSubscription>[1][] = []
|
||||
|
||||
const userSettingsUpdatedSub = setupUserSettingsUpdatedSubscription()
|
||||
|
||||
subs = [userSettingsUpdatedSub]
|
||||
|
||||
return () => {
|
||||
subs.forEach((sub) => sub.unsubscribe())
|
||||
}
|
||||
}
|
||||
|
||||
function setupUserSettingsUpdatedSubscription() {
|
||||
const [userSettingsUpdated$, userSettingsUpdatedSub] =
|
||||
runUserSettingsUpdatedSubscription()
|
||||
|
||||
userSettingsUpdated$.subscribe((res) => {
|
||||
if (E.isRight(res)) {
|
||||
runDispatchWithOutSyncing(() => {
|
||||
bulkApplySettings(JSON.parse(res.right.userSettingsUpdated.properties))
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
return userSettingsUpdatedSub
|
||||
}
|
||||
|
||||
export const def: SettingsPlatformDef = {
|
||||
initSettingsSync,
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { settingsStore } from "@hoppscotch/common/newstore/settings"
|
||||
|
||||
import { getSyncInitFunction } from "../../lib/sync"
|
||||
|
||||
import { StoreSyncDefinitionOf } from "../../lib/sync"
|
||||
|
||||
import { updateUserSettings } from "./settings.api"
|
||||
|
||||
export const settingsSyncDefinition: StoreSyncDefinitionOf<
|
||||
typeof settingsStore
|
||||
> = {
|
||||
applySetting() {
|
||||
updateUserSettings(JSON.stringify(settingsStore.value))
|
||||
},
|
||||
}
|
||||
|
||||
export const settingsSyncer = getSyncInitFunction(
|
||||
settingsStore,
|
||||
settingsSyncDefinition,
|
||||
() => true
|
||||
)
|
||||
Reference in New Issue
Block a user