mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: Changelog: [Internal] Update title when navigating to an example Reviewed By: kacieb Differential Revision: D29545092 fbshipit-source-id: 7ce2f7d978601a5e11e2d60a8fd17be0249ce122
149 lines
3.4 KiB
JavaScript
149 lines
3.4 KiB
JavaScript
/**
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*
|
|
* @format
|
|
* @flow
|
|
*/
|
|
|
|
import {AsyncStorage} from 'react-native';
|
|
|
|
import RNTesterList from './RNTesterList';
|
|
|
|
import type {
|
|
ExamplesList,
|
|
RNTesterState,
|
|
ComponentList,
|
|
} from '../types/RNTesterTypes';
|
|
|
|
export const Screens = {
|
|
COMPONENTS: 'components',
|
|
APIS: 'apis',
|
|
BOOKMARKS: 'bookmarks',
|
|
};
|
|
|
|
export const initialState: RNTesterState = {
|
|
activeModuleKey: null,
|
|
activeModuleTitle: null,
|
|
activeModuleExampleKey: null,
|
|
screen: null,
|
|
bookmarks: null,
|
|
recentlyUsed: null,
|
|
};
|
|
|
|
const filterEmptySections = (examplesList: ExamplesList): any => {
|
|
const filteredSections = {};
|
|
const sectionKeys = Object.keys(examplesList);
|
|
|
|
sectionKeys.forEach(key => {
|
|
filteredSections[key] = examplesList[key].filter(
|
|
section => section.data.length > 0,
|
|
);
|
|
});
|
|
|
|
return filteredSections;
|
|
};
|
|
|
|
export const getExamplesListWithBookmarksAndRecentlyUsed = ({
|
|
bookmarks,
|
|
recentlyUsed,
|
|
}: {
|
|
bookmarks: ComponentList,
|
|
recentlyUsed: ComponentList,
|
|
}): ExamplesList | null => {
|
|
// Return early if state has not been initialized from storage
|
|
if (!bookmarks || !recentlyUsed) {
|
|
return null;
|
|
}
|
|
|
|
const components = RNTesterList.Components.map(componentExample => ({
|
|
...componentExample,
|
|
isBookmarked: bookmarks.components.includes(componentExample.key),
|
|
exampleType: Screens.COMPONENTS,
|
|
}));
|
|
|
|
const recentlyUsedComponents = recentlyUsed.components
|
|
.map(recentComponentKey =>
|
|
components.find(component => component.key === recentComponentKey),
|
|
)
|
|
.filter(Boolean);
|
|
|
|
const bookmarkedComponents = components.filter(
|
|
component => component.isBookmarked,
|
|
);
|
|
|
|
const apis = RNTesterList.APIs.map(apiExample => ({
|
|
...apiExample,
|
|
isBookmarked: bookmarks.apis.includes(apiExample.key),
|
|
exampleType: Screens.APIS,
|
|
}));
|
|
|
|
const recentlyUsedAPIs = recentlyUsed.apis
|
|
.map(recentAPIKey => apis.find(apiEample => apiEample.key === recentAPIKey))
|
|
.filter(Boolean);
|
|
|
|
const bookmarkedAPIs = apis.filter(apiEample => apiEample.isBookmarked);
|
|
|
|
const examplesList: ExamplesList = {
|
|
[Screens.COMPONENTS]: [
|
|
{
|
|
key: 'RECENT_COMPONENTS',
|
|
data: recentlyUsedComponents,
|
|
title: 'Recently Viewed',
|
|
},
|
|
{
|
|
key: 'COMPONENTS',
|
|
data: components,
|
|
title: 'Components',
|
|
},
|
|
],
|
|
[Screens.APIS]: [
|
|
{
|
|
key: 'RECENT_APIS',
|
|
data: recentlyUsedAPIs,
|
|
title: 'Recently viewed',
|
|
},
|
|
{
|
|
key: 'APIS',
|
|
data: apis,
|
|
title: 'APIs',
|
|
},
|
|
],
|
|
[Screens.BOOKMARKS]: [
|
|
{
|
|
key: 'COMPONENTS',
|
|
data: bookmarkedComponents,
|
|
title: 'Components',
|
|
},
|
|
{
|
|
key: 'APIS',
|
|
data: bookmarkedAPIs,
|
|
title: 'APIs',
|
|
},
|
|
],
|
|
};
|
|
|
|
return filterEmptySections(examplesList);
|
|
};
|
|
|
|
export const getInitialStateFromAsyncStorage = async (
|
|
storageKey: string,
|
|
): Promise<RNTesterState> => {
|
|
const initialStateString = await AsyncStorage.getItem(storageKey);
|
|
|
|
if (!initialStateString) {
|
|
return {
|
|
activeModuleKey: null,
|
|
activeModuleTitle: null,
|
|
activeModuleExampleKey: null,
|
|
screen: Screens.COMPONENTS,
|
|
bookmarks: {components: [], apis: []},
|
|
recentlyUsed: {components: [], apis: []},
|
|
};
|
|
} else {
|
|
return JSON.parse(initialStateString);
|
|
}
|
|
};
|