Skip to content

Commit

Permalink
Merge pull request #421 from skip-mev/eg/dont-persist-feature
Browse files Browse the repository at this point in the history
refactor: update setCookie function to accept hours instead of days
  • Loading branch information
ericHgorski authored Nov 20, 2024
2 parents 82cf856 + eee288c commit da6492e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/hooks/useFeatureEnabled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ export function useFeatureEnabled(featureName: string): boolean {
if (featureEnabled === undefined) {
const randomNumber = Math.random() * 100;
featureEnabled = randomNumber < rolloutPercentage ? "true" : "false";

// Set the cookie for 1 day
setCookie(featureName, featureEnabled, 1);
setCookie(featureName, featureEnabled);
}

setIsFeatureEnabled(featureEnabled === "true");
Expand Down
6 changes: 3 additions & 3 deletions src/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export const getCookie = (name: string): string | undefined => {
return undefined;
};

export const setCookie = (name: string, value: string, days?: number): void => {
export const setCookie = (name: string, value: string, hours?: number): void => {
let expires = "";
if (days) {
if (hours) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
date.setTime(date.getTime() + hours * 60 * 60 * 1000);
expires = `; expires=${date.toUTCString()}`;
}
document.cookie = `${name}=${encodeURIComponent(value || "")}${expires}; path=/`;
Expand Down

0 comments on commit da6492e

Please sign in to comment.