Skip to content

Automate browser based workflows with AI

Fix organization switching on page refresh (#4944)

Shuchang Zhengb517e3d7fbea
diff --git a/skyvern-frontend/src/store/useOrgPersistStore.ts b/skyvern-frontend/src/store/useOrgPersistStore.ts
new file mode 100644
index 000000000..327d88633
--- /dev/null
+++ b/skyvern-frontend/src/store/useOrgPersistStore.ts
@@ -0,0 +1,38 @@
+import { create } from "zustand";
+import { persist } from "zustand/middleware";
+
+type OrgPersistState = {
+ /** Map of userId → org bootstrap has already run */
+ hasInitializedByUser: Record<string, boolean>;
+ /** Set to true once the persist middleware has finished rehydrating from localStorage */
+ _hasHydrated: boolean;
+ markInitialized: (userId: string) => void;
+ hasInitialized: (userId: string) => boolean;
+};
+
+const useOrgPersistStore = create<OrgPersistState>()(
+ persist(
+ (set, get) => ({
+ hasInitializedByUser: {},
+ _hasHydrated: false,
+ markInitialized: (userId) =>
+ set((state) => ({
+ hasInitializedByUser: state.hasInitializedByUser[userId]
+ ? state.hasInitializedByUser
+ : { ...state.hasInitializedByUser, [userId]: true },
+ })),
+ hasInitialized: (userId) => Boolean(get().hasInitializedByUser[userId]),
+ }),
+ {
+ name: "skyvern-org-persist",
+ onRehydrateStorage: () => () => {
+ useOrgPersistStore.setState({ _hasHydrated: true });
+ },
+ partialize: (state) => ({
+ hasInitializedByUser: state.hasInitializedByUser,
+ }),
+ },
+ ),
+);
+
+export { useOrgPersistStore };