2022-12-20 02:06:39 +00:00
|
|
|
import {showBehavioralPrompt} from 'app/client/components/modals';
|
2024-02-13 17:49:00 +00:00
|
|
|
import {logTelemetryEvent} from 'app/client/lib/telemetry';
|
2022-12-20 02:06:39 +00:00
|
|
|
import {AppModel} from 'app/client/models/AppModel';
|
2023-01-13 07:39:33 +00:00
|
|
|
import {getUserPrefObs} from 'app/client/models/UserPrefs';
|
2022-12-20 02:06:39 +00:00
|
|
|
import {GristBehavioralPrompts} from 'app/client/ui/GristTooltips';
|
|
|
|
import {isNarrowScreen} from 'app/client/ui2018/cssVars';
|
2023-01-13 07:39:33 +00:00
|
|
|
import {BehavioralPrompt, BehavioralPromptPrefs} from 'app/common/Prefs';
|
2022-12-20 16:37:11 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2023-01-13 07:39:33 +00:00
|
|
|
import {Computed, Disposable, dom, Observable} from 'grainjs';
|
2022-12-20 02:06:39 +00:00
|
|
|
import {IPopupOptions} from 'popweasel';
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
/**
|
|
|
|
* Options for showing a tip.
|
|
|
|
*/
|
|
|
|
export interface ShowTipOptions {
|
|
|
|
/** Defaults to `false`. */
|
2022-12-20 02:06:39 +00:00
|
|
|
hideArrow?: boolean;
|
|
|
|
popupOptions?: IPopupOptions;
|
|
|
|
onDispose?(): void;
|
2023-09-27 00:40:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options for attaching a tip to a DOM element.
|
|
|
|
*/
|
|
|
|
export interface AttachTipOptions extends ShowTipOptions {
|
|
|
|
/**
|
|
|
|
* Optional callback that should return true if the tip should be disabled.
|
|
|
|
*
|
|
|
|
* If omitted, the tip is enabled.
|
|
|
|
*/
|
|
|
|
isDisabled?(): boolean;
|
2022-12-20 02:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface QueuedTip {
|
|
|
|
prompt: BehavioralPrompt;
|
|
|
|
refElement: Element;
|
2023-09-27 00:40:34 +00:00
|
|
|
options: ShowTipOptions;
|
2022-12-20 02:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages tips that are shown the first time a user performs some action.
|
|
|
|
*
|
|
|
|
* Tips are shown in the order that they are attached.
|
|
|
|
*/
|
2023-01-13 07:39:33 +00:00
|
|
|
export class BehavioralPromptsManager extends Disposable {
|
2023-03-29 19:52:04 +00:00
|
|
|
private _isDisabled: boolean = false;
|
|
|
|
|
2023-01-13 07:39:33 +00:00
|
|
|
private readonly _prefs = getUserPrefObs(this._appModel.userPrefsObs, 'behavioralPrompts',
|
|
|
|
{ defaultValue: { dontShowTips: false, dismissedTips: [] } }) as Observable<BehavioralPromptPrefs>;
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
private _dismissedTips: Computed<Set<BehavioralPrompt>> = Computed.create(this, use => {
|
|
|
|
const {dismissedTips} = use(this._prefs);
|
|
|
|
return new Set(dismissedTips.filter(BehavioralPrompt.guard));
|
|
|
|
});
|
2023-01-13 07:39:33 +00:00
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
private _queuedTips: QueuedTip[] = [];
|
|
|
|
|
|
|
|
constructor(private _appModel: AppModel) {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
public showTip(refElement: Element, prompt: BehavioralPrompt, options: ShowTipOptions = {}) {
|
2022-12-20 02:06:39 +00:00
|
|
|
this._queueTip(refElement, prompt, options);
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
public attachTip(prompt: BehavioralPrompt, options: AttachTipOptions = {}) {
|
2022-12-20 02:06:39 +00:00
|
|
|
return (element: Element) => {
|
2023-09-27 00:40:34 +00:00
|
|
|
if (options.isDisabled?.()) { return; }
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
this._queueTip(element, prompt, options);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public hasSeenTip(prompt: BehavioralPrompt) {
|
|
|
|
return this._dismissedTips.get().has(prompt);
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
public shouldShowTip(prompt: BehavioralPrompt): boolean {
|
|
|
|
if (this._isDisabled) { return false; }
|
|
|
|
|
2023-10-30 03:21:28 +00:00
|
|
|
// For non-SaaS flavors of Grist, don't show tips if the Help Center is explicitly
|
|
|
|
// disabled. A separate opt-out feature could be added down the road for more granularity,
|
|
|
|
// but will require communication in advance to avoid disrupting users.
|
|
|
|
const {deploymentType, features} = getGristConfig();
|
|
|
|
if (
|
|
|
|
!features?.includes('helpCenter') &&
|
|
|
|
// This one is an easter egg, so we make an exception.
|
|
|
|
prompt !== 'rickRow'
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
const {
|
|
|
|
showContext = 'desktop',
|
|
|
|
showDeploymentTypes,
|
|
|
|
forceShow = false,
|
|
|
|
} = GristBehavioralPrompts[prompt];
|
|
|
|
|
|
|
|
if (
|
|
|
|
showDeploymentTypes !== '*' &&
|
|
|
|
(!deploymentType || !showDeploymentTypes.includes(deploymentType))
|
|
|
|
) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const context = isNarrowScreen() ? 'mobile' : 'desktop';
|
|
|
|
if (showContext !== '*' && showContext !== context) { return false; }
|
|
|
|
|
|
|
|
return forceShow || (!this._prefs.get().dontShowTips && !this.hasSeenTip(prompt));
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:52:04 +00:00
|
|
|
public enable() {
|
|
|
|
this._isDisabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public disable() {
|
|
|
|
this._isDisabled = true;
|
|
|
|
}
|
|
|
|
|
2023-04-14 10:09:50 +00:00
|
|
|
public reset() {
|
|
|
|
this._prefs.set({...this._prefs.get(), dismissedTips: [], dontShowTips: false});
|
|
|
|
this.enable();
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
private _queueTip(refElement: Element, prompt: BehavioralPrompt, options: ShowTipOptions) {
|
|
|
|
if (!this.shouldShowTip(prompt)) { return; }
|
2022-12-20 02:06:39 +00:00
|
|
|
|
|
|
|
this._queuedTips.push({prompt, refElement, options});
|
|
|
|
if (this._queuedTips.length > 1) {
|
|
|
|
// If we're already showing a tip, wait for that one to be dismissed, which will
|
|
|
|
// cause the next one in the queue to be shown.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._showTip(refElement, prompt, options);
|
|
|
|
}
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
private _showTip(refElement: Element, prompt: BehavioralPrompt, options: ShowTipOptions) {
|
2022-12-20 02:06:39 +00:00
|
|
|
const close = () => {
|
|
|
|
if (!ctl.isDisposed()) {
|
|
|
|
ctl.close();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-27 00:40:34 +00:00
|
|
|
const {hideArrow, onDispose, popupOptions} = options;
|
|
|
|
const {title, content, hideDontShowTips = false, markAsSeen = true} = GristBehavioralPrompts[prompt];
|
2023-01-31 16:35:46 +00:00
|
|
|
const ctl = showBehavioralPrompt(refElement, title(), content(), {
|
2022-12-20 02:06:39 +00:00
|
|
|
onClose: (dontShowTips) => {
|
|
|
|
if (dontShowTips) { this._dontShowTips(); }
|
2023-03-27 17:25:25 +00:00
|
|
|
if (markAsSeen) { this._markAsSeen(prompt); }
|
2022-12-20 02:06:39 +00:00
|
|
|
},
|
|
|
|
hideArrow,
|
|
|
|
popupOptions,
|
2023-03-27 17:25:25 +00:00
|
|
|
hideDontShowTips,
|
2022-12-20 02:06:39 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
ctl.onDispose(() => {
|
|
|
|
onDispose?.();
|
|
|
|
this._showNextQueuedTip();
|
|
|
|
});
|
|
|
|
dom.onElem(refElement, 'click', () => close());
|
|
|
|
dom.onDisposeElem(refElement, () => close());
|
2024-02-13 17:49:00 +00:00
|
|
|
|
|
|
|
logTelemetryEvent('viewedTip', {full: {tipName: prompt}});
|
2022-12-20 02:06:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _showNextQueuedTip() {
|
|
|
|
this._queuedTips.shift();
|
|
|
|
if (this._queuedTips.length !== 0) {
|
|
|
|
const [nextTip] = this._queuedTips;
|
|
|
|
const {refElement, prompt, options} = nextTip;
|
|
|
|
this._showTip(refElement, prompt, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private _markAsSeen(prompt: BehavioralPrompt) {
|
|
|
|
const {dismissedTips} = this._prefs.get();
|
|
|
|
const newDismissedTips = new Set(dismissedTips);
|
|
|
|
newDismissedTips.add(prompt);
|
|
|
|
this._prefs.set({...this._prefs.get(), dismissedTips: [...newDismissedTips]});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _dontShowTips() {
|
|
|
|
this._prefs.set({...this._prefs.get(), dontShowTips: true});
|
|
|
|
this._queuedTips = [];
|
|
|
|
}
|
|
|
|
}
|