2022-10-28 16:11:08 +00:00
|
|
|
import {makeT} from 'app/client/lib/localization';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {loadUserManager} from 'app/client/lib/imports';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {ImportSourceElement} from 'app/client/lib/ImportSourceElement';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {reportError} from 'app/client/models/AppModel';
|
|
|
|
import {docUrl, urlState} from 'app/client/models/gristUrlState';
|
|
|
|
import {HomeModel} from 'app/client/models/HomeModel';
|
|
|
|
import {getWorkspaceInfo, workspaceName} from 'app/client/models/WorkspaceInfo';
|
|
|
|
import {addNewButton, cssAddNewButton} from 'app/client/ui/AddNewButton';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {docImport, importFromPlugin} from 'app/client/ui/HomeImports';
|
2023-01-09 16:26:09 +00:00
|
|
|
import {
|
|
|
|
cssLinkText, cssMenuTrigger, cssPageEntry, cssPageIcon, cssPageLink, cssSpacer
|
|
|
|
} from 'app/client/ui/LeftPanelCommon';
|
2022-06-17 03:43:26 +00:00
|
|
|
import {createVideoTourToolsButton} from 'app/client/ui/OpenVideoTour';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {transientInput} from 'app/client/ui/transientInput';
|
2022-09-06 01:51:57 +00:00
|
|
|
import {testId, theme} from 'app/client/ui2018/cssVars';
|
2020-10-02 15:10:00 +00:00
|
|
|
import {icon} from 'app/client/ui2018/icons';
|
|
|
|
import {menu, menuIcon, menuItem, upgradableMenuItem, upgradeText} from 'app/client/ui2018/menus';
|
|
|
|
import {confirmModal} from 'app/client/ui2018/modals';
|
2023-05-20 23:58:41 +00:00
|
|
|
import {commonUrls, isFeatureEnabled} from 'app/common/gristUrls';
|
2020-10-02 15:10:00 +00:00
|
|
|
import * as roles from 'app/common/roles';
|
|
|
|
import {Workspace} from 'app/common/UserAPI';
|
2021-08-05 15:12:46 +00:00
|
|
|
import {computed, dom, domComputed, DomElementArg, observable, Observable, styled} from 'grainjs';
|
|
|
|
import {createHelpTools, cssLeftPanel, cssScrollPane,
|
|
|
|
cssSectionHeader, cssTools} from 'app/client/ui/LeftPanelCommon';
|
2020-10-02 15:10:00 +00:00
|
|
|
|
2022-10-28 16:11:08 +00:00
|
|
|
const t = makeT('HomeLeftPane');
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
export function createHomeLeftPane(leftPanelOpen: Observable<boolean>, home: HomeModel) {
|
|
|
|
const creating = observable<boolean>(false);
|
|
|
|
const renaming = observable<Workspace|null>(null);
|
|
|
|
|
|
|
|
return cssContent(
|
|
|
|
dom.autoDispose(creating),
|
|
|
|
dom.autoDispose(renaming),
|
|
|
|
addNewButton(leftPanelOpen,
|
|
|
|
menu(() => addMenu(home, creating), {
|
|
|
|
placement: 'bottom-start',
|
|
|
|
// "Add New" menu should have the same width as the "Add New" button that opens it.
|
|
|
|
stretchToSelector: `.${cssAddNewButton.className}`
|
|
|
|
}),
|
2023-01-13 07:39:33 +00:00
|
|
|
dom.cls('behavioral-prompt-add-new'),
|
|
|
|
testId('dm-add-new'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
cssScrollPane(
|
|
|
|
cssPageEntry(
|
|
|
|
cssPageEntry.cls('-selected', (use) => use(home.currentPage) === "all"),
|
|
|
|
cssPageLink(cssPageIcon('Home'),
|
2022-12-06 13:57:29 +00:00
|
|
|
cssLinkText(t("All Documents")),
|
2020-10-02 15:10:00 +00:00
|
|
|
urlState().setLinkUrl({ws: undefined, homePage: undefined}),
|
|
|
|
testId('dm-all-docs'),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
dom.maybe(use => !use(home.singleWorkspace), () =>
|
2022-10-28 16:11:08 +00:00
|
|
|
cssSectionHeader(
|
2022-12-06 13:57:29 +00:00
|
|
|
t("Workspaces"),
|
2020-10-02 15:10:00 +00:00
|
|
|
// Give it a testId, because it's a good element to simulate "click-away" in tests.
|
|
|
|
testId('dm-ws-label')
|
|
|
|
),
|
|
|
|
),
|
|
|
|
dom.forEach(home.workspaces, (ws) => {
|
|
|
|
if (ws.isSupportWorkspace) { return null; }
|
2022-08-03 15:19:33 +00:00
|
|
|
const info = getWorkspaceInfo(home.app, ws);
|
2020-10-02 15:10:00 +00:00
|
|
|
const isTrivial = computed((use) => Boolean(getWorkspaceInfo(home.app, ws).isDefault &&
|
|
|
|
use(home.singleWorkspace)));
|
|
|
|
// TODO: Introduce a "SwitchSelector" pattern to avoid the need for N computeds (and N
|
|
|
|
// recalculations) to select one of N items.
|
|
|
|
const isRenaming = computed((use) => use(renaming) === ws);
|
|
|
|
return cssPageEntry(
|
|
|
|
dom.autoDispose(isRenaming),
|
|
|
|
dom.autoDispose(isTrivial),
|
|
|
|
dom.hide(isTrivial),
|
|
|
|
cssPageEntry.cls('-selected', (use) => use(home.currentWSId) === ws.id),
|
|
|
|
cssPageLink(cssPageIcon('Folder'), cssLinkText(workspaceName(home.app, ws)),
|
|
|
|
dom.hide(isRenaming),
|
|
|
|
urlState().setLinkUrl({ws: ws.id}),
|
2022-08-03 15:19:33 +00:00
|
|
|
// Don't show menu if workspace is personal and shared by another user; we could
|
|
|
|
// be a bit more nuanced here, but as of today the menu isn't particularly useful
|
|
|
|
// as all the menu options are disabled.
|
|
|
|
!info.self && info.owner ? null : cssMenuTrigger(icon('Dots'),
|
2020-10-02 15:10:00 +00:00
|
|
|
menu(() => workspaceMenu(home, ws, renaming),
|
|
|
|
{placement: 'bottom-start', parentSelectorToMark: '.' + cssPageEntry.className}),
|
|
|
|
|
|
|
|
// Clicks on the menu trigger shouldn't follow the link that it's contained in.
|
|
|
|
dom.on('click', (ev) => { ev.stopPropagation(); ev.preventDefault(); }),
|
|
|
|
testId('dm-workspace-options'),
|
|
|
|
),
|
|
|
|
testId('dm-workspace'),
|
|
|
|
),
|
|
|
|
cssPageEntry.cls('-renaming', isRenaming),
|
|
|
|
dom.maybe(isRenaming, () =>
|
|
|
|
cssPageLink(cssPageIcon('Folder'),
|
|
|
|
cssEditorInput({
|
|
|
|
initialValue: ws.name || '',
|
|
|
|
save: async (val) => (val !== ws.name) ? home.renameWorkspace(ws.id, val) : undefined,
|
|
|
|
close: () => renaming.set(null),
|
|
|
|
}, testId('dm-ws-name-editor'))
|
|
|
|
)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
dom.maybe(creating, () => cssPageEntry(
|
|
|
|
cssPageLink(cssPageIcon('Folder'),
|
|
|
|
cssEditorInput({
|
|
|
|
initialValue: '',
|
|
|
|
save: async (val) => (val !== '') ? home.createWorkspace(val) : undefined,
|
|
|
|
close: () => creating.set(false),
|
|
|
|
}, testId('dm-ws-name-editor'))
|
|
|
|
)
|
|
|
|
)),
|
|
|
|
cssTools(
|
2021-07-28 19:02:06 +00:00
|
|
|
cssPageEntry(
|
2023-05-20 23:58:41 +00:00
|
|
|
dom.show(isFeatureEnabled("templates")),
|
2021-07-28 19:02:06 +00:00
|
|
|
cssPageEntry.cls('-selected', (use) => use(home.currentPage) === "templates"),
|
2023-03-28 16:11:40 +00:00
|
|
|
cssPageLink(cssPageIcon('Board'), cssLinkText(t("Examples & Templates")),
|
2021-07-28 19:02:06 +00:00
|
|
|
urlState().setLinkUrl({homePage: "templates"}),
|
|
|
|
testId('dm-templates-page'),
|
2020-10-02 15:10:00 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
cssPageEntry(
|
|
|
|
cssPageEntry.cls('-selected', (use) => use(home.currentPage) === "trash"),
|
2023-03-28 16:11:40 +00:00
|
|
|
cssPageLink(cssPageIcon('RemoveBig'), cssLinkText(t("Trash")),
|
2020-10-02 15:10:00 +00:00
|
|
|
urlState().setLinkUrl({homePage: "trash"}),
|
|
|
|
testId('dm-trash'),
|
|
|
|
),
|
|
|
|
),
|
2022-06-17 03:43:26 +00:00
|
|
|
cssSpacer(),
|
2023-03-28 16:11:40 +00:00
|
|
|
cssPageEntry(
|
2023-05-20 23:58:41 +00:00
|
|
|
dom.show(isFeatureEnabled('tutorials')),
|
2023-03-28 16:11:40 +00:00
|
|
|
cssPageLink(cssPageIcon('Bookmark'), cssLinkText(t("Tutorial")),
|
|
|
|
{ href: commonUrls.basicTutorial, target: '_blank' },
|
|
|
|
testId('dm-basic-tutorial'),
|
|
|
|
),
|
|
|
|
),
|
2022-06-17 03:43:26 +00:00
|
|
|
createVideoTourToolsButton(),
|
2020-10-02 15:10:00 +00:00
|
|
|
createHelpTools(home.app),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createDocAndOpen(home: HomeModel) {
|
|
|
|
const destWS = home.newDocWorkspace.get();
|
|
|
|
if (!destWS) { return; }
|
|
|
|
try {
|
|
|
|
const docId = await home.createDoc("Untitled document", destWS === "unsaved" ? "unsaved" : destWS.id);
|
|
|
|
// Fetch doc information including urlId.
|
|
|
|
// TODO: consider changing API to return same response as a GET when creating an
|
|
|
|
// object, which is a semi-standard.
|
|
|
|
const doc = await home.app.api.getDoc(docId);
|
|
|
|
await urlState().pushUrl(docUrl(doc));
|
|
|
|
} catch (err) {
|
|
|
|
reportError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function importDocAndOpen(home: HomeModel) {
|
|
|
|
const destWS = home.newDocWorkspace.get();
|
|
|
|
if (!destWS) { return; }
|
|
|
|
const docId = await docImport(home.app, destWS === "unsaved" ? "unsaved" : destWS.id);
|
|
|
|
if (docId) {
|
|
|
|
const doc = await home.app.api.getDoc(docId);
|
|
|
|
await urlState().pushUrl(docUrl(doc));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-05 15:12:46 +00:00
|
|
|
export async function importFromPluginAndOpen(home: HomeModel, source: ImportSourceElement) {
|
|
|
|
try {
|
|
|
|
const destWS = home.newDocWorkspace.get();
|
|
|
|
if (!destWS) { return; }
|
|
|
|
const docId = await importFromPlugin(
|
|
|
|
home.app,
|
|
|
|
destWS === "unsaved" ? "unsaved" : destWS.id,
|
|
|
|
source);
|
|
|
|
if (docId) {
|
|
|
|
const doc = await home.app.api.getDoc(docId);
|
|
|
|
await urlState().pushUrl(docUrl(doc));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
reportError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-02 15:10:00 +00:00
|
|
|
function addMenu(home: HomeModel, creating: Observable<boolean>): DomElementArg[] {
|
|
|
|
const org = home.app.currentOrg;
|
|
|
|
const orgAccess: roles.Role|null = org ? org.access : null;
|
|
|
|
const needUpgrade = home.app.currentFeatures.maxWorkspacesPerOrg === 1;
|
|
|
|
|
|
|
|
return [
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItem(() => createDocAndOpen(home), menuIcon('Page'), t("Create Empty Document"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', !home.newDocWorkspace.get()),
|
|
|
|
testId("dm-new-doc")
|
|
|
|
),
|
2022-12-06 13:57:29 +00:00
|
|
|
menuItem(() => importDocAndOpen(home), menuIcon('Import'), t("Import Document"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', !home.newDocWorkspace.get()),
|
|
|
|
testId("dm-import")
|
|
|
|
),
|
2021-08-05 15:12:46 +00:00
|
|
|
domComputed(home.importSources, importSources => ([
|
|
|
|
...importSources.map((source, i) =>
|
|
|
|
menuItem(() => importFromPluginAndOpen(home, source),
|
|
|
|
menuIcon('Import'),
|
|
|
|
source.importSource.label,
|
2021-08-09 13:14:03 +00:00
|
|
|
dom.cls('disabled', !home.newDocWorkspace.get()),
|
2021-08-05 15:12:46 +00:00
|
|
|
testId(`dm-import-plugin`)
|
|
|
|
))
|
|
|
|
])),
|
2020-10-02 15:10:00 +00:00
|
|
|
// For workspaces: if ACL says we can create them, but product says we can't,
|
|
|
|
// then offer an upgrade link.
|
2022-12-06 13:57:29 +00:00
|
|
|
upgradableMenuItem(needUpgrade, () => creating.set(true), menuIcon('Folder'), t("Create Workspace"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', (use) => !roles.canEdit(orgAccess) || !use(home.available)),
|
|
|
|
testId("dm-new-workspace")
|
|
|
|
),
|
2022-07-26 17:49:35 +00:00
|
|
|
upgradeText(needUpgrade, () => home.app.showUpgradeModal()),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function workspaceMenu(home: HomeModel, ws: Workspace, renaming: Observable<Workspace|null>) {
|
|
|
|
function deleteWorkspace() {
|
2022-12-06 13:57:29 +00:00
|
|
|
confirmModal(t("Delete {{workspace}} and all included documents?", {workspace: ws.name}), t("Delete"),
|
2020-10-02 15:10:00 +00:00
|
|
|
() => home.deleteWorkspace(ws.id, false),
|
2023-04-20 13:07:45 +00:00
|
|
|
{explanation: t("Workspace will be moved to Trash.")});
|
2020-10-02 15:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function manageWorkspaceUsers() {
|
|
|
|
const api = home.app.api;
|
|
|
|
const user = home.app.currentUser;
|
|
|
|
(await loadUserManager()).showUserManagerModal(api, {
|
|
|
|
permissionData: api.getWorkspaceAccess(ws.id),
|
2022-05-19 04:51:48 +00:00
|
|
|
activeUser: user,
|
2020-10-02 15:10:00 +00:00
|
|
|
resourceType: 'workspace',
|
2022-04-12 19:31:41 +00:00
|
|
|
resourceId: ws.id,
|
|
|
|
resource: ws,
|
2020-10-02 15:10:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const needUpgrade = home.app.currentFeatures.maxWorkspacesPerOrg === 1;
|
|
|
|
|
|
|
|
return [
|
2022-10-28 16:11:08 +00:00
|
|
|
upgradableMenuItem(needUpgrade, () => renaming.set(ws), t("Rename"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', !roles.canEdit(ws.access)),
|
|
|
|
testId('dm-rename-workspace')),
|
2022-10-28 16:11:08 +00:00
|
|
|
upgradableMenuItem(needUpgrade, deleteWorkspace, t("Delete"),
|
2020-10-02 15:10:00 +00:00
|
|
|
dom.cls('disabled', user => !roles.canEdit(ws.access)),
|
|
|
|
testId('dm-delete-workspace')),
|
2022-08-03 15:19:33 +00:00
|
|
|
// TODO: Personal plans can't currently share workspaces, but that restriction
|
|
|
|
// should formally be documented and defined in `Features`, with this check updated
|
|
|
|
// to look there instead.
|
|
|
|
home.app.isPersonal ? null : upgradableMenuItem(needUpgrade, manageWorkspaceUsers,
|
2022-12-06 13:57:29 +00:00
|
|
|
roles.canEditAccess(ws.access) ? t("Manage Users") : t("Access Details"),
|
2020-10-02 15:10:00 +00:00
|
|
|
testId('dm-workspace-access')),
|
2022-07-26 17:49:35 +00:00
|
|
|
upgradeText(needUpgrade, () => home.app.showUpgradeModal()),
|
2020-10-02 15:10:00 +00:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Below are all the styled elements.
|
|
|
|
|
|
|
|
const cssContent = styled(cssLeftPanel, `
|
|
|
|
--page-icon-margin: 12px;
|
|
|
|
`);
|
|
|
|
|
|
|
|
export const cssEditorInput = styled(transientInput, `
|
|
|
|
height: 24px;
|
|
|
|
flex: 1 1 0px;
|
|
|
|
min-width: 0px;
|
2022-09-06 01:51:57 +00:00
|
|
|
background-color: ${theme.inputBg};
|
2020-10-02 15:10:00 +00:00
|
|
|
margin-right: 16px;
|
|
|
|
font-size: inherit;
|
|
|
|
`);
|