2023-01-13 07:39:33 +00:00
|
|
|
import {BehavioralPromptsManager} from 'app/client/components/BehavioralPromptsManager';
|
2023-05-23 19:17:28 +00:00
|
|
|
import {hooks} from 'app/client/Hooks';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {get as getBrowserGlobals} from 'app/client/lib/browserGlobals';
|
2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2023-05-01 18:24:23 +00:00
|
|
|
import {sessionStorageObs} from 'app/client/lib/localStorageObs';
|
2022-03-11 20:35:29 +00:00
|
|
|
import {error} from 'app/client/lib/log';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {reportError, setErrorNotifier} from 'app/client/models/errors';
|
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
|
|
|
import {Notifier} from 'app/client/models/NotifyModel';
|
|
|
|
import {getFlavor, ProductFlavor} from 'app/client/ui/CustomThemes';
|
2022-08-09 14:49:51 +00:00
|
|
|
import {buildNewSiteModal, buildUpgradeModal} from 'app/client/ui/ProductUpgrades';
|
2023-07-04 21:21:34 +00:00
|
|
|
import {SupportGristNudge} from 'app/client/ui/SupportGristNudge';
|
2024-02-21 19:22:01 +00:00
|
|
|
import {prefersDarkModeObs} from 'app/client/ui2018/cssVars';
|
2023-10-27 19:34:42 +00:00
|
|
|
import {AsyncCreate} from 'app/common/AsyncCreate';
|
|
|
|
import {ICustomWidget} from 'app/common/CustomWidget';
|
2022-06-06 16:21:26 +00:00
|
|
|
import {OrgUsageSummary} from 'app/common/DocUsage';
|
2022-07-26 17:49:35 +00:00
|
|
|
import {Features, isLegacyPlan, Product} from 'app/common/Features';
|
2023-07-26 22:31:02 +00:00
|
|
|
import {GristLoadConfig, IGristUrlState} from 'app/common/gristUrls';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {FullUser} from 'app/common/LoginSessionAPI';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {LocalPlugin} from 'app/common/plugin';
|
2023-03-14 16:00:38 +00:00
|
|
|
import {DismissedPopup, DismissedReminder, UserPrefs} from 'app/common/Prefs';
|
2023-01-13 07:39:33 +00:00
|
|
|
import {isOwner, isOwnerOrEditor} from 'app/common/roles';
|
2022-03-11 20:35:29 +00:00
|
|
|
import {getTagManagerScript} from 'app/common/tagManager';
|
2023-08-15 17:29:29 +00:00
|
|
|
import {getDefaultThemePrefs, Theme, ThemeColors, ThemePrefs,
|
2022-09-06 01:51:57 +00:00
|
|
|
ThemePrefsChecker} from 'app/common/ThemePrefs';
|
|
|
|
import {getThemeColors} from 'app/common/Themes';
|
2022-03-11 20:35:29 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2024-03-23 17:11:06 +00:00
|
|
|
import {ExtendedUser} from 'app/common/UserAPI';
|
2023-07-26 22:31:02 +00:00
|
|
|
import {getOrgName, isTemplatesOrg, Organization, OrgError, UserAPI, UserAPIImpl} from 'app/common/UserAPI';
|
2024-03-23 17:11:06 +00:00
|
|
|
import {getUserPrefObs, getUserPrefsObs, markAsSeen} from 'app/client/models/UserPrefs';
|
2021-08-18 17:49:34 +00:00
|
|
|
import {bundleChanges, Computed, Disposable, Observable, subscribe} from 'grainjs';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-12-09 15:46:03 +00:00
|
|
|
const t = makeT('AppModel');
|
2022-10-28 16:11:08 +00:00
|
|
|
|
2022-10-21 10:55:01 +00:00
|
|
|
// Reexported for convenience.
|
2020-10-02 15:10:00 +00:00
|
|
|
export {reportError} from 'app/client/models/errors';
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
export type PageType =
|
|
|
|
| "doc"
|
|
|
|
| "home"
|
|
|
|
| "billing"
|
|
|
|
| "welcome"
|
|
|
|
| "account"
|
2024-03-23 17:11:06 +00:00
|
|
|
| "admin"
|
2023-07-04 21:21:34 +00:00
|
|
|
| "activation";
|
|
|
|
|
2021-08-05 15:12:46 +00:00
|
|
|
const G = getBrowserGlobals('document', 'window');
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// TopAppModel is the part of the app model that persists across org and user switches.
|
|
|
|
export interface TopAppModel {
|
|
|
|
api: UserAPI;
|
|
|
|
isSingleOrg: boolean;
|
|
|
|
productFlavor: ProductFlavor;
|
|
|
|
currentSubdomain: Observable<string|undefined>;
|
|
|
|
|
|
|
|
notifier: Notifier;
|
2021-08-05 15:12:46 +00:00
|
|
|
plugins: LocalPlugin[];
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Everything else gets fully rebuilt when the org/user changes. This is to ensure that
|
|
|
|
// different parts of the code aren't using different users/orgs while the switch is pending.
|
|
|
|
appObs: Observable<AppModel|null>;
|
|
|
|
|
2021-08-18 17:49:34 +00:00
|
|
|
orgs: Observable<Organization[]>;
|
|
|
|
users: Observable<FullUser[]>;
|
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
customWidgets: Observable<ICustomWidget[]|null>;
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Reinitialize the app. This is called when org or user changes.
|
|
|
|
initialize(): void;
|
|
|
|
|
|
|
|
// Rebuilds the AppModel and consequently the AppUI, without changing the user or the org.
|
|
|
|
reload(): void;
|
2021-08-05 15:12:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the UntrustedContentOrigin use settings. Throws if not defined.
|
|
|
|
*/
|
|
|
|
getUntrustedContentOrigin(): string;
|
2022-06-29 10:19:20 +00:00
|
|
|
/**
|
|
|
|
* Reloads orgs and accounts for current user.
|
|
|
|
*/
|
|
|
|
fetchUsersAndOrgs(): Promise<void>;
|
2023-10-27 19:34:42 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enumerate the widgets in the WidgetRepository for this installation
|
|
|
|
* of Grist.
|
|
|
|
*/
|
|
|
|
getWidgets(): Promise<ICustomWidget[]>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reload cached list of widgets, for testing purposes.
|
|
|
|
*/
|
|
|
|
testReloadWidgets(): Promise<void>;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 10:55:01 +00:00
|
|
|
/**
|
|
|
|
* AppModel is specific to the currently loaded organization and active user. It gets rebuilt when
|
|
|
|
* we switch the current organization or the current user.
|
|
|
|
*/
|
2020-10-02 15:10:00 +00:00
|
|
|
export interface AppModel {
|
|
|
|
topAppModel: TopAppModel;
|
|
|
|
api: UserAPI;
|
|
|
|
|
2024-03-23 17:11:06 +00:00
|
|
|
currentUser: ExtendedUser|null;
|
|
|
|
currentValidUser: ExtendedUser|null; // Like currentUser, but null when anonymous
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
currentOrg: Organization|null; // null if no access to currentSubdomain
|
|
|
|
currentOrgName: string; // Our best guess for human-friendly name.
|
2022-06-06 16:21:26 +00:00
|
|
|
currentOrgUsage: Observable<OrgUsageSummary|null>;
|
2022-06-03 14:58:07 +00:00
|
|
|
isPersonal: boolean; // Is it a personal site?
|
|
|
|
isTeamSite: boolean; // Is it a team site?
|
2022-07-26 17:49:35 +00:00
|
|
|
isLegacySite: boolean; // Is it a legacy site?
|
2023-07-26 22:31:02 +00:00
|
|
|
isTemplatesSite: boolean; // Is it the templates site?
|
2020-10-02 15:10:00 +00:00
|
|
|
orgError?: OrgError; // If currentOrg is null, the error that caused it.
|
2023-05-01 18:24:23 +00:00
|
|
|
lastVisitedOrgDomain: Observable<string|null>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-07-26 17:49:35 +00:00
|
|
|
currentProduct: Product|null; // The current org's product.
|
|
|
|
currentFeatures: Features; // Features of the current org's product.
|
2022-09-06 01:51:57 +00:00
|
|
|
|
2022-01-14 02:55:55 +00:00
|
|
|
userPrefsObs: Observable<UserPrefs>;
|
2022-09-06 01:51:57 +00:00
|
|
|
themePrefs: Observable<ThemePrefs>;
|
|
|
|
currentTheme: Computed<Theme>;
|
2022-10-21 10:55:01 +00:00
|
|
|
/**
|
|
|
|
* Popups that user has seen.
|
|
|
|
*/
|
|
|
|
dismissedPopups: Observable<DismissedPopup[]>;
|
2022-12-20 02:06:39 +00:00
|
|
|
dismissedWelcomePopups: Observable<DismissedReminder[]>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
pageType: Observable<PageType>;
|
2023-02-20 15:45:55 +00:00
|
|
|
needsOrg: Observable<boolean>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
notifier: Notifier;
|
2022-06-08 17:54:00 +00:00
|
|
|
planName: string|null;
|
2022-06-06 16:21:26 +00:00
|
|
|
|
2023-01-13 07:39:33 +00:00
|
|
|
behavioralPromptsManager: BehavioralPromptsManager;
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
supportGristNudge: SupportGristNudge;
|
|
|
|
|
2022-06-06 16:21:26 +00:00
|
|
|
refreshOrgUsage(): Promise<void>;
|
2022-06-08 17:54:00 +00:00
|
|
|
showUpgradeModal(): void;
|
|
|
|
showNewSiteModal(): void;
|
|
|
|
isBillingManager(): boolean; // If user is a billing manager for this org
|
|
|
|
isSupport(): boolean; // If user is a Support user
|
2022-12-20 02:06:39 +00:00
|
|
|
isOwner(): boolean; // If user is an owner of this org
|
2023-01-13 07:39:33 +00:00
|
|
|
isOwnerOrEditor(): boolean; // If user is an owner or editor of this org
|
2024-03-23 17:11:06 +00:00
|
|
|
isInstallAdmin(): boolean; // Is user an admin of this installation
|
|
|
|
dismissPopup(name: DismissedPopup, isSeen: boolean): void; // Mark popup as dismissed or not.
|
2023-09-19 05:31:22 +00:00
|
|
|
switchUser(user: FullUser, org?: string): Promise<void>;
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
2024-03-04 21:22:47 +00:00
|
|
|
export interface TopAppModelOptions {
|
|
|
|
/** Defaults to true. */
|
|
|
|
useApi?: boolean;
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export class TopAppModelImpl extends Disposable implements TopAppModel {
|
|
|
|
public readonly isSingleOrg: boolean;
|
|
|
|
public readonly productFlavor: ProductFlavor;
|
|
|
|
|
|
|
|
public readonly currentSubdomain = Computed.create(this, urlState().state, (use, s) => s.org);
|
|
|
|
public readonly notifier = Notifier.create(this);
|
|
|
|
public readonly appObs = Observable.create<AppModel|null>(this, null);
|
2021-08-18 17:49:34 +00:00
|
|
|
public readonly orgs = Observable.create<Organization[]>(this, []);
|
|
|
|
public readonly users = Observable.create<FullUser[]>(this, []);
|
2021-08-05 15:12:46 +00:00
|
|
|
public readonly plugins: LocalPlugin[] = [];
|
2023-10-27 19:34:42 +00:00
|
|
|
public readonly customWidgets = Observable.create<ICustomWidget[]|null>(this, null);
|
2021-08-05 15:12:46 +00:00
|
|
|
private readonly _gristConfig?: GristLoadConfig;
|
2023-10-27 19:34:42 +00:00
|
|
|
// Keep a list of available widgets, once requested, so we don't have to
|
|
|
|
// keep reloading it. Downside: browser page will need reloading to pick
|
|
|
|
// up new widgets - that seems ok.
|
|
|
|
private readonly _widgets: AsyncCreate<ICustomWidget[]>;
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2024-03-04 21:22:47 +00:00
|
|
|
constructor(window: {gristConfig?: GristLoadConfig},
|
|
|
|
public readonly api: UserAPI = newUserAPIImpl(),
|
|
|
|
public readonly options: TopAppModelOptions = {}) {
|
2020-10-02 15:10:00 +00:00
|
|
|
super();
|
|
|
|
setErrorNotifier(this.notifier);
|
|
|
|
this.isSingleOrg = Boolean(window.gristConfig && window.gristConfig.singleOrg);
|
|
|
|
this.productFlavor = getFlavor(window.gristConfig && window.gristConfig.org);
|
2021-08-05 15:12:46 +00:00
|
|
|
this._gristConfig = window.gristConfig;
|
2023-10-27 19:34:42 +00:00
|
|
|
this._widgets = new AsyncCreate<ICustomWidget[]>(async () => {
|
2024-03-04 21:22:47 +00:00
|
|
|
const widgets = this.options.useApi === false ? [] : await this.api.getWidgets();
|
2023-10-27 19:34:42 +00:00
|
|
|
this.customWidgets.set(widgets);
|
|
|
|
return widgets;
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
|
|
|
// Initially, and on any change to subdomain, call initialize() to get the full Organization
|
|
|
|
// and the FullUser to use for it (the user may change when switching orgs).
|
|
|
|
this.autoDispose(subscribe(this.currentSubdomain, (use) => this.initialize()));
|
2021-08-05 15:12:46 +00:00
|
|
|
this.plugins = this._gristConfig?.plugins || [];
|
2021-08-18 17:49:34 +00:00
|
|
|
|
2024-03-04 21:22:47 +00:00
|
|
|
if (this.options.useApi !== false) {
|
|
|
|
this.fetchUsersAndOrgs().catch(reportError);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public initialize(): void {
|
|
|
|
this._doInitialize().catch(reportError);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rebuilds the AppModel and consequently the AppUI, etc, without changing the user or the org.
|
|
|
|
public reload(): void {
|
|
|
|
const app = this.appObs.get();
|
|
|
|
if (app) {
|
|
|
|
const {currentUser, currentOrg, orgError} = app;
|
|
|
|
AppModelImpl.create(this.appObs, this, currentUser, currentOrg, orgError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:34:42 +00:00
|
|
|
public async getWidgets(): Promise<ICustomWidget[]> {
|
|
|
|
return this._widgets.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async testReloadWidgets() {
|
|
|
|
console.log("testReloadWidgets");
|
|
|
|
this._widgets.clear();
|
|
|
|
this.customWidgets.set(null);
|
|
|
|
console.log("testReloadWidgets cleared and nulled");
|
|
|
|
const result = await this.getWidgets();
|
|
|
|
console.log("testReloadWidgets got", {result});
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:12:46 +00:00
|
|
|
public getUntrustedContentOrigin() {
|
|
|
|
if (G.window.isRunningUnderElectron) {
|
|
|
|
// when loaded within webviews it is safe to serve plugin's content from the same domain
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const origin = this._gristConfig?.pluginUrl;
|
|
|
|
if (!origin) {
|
|
|
|
throw new Error("Missing untrustedContentOrigin configuration");
|
|
|
|
}
|
|
|
|
if (origin.match(/:[0-9]+$/)) {
|
|
|
|
// Port number already specified, no need to add.
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
return origin + ":" + G.window.location.port;
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:19:20 +00:00
|
|
|
public async fetchUsersAndOrgs() {
|
|
|
|
const data = await this.api.getSessionAll();
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
bundleChanges(() => {
|
|
|
|
this.users.set(data.users);
|
|
|
|
this.orgs.set(data.orgs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
private async _doInitialize() {
|
|
|
|
this.appObs.set(null);
|
2024-03-04 21:22:47 +00:00
|
|
|
if (this.options.useApi === false) {
|
|
|
|
AppModelImpl.create(this.appObs, this, null, null, {error: 'no-api', status: 500});
|
|
|
|
return;
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
try {
|
|
|
|
const {user, org, orgError} = await this.api.getSessionActive();
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
if (org) {
|
|
|
|
// Check that our domain matches what the api returns.
|
|
|
|
const state = urlState().state.get();
|
|
|
|
if (state.org !== org.domain && org.domain !== null) {
|
|
|
|
// If not, redirect. This is to allow vanity domains
|
|
|
|
// to "stick" only if paid for.
|
|
|
|
await urlState().pushUrl({...state, org: org.domain});
|
|
|
|
}
|
|
|
|
if (org.billingAccount && org.billingAccount.product &&
|
|
|
|
org.billingAccount.product.name === 'suspended') {
|
2021-10-01 19:38:58 +00:00
|
|
|
this.notifier.createUserMessage(
|
2022-12-06 13:23:59 +00:00
|
|
|
t("This team site is suspended. Documents can be read, but not modified."),
|
2021-07-13 14:46:26 +00:00
|
|
|
{actions: ['renew', 'personal']}
|
2020-10-02 15:10:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
AppModelImpl.create(this.appObs, this, user, org, orgError);
|
|
|
|
} catch (err) {
|
|
|
|
// tslint:disable-next-line:no-console
|
|
|
|
console.log(`getSessionActive() failed: ${err}`);
|
|
|
|
if (this.isDisposed()) { return; }
|
|
|
|
AppModelImpl.create(this.appObs, this, null, null, {error: err.message, status: err.status || 500});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AppModelImpl extends Disposable implements AppModel {
|
|
|
|
public readonly api: UserAPI = this.topAppModel.api;
|
|
|
|
|
|
|
|
// Compute currentValidUser, turning anonymous into null.
|
2024-03-23 17:11:06 +00:00
|
|
|
public readonly currentValidUser: ExtendedUser|null =
|
2020-10-02 15:10:00 +00:00
|
|
|
this.currentUser && !this.currentUser.anonymous ? this.currentUser : null;
|
|
|
|
|
|
|
|
// Figure out the org name, or blank if details are unavailable.
|
|
|
|
public readonly currentOrgName = getOrgNameOrGuest(this.currentOrg, this.currentUser);
|
|
|
|
|
2022-06-06 16:21:26 +00:00
|
|
|
public readonly currentOrgUsage: Observable<OrgUsageSummary|null> = Observable.create(this, null);
|
|
|
|
|
2023-05-01 18:24:23 +00:00
|
|
|
public readonly lastVisitedOrgDomain = this.autoDispose(sessionStorageObs('grist-last-visited-org-domain'));
|
|
|
|
|
2022-07-26 17:49:35 +00:00
|
|
|
public readonly currentProduct = this.currentOrg?.billingAccount?.product ?? null;
|
|
|
|
public readonly currentFeatures = this.currentProduct?.features ?? {};
|
|
|
|
|
2022-06-03 14:58:07 +00:00
|
|
|
public readonly isPersonal = Boolean(this.currentOrg?.owner);
|
|
|
|
public readonly isTeamSite = Boolean(this.currentOrg) && !this.isPersonal;
|
2022-08-09 14:49:51 +00:00
|
|
|
public readonly isLegacySite = Boolean(this.currentProduct && isLegacyPlan(this.currentProduct.name));
|
2023-07-26 22:31:02 +00:00
|
|
|
public readonly isTemplatesSite = isTemplatesOrg(this.currentOrg);
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-01-14 02:55:55 +00:00
|
|
|
public readonly userPrefsObs = getUserPrefsObs(this);
|
2022-09-06 01:51:57 +00:00
|
|
|
public readonly themePrefs = getUserPrefObs(this.userPrefsObs, 'theme', {
|
|
|
|
defaultValue: getDefaultThemePrefs(),
|
|
|
|
checker: ThemePrefsChecker,
|
|
|
|
}) as Observable<ThemePrefs>;
|
|
|
|
public readonly currentTheme = this._getCurrentThemeObs();
|
2022-01-14 02:55:55 +00:00
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
public readonly dismissedPopups = getUserPrefObs(this.userPrefsObs, 'dismissedPopups',
|
|
|
|
{ defaultValue: [] }) as Observable<DismissedPopup[]>;
|
|
|
|
public readonly dismissedWelcomePopups = getUserPrefObs(this.userPrefsObs, 'dismissedWelcomePopups',
|
|
|
|
{ defaultValue: [] }) as Observable<DismissedReminder[]>;
|
2022-10-21 10:55:01 +00:00
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
// Get the current PageType from the URL.
|
|
|
|
public readonly pageType: Observable<PageType> = Computed.create(this, urlState().state,
|
2023-07-04 21:21:34 +00:00
|
|
|
(_use, state) => {
|
|
|
|
if (state.doc) {
|
|
|
|
return 'doc';
|
|
|
|
} else if (state.billing) {
|
|
|
|
return 'billing';
|
|
|
|
} else if (state.welcome) {
|
|
|
|
return 'welcome';
|
|
|
|
} else if (state.account) {
|
|
|
|
return 'account';
|
2024-03-23 17:11:06 +00:00
|
|
|
} else if (state.adminPanel) {
|
|
|
|
return 'admin';
|
2023-07-04 21:21:34 +00:00
|
|
|
} else if (state.activation) {
|
|
|
|
return 'activation';
|
|
|
|
} else {
|
|
|
|
return 'home';
|
|
|
|
}
|
|
|
|
});
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2023-02-20 15:45:55 +00:00
|
|
|
public readonly needsOrg: Observable<boolean> = Computed.create(
|
|
|
|
this, urlState().state, (use, state) => {
|
2023-07-06 04:28:48 +00:00
|
|
|
return !(
|
|
|
|
Boolean(state.welcome) ||
|
|
|
|
state.billing === 'scheduled' ||
|
|
|
|
Boolean(state.account) ||
|
|
|
|
Boolean(state.activation) ||
|
2024-03-23 17:11:06 +00:00
|
|
|
Boolean(state.adminPanel)
|
2023-07-06 04:28:48 +00:00
|
|
|
);
|
2023-02-20 15:45:55 +00:00
|
|
|
});
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
public readonly notifier = this.topAppModel.notifier;
|
|
|
|
|
2023-01-13 07:39:33 +00:00
|
|
|
public readonly behavioralPromptsManager: BehavioralPromptsManager =
|
|
|
|
BehavioralPromptsManager.create(this, this);
|
|
|
|
|
2023-07-04 21:21:34 +00:00
|
|
|
public readonly supportGristNudge: SupportGristNudge = SupportGristNudge.create(this, this);
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
constructor(
|
|
|
|
public readonly topAppModel: TopAppModel,
|
2024-03-23 17:11:06 +00:00
|
|
|
public readonly currentUser: ExtendedUser|null,
|
2020-10-02 15:10:00 +00:00
|
|
|
public readonly currentOrg: Organization|null,
|
|
|
|
public readonly orgError?: OrgError,
|
|
|
|
) {
|
|
|
|
super();
|
2022-03-11 20:35:29 +00:00
|
|
|
this._recordSignUpIfIsNewUser();
|
2022-08-03 10:48:01 +00:00
|
|
|
|
|
|
|
const state = urlState().state.get();
|
|
|
|
if (state.createTeam) {
|
|
|
|
// Remove params from the URL.
|
|
|
|
urlState().pushUrl({createTeam: false, params: {}}, {avoidReload: true, replace: true}).catch(() => {});
|
|
|
|
this.showNewSiteModal(state.params?.planType);
|
|
|
|
}
|
2023-03-28 16:11:40 +00:00
|
|
|
|
2023-06-02 11:25:14 +00:00
|
|
|
G.window.resetDismissedPopups = (seen = false) => {
|
2023-03-28 16:11:40 +00:00
|
|
|
this.dismissedPopups.set(seen ? DismissedPopup.values : []);
|
2023-04-14 10:09:50 +00:00
|
|
|
this.behavioralPromptsManager.reset();
|
2023-03-28 16:11:40 +00:00
|
|
|
};
|
2023-05-01 18:24:23 +00:00
|
|
|
|
2023-07-26 22:31:02 +00:00
|
|
|
this.autoDispose(subscribe(urlState().state, this.topAppModel.orgs, async (_use, s, orgs) => {
|
|
|
|
this._updateLastVisitedOrgDomain(s, orgs);
|
2023-05-01 18:24:23 +00:00
|
|
|
}));
|
2022-03-11 20:35:29 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 17:54:00 +00:00
|
|
|
public get planName() {
|
2022-07-26 17:49:35 +00:00
|
|
|
return this.currentProduct?.name ?? null;
|
2022-06-08 17:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async showUpgradeModal() {
|
|
|
|
if (this.planName && this.currentOrg) {
|
2022-06-29 10:19:20 +00:00
|
|
|
if (this.isPersonal) {
|
|
|
|
this.showNewSiteModal();
|
|
|
|
} else if (this.isTeamSite) {
|
|
|
|
buildUpgradeModal(this, this.planName);
|
|
|
|
} else {
|
|
|
|
throw new Error("Unexpected state");
|
|
|
|
}
|
2022-06-08 17:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 10:48:01 +00:00
|
|
|
public showNewSiteModal(selectedPlan?: string) {
|
2022-06-08 17:54:00 +00:00
|
|
|
if (this.planName) {
|
2022-06-29 10:19:20 +00:00
|
|
|
buildNewSiteModal(this, {
|
|
|
|
planName: this.planName,
|
2022-08-03 10:48:01 +00:00
|
|
|
selectedPlan,
|
2022-06-29 10:19:20 +00:00
|
|
|
onCreate: () => this.topAppModel.fetchUsersAndOrgs().catch(reportError)
|
|
|
|
});
|
2022-06-08 17:54:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public isSupport() {
|
2022-10-14 15:39:15 +00:00
|
|
|
return Boolean(this.currentValidUser?.isSupport);
|
2022-06-08 17:54:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public isBillingManager() {
|
|
|
|
return Boolean(this.currentOrg?.billingAccount?.isManager);
|
|
|
|
}
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
public isOwner() {
|
|
|
|
return Boolean(this.currentOrg && isOwner(this.currentOrg));
|
|
|
|
}
|
|
|
|
|
2023-01-13 07:39:33 +00:00
|
|
|
public isOwnerOrEditor() {
|
|
|
|
return Boolean(this.currentOrg && isOwnerOrEditor(this.currentOrg));
|
|
|
|
}
|
|
|
|
|
2024-03-23 17:11:06 +00:00
|
|
|
public isInstallAdmin(): boolean {
|
|
|
|
return Boolean(this.currentUser?.isInstallAdmin);
|
|
|
|
}
|
|
|
|
|
2022-06-06 16:21:26 +00:00
|
|
|
/**
|
|
|
|
* Fetch and update the current org's usage.
|
|
|
|
*/
|
|
|
|
public async refreshOrgUsage() {
|
2022-12-20 02:06:39 +00:00
|
|
|
if (!this.isOwner()) {
|
2022-06-06 16:21:26 +00:00
|
|
|
// Note: getOrgUsageSummary already checks for owner access; we do an early return
|
|
|
|
// here to skip making unnecessary API calls.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-20 02:06:39 +00:00
|
|
|
const usage = await this.api.getOrgUsageSummary(this.currentOrg!.id);
|
2022-06-06 16:21:26 +00:00
|
|
|
if (!this.isDisposed()) {
|
|
|
|
this.currentOrgUsage.set(usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-23 17:11:06 +00:00
|
|
|
public dismissPopup(name: DismissedPopup, isSeen: boolean): void {
|
|
|
|
markAsSeen(this.dismissedPopups, name, isSeen);
|
2023-03-28 16:11:40 +00:00
|
|
|
}
|
|
|
|
|
2023-09-19 05:31:22 +00:00
|
|
|
public async switchUser(user: FullUser, org?: string) {
|
|
|
|
await this.api.setSessionActive(user.email, org);
|
|
|
|
this.lastVisitedOrgDomain.set(null);
|
|
|
|
}
|
|
|
|
|
2023-07-26 22:31:02 +00:00
|
|
|
private _updateLastVisitedOrgDomain({doc, org}: IGristUrlState, availableOrgs: Organization[]) {
|
|
|
|
if (
|
|
|
|
!org ||
|
|
|
|
// Invalid or inaccessible sites shouldn't be counted as visited.
|
|
|
|
!this.currentOrg ||
|
|
|
|
// Visits to a document shouldn't be counted either.
|
|
|
|
doc
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only count sites that a user has access to (i.e. those listed in the Site Switcher).
|
|
|
|
if (!availableOrgs.some(({domain}) => domain === org)) { return; }
|
|
|
|
|
|
|
|
this.lastVisitedOrgDomain.set(org);
|
|
|
|
}
|
|
|
|
|
2022-03-11 20:35:29 +00:00
|
|
|
/**
|
|
|
|
* If the current user is a new user, record a sign-up event via Google Tag Manager.
|
|
|
|
*/
|
|
|
|
private _recordSignUpIfIsNewUser() {
|
|
|
|
const isNewUser = this.userPrefsObs.get().recordSignUpEvent;
|
|
|
|
if (!isNewUser) { return; }
|
|
|
|
|
|
|
|
// If Google Tag Manager isn't configured, don't record anything.
|
|
|
|
const {tagManagerId} = getGristConfig();
|
|
|
|
if (!tagManagerId) { return; }
|
|
|
|
|
|
|
|
let dataLayer = (window as any).dataLayer;
|
|
|
|
if (!dataLayer) {
|
|
|
|
// Load the Google Tag Manager script into the document.
|
|
|
|
const script = document.createElement('script');
|
|
|
|
script.innerHTML = getTagManagerScript(tagManagerId);
|
|
|
|
document.head.appendChild(script);
|
|
|
|
dataLayer = (window as any).dataLayer;
|
|
|
|
if (!dataLayer) {
|
|
|
|
error(`_recordSignUpIfIsNewUser() failed to load Google Tag Manager`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the sign-up event, and remove the recordSignUpEvent flag from preferences.
|
|
|
|
dataLayer.push({event: 'new-sign-up'});
|
|
|
|
getUserPrefObs(this.userPrefsObs, 'recordSignUpEvent').set(undefined);
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
2022-09-06 01:51:57 +00:00
|
|
|
|
|
|
|
private _getCurrentThemeObs() {
|
|
|
|
return Computed.create(this, this.themePrefs, prefersDarkModeObs(),
|
|
|
|
(_use, themePrefs, prefersDarkMode) => {
|
2023-08-15 17:29:29 +00:00
|
|
|
let {appearance, syncWithOS} = themePrefs;
|
|
|
|
|
|
|
|
const urlParams = urlState().state.get().params;
|
|
|
|
if (urlParams?.themeAppearance) {
|
|
|
|
appearance = urlParams?.themeAppearance;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (urlParams?.themeSyncWithOs !== undefined) {
|
|
|
|
syncWithOS = urlParams?.themeSyncWithOs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (syncWithOS) {
|
2022-09-06 01:51:57 +00:00
|
|
|
appearance = prefersDarkMode ? 'dark' : 'light';
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:29:29 +00:00
|
|
|
let nameOrColors = themePrefs.colors[appearance];
|
|
|
|
if (urlParams?.themeName) {
|
|
|
|
nameOrColors = urlParams?.themeName;
|
|
|
|
}
|
|
|
|
|
2022-09-06 01:51:57 +00:00
|
|
|
let colors: ThemeColors;
|
|
|
|
if (typeof nameOrColors === 'string') {
|
|
|
|
colors = getThemeColors(nameOrColors);
|
|
|
|
} else {
|
|
|
|
colors = nameOrColors;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {appearance, colors};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getHomeUrl(): string {
|
|
|
|
const {host, protocol} = window.location;
|
|
|
|
const gristConfig: any = (window as any).gristConfig;
|
|
|
|
return (gristConfig && gristConfig.homeUrl) || `${protocol}//${host}`;
|
|
|
|
}
|
|
|
|
|
2023-05-23 19:17:28 +00:00
|
|
|
export function newUserAPIImpl(): UserAPIImpl {
|
|
|
|
return new UserAPIImpl(getHomeUrl(), {
|
|
|
|
fetch: hooks.fetch,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export function getOrgNameOrGuest(org: Organization|null, user: FullUser|null) {
|
|
|
|
if (!org) { return ''; }
|
|
|
|
if (user && user.anonymous && org.owner && org.owner.id === user.id) {
|
|
|
|
return "@Guest";
|
|
|
|
}
|
|
|
|
return getOrgName(org);
|
|
|
|
}
|