|
| 1 | +/** |
| 2 | + * ------------------------------------------------------------------------------------------- |
| 3 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. |
| 4 | + * See License in the project root for license information. |
| 5 | + * ------------------------------------------------------------------------------------------- |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { useState } from 'react'; |
| 9 | +import { addons, types } from '@storybook/addons'; |
| 10 | +import { STORIES_CONFIGURED, STORY_MISSING } from '@storybook/core-events'; |
| 11 | +import { AddonPanel } from '@storybook/components'; |
| 12 | +import { useParameter, useChannel } from '@storybook/api'; |
| 13 | +import { Providers, MsalProvider, LoginType, ProviderState } from '../dist/commonjs'; |
| 14 | +import { CLIENTID, GETPROVIDER_EVENT, SETPROVIDER_EVENT } from './env'; |
| 15 | + |
| 16 | +const PARAM_KEY = 'signInAddon'; |
| 17 | +const _allow_signin = false; |
| 18 | + |
| 19 | +const msalProvider = new MsalProvider({ |
| 20 | + clientId: CLIENTID, |
| 21 | + loginType: LoginType.Popup |
| 22 | +}); |
| 23 | + |
| 24 | +Providers.globalProvider = msalProvider; |
| 25 | + |
| 26 | +const SignInPanel = () => { |
| 27 | + const value = useParameter(PARAM_KEY, null); |
| 28 | + |
| 29 | + const [state, setState] = useState(Providers.globalProvider.state); |
| 30 | + |
| 31 | + const emit = useChannel({ |
| 32 | + STORY_RENDERED: id => { |
| 33 | + console.log('storyRendered', id); |
| 34 | + }, |
| 35 | + [GETPROVIDER_EVENT]: params => { |
| 36 | + emitProvider(state); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + const emitProvider = loginState => { |
| 41 | + emit(SETPROVIDER_EVENT, { state: loginState }); |
| 42 | + }; |
| 43 | + |
| 44 | + Providers.onProviderUpdated(() => { |
| 45 | + setState(Providers.globalProvider.state); |
| 46 | + emitProvider(Providers.globalProvider.state); |
| 47 | + }); |
| 48 | + |
| 49 | + emitProvider(state); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div> |
| 53 | + {_allow_signin ? ( |
| 54 | + <mgt-login /> |
| 55 | + ) : ( |
| 56 | + 'All components are using mock data - sign in function will be available in a future release' |
| 57 | + )} |
| 58 | + </div> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +addons.register('microsoft/graph-toolkit', storybookAPI => { |
| 63 | + storybookAPI.on(STORIES_CONFIGURED, (kind, story) => { |
| 64 | + if (storybookAPI.getUrlState().path === '/story/*') { |
| 65 | + storybookAPI.selectStory('mgt-login', 'login'); |
| 66 | + } |
| 67 | + }); |
| 68 | + storybookAPI.on(STORY_MISSING, (kind, story) => { |
| 69 | + storybookAPI.selectStory('mgt-login', 'login'); |
| 70 | + }); |
| 71 | + |
| 72 | + const render = ({ active, key }) => ( |
| 73 | + <AddonPanel active={active} key={key}> |
| 74 | + <SignInPanel /> |
| 75 | + </AddonPanel> |
| 76 | + ); |
| 77 | + |
| 78 | + addons.add('mgt/sign-in', { |
| 79 | + type: types.PANEL, |
| 80 | + title: 'Sign In', |
| 81 | + render, |
| 82 | + paramKey: PARAM_KEY |
| 83 | + }); |
| 84 | +}); |
0 commit comments