|
| 1 | +import { Disposable, dom, Observable, styled } from 'grainjs'; |
| 2 | +import { get as getBrowserGlobals } from 'app/client/lib/browserGlobals'; |
| 3 | +import { testId } from 'app/client/lib/dom'; |
| 4 | +import { getStorage } from 'app/client/lib/storage'; |
| 5 | +import { confirmModal, cssModalBody, cssModalButtons, cssModalTitle, modal } from 'app/client/ui2018/modals'; |
| 6 | +import { bigPrimaryButton } from 'app/client/ui2018/buttons'; |
| 7 | +import { cssLink } from 'app/client/ui2018/links'; |
| 8 | +import { makeT } from 'app/client/lib/localization'; |
| 9 | +import { App } from 'app/client/ui/App'; |
| 10 | + |
| 11 | +const t = makeT('Experiments'); |
| 12 | + |
| 13 | +const G = getBrowserGlobals('document', 'window'); |
| 14 | + |
| 15 | +const EXPERIMENTS = { |
| 16 | + newRecordButton: () => t('New record button'), |
| 17 | +}; |
| 18 | + |
| 19 | +type Experiment = keyof typeof EXPERIMENTS; |
| 20 | + |
| 21 | +const EXPERIMENT_URL_PARAM = 'experiment'; |
| 22 | + |
| 23 | +export class Experiments extends Disposable { |
| 24 | + private _app: App; |
| 25 | + |
| 26 | + constructor(app: App) { |
| 27 | + super(); |
| 28 | + this._app = app; |
| 29 | + } |
| 30 | + |
| 31 | + public isEnabled(experiment: Experiment) { |
| 32 | + const experimentState = this._getExperimentState(experiment); |
| 33 | + return experimentState.enabled; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns whether or not the user wants to show the experiments modal. |
| 38 | + */ |
| 39 | + public isRequested() { |
| 40 | + const urlExperiment = this.getCurrentRequest(); |
| 41 | + return urlExperiment && this._isSupported(urlExperiment); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Returns the experiment that the user wants to show the modal for. |
| 46 | + */ |
| 47 | + public getCurrentRequest() { |
| 48 | + const searchParams = new URLSearchParams(G.window.location.search); |
| 49 | + return searchParams.get(EXPERIMENT_URL_PARAM); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Shows the modal for the given experiment, allowing the user to enable or disable it. |
| 54 | + */ |
| 55 | + public showModal(experiment: string) { |
| 56 | + if (!this._isSupported(experiment)) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + // if the app is not initialized, wait and retry, we need the current user info |
| 61 | + const appObs = this._app.topAppModel.appObs; |
| 62 | + if (appObs.get() === null) { |
| 63 | + appObs.addListener((app, prevApp) => { |
| 64 | + if (app && prevApp === null) { |
| 65 | + this.showModal(experiment); |
| 66 | + } |
| 67 | + }); |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + const experimentState = this._getExperimentState(experiment); |
| 72 | + |
| 73 | + const alreadyEnabled = experimentState.enabled; |
| 74 | + |
| 75 | + const experimentLabel = EXPERIMENTS[experiment as keyof typeof EXPERIMENTS](); |
| 76 | + const hasConfirmedModal = Observable.create(this, false); |
| 77 | + const urlBlock = ` |
| 78 | + <a class="${cssLink.className}" href="${window.location.href}"> |
| 79 | + ${window.location.href} |
| 80 | + </a> |
| 81 | + `; |
| 82 | + |
| 83 | + if (!hasConfirmedModal.get()) { |
| 84 | + confirmModal( |
| 85 | + t('Experimental feature'), |
| 86 | + alreadyEnabled ? t('Disable feature') : t('Enable feature'), |
| 87 | + () => { |
| 88 | + this._setExperimentState(experiment, !alreadyEnabled); |
| 89 | + hasConfirmedModal.set(true); |
| 90 | + }, |
| 91 | + { |
| 92 | + explanation: cssWrapper( |
| 93 | + cssWrapper((el) => { |
| 94 | + el.innerHTML = t( |
| 95 | + alreadyEnabled |
| 96 | + ? 'You are about to disable this experimental feature: {{- experiment}}' |
| 97 | + : 'You are about to enable this experimental feature: {{- experiment}}', |
| 98 | + {experiment: ` |
| 99 | + <strong> |
| 100 | + ${experimentLabel} |
| 101 | + </strong>`} |
| 102 | + ); |
| 103 | + }), |
| 104 | + !alreadyEnabled ? dom('p', t('This feature is experimental and may not work as expected.')) : null, |
| 105 | + cssWrapper((el) => { |
| 106 | + el.innerHTML = t(alreadyEnabled |
| 107 | + ? 'To start using it again, you can visit this URL at any time: {{- url}}' |
| 108 | + : 'Visit this URL at any time to stop using this feature: {{- url}}', |
| 109 | + { url: urlBlock } |
| 110 | + ); |
| 111 | + }), |
| 112 | + ) |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + // If the user just confirmed the feature toggle, feedback the change and force a page reload |
| 118 | + hasConfirmedModal.addListener((val) => { |
| 119 | + if (!val) { |
| 120 | + return; |
| 121 | + } |
| 122 | + modal( |
| 123 | + (ctl, owner) => { |
| 124 | + return [ |
| 125 | + cssModalTitle(t('Experimental feature'), testId('modal-title')), |
| 126 | + cssModalBody(dom('div', |
| 127 | + dom('p', (el) => { |
| 128 | + el.innerHTML = t( |
| 129 | + alreadyEnabled ? '{{- experiment}} disabled.' : '{{- experiment}} enabled.', |
| 130 | + {experiment: `<strong>${experimentLabel}</strong>`} |
| 131 | + ); |
| 132 | + }) |
| 133 | + )), |
| 134 | + cssModalButtons( |
| 135 | + bigPrimaryButton(t('Reload the page'), dom.on('click', () => { |
| 136 | + const url = new URL(window.location.href); |
| 137 | + url.searchParams.delete(EXPERIMENT_URL_PARAM); |
| 138 | + window.location.href = url.toString(); |
| 139 | + })), |
| 140 | + ), |
| 141 | + ]; |
| 142 | + }, |
| 143 | + { |
| 144 | + noEscapeKey: true, |
| 145 | + noClickAway: true, |
| 146 | + } |
| 147 | + ); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + private _isSupported(experiment: string) { |
| 152 | + return !!(EXPERIMENTS[experiment as keyof typeof EXPERIMENTS]); |
| 153 | + } |
| 154 | + |
| 155 | + private _getExperimentState(experiment: string) { |
| 156 | + const localStorage = getStorage(); |
| 157 | + const storageKey = this._getStorageKey(experiment); |
| 158 | + |
| 159 | + const experimentState = localStorage.getItem(storageKey) |
| 160 | + ? JSON.parse(localStorage.getItem(storageKey)!) |
| 161 | + : {enabled: false, timestamp: null}; |
| 162 | + |
| 163 | + return experimentState; |
| 164 | + } |
| 165 | + |
| 166 | + private _setExperimentState(experiment: string, enabled: boolean) { |
| 167 | + const localStorage = getStorage(); |
| 168 | + const storageKey = this._getStorageKey(experiment); |
| 169 | + localStorage.setItem(storageKey, JSON.stringify({enabled, timestamp: Date.now()})); |
| 170 | + } |
| 171 | + |
| 172 | + private _getStorageKey(experiment: string) { |
| 173 | + const userId = this._app.topAppModel.appObs.get()?.currentUser?.id || 0; |
| 174 | + return `u=${userId}:experiment=${experiment}`; |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +const cssWrapper = styled('div', ` |
| 179 | + display: flex; |
| 180 | + flex-direction: column; |
| 181 | + gap: 1rem; |
| 182 | + & > p { |
| 183 | + margin-bottom: 0; |
| 184 | + } |
| 185 | +`); |
0 commit comments