Skip to content

Commit

Permalink
fix: Maintain order of themes & widths as specified in config (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltaranto committed Dec 20, 2019
1 parent d57e538 commit 7aa5962
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
7 changes: 1 addition & 6 deletions src/Playroom/Playroom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ export interface PlayroomProps {
widths: number[];
}

export default ({
components,
themes,
widths: configuredWidths
}: PlayroomProps) => {
const widths = configuredWidths.sort((a, b) => a - b);
export default ({ components, themes, widths }: PlayroomProps) => {
const [
{
editorPosition,
Expand Down
2 changes: 1 addition & 1 deletion src/Playroom/ViewPreference/ViewPreference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default <PreferenceType extends string | number>({
const newVisiblePreference = [...visible, preference];
const isOriginalList =
JSON.stringify(newVisiblePreference.sort()) ===
JSON.stringify(available.sort());
JSON.stringify([...available].sort());

onChange(isOriginalList ? undefined : newVisiblePreference);
} else {
Expand Down
30 changes: 21 additions & 9 deletions src/StoreContext/StoreContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ type Action =
| { type: 'updateVisibleWidths'; payload: { widths: number[] } }
| { type: 'resetVisibleWidths' };

const reducer = (state: State, action: Action): State => {
interface CreateReducerParams {
themes: PlayroomProps['themes'];
widths: PlayroomProps['widths'];
}
const createReducer = ({
themes: configuredThemes,
widths: configuredWidths
}: CreateReducerParams) => (state: State, action: Action): State => {
switch (action.type) {
case 'initialLoad': {
return {
Expand Down Expand Up @@ -115,11 +122,12 @@ const reducer = (state: State, action: Action): State => {

case 'updateVisibleThemes': {
const { themes } = action.payload;
store.setItem('visibleThemes', themes);
const visibleThemes = configuredThemes.filter(t => themes.includes(t));
store.setItem('visibleThemes', visibleThemes);

return {
...state,
visibleThemes: themes
visibleThemes
};
}

Expand All @@ -132,13 +140,12 @@ const reducer = (state: State, action: Action): State => {

case 'updateVisibleWidths': {
const { widths } = action.payload;
const orderedWidths = widths.sort((a, b) => a - b);

store.setItem('visibleWidths', orderedWidths);
const visibleWidths = configuredWidths.filter(w => widths.includes(w));
store.setItem('visibleWidths', visibleWidths);

return {
...state,
visibleWidths: orderedWidths
visibleWidths
};
}

Expand Down Expand Up @@ -171,12 +178,17 @@ export const StoreContext = createContext<StoreContextValues>([

export const StoreProvider = ({
children,
themes
themes,
widths
}: {
children: ReactNode;
themes: PlayroomProps['themes'];
widths: PlayroomProps['widths'];
}) => {
const [state, dispatch] = useReducer(reducer, initialState);
const [state, dispatch] = useReducer(
createReducer({ themes, widths }),
initialState
);
const [debouncedCodeUpdate] = useDebouncedCallback(
(params: DebounceUpdateUrl) => {
// Ensure that when removing theme/width preferences
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const renderPlayroom = ({
const themeNames = Object.keys(themes);

render(
<StoreProvider themes={themeNames}>
<StoreProvider themes={themeNames} widths={widths}>
<Playroom components={components} widths={widths} themes={themeNames} />
</StoreProvider>,
outlet
Expand Down

0 comments on commit 7aa5962

Please sign in to comment.