Handle marketplace production environment

This commit is contained in:
Rémi de Juvigny 2022-03-22 16:57:01 +01:00
parent 7cbf20c762
commit f853e00e76
4 changed files with 94 additions and 32 deletions

View File

@ -28,7 +28,7 @@ const EllipsisText = styled(Typography)`
overflow: hidden; overflow: hidden;
`; `;
const PluginCard = ({ plugin, installedPluginNames, useYarn }) => { const PluginCard = ({ plugin, installedPluginNames, useYarn, isInDevelopmentMode }) => {
const { attributes } = plugin; const { attributes } = plugin;
const { formatMessage } = useIntl(); const { formatMessage } = useIntl();
const toggleNotification = useNotification(); const toggleNotification = useNotification();
@ -45,6 +45,50 @@ const PluginCard = ({ plugin, installedPluginNames, useYarn }) => {
defaultMessage: 'Made by Strapi', defaultMessage: 'Made by Strapi',
}); });
// Decide to show install button or not
const showInstallButton = () => {
// Already installed
if (isInstalled) {
return (
<Box paddingLeft={4}>
<Icon as={Check} marginRight={2} width={12} height={12} color="success600" />
<Typography variant="omega" textColor="success600" fontWeight="bold">
{formatMessage({
id: 'admin.pages.MarketPlacePage.plugin.installed',
defaultMessage: 'Installed',
})}
</Typography>
</Box>
);
}
// In development, show install button
if (isInDevelopmentMode) {
return (
<CopyToClipboard
onCopy={() => {
trackUsage('willInstallPlugin');
toggleNotification({
type: 'success',
message: { id: 'admin.pages.MarketPlacePage.plugin.copy.success' },
});
}}
text={commandToCopy}
>
<Button size="S" startIcon={<Duplicate />} variant="secondary">
{formatMessage({
id: 'admin.pages.MarketPlacePage.plugin.copy',
defaultMessage: 'Copy install command',
})}
</Button>
</CopyToClipboard>
);
}
// Not in development and plugin not installed already. Show nothing
return null;
};
return ( return (
<Flex <Flex
direction="column" direction="column"
@ -128,40 +172,16 @@ const PluginCard = ({ plugin, installedPluginNames, useYarn }) => {
defaultMessage: 'Learn more', defaultMessage: 'Learn more',
})} })}
</LinkButton> </LinkButton>
{isInstalled ? ( {showInstallButton()}
<Box paddingLeft={4}>
<Icon as={Check} marginRight={2} width={12} height={12} color="success600" />
<Typography variant="omega" textColor="success600" fontWeight="bold">
{formatMessage({
id: 'admin.pages.MarketPlacePage.plugin.installed',
defaultMessage: 'Installed',
})}
</Typography>
</Box>
) : (
<CopyToClipboard
onCopy={() => {
trackUsage('willInstallPlugin');
toggleNotification({
type: 'success',
message: { id: 'admin.pages.MarketPlacePage.plugin.copy.success' },
});
}}
text={commandToCopy}
>
<Button size="S" startIcon={<Duplicate />} variant="secondary">
{formatMessage({
id: 'admin.pages.MarketPlacePage.plugin.copy',
defaultMessage: 'Copy install command',
})}
</Button>
</CopyToClipboard>
)}
</Stack> </Stack>
</Flex> </Flex>
); );
}; };
PluginCard.defaultProps = {
isInDevelopmentMode: false,
};
PluginCard.propTypes = { PluginCard.propTypes = {
plugin: PropTypes.shape({ plugin: PropTypes.shape({
id: PropTypes.string.isRequired, id: PropTypes.string.isRequired,
@ -181,6 +201,7 @@ PluginCard.propTypes = {
}).isRequired, }).isRequired,
installedPluginNames: PropTypes.arrayOf(PropTypes.string).isRequired, installedPluginNames: PropTypes.arrayOf(PropTypes.string).isRequired,
useYarn: PropTypes.bool.isRequired, useYarn: PropTypes.bool.isRequired,
isInDevelopmentMode: PropTypes.bool,
}; };
export default PluginCard; export default PluginCard;

View File

@ -10,6 +10,7 @@ import {
useTracking, useTracking,
LoadingIndicatorPage, LoadingIndicatorPage,
useNotification, useNotification,
useAppInfos,
} from '@strapi/helper-plugin'; } from '@strapi/helper-plugin';
import { Grid, GridItem } from '@strapi/design-system/Grid'; import { Grid, GridItem } from '@strapi/design-system/Grid';
import { Layout, HeaderLayout, ContentLayout } from '@strapi/design-system/Layout'; import { Layout, HeaderLayout, ContentLayout } from '@strapi/design-system/Layout';
@ -46,6 +47,7 @@ const MarketPlacePage = () => {
const trackUsageRef = useRef(trackUsage); const trackUsageRef = useRef(trackUsage);
const toggleNotification = useNotification(); const toggleNotification = useNotification();
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const { autoReload: isInDevelopmentMode } = useAppInfos();
useFocusWhenNavigate(); useFocusWhenNavigate();
@ -101,6 +103,19 @@ const MarketPlacePage = () => {
trackUsageRef.current('didGoToMarketplace'); trackUsageRef.current('didGoToMarketplace');
}, []); }, []);
useEffect(() => {
if (!isInDevelopmentMode) {
toggleNotification({
type: 'info',
message: {
id: 'admin.pages.MarketPlacePage.production',
defaultMessage: 'Manage plugins from the development environment',
},
blockTransition: true,
});
}
}, [toggleNotification, isInDevelopmentMode]);
if (hasFailed) { if (hasFailed) {
return ( return (
<Layout> <Layout>
@ -198,6 +213,7 @@ const MarketPlacePage = () => {
plugin={plugin} plugin={plugin}
installedPluginNames={installedPluginNames} installedPluginNames={installedPluginNames}
useYarn={appInfoResponse.data.useYarn} useYarn={appInfoResponse.data.useYarn}
isInDevelopmentMode={isInDevelopmentMode}
/> />
</GridItem> </GridItem>
))} ))}

View File

@ -10,16 +10,21 @@ import {
import { IntlProvider } from 'react-intl'; import { IntlProvider } from 'react-intl';
import { QueryClient, QueryClientProvider } from 'react-query'; import { QueryClient, QueryClientProvider } from 'react-query';
import { ThemeProvider, lightTheme } from '@strapi/design-system'; import { ThemeProvider, lightTheme } from '@strapi/design-system';
import { useTracking } from '@strapi/helper-plugin'; import { useTracking, useAppInfos } from '@strapi/helper-plugin';
import MarketPlacePage from '../index'; import MarketPlacePage from '../index';
import server from './server'; import server from './server';
const toggleNotification = jest.fn();
jest.mock('@strapi/helper-plugin', () => ({ jest.mock('@strapi/helper-plugin', () => ({
...jest.requireActual('@strapi/helper-plugin'), ...jest.requireActual('@strapi/helper-plugin'),
useNotification: jest.fn(), useNotification: jest.fn(() => {
return toggleNotification;
}),
pxToRem: jest.fn(), pxToRem: jest.fn(),
CheckPagePermissions: ({ children }) => children, CheckPagePermissions: ({ children }) => children,
useTracking: jest.fn(() => ({ trackUsage: jest.fn() })), useTracking: jest.fn(() => ({ trackUsage: jest.fn() })),
useAppInfos: jest.fn(() => ({ autoReload: true })),
})); }));
const client = new QueryClient({ const client = new QueryClient({
@ -1527,4 +1532,23 @@ describe('Marketplace page', () => {
expect(noResult).toBeVisible(); expect(noResult).toBeVisible();
}); });
it('handles production environment', async () => {
// Simulate production environment
useAppInfos.mockImplementation(() => ({ autoReload: false }));
const { queryByText } = render(App);
// Should display notification
expect(toggleNotification).toHaveBeenCalledWith({
type: 'info',
message: {
id: 'admin.pages.MarketPlacePage.production',
defaultMessage: 'Manage plugins from the development environment',
},
blockTransition: true,
});
// Should not show install buttons
expect(queryByText(/copy install command/i)).not.toBeInTheDocument();
});
}); });

View File

@ -61,6 +61,7 @@
"admin.pages.MarketPlacePage.helmet": "Marketplace - Plugins", "admin.pages.MarketPlacePage.helmet": "Marketplace - Plugins",
"admin.pages.MarketPlacePage.title": "Marketplace", "admin.pages.MarketPlacePage.title": "Marketplace",
"admin.pages.MarketPlacePage.subtitle": "Get more out of Strapi", "admin.pages.MarketPlacePage.subtitle": "Get more out of Strapi",
"admin.pages.MarketPlacePage.production": "Manage plugins from the development environment",
"admin.pages.MarketPlacePage.plugin.info.text": "Learn more", "admin.pages.MarketPlacePage.plugin.info.text": "Learn more",
"admin.pages.MarketPlacePage.plugin.info.label": "Learn more about {pluginName}", "admin.pages.MarketPlacePage.plugin.info.label": "Learn more about {pluginName}",
"admin.pages.MarketPlacePage.plugin.copy": "Copy install command", "admin.pages.MarketPlacePage.plugin.copy": "Copy install command",