From 4cd94370e8f87b1fc70200e9383a7c90f6aff03c Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Fri, 3 May 2024 05:25:11 +0000 Subject: [PATCH 1/8] fix i think --- app/store/sync.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index 8ee6c1819f4..d5a7f7b6fab 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -95,22 +95,29 @@ export const useSyncStore = createPersistStore( const provider = get().provider; const config = get()[provider]; const client = this.getClient(); - + try { - const remoteState = JSON.parse( - await client.get(config.username), - ) as AppState; - mergeAppState(localState, remoteState); - setLocalAppState(localState); + const remoteState = await client.get(config.username); + if (!remoteState || remoteState === "") { + console.log("[Sync] Remote state is empty, using local state instead."); + return + } else { + const parsedRemoteState = JSON.parse( + await client.get(config.username), + ) as AppState; + + mergeAppState(localState, parsedRemoteState); + setLocalAppState(localState); + } + } catch (e) { console.log("[Sync] failed to get remote state", e); throw e; } - + await client.set(config.username, JSON.stringify(localState)); - this.markSyncTime(); - }, + }, async check() { const client = this.getClient(); From 9cd3358e4e08e207dacc7d6b032283e351d8a58d Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 22:40:52 -0700 Subject: [PATCH 2/8] this is the fix --- app/store/sync.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/app/store/sync.ts b/app/store/sync.ts index d5a7f7b6fab..aa516057a5c 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -116,6 +116,7 @@ export const useSyncStore = createPersistStore( } await client.set(config.username, JSON.stringify(localState)); + console.log("client set", localState); this.markSyncTime(); }, From 9d728ec3c560d69282e0d867fe9c490bbb26bc3a Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 22:50:35 -0700 Subject: [PATCH 3/8] this is ti --- app/store/sync.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/store/sync.ts b/app/store/sync.ts index aa516057a5c..9d01a787132 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -95,6 +95,9 @@ export const useSyncStore = createPersistStore( const provider = get().provider; const config = get()[provider]; const client = this.getClient(); + + console.log("client set", localState); + await client.set(config.username, JSON.stringify(localState)); try { const remoteState = await client.get(config.username); From 6fc7c50f193a7be50a00233d4ea1bf0668f182da Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 22:55:41 -0700 Subject: [PATCH 4/8] this --- app/store/sync.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index 9d01a787132..aa516057a5c 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -95,9 +95,6 @@ export const useSyncStore = createPersistStore( const provider = get().provider; const config = get()[provider]; const client = this.getClient(); - - console.log("client set", localState); - await client.set(config.username, JSON.stringify(localState)); try { const remoteState = await client.get(config.username); From 7b61d05e880c36f5ba3643fdf65a077f90b8ddc3 Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 23:08:17 -0700 Subject: [PATCH 5/8] new fix --- app/store/sync.ts | 164 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 160 insertions(+), 4 deletions(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index aa516057a5c..af7888d5724 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -99,24 +99,22 @@ export const useSyncStore = createPersistStore( try { const remoteState = await client.get(config.username); if (!remoteState || remoteState === "") { + await client.set(config.username, JSON.stringify(localState)); console.log("[Sync] Remote state is empty, using local state instead."); return } else { const parsedRemoteState = JSON.parse( await client.get(config.username), ) as AppState; - mergeAppState(localState, parsedRemoteState); setLocalAppState(localState); - } - + } } catch (e) { console.log("[Sync] failed to get remote state", e); throw e; } await client.set(config.username, JSON.stringify(localState)); - console.log("client set", localState); this.markSyncTime(); }, @@ -149,3 +147,161 @@ export const useSyncStore = createPersistStore( }, }, ); + +``` + +**Output:** + +```tsx +import { getClientConfig } from "../config/client"; +import { Updater } from "../typing"; +import { ApiPath, STORAGE_KEY, StoreKey } from "../constant"; +import { createPersistStore } from "../utils/store"; +import { + AppState, + getLocalAppState, + GetStoreState, + mergeAppState, + setLocalAppState, +} from "../utils/sync"; +import { downloadAs, readFromFile } from "../utils"; +import { showToast } from "../components/ui-lib"; +import Locale from "../locales"; +import { createSyncClient, ProviderType } from "../utils/cloud"; +import { corsPath } from "../utils/cors"; + +export interface WebDavConfig { + server: string; + username: string; + password: string; +} + +const isApp = !!getClientConfig()?.isApp; +export type SyncStore = GetStoreState; + +const DEFAULT_SYNC_STATE = { + provider: ProviderType.WebDAV, + useProxy: true, + proxyUrl: corsPath(ApiPath.Cors), + + webdav: { + endpoint: "", + username: "", + password: "", + }, + + upstash: { + endpoint: "", + username: STORAGE_KEY, + apiKey: "", + }, + + lastSyncTime: 0, + lastProvider: "", +}; + +export const useSyncStore = createPersistStore( + DEFAULT_SYNC_STATE, + (set, get) => ({ + cloudSync() { + const config = get()[get().provider]; + return Object.values(config).every((c) => c.toString().length > 0); + }, + + markSyncTime() { + set({ lastSyncTime: Date.now(), lastProvider: get().provider }); + }, + + export() { + const state = getLocalAppState(); + const datePart = isApp + ? `${new Date().toLocaleDateString().replace(/\//g, "_")} ${new Date() + .toLocaleTimeString() + .replace(/:/g, "_")}` + : new Date().toLocaleString(); + + const fileName = `Backup-${datePart}.json`; + downloadAs(JSON.stringify(state), fileName); + }, + + async import() { + const rawContent = await readFromFile(); + + try { + const remoteState = JSON.parse(rawContent) as AppState; + const localState = getLocalAppState(); + mergeAppState(localState, remoteState); + setLocalAppState(localState); + location.reload(); + } catch (e) { + console.error("[Import]", e); + showToast(Locale.Settings.Sync.ImportFailed); + } + }, + + getClient() { + const provider = get().provider; + const client = createSyncClient(provider, get()); + return client; + }, + + async sync() { + const localState = getLocalAppState(); + const provider = get().provider; + const config = get()[provider]; + const client = this.getClient(); + + try { + const remoteState = await client.get(config.username); + if (!remoteState || remoteState === "") { + console.log( + "[Sync] Remote state is empty, using local state instead.", + ); + return; + } else { + const parsedRemoteState = JSON.parse( + await client.get(config.username), + ) as AppState; + + mergeAppState(localState, parsedRemoteState); + setLocalAppState(localState); + } + } catch (e) { + console.log("[Sync] failed to get remote state", e); + throw e; + } + + await client.set(config.username, JSON.stringify(localState)); + console.log("client set", localState); + this.markSyncTime(); + }, + + async check() { + const client = this.getClient(); + return await client.check(); + }, + }), + { + name: StoreKey.Sync, + version: 1.2, + + migrate(persistedState, version) { + const newState = persistedState as typeof DEFAULT_SYNC_STATE; + + if (version < 1.1) { + newState.upstash.username = STORAGE_KEY; + } + + if (version < 1.2) { + if ( + (persistedState as typeof DEFAULT_SYNC_STATE).proxyUrl === + "/api/cors/" + ) { + newState.proxyUrl = ""; + } + } + + return newState as any; + }, + }, +); \ No newline at end of file From a10358234641dd209488d68e3fbf587b3dd61d91 Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 23:10:10 -0700 Subject: [PATCH 6/8] fix --- app/store/sync.ts | 158 ---------------------------------------------- 1 file changed, 158 deletions(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index af7888d5724..d22d6baf8e7 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -127,164 +127,6 @@ export const useSyncStore = createPersistStore( name: StoreKey.Sync, version: 1.2, - migrate(persistedState, version) { - const newState = persistedState as typeof DEFAULT_SYNC_STATE; - - if (version < 1.1) { - newState.upstash.username = STORAGE_KEY; - } - - if (version < 1.2) { - if ( - (persistedState as typeof DEFAULT_SYNC_STATE).proxyUrl === - "/api/cors/" - ) { - newState.proxyUrl = ""; - } - } - - return newState as any; - }, - }, -); - -``` - -**Output:** - -```tsx -import { getClientConfig } from "../config/client"; -import { Updater } from "../typing"; -import { ApiPath, STORAGE_KEY, StoreKey } from "../constant"; -import { createPersistStore } from "../utils/store"; -import { - AppState, - getLocalAppState, - GetStoreState, - mergeAppState, - setLocalAppState, -} from "../utils/sync"; -import { downloadAs, readFromFile } from "../utils"; -import { showToast } from "../components/ui-lib"; -import Locale from "../locales"; -import { createSyncClient, ProviderType } from "../utils/cloud"; -import { corsPath } from "../utils/cors"; - -export interface WebDavConfig { - server: string; - username: string; - password: string; -} - -const isApp = !!getClientConfig()?.isApp; -export type SyncStore = GetStoreState; - -const DEFAULT_SYNC_STATE = { - provider: ProviderType.WebDAV, - useProxy: true, - proxyUrl: corsPath(ApiPath.Cors), - - webdav: { - endpoint: "", - username: "", - password: "", - }, - - upstash: { - endpoint: "", - username: STORAGE_KEY, - apiKey: "", - }, - - lastSyncTime: 0, - lastProvider: "", -}; - -export const useSyncStore = createPersistStore( - DEFAULT_SYNC_STATE, - (set, get) => ({ - cloudSync() { - const config = get()[get().provider]; - return Object.values(config).every((c) => c.toString().length > 0); - }, - - markSyncTime() { - set({ lastSyncTime: Date.now(), lastProvider: get().provider }); - }, - - export() { - const state = getLocalAppState(); - const datePart = isApp - ? `${new Date().toLocaleDateString().replace(/\//g, "_")} ${new Date() - .toLocaleTimeString() - .replace(/:/g, "_")}` - : new Date().toLocaleString(); - - const fileName = `Backup-${datePart}.json`; - downloadAs(JSON.stringify(state), fileName); - }, - - async import() { - const rawContent = await readFromFile(); - - try { - const remoteState = JSON.parse(rawContent) as AppState; - const localState = getLocalAppState(); - mergeAppState(localState, remoteState); - setLocalAppState(localState); - location.reload(); - } catch (e) { - console.error("[Import]", e); - showToast(Locale.Settings.Sync.ImportFailed); - } - }, - - getClient() { - const provider = get().provider; - const client = createSyncClient(provider, get()); - return client; - }, - - async sync() { - const localState = getLocalAppState(); - const provider = get().provider; - const config = get()[provider]; - const client = this.getClient(); - - try { - const remoteState = await client.get(config.username); - if (!remoteState || remoteState === "") { - console.log( - "[Sync] Remote state is empty, using local state instead.", - ); - return; - } else { - const parsedRemoteState = JSON.parse( - await client.get(config.username), - ) as AppState; - - mergeAppState(localState, parsedRemoteState); - setLocalAppState(localState); - } - } catch (e) { - console.log("[Sync] failed to get remote state", e); - throw e; - } - - await client.set(config.username, JSON.stringify(localState)); - console.log("client set", localState); - this.markSyncTime(); - }, - - async check() { - const client = this.getClient(); - return await client.check(); - }, - }), - { - name: StoreKey.Sync, - version: 1.2, - migrate(persistedState, version) { const newState = persistedState as typeof DEFAULT_SYNC_STATE; From 1da7d81122905362e1e6d76c1b8aba47f1a90a67 Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 23:22:32 -0700 Subject: [PATCH 7/8] Fix cloud data sync issue with Upstash (#4563) --- app/store/sync.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index d22d6baf8e7..ce31ebd8f24 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -115,6 +115,7 @@ export const useSyncStore = createPersistStore( } await client.set(config.username, JSON.stringify(localState)); + this.markSyncTime(); }, @@ -146,4 +147,4 @@ export const useSyncStore = createPersistStore( return newState as any; }, }, -); \ No newline at end of file +); From 8ef2617eec823ea5a6f647be762a3345f52cae0c Mon Sep 17 00:00:00 2001 From: ruban <51721541+rooben-me@users.noreply.github.com> Date: Thu, 2 May 2024 23:24:41 -0700 Subject: [PATCH 8/8] Removed spaces --- app/store/sync.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/store/sync.ts b/app/store/sync.ts index ce31ebd8f24..d3582e3c935 100644 --- a/app/store/sync.ts +++ b/app/store/sync.ts @@ -95,7 +95,7 @@ export const useSyncStore = createPersistStore( const provider = get().provider; const config = get()[provider]; const client = this.getClient(); - + try { const remoteState = await client.get(config.username); if (!remoteState || remoteState === "") { @@ -113,11 +113,11 @@ export const useSyncStore = createPersistStore( console.log("[Sync] failed to get remote state", e); throw e; } - + await client.set(config.username, JSON.stringify(localState)); this.markSyncTime(); - }, + }, async check() { const client = this.getClient();