Files
react-native/packages/rn-tester/js/utils/testerStateUtils.js
T
Nick Gerleman 26b41456d2 Remove RNTester Bookmarks (#41016)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41016

This hasn't been very useful since AsyncStorage/persistence was removed, but takes up a good amount of usable screen real-estate for information in the center of the screen. Remove it.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D50297980

fbshipit-source-id: 296a377dffc89c5c203ca6264351a2a1a8281cc3
2023-10-17 10:32:34 -07:00

112 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and 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 type {
ComponentList,
ExamplesList,
RNTesterModuleInfo,
RNTesterNavigationState,
SectionData,
} from '../types/RNTesterTypes';
import RNTesterList from './RNTesterList';
export const Screens = {
COMPONENTS: 'components',
APIS: 'apis',
};
export const initialNavigationState: RNTesterNavigationState = {
activeModuleKey: null,
activeModuleTitle: null,
activeModuleExampleKey: null,
screen: Screens.COMPONENTS,
recentlyUsed: {components: [], apis: []},
};
const filterEmptySections = (examplesList: ExamplesList): any => {
const filteredSections: {
['apis' | 'components']: Array<SectionData<RNTesterModuleInfo>>,
} = {};
const sectionKeys = Object.keys(examplesList);
sectionKeys.forEach(key => {
filteredSections[key] = examplesList[key].filter(
section => section.data.length > 0,
);
});
return filteredSections;
};
export const getExamplesListWithRecentlyUsed = ({
recentlyUsed,
}: {
recentlyUsed: ComponentList,
}): ExamplesList | null => {
// Return early if state has not been initialized from storage
if (!recentlyUsed) {
return null;
}
const components = RNTesterList.Components.map(
(componentExample): RNTesterModuleInfo => ({
...componentExample,
exampleType: Screens.COMPONENTS,
}),
);
const recentlyUsedComponents = recentlyUsed.components
.map(recentComponentKey =>
components.find(component => component.key === recentComponentKey),
)
.filter(Boolean);
const apis = RNTesterList.APIs.map((apiExample): RNTesterModuleInfo => ({
...apiExample,
exampleType: Screens.APIS,
}));
const recentlyUsedAPIs = recentlyUsed.apis
.map(recentAPIKey =>
apis.find(apiExample => apiExample.key === recentAPIKey),
)
.filter(Boolean);
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',
},
],
};
return filterEmptySections(examplesList);
};