-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #416 from skip-mev/eg/eg-1234
feat: generic feature enabled rollout logic
- Loading branch information
Showing
5 changed files
with
75 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// hooks/useFeatureRollout.js | ||
import { useEffect, useState } from "react"; | ||
|
||
import { getCookie, setCookie } from "@/utils/cookies"; | ||
|
||
export function useFeatureEnabled(featureName: string): boolean { | ||
const [isFeatureEnabled, setIsFeatureEnabled] = useState(false); | ||
const rolloutPercentage = parseInt(process.env.NEXT_PUBLIC_FEATURE_ROLLOUT_PERCENTAGE || "0", 10); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
let featureEnabled = getCookie(featureName); | ||
|
||
if (featureEnabled === undefined) { | ||
const randomNumber = Math.random() * 100; | ||
featureEnabled = randomNumber < rolloutPercentage ? "true" : "false"; | ||
|
||
// Set the cookie for 1 day | ||
setCookie(featureName, featureEnabled, 1); | ||
} | ||
|
||
setIsFeatureEnabled(featureEnabled === "true"); | ||
} | ||
}, [featureName, rolloutPercentage]); | ||
|
||
return isFeatureEnabled; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export function useTheme() { | ||
const [theme, setTheme] = useState<"light" | "dark">(); | ||
|
||
useEffect(() => { | ||
if (typeof window !== "undefined") { | ||
if (window.matchMedia && window.matchMedia("(prefers-color-scheme: light)").matches) { | ||
setTheme("light"); | ||
} else { | ||
setTheme("dark"); | ||
} | ||
|
||
window.matchMedia("(prefers-color-scheme: light)").addEventListener("change", (event) => { | ||
const newColorScheme = event.matches ? "light" : "dark"; | ||
setTheme(newColorScheme); | ||
}); | ||
} | ||
}, []); | ||
|
||
return theme; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// utils/cookies.ts | ||
|
||
export const getCookie = (name: string): string | undefined => { | ||
const value = `; ${document.cookie}`; | ||
const parts = value.split(`; ${name}=`); | ||
if (parts.length === 2) { | ||
return parts.pop()?.split(";").shift(); | ||
} | ||
return undefined; | ||
}; | ||
|
||
export const setCookie = (name: string, value: string, days?: number): void => { | ||
let expires = ""; | ||
if (days) { | ||
const date = new Date(); | ||
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); | ||
expires = `; expires=${date.toUTCString()}`; | ||
} | ||
document.cookie = `${name}=${encodeURIComponent(value || "")}${expires}; path=/`; | ||
}; |