@craftercms/studio-ui
Version:
Services, components, models & utils to build CrafterCMS authoring extensions.
108 lines (106 loc) • 4.21 kB
JavaScript
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var __rest =
(this && this.__rest) ||
function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === 'function')
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
import * as React from 'react';
import MenuItem from '@mui/material/MenuItem';
import { FormattedMessage } from 'react-intl';
import Select from '@mui/material/Select';
import useStyles from './styles';
import { isBlank } from '../../utils/string';
import { changeSite } from '../../state/actions/sites';
import { setSiteCookie } from '../../utils/auth';
import { useDispatch } from 'react-redux';
import { useEnv } from '../../hooks/useEnv';
import { useSiteList } from '../../hooks/useSiteList';
import { getSystemLink } from '../../utils/system';
import { PREVIEW_URL_PATH } from '../../utils/constants';
import useMinimizedDialogWarning from '../../hooks/useMinimizedDialogWarning';
function SiteSwitcherSelect(props) {
var _a;
const { site } = props,
rest = __rest(props, ['site']);
const sites = useSiteList();
const { classes, cx: clsx } = useStyles();
const { authoringBase, useBaseDomain } = useEnv();
const dispatch = useDispatch();
const checkMinimized = useMinimizedDialogWarning();
const onSiteChange = ({ target: { value } }) => {
if (!isBlank(value) && site !== value && !checkMinimized()) {
if (window.location.href.includes(PREVIEW_URL_PATH)) {
dispatch(changeSite(value));
} else {
setSiteCookie(value, useBaseDomain);
setTimeout(
() =>
(window.location.href = getSystemLink({
site: value,
systemLinkId: 'preview',
authoringBase
}))
);
}
}
};
return React.createElement(
Select,
Object.assign({ displayEmpty: true, variant: 'standard' }, rest, {
className: clsx(classes.menuRoot, props.className),
classes: Object.assign(Object.assign({}, props.classes), {
select: clsx(classes.input, (_a = props.classes) === null || _a === void 0 ? void 0 : _a.select, classes.menu)
}),
value: site,
onChange: onSiteChange
}),
sites.length === 0 &&
React.createElement(
MenuItem,
{ value: '' },
React.createElement(FormattedMessage, {
id: 'siteSwitcherSelected.siteSelectorNoSiteSelected',
defaultMessage: 'Choose site'
})
),
sites.map(({ id, name }) =>
React.createElement(MenuItem, { key: id, value: id, className: classes.menuItem }, name)
)
);
}
export default SiteSwitcherSelect;