Skip to content

Commit

Permalink
Merge pull request #2752 from daostack/CW-remove-persistance
Browse files Browse the repository at this point in the history
CW-remove-persistance
  • Loading branch information
MeyerPV authored Oct 28, 2024
2 parents 05f3419 + ee9719f commit 6b3cf10
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ import { useLocation } from "react-router";
import classNames from "classnames";
import { Menu } from "@headlessui/react";
import { logOut } from "@/pages/Auth/store/actions";
import { FeatureFlags } from "@/shared/constants";
import { useRoutesContext } from "@/shared/contexts";
import { useFeatureFlag } from "@/shared/hooks/useFeatureFlag";
import {
Avatar3Icon,
BillingIcon,
LogoutIcon,
NotificationsIcon,
} from "@/shared/icons";
import ReportIcon from "@/shared/icons/report.icon";
import ThemeIcon from "@/shared/icons/theme.icon";
import { toggleTheme } from "@/shared/store/actions";
import { clearFirestoreCache } from "@/shared/utils/firebase";
import { MenuItem } from "./components";
import { Item, ItemType } from "./types";
import styles from "./MenuItems.module.scss";
Expand Down Expand Up @@ -47,9 +43,6 @@ const MenuItems: FC<MenuItemsProps> = (props) => {
const { pathname } = useLocation();
const isV04 = pathname.includes("-v04");

const featureFlags = useFeatureFlag();
const isHavingAnIssueEnabled = featureFlags?.get(FeatureFlags.HavingAnIssue);

const toggleThemeMenuItem = {
key: "theme",
type: ItemType.Button,
Expand Down Expand Up @@ -93,20 +86,8 @@ const MenuItems: FC<MenuItemsProps> = (props) => {
},
];

if (isHavingAnIssueEnabled) {
menuItems.push({
key: "issue",
text: "Having an issue?",
icon: <ReportIcon />,
type: ItemType.Button,
onClick: () => {
clearFirestoreCache();
},
});
}

return menuItems;
}, [isHavingAnIssueEnabled, isV04, toggleThemeMenuItem]);
}, [isV04, toggleThemeMenuItem]);

return (
<Menu.Items as={React.Fragment}>
Expand Down
68 changes: 0 additions & 68 deletions src/shared/utils/firebase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,79 +9,11 @@ import { local } from "@/config";
import { Environment, REACT_APP_ENV } from "@/shared/constants";
import config from "../../config";

const CACHE_SIZE_LIMIT = 104857600; // 100 MB

interface FirebaseError extends Error {
code: string;
}

const app = firebase.initializeApp(config.firebase);
let db = firebase.firestore();

enableUnlimitedCachePersistence();
// Function to handle Firestore persistence errors
function handlePersistenceError(err: any) {
if (err.code === "failed-precondition") {
console.log("Multiple tabs open or other conflict.");
} else if (err.code === "unimplemented") {
console.log("Persistence is not supported in this browser.");
} else if (err.name === "QuotaExceededError") {
console.log("Storage quota exceeded. Consider clearing cache.");
clearFirestoreCache();
} else {
console.error("Error enabling persistence:", err);
reinitializeFirestoreWithPersistence();
}
}

function reinitializeFirestoreWithPersistence() {
db = firebase.firestore(); // Reinitialize Firestore instance
const settings = { cacheSizeBytes: CACHE_SIZE_LIMIT };
db.settings(settings);

db.enablePersistence({ synchronizeTabs: true })
.then(() => {
console.log("Persistence re-enabled.");
return;
})
.catch(handlePersistenceError);
}

// Function to clear Firestore cache and re-enable persistence
export function clearFirestoreCache() {
db.terminate()
.then(() => {
console.log("Firestore instance terminated.");
return db.clearPersistence(); // Safe to clear persistence now
})
.then(() => {
console.log("Persistence cleared. Waiting before reinitializing...");
return new Promise((resolve) => setTimeout(resolve, 2000)); // Wait 2 second
})
.then(() => {
console.log("Cache cleared successfully.");
reinitializeFirestoreWithPersistence(); // Reinitialize Firestore
window.location.reload();
return;
})
.catch((err) => {
if (err.code === "failed-precondition") {
console.log("Cannot clear persistence: Firestore is still running.");
} else {
console.error("Error clearing persistence cache:", err);
}
});
}

// Enable Firestore persistence with unlimited cache size and error handling
function enableUnlimitedCachePersistence() {
const settings = {
cacheSizeBytes: CACHE_SIZE_LIMIT,
};
db.settings(settings);

db.enablePersistence({ synchronizeTabs: true }).catch(handlePersistenceError);
}

// Enable persistence in the local environment (with Firestore and Auth emulators)
if (REACT_APP_ENV === Environment.Local) {
Expand Down

0 comments on commit 6b3cf10

Please sign in to comment.