Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scope selector to the documentation #55

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/docs/.storybook/addon-drop-in-css/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ export const Tool = () => {

return (
<IconButton
autoFocus={null}
rev={null}
content={null}
nonce={null}
rel={null}
key={TOOL_ID}
active={active}
title={ADDON_TITLE}
active={active}
onClick={toggle}
>
<Icons icon="markup" />
Expand Down
7 changes: 6 additions & 1 deletion packages/docs/.storybook/addon-gh-repository/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ export const Tool = () => {
<>
<Separator />
<IconButton
autoFocus={null}
rev={null}
content={null}
nonce={null}
rel={null}
key={TOOL_ID}
active={false}
title={ADDON_NAME}
active={false}
>

<A target='_blank' href={REPOSITORY_URL}>
Expand Down
7 changes: 6 additions & 1 deletion packages/docs/.storybook/addon-minicart-css/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ export const Tool = () => {

return (
<IconButton
autoFocus={null}
rev={null}
content={null}
nonce={null}
rel={null}
key={TOOL_ID}
active={active}
title={ADDON_TITLE}
active={active}
onClick={toggle}
>
<Icons icon="markup" />
Expand Down
63 changes: 63 additions & 0 deletions packages/docs/.storybook/addon-scope-selector/Tool.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { IconButton, Icons, Separator, WithTooltip, TooltipLinkList, } from '@storybook/components'
import React, { useCallback } from 'react'
import { SCOPES, PARAM_KEY, getSelectedScopeKey, ADDON_TITLE, TOOL_ID } from './constants'
import { styled, color } from "@storybook/theming"

const scopes = Object.keys(SCOPES) as (keyof typeof SCOPES)[]

type Scope = keyof typeof SCOPES

export const Tool = () => {
const isActive = useCallback(
(scope: Scope) => {
return getSelectedScopeKey() === scope
},
[]
)

return (
<>
<Separator />
<WithTooltip
placement="top"
trigger="click"
tooltip={() => (
<TooltipLinkList
links={scopes.map((scope) => ({
id: scope,
title: <LinkTitle active={isActive(scope)}>{scope}</LinkTitle>,
right: <LinkIcon icon="check" width={12} height={12} active={isActive(scope)} />,
onClick: () => {
localStorage.setItem(PARAM_KEY, scope)
location.reload()
},
active: isActive(scope),
}))}
/>
)}
>
<IconButton
autoFocus={null}
rev={null}
content={null}
nonce={null}
rel={null}
key={TOOL_ID}
title={ADDON_TITLE}
active={scopes.some(isActive)}
>
<Icons icon="basket" />&nbsp;&nbsp;{getSelectedScopeKey()}
</IconButton>
</WithTooltip>
</>
)
}

const LinkTitle = styled.span<{ active?: boolean }>(({ active }) => ({
color: active ? color.secondary : "inherit",
}))

const LinkIcon = styled(Icons)<{ active?: boolean }>(({ active }) => ({
opacity: active ? 1 : 0,
path: { fill: active ? color.secondary : "inherit" },
}))
23 changes: 23 additions & 0 deletions packages/docs/.storybook/addon-scope-selector/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const DESCRIPTION = 'Select an authorization scope.'
export const ADDON_ID = 'addon-scope-selector' as const
export const ADDON_TITLE = DESCRIPTION
export const TOOL_ID = `${ADDON_ID}/tool` as const
export const PARAM_KEY = ADDON_ID
export const SCOPES = {
'market: USA': 'market:11709',
'market: Europe': 'market:11708'
} as const

export type Scope = keyof typeof SCOPES

export function getSelectedScopeKey() {
const fromLocalStorage = localStorage.getItem(PARAM_KEY) ?? ''
const availableScopes = Object.keys(SCOPES)

return (availableScopes.includes(fromLocalStorage) ? fromLocalStorage : availableScopes[0]) as Scope
}

export function getSelectedScopeValue() {
const selectedScopeKey = getSelectedScopeKey()
return SCOPES[selectedScopeKey]
}
13 changes: 13 additions & 0 deletions packages/docs/.storybook/addon-scope-selector/manager.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { addons, types } from '@storybook/addons'
import React from 'react'
import { Tool } from './Tool'
import { ADDON_ID, ADDON_TITLE } from './constants'

addons.register(ADDON_ID, () => {
addons.add(ADDON_ID, {
title: ADDON_TITLE,
type: types.TOOL,
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
render: () => <Tool />,
})
})
3 changes: 2 additions & 1 deletion packages/docs/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const storybookConfig: StorybookConfig = {
managerEntries: [
require.resolve('./addon-drop-in-css/manager.tsx'),
require.resolve('./addon-minicart-css/manager.tsx'),
require.resolve('./addon-gh-repository/manager.tsx')
require.resolve('./addon-scope-selector/manager.tsx'),
require.resolve('./addon-gh-repository/manager.tsx'),
],
framework: {
name: '@storybook/html-webpack5',
Expand Down
7 changes: 3 additions & 4 deletions packages/docs/.storybook/preview.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { defineCustomElements } from '@commercelayer/drop-in.js/dist/loader'
import { Preview } from '@storybook/html'
import { clConfig } from '../stories/assets/constants'
import { createConfig } from '../stories/assets/constants'
import { FILENAME as DROP_IN_CSS_FILENAME, PARAM_KEY as DROP_IN_CSS_PARAM_KEY } from './addon-drop-in-css/constants'
import { FILENAME as MINICART_CSS_FILENAME, PARAM_KEY as MINICART_CSS_PARAM_KEY } from './addon-minicart-css/constants'
import { getSelectedScopeValue } from './addon-scope-selector/constants'

import customElements, { type JsonDocsProp } from '@commercelayer/drop-in.js/dist/custom-elements'

Expand Down Expand Up @@ -150,9 +151,7 @@ const preview: Preview = {
},
(story) => {
// @ts-expect-error
window.commercelayerConfig = {
...clConfig
}
window.commercelayerConfig = createConfig(getSelectedScopeValue())

return story()
},
Expand Down
12 changes: 7 additions & 5 deletions packages/docs/stories/assets/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ export const codes = {
}

// // stg
// export const clConfig = {
// // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
// export const createConfig = (scope: string) => ({
// clientId: '2oh-47l4PN_yb8p4mp7AxUsb-Ue3e7tBqteoZcQM-nU',
// slug: 'drop-in-js-stg',
// scope: 'market:545',
// debug: 'all',
// domain: 'commercelayer.co'
// }
// })

// prd
export const clConfig = {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const createConfig = (scope: string) => ({
clientId: 'kuSKPbeKbU9LG9LjndzieKWRcfiXFuEfO0OYHXKH9J8',
slug: 'drop-in-js',
scope: 'market:11709',
scope,
debug: 'all'
}
})
3 changes: 2 additions & 1 deletion packages/docs/stories/getting-started.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Meta, Source } from '@storybook/blocks'
import { linkTo } from '@storybook/addon-links'
import { Alert } from './assets/components'
import { getSelectedScopeValue } from '../.storybook/addon-scope-selector/constants'

<Meta title="Getting started" />

Expand Down Expand Up @@ -30,7 +31,7 @@ Assuming you already have a Commerce Layer account ([sign up](https://dashboard.
window.commercelayerConfig = {
clientId: 'kuSKPbeKbU9LG9LjndzieKWRcfiXFuEfO0OYHXKH9J8',
slug: 'drop-in-js',
scope: 'market:11709',
scope: '${getSelectedScopeValue()}',
debug: 'all', // default is 'none'
orderReturnUrl: 'https://example.com' // optional
}
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/stories/introduction.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const Basic: StoryFn = () => {
<section class="grid gap-6 sm:grid-cols-2 lg:grid-cols-3 text-center">

<!-- 1st product: Gray Five-Panel Cap with White Logo -->
<div class="flex flex-col items-start">
<div class="flex flex-col">
<div class="h-80 sm:h-64">
<img
src="https://data.commercelayer.app/seed/images/skus/${codes.outOfStock}_FLAT.png"
Expand Down Expand Up @@ -83,7 +83,7 @@ export const Basic: StoryFn = () => {
</div>

<!-- Editorial Banner -->
<div class="flex flex-col items-start">
<div class="flex flex-col">
<cl-identity-status class="h-full" type="guest">
<img class="object-contain position-top" src="register.jpg" />
</cl-identity-status>
Expand Down
Loading