-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(context): add valhalla contexts
- Loading branch information
1 parent
f7dc6cb
commit d2197b6
Showing
123 changed files
with
2,224 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import '../src/style.css'; | ||
import { DefaultTestProviders } from '../src/utils'; | ||
|
||
// https://storybook.js.org/docs/react/writing-stories/parameters#global-parameters | ||
export const parameters = { | ||
actions: { argTypesRegex: '^on[A-Z].*' }, | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/, | ||
}, | ||
}, | ||
// https://storybook.js.org/docs/react/essentials/actions#automatically-matching-args | ||
actions: { argTypesRegex: '^on.*' }, | ||
viewMode: 'docs', | ||
}; | ||
|
||
export const decorators = [ | ||
(Story) => ( | ||
<DefaultTestProviders> | ||
<Story /> | ||
</DefaultTestProviders> | ||
), | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Formatting } from './formatting.model'; | ||
|
||
export const DEFAULT_FORMATTING: Formatting = { | ||
date: 'E, d MMM yyyy', | ||
dateTime: 'E, d MMM yyy HH:mm:ss', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import React, { createContext, ReactNode } from 'react'; | ||
|
||
import { useContextFallback } from '../../hooks'; | ||
import { DEFAULT_FORMATTING } from './formatting.constant'; | ||
import { Formatting } from './formatting.model'; | ||
|
||
export const FormattingContext = createContext<Formatting | undefined>( | ||
undefined | ||
); | ||
|
||
type FormattingProps = { children: ReactNode; formatting?: Formatting }; | ||
|
||
export const FormattingProvider = ({ | ||
children, | ||
formatting = DEFAULT_FORMATTING, | ||
}: FormattingProps) => ( | ||
<FormattingContext.Provider value={formatting}> | ||
{children} | ||
</FormattingContext.Provider> | ||
); | ||
|
||
export const useFormatting = () => useContextFallback(FormattingContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type Formatting = { | ||
date: string; | ||
dateTime: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './formatting.constant'; | ||
export * from './formatting.context'; | ||
export * from './formatting.model'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './formatting'; | ||
export * from './notifications'; | ||
export * from './valhalla.context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './notifications.context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import React, { | ||
createContext, | ||
ReactNode, | ||
useCallback, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
import { useContextFallback } from '../../hooks'; | ||
import { NotificationGateway } from '../../ui'; | ||
import { Notification } from '../../ui/notification/notification.model'; | ||
|
||
export type NotificationsState = { | ||
notifications: Notification[]; | ||
addNotification: (notification: Omit<Notification, 'id'>) => void; | ||
removeNotification: (id: string) => void; | ||
}; | ||
|
||
export const NotificationsContext = createContext< | ||
NotificationsState | undefined | ||
>(undefined); | ||
|
||
type NotificationsProviderProps = { | ||
children: ReactNode; | ||
}; | ||
|
||
let notificationCounter = 0; | ||
|
||
export const NotificationsProvider = ({ | ||
children, | ||
}: NotificationsProviderProps) => { | ||
const [notifications, setNotifications] = useState<Notification[]>([]); | ||
|
||
const addNotification = useCallback( | ||
(notification: Omit<Notification, 'id'>) => { | ||
const id = `${notificationCounter++}`; | ||
|
||
setNotifications((notifications) => [ | ||
...notifications, | ||
{ | ||
...notification, | ||
id, | ||
}, | ||
]); | ||
}, | ||
[] | ||
); | ||
|
||
const removeNotification = useCallback((id: string) => { | ||
setNotifications((notifications) => | ||
notifications.filter((notification) => notification.id !== id) | ||
); | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
notifications, | ||
addNotification, | ||
removeNotification, | ||
}), | ||
[notifications, addNotification, removeNotification] | ||
); | ||
|
||
return ( | ||
<NotificationsContext.Provider value={value}> | ||
<NotificationGateway /> | ||
|
||
{children} | ||
</NotificationsContext.Provider> | ||
); | ||
}; | ||
|
||
export const useNotifications = () => useContextFallback(NotificationsContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './tooltip.context'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Provider } from '@radix-ui/react-tooltip'; | ||
|
||
export const TooltipProvider = Provider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React, { ReactNode } from 'react'; | ||
|
||
import { I18n } from '../i18n'; | ||
import { | ||
DEFAULT_FORMATTING, | ||
Formatting, | ||
FormattingProvider, | ||
} from './formatting'; | ||
import { NotificationsProvider } from './notifications'; | ||
import { TooltipProvider } from './tooltip'; | ||
|
||
type ValhallaProviderProps<Locales extends 'en'> = { | ||
children: ReactNode; | ||
language: Locales; | ||
locales: Record<Locales, any>; | ||
formatting?: Formatting; | ||
}; | ||
|
||
export const ValhallaProvider = <Locales extends 'en'>({ | ||
children, | ||
formatting = DEFAULT_FORMATTING, | ||
language, | ||
locales, | ||
}: ValhallaProviderProps<Locales>) => ( | ||
<I18n language={language} locales={locales}> | ||
<FormattingProvider formatting={formatting}> | ||
<NotificationsProvider> | ||
<TooltipProvider>{children}</TooltipProvider> | ||
</NotificationsProvider> | ||
</FormattingProvider> | ||
</I18n> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import { IntlProvider } from 'react-intl'; | ||
|
||
import { AVAILABLE_LOCALES } from './i18n.constant'; | ||
import { flattenMessages } from './i18n.utils'; | ||
|
||
type I18nProps<Locales extends 'en'> = { | ||
/** | ||
* language which should be used in this context | ||
* | ||
* @defaultValue en | ||
*/ | ||
language: Locales; | ||
locales: Record<Locales, any>; | ||
children?: React.ReactNode; | ||
}; | ||
|
||
export const I18n = <Locales extends 'en'>({ | ||
language, | ||
locales, | ||
children, | ||
}: I18nProps<Locales>) => { | ||
const supportedLanguages = Object.keys(locales); | ||
const currentLocale = supportedLanguages.some((l) => l === language) | ||
? language | ||
: ('en' as Locales); | ||
|
||
const mergedMessages = | ||
currentLocale in AVAILABLE_LOCALES | ||
? { | ||
...AVAILABLE_LOCALES[currentLocale], | ||
...locales[currentLocale], | ||
} | ||
: locales[currentLocale]; | ||
|
||
return ( | ||
<IntlProvider | ||
locale={currentLocale} | ||
messages={flattenMessages(mergedMessages)} | ||
defaultLocale={currentLocale} | ||
> | ||
{children} | ||
</IntlProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as en from './locales/en.json'; | ||
import * as storybook from './locales/storybook.json'; | ||
|
||
export const AVAILABLE_LOCALES = { | ||
en: (en as any).default || en, | ||
}; | ||
|
||
export const STORYBOOK_LOCALES = (storybook as any).default || storybook; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* flattenMessages traverse nested messages object and return it | ||
* as a flat object | ||
* | ||
* @example | ||
* flattenMessages({ | ||
* A: { | ||
* B: { | ||
* C: 'TEST' | ||
* }, | ||
* D: 'Hello!' | ||
* } | ||
* }) == { | ||
* 'A.B.C': 'TEST', | ||
* 'A.D': 'Hello!', | ||
* } | ||
* | ||
* @remarks | ||
* | ||
* flattenMessages use recursion underneath for unwrapping nested objects | ||
* | ||
* @param object - nested messages object | ||
* @param [prefix=''] - prefix that should be prepended to each key in flat object, shouldn't be used by developer | ||
* | ||
* @returns flat messages object | ||
* | ||
* @private | ||
*/ | ||
export const flattenMessages = ( | ||
object: Record<string, any>, | ||
prefix: string = '' | ||
) => | ||
Object.keys(object).reduce((translations: any, key) => { | ||
const value = object[key]; | ||
const prefixedKey: string = prefix ? `${prefix}.${key}` : key; | ||
if (typeof value === 'string') { | ||
translations[prefixedKey] = value; | ||
} else { | ||
Object.assign(translations, flattenMessages(value, prefixedKey)); | ||
} | ||
return translations; | ||
}, {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { VALHALLA_EN_LOCALES } from './locales'; | ||
export * from './i18n.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"VALHALLA": { | ||
"ACTIONS": { | ||
"CLOSE": "Close" | ||
}, | ||
"CHECKBOX": { | ||
"ALL": "All" | ||
}, | ||
"PAGINATION": { | ||
"NEXT": "Next", | ||
"PREV": "Previous", | ||
"RESULTS": "{from}-{to} of {total}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as COMMON_EN_LOCALES from './en.json'; | ||
|
||
export { COMMON_EN_LOCALES as VALHALLA_EN_LOCALES }; |
Oops, something went wrong.