Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add utility to show experiment invitation notices (#28120)
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 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,37 @@ | ||
import { getConfigStore } from "gatsby-core-utils" | ||
import reporter from "gatsby-cli/lib/reporter" | ||
|
||
type CancelExperimentNoticeCallback = () => void | ||
|
||
export type CancelExperimentNoticeCallbackOrUndefined = | ||
| CancelExperimentNoticeCallback | ||
| undefined | ||
|
||
const ONE_DAY = 24 * 60 * 60 * 1000 | ||
|
||
export function showExperimentNoticeAfterTimeout( | ||
experimentIdentifier: string, | ||
noticeText: string, | ||
showNoticeAfterMs: number, | ||
minimumIntervalBetweenNoticesMs: number = ONE_DAY | ||
): CancelExperimentNoticeCallbackOrUndefined { | ||
const configStoreKey = `lastExperimentNotice.${experimentIdentifier}` | ||
|
||
const lastTimeWeShowedNotice = getConfigStore().get(configStoreKey) | ||
|
||
if (lastTimeWeShowedNotice) { | ||
if (Date.now() - lastTimeWeShowedNotice < minimumIntervalBetweenNoticesMs) { | ||
return undefined | ||
} | ||
} | ||
|
||
const noticeTimeout = setTimeout(() => { | ||
reporter.info(noticeText) | ||
|
||
getConfigStore().set(configStoreKey, Date.now()) | ||
}, showNoticeAfterMs) | ||
|
||
return function clearNoticeTimeout(): void { | ||
clearTimeout(noticeTimeout) | ||
} | ||
} |