2022-01-07 18:11:52 +00:00
|
|
|
import {AppModel, reportError} from 'app/client/models/AppModel';
|
2022-03-17 02:32:17 +00:00
|
|
|
import {urlState} from 'app/client/models/gristUrlState';
|
2022-09-06 01:51:57 +00:00
|
|
|
import * as css from 'app/client/ui/AccountPageCss';
|
2022-01-07 18:11:52 +00:00
|
|
|
import {ApiKey} from 'app/client/ui/ApiKey';
|
|
|
|
import {AppHeader} from 'app/client/ui/AppHeader';
|
2022-03-17 02:32:17 +00:00
|
|
|
import {buildChangePasswordDialog} from 'app/client/ui/ChangePasswordDialog';
|
2022-01-07 18:11:52 +00:00
|
|
|
import {leftPanelBasic} from 'app/client/ui/LeftPanelCommon';
|
2022-01-19 19:41:06 +00:00
|
|
|
import {MFAConfig} from 'app/client/ui/MFAConfig';
|
2022-01-07 18:11:52 +00:00
|
|
|
import {pagePanels} from 'app/client/ui/PagePanels';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {ThemeConfig} from 'app/client/ui/ThemeConfig';
|
2022-01-07 18:11:52 +00:00
|
|
|
import {createTopBarHome} from 'app/client/ui/TopBar';
|
|
|
|
import {transientInput} from 'app/client/ui/transientInput';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {cssBreadcrumbs, separator} from 'app/client/ui2018/breadcrumbs';
|
2022-02-14 21:26:21 +00:00
|
|
|
import {labeledSquareCheckbox} from 'app/client/ui2018/checkbox';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {cssLink} from 'app/client/ui2018/links';
|
2023-01-24 13:13:18 +00:00
|
|
|
import {select} from 'app/client/ui2018/menus';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {getGristConfig} from 'app/common/urlUtils';
|
2022-03-17 02:32:17 +00:00
|
|
|
import {FullUser} from 'app/common/UserAPI';
|
2023-01-24 13:13:18 +00:00
|
|
|
import {detectCurrentLang, makeT} from 'app/client/lib/localization';
|
|
|
|
import {translateLocale} from 'app/client/ui/LanguageMenu';
|
2022-01-07 18:11:52 +00:00
|
|
|
import {Computed, Disposable, dom, domComputed, makeTestId, Observable, styled} from 'grainjs';
|
|
|
|
|
|
|
|
const testId = makeTestId('test-account-page-');
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('AccountPage');
|
2022-01-07 18:11:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the account page where a user can manage their profile settings.
|
|
|
|
*/
|
|
|
|
export class AccountPage extends Disposable {
|
|
|
|
private _apiKey = Observable.create<string>(this, '');
|
|
|
|
private _userObs = Observable.create<FullUser|null>(this, null);
|
|
|
|
private _isEditingName = Observable.create(this, false);
|
|
|
|
private _nameEdit = Observable.create<string>(this, '');
|
|
|
|
private _isNameValid = Computed.create(this, this._nameEdit, (_use, val) => checkName(val));
|
2022-02-14 21:26:21 +00:00
|
|
|
private _allowGoogleLogin = Computed.create(this, (use) => use(this._userObs)?.allowGoogleLogin ?? false)
|
|
|
|
.onWrite((val) => this._updateAllowGooglelogin(val));
|
2022-01-07 18:11:52 +00:00
|
|
|
|
|
|
|
constructor(private _appModel: AppModel) {
|
|
|
|
super();
|
|
|
|
this._fetchAll().catch(reportError);
|
|
|
|
}
|
|
|
|
|
|
|
|
public buildDom() {
|
|
|
|
const panelOpen = Observable.create(this, false);
|
|
|
|
return pagePanels({
|
|
|
|
leftPanel: {
|
|
|
|
panelWidth: Observable.create(this, 240),
|
|
|
|
panelOpen,
|
|
|
|
hideOpener: true,
|
|
|
|
header: dom.create(AppHeader, this._appModel.currentOrgName, this._appModel),
|
|
|
|
content: leftPanelBasic(this._appModel, panelOpen),
|
|
|
|
},
|
|
|
|
headerMain: this._buildHeaderMain(),
|
|
|
|
contentMain: this._buildContentMain(),
|
2022-09-06 01:51:57 +00:00
|
|
|
testId,
|
2022-01-07 18:11:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private _buildContentMain() {
|
2022-09-06 01:51:57 +00:00
|
|
|
const {enableCustomCss} = getGristConfig();
|
2023-01-24 13:13:18 +00:00
|
|
|
const supportedLngs = getGristConfig().supportedLngs ?? ['en'];
|
|
|
|
const languageOptions = supportedLngs
|
|
|
|
.map((lng) => ({value: lng, label: translateLocale(lng)!}))
|
|
|
|
.sort((a, b) => a.value.localeCompare(b.value));
|
|
|
|
|
|
|
|
const userLocale = Computed.create(this, use => {
|
|
|
|
const selected = detectCurrentLang();
|
|
|
|
if (!supportedLngs.includes(selected)) { return 'en'; }
|
|
|
|
return selected;
|
|
|
|
});
|
|
|
|
userLocale.onWrite(async value => {
|
|
|
|
await this._appModel.api.updateUserLocale(value || null);
|
|
|
|
// Reload the page to apply the new locale.
|
|
|
|
window.location.reload();
|
|
|
|
});
|
|
|
|
|
2022-01-07 18:11:52 +00:00
|
|
|
return domComputed(this._userObs, (user) => user && (
|
2022-09-06 01:51:57 +00:00
|
|
|
css.container(css.accountPage(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.header(t("Account settings")),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.dataRow(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.inlineSubHeader(t("Email")),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.email(user.email),
|
2022-01-24 20:38:32 +00:00
|
|
|
),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.dataRow(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.inlineSubHeader(t("Name")),
|
2022-01-07 18:11:52 +00:00
|
|
|
domComputed(this._isEditingName, (isEditing) => (
|
|
|
|
isEditing ? [
|
|
|
|
transientInput(
|
|
|
|
{
|
|
|
|
initialValue: user.name,
|
|
|
|
save: (val) => this._isNameValid.get() && this._updateUserName(val),
|
|
|
|
close: () => { this._isEditingName.set(false); this._nameEdit.set(''); },
|
|
|
|
},
|
2023-01-24 13:13:18 +00:00
|
|
|
{size: '5'}, // Lower size so that input can shrink below ~152px.
|
2022-01-07 18:11:52 +00:00
|
|
|
dom.on('input', (_ev, el) => this._nameEdit.set(el.value)),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.flexGrow.cls(''),
|
2022-01-07 18:11:52 +00:00
|
|
|
),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.textBtn(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.icon('Settings'), t("Save"),
|
2022-01-07 18:11:52 +00:00
|
|
|
// No need to save on 'click'. The transient input already does it on close.
|
|
|
|
),
|
|
|
|
] : [
|
2022-09-06 01:51:57 +00:00
|
|
|
css.name(user.name),
|
|
|
|
css.textBtn(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.icon('Settings'), t("Edit"),
|
2022-01-07 18:11:52 +00:00
|
|
|
dom.on('click', () => this._isEditingName.set(true)),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
)),
|
|
|
|
testId('username'),
|
|
|
|
),
|
|
|
|
// show warning for invalid name but not for the empty string
|
2023-01-24 13:13:18 +00:00
|
|
|
dom.maybe(use => use(this._nameEdit) && !use(this._isNameValid), this._buildNameWarningsDom.bind(this)),
|
2022-12-06 13:57:29 +00:00
|
|
|
css.header(t("Password & Security")),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.dataRow(
|
2022-12-06 13:57:29 +00:00
|
|
|
css.inlineSubHeader(t("Login Method")),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.loginMethod(user.loginMethod),
|
2022-12-06 13:57:29 +00:00
|
|
|
user.loginMethod === 'Email + Password' ? css.textBtn(t("Change Password"),
|
2022-03-17 02:32:17 +00:00
|
|
|
dom.on('click', () => this._showChangePasswordDialog()),
|
2022-01-07 18:11:52 +00:00
|
|
|
) : null,
|
|
|
|
testId('login-method'),
|
|
|
|
),
|
2022-01-19 19:41:06 +00:00
|
|
|
user.loginMethod !== 'Email + Password' ? null : dom.frag(
|
2022-09-06 01:51:57 +00:00
|
|
|
css.dataRow(
|
2022-02-14 21:26:21 +00:00
|
|
|
labeledSquareCheckbox(
|
|
|
|
this._allowGoogleLogin,
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Allow signing in to this account with Google"),
|
2022-02-14 21:26:21 +00:00
|
|
|
testId('allow-google-login-checkbox'),
|
|
|
|
),
|
|
|
|
testId('allow-google-login'),
|
|
|
|
),
|
2022-12-06 13:57:29 +00:00
|
|
|
css.subHeader(t("Two-factor authentication")),
|
2022-09-06 01:51:57 +00:00
|
|
|
css.description(
|
2023-01-03 16:45:14 +00:00
|
|
|
t("Two-factor authentication is an extra layer of security for your Grist account " +
|
|
|
|
"designed to ensure that you're the only person who can access your account, " +
|
|
|
|
"even if someone knows your password.")
|
2022-01-19 19:41:06 +00:00
|
|
|
),
|
2022-03-17 02:32:17 +00:00
|
|
|
dom.create(MFAConfig, user),
|
2022-01-19 19:41:06 +00:00
|
|
|
),
|
2022-09-06 01:51:57 +00:00
|
|
|
// Custom CSS is incompatible with custom themes.
|
|
|
|
enableCustomCss ? null : [
|
2022-12-06 13:57:29 +00:00
|
|
|
css.header(t("Theme")),
|
2022-09-06 01:51:57 +00:00
|
|
|
dom.create(ThemeConfig, this._appModel),
|
2023-01-30 14:52:56 +00:00
|
|
|
css.subHeader(t("Language")),
|
2023-01-24 13:13:18 +00:00
|
|
|
css.dataRow({ style: 'width: 300px'},
|
|
|
|
select(userLocale, languageOptions, {
|
|
|
|
renderOptionArgs: () => {
|
|
|
|
return dom.cls(cssFirstUpper.className);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
testId('language'),
|
|
|
|
)
|
2022-09-06 01:51:57 +00:00
|
|
|
],
|
2022-12-06 13:57:29 +00:00
|
|
|
css.header(t("API")),
|
|
|
|
css.dataRow(css.inlineSubHeader(t("API Key")), css.content(
|
2022-01-07 18:11:52 +00:00
|
|
|
dom.create(ApiKey, {
|
|
|
|
apiKey: this._apiKey,
|
|
|
|
onCreate: () => this._createApiKey(),
|
|
|
|
onDelete: () => this._deleteApiKey(),
|
|
|
|
anonymous: false,
|
2023-01-24 13:13:18 +00:00
|
|
|
inputArgs: [{size: '5'}], // Lower size so that input can shrink below ~152px.
|
2022-01-07 18:11:52 +00:00
|
|
|
})
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
testId('body'),
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private _buildHeaderMain() {
|
|
|
|
return dom.frag(
|
2023-01-24 13:13:18 +00:00
|
|
|
cssBreadcrumbs({style: 'margin-left: 16px;'},
|
2022-09-06 01:51:57 +00:00
|
|
|
cssLink(
|
2022-01-07 18:11:52 +00:00
|
|
|
urlState().setLinkUrl({}),
|
|
|
|
'Home',
|
|
|
|
testId('home'),
|
|
|
|
),
|
|
|
|
separator(' / '),
|
|
|
|
dom('span', 'Account'),
|
|
|
|
),
|
|
|
|
createTopBarHome(this._appModel),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _fetchApiKey() {
|
|
|
|
this._apiKey.set(await this._appModel.api.fetchApiKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _createApiKey() {
|
|
|
|
this._apiKey.set(await this._appModel.api.createApiKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _deleteApiKey() {
|
|
|
|
await this._appModel.api.deleteApiKey();
|
|
|
|
this._apiKey.set('');
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _fetchUserProfile() {
|
|
|
|
this._userObs.set(await this._appModel.api.getUserProfile());
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _fetchAll() {
|
|
|
|
await Promise.all([
|
|
|
|
this._fetchApiKey(),
|
|
|
|
this._fetchUserProfile(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private async _updateUserName(val: string) {
|
|
|
|
const user = this._userObs.get();
|
|
|
|
if (user && val && val === user.name) { return; }
|
|
|
|
|
|
|
|
await this._appModel.api.updateUserName(val);
|
|
|
|
await this._fetchAll();
|
|
|
|
}
|
2022-02-14 21:26:21 +00:00
|
|
|
|
|
|
|
private async _updateAllowGooglelogin(allowGoogleLogin: boolean) {
|
|
|
|
await this._appModel.api.updateAllowGoogleLogin(allowGoogleLogin);
|
|
|
|
await this._fetchUserProfile();
|
|
|
|
}
|
2022-01-07 18:11:52 +00:00
|
|
|
|
2022-03-17 02:32:17 +00:00
|
|
|
private _showChangePasswordDialog() {
|
|
|
|
return buildChangePasswordDialog();
|
|
|
|
}
|
2023-01-24 13:13:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Builds dom to show marning messages to the user.
|
|
|
|
*/
|
|
|
|
private _buildNameWarningsDom() {
|
|
|
|
return cssWarnings(
|
|
|
|
t("Names only allow letters, numbers and certain special characters"),
|
|
|
|
testId('username-warning'),
|
|
|
|
);
|
|
|
|
}
|
2022-01-07 18:11:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 05:50:26 +00:00
|
|
|
/**
|
|
|
|
* We allow alphanumeric characters and certain common whitelisted characters (except at the start),
|
|
|
|
* plus everything non-ASCII (for non-English alphabets, which we want to allow but it's hard to be
|
|
|
|
* more precise about what exactly to allow).
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
const VALID_NAME_REGEXP = /^(\w|[^\u0000-\u007F])(\w|[- ./'"()]|[^\u0000-\u007F])*$/;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test name against various rules to check if it is a valid username.
|
|
|
|
*/
|
|
|
|
export function checkName(name: string): boolean {
|
|
|
|
return VALID_NAME_REGEXP.test(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-24 13:13:18 +00:00
|
|
|
|
|
|
|
const cssWarnings = styled(css.warning, `
|
2022-01-07 18:11:52 +00:00
|
|
|
margin: -8px 0 0 110px;
|
|
|
|
`);
|
2023-01-24 13:13:18 +00:00
|
|
|
|
|
|
|
const cssFirstUpper = styled('div', `
|
|
|
|
& > div::first-letter {
|
|
|
|
text-transform: capitalize;
|
|
|
|
}
|
|
|
|
`);
|