-
hi! had an experiment of wrapping telegram cloudstorage into State Storage, but have a warning that it doesn't work with persist-middleware. here's some code: import { create } from "zustand";
import { persist, createJSONStorage } from "zustand/middleware";
import CloudStateStorage from "@/context/CloudStateStorage";
import { useCloudStorageWrapper } from "@/hooks/useCloudStorage";
interface UserState {
username: string;
picture: string;
rank: number;
points: number;
frensQty: number;
}
interface UserActions {
updateUser: (newUser: Partial<UserState>) => void;
}
export const useStore = create<UserState & UserActions>()(
persist(
(set) => ({
username: "",
picture: "",
rank: 0,
points: 0,
frensQty: 0,
updateUser: (newUser) => set((state) => ({ ...state, ...newUser })),
}),
{
name: "user-storage",
storage: createJSONStorage(() => {
const cloudstorage = useCloudStorageWrapper();
return new CloudStateStorage(cloudstorage);
}),
}
)
); import { StateStorage } from "zustand/middleware";
interface CloudStorageWrapper {
getItem: (name: string) => Promise<string | null>;
setItem: (name: string, value: string) => Promise<void>;
removeItem: (name: string) => Promise<void>;
}
class CloudStateStorage implements StateStorage {
private cloudStorage: CloudStorageWrapper;
constructor(cloudStorage: CloudStorageWrapper) {
this.cloudStorage = cloudStorage;
}
async getItem(name: string): Promise<string | null> {
try {
const value = await this.cloudStorage.getItem(name);
return value ? value : null;
} catch (error) {
console.error(`Error getting item ${name} from CloudStorage:`, error);
return null;
}
}
async setItem(name: string, value: string): Promise<void> {
try {
await this.cloudStorage.setItem(name, value);
} catch (error) {
console.error(`Error saving item ${name} to CloudStorage:`, error);
}
}
async removeItem(name: string): Promise<void> {
try {
await this.cloudStorage.removeItem(name);
} catch (error) {
console.error(`Error removing item ${name} from CloudStorage:`, error);
}
}
}
export default CloudStateStorage; import { useCloudStorage } from "@telegram-apps/sdk-react";
export function useCloudStorageWrapper() {
const cloudstorage = useCloudStorage();
const cloudStorageWrapper = {
getItem: async (name: string): Promise<string | null> => {
try {
const value = await cloudstorage.get(name);
return value ? value : null;
} catch (error) {
console.error(`Error getting item ${name} from CloudStorage:`, error);
return null;
}
},
setItem: async (name: string, value: string): Promise<void> => {
try {
await cloudstorage.set(name, JSON.parse(value));
} catch (error) {
console.error(`Error saving item ${name} to CloudStorage:`, error);
}
},
removeItem: async (name: string): Promise<void> => {
try {
await cloudstorage.delete(name);
} catch (error) {
console.error(`Error removing item ${name} from CloudStorage:`, error);
}
},
};
return cloudStorageWrapper;
} could somebody explain is it possible to do it with cloudstorage? |
Beta Was this translation helpful? Give feedback.
Answered by
Xyzjesus
Oct 8, 2024
Replies: 1 comment 7 replies
-
how about avoid using |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
think i got your point, i've done some changes: