UNPKG

burdy

Version:

Open Source Headless Platform

225 lines (224 loc) 11.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const react_1 = require("@fluentui/react"); const react_2 = __importStar(require("react")); const react_router_1 = require("react-router"); const use_debounce_1 = require("use-debounce"); const auth_context_1 = require("../../authentication/context/auth.context"); const posts_context_1 = require("../context/posts.context"); const snackbar_1 = require("../../../context/snackbar"); const utility_1 = require("../../../helpers/utility"); const hooks_1 = require("../../../helpers/hooks"); const SitesCommandBar = () => { const { selectedPosts, getPosts, params, setParams, stateData, setStateData, } = (0, posts_context_1.usePosts)(); const history = (0, react_router_1.useHistory)(); const { filterPermissions } = (0, auth_context_1.useAuth)(); const allowedPaths = (0, hooks_1.useAllowedPaths)(); const snackbar = (0, snackbar_1.useSnackbar)(); const debounced = (0, use_debounce_1.useDebouncedCallback)((val) => __awaiter(void 0, void 0, void 0, function* () { setParams({ search: val, }); getPosts.execute({ type: 'page,folder,fragment,hierarchical_post', search: val, }); }), 500); const commandItems = (0, react_2.useMemo)(() => { var _a, _b, _c, _d, _e, _f, _g; const items = filterPermissions([ ((_a = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _a === void 0 ? void 0 : _a.type) !== 'hierarchical_post' ? { key: 'newItem', text: 'New', iconProps: { iconName: 'Add' }, permissions: ['sites_create'], disabled: ((_b = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _b === void 0 ? void 0 : _b.type) === 'hierarchical_post', subMenuProps: { items: [ { key: 'page', text: 'Page', onClick: () => setStateData('createPageOpen', true), }, { key: 'fragment', text: 'Fragment', onClick: () => setStateData('createFragmentOpen', true), }, { key: 'folder', text: 'Folder', onClick: () => setStateData('createFolderOpen', true), }, { key: 'hierarchical_post', text: 'Hierarchical Posts', onClick: () => setStateData('createPostContainerOpen', true), }, ], }, } : { key: 'open', text: 'Open', iconProps: { iconName: 'OpenInNewWindow' }, onClick: () => { var _a; history.push(`/sites/post-container/${(_a = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _a === void 0 ? void 0 : _a.id}`); }, }, ['page', 'hierarchical_post'].includes((_c = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _c === void 0 ? void 0 : _c.type) && (0, utility_1.testPaths)(allowedPaths, (_d = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _d === void 0 ? void 0 : _d.slugPath) ? { key: 'edit', text: 'Edit', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) !== 1 || ((_e = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _e === void 0 ? void 0 : _e.type) === 'folder', iconProps: { iconName: 'Edit' }, split: true, permissions: ['sites_update'], subMenuProps: { items: [ { key: 'preview', text: 'Preview', onClick: () => { var _a; history.push(`/sites/editor/${(_a = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _a === void 0 ? void 0 : _a.id}?editor=preview`); }, }, ], }, onClick: () => { var _a; history.push(`/sites/editor/${(_a = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _a === void 0 ? void 0 : _a.id}`); }, } : { key: 'edit', text: 'Edit', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) !== 1 || ((_f = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _f === void 0 ? void 0 : _f.type) === 'folder', iconProps: { iconName: 'Edit' }, permissions: ['sites_update'], onClick: () => { var _a; history.push(`/sites/editor/${(_a = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _a === void 0 ? void 0 : _a.id}`); }, }, { key: 'settings', text: 'Settings', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) !== 1, iconProps: { iconName: 'Settings' }, permissions: ['sites_update'], onClick: () => { setStateData('updatePostOpen', true); }, }, { key: 'copyItem', text: 'Copy', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) !== 1, iconProps: { iconName: 'Copy' }, permissions: ['sites_create'], onClick: () => { setStateData('copyPostsOpen', true); }, }, { key: 'delete', text: 'Delete', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) === 0, iconProps: { iconName: 'Delete' }, permissions: ['sites_delete'], onClick: () => { setStateData('deletePostsOpen', true); }, }, { key: 'quickPublish', text: 'Publish', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) === 0, iconProps: { iconName: 'WebPublish' }, permissions: ['sites_update'], onClick: () => { setStateData('publishPostOpen', true); }, }, { key: 'quickUnpublish', text: 'Unpublish', disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) === 0, iconProps: { iconName: 'UnpublishContent' }, permissions: ['sites_update'], onClick: () => { setStateData('unpublishPostOpen', true); }, }, { key: 'copyUrl', text: 'Copy API URL', iconProps: { iconName: 'ClipboardList' }, disabled: (selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts.length) === 0 || ((_g = selectedPosts === null || selectedPosts === void 0 ? void 0 : selectedPosts[0]) === null || _g === void 0 ? void 0 : _g.type) === 'folder', onClick: () => { const [selectedPost] = selectedPosts; (0, utility_1.copyToClipboard)(`${window.location.origin}/api/content/${selectedPost.slugPath}`); snackbar.openSnackbar({ message: 'Successfully copied URL to clipboard!', messageBarType: react_1.MessageBarType.success, duration: 1000, }); }, }, { key: 'refresh', text: 'Refresh', iconProps: { iconName: 'Refresh' }, onClick: () => { getPosts.execute(Object.assign({ type: 'page,folder,fragment,hierarchical_post' }, (params || {}))); }, }, ]); return items; }, [selectedPosts, params, stateData]); const farToolbarItems = (0, react_2.useMemo)(() => [ { key: 'search', onRenderIcon: () => (react_2.default.createElement(react_1.SearchBox, { placeholder: "Search posts...", onChange: (_event, newValue) => { debounced(newValue); } })), }, ], []); return (react_2.default.createElement(react_1.CommandBar, { items: commandItems, style: { borderBottom: `1px solid ${react_1.NeutralColors.gray30}` }, farItems: farToolbarItems })); }; exports.default = SitesCommandBar;