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

[TS migration] Migrate 'TagPicker' component to TypeScript #34561

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
133 changes: 133 additions & 0 deletions src/components/TagPicker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import {useMemo, useState} from 'react';
import type {OnyxCollection} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import useLocalize from '@hooks/useLocalize';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
import type SafeAreaInsetProps from '@pages/SafeAreaInsetProps';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PolicyTags, RecentlyUsedTags} from '@src/types/onyx';
import OptionsSelector from './OptionsSelector';

type TagPickerOnyxProps = {
/** Collection of tags attached to a policy */
policyTags: OnyxCollection<PolicyTags>;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** List of recently used tags */
policyRecentlyUsedTags: OnyxCollection<RecentlyUsedTags>;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** Should show the selected option that is disabled? */
shouldShowDisabledAndSelectedOption?: boolean;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
};

type TagPickerProps = TagPickerOnyxProps & {
/** The policyID we are getting tags for */
// eslint-disable-next-line react/no-unused-prop-types
policyID: string;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

/** The selected tag of the money request */
selectedTag: string;

/** The name of tag list we are getting tags for */
tag: string;

/** Callback to submit the selected tag */
onSubmit: () => void;

/**
* Safe area insets required for reflecting the portion of the view,
* that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.
*/
insets: SafeAreaInsetProps;
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
};

function TagPicker({selectedTag, tag, policyTags, policyRecentlyUsedTags, shouldShowDisabledAndSelectedOption, insets, onSubmit}: TagPickerProps) {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {translate} = useLocalize();
const [searchValue, setSearchValue] = useState('');

const policyTagList = PolicyUtils.getTagList(policyTags, tag);
const policyTagsCount = Object.values(policyTagList).filter((policyTag) => policyTag.enabled).length;
const isTagsCountBelowThreshold = policyTagsCount < CONST.TAG_LIST_THRESHOLD;

const shouldShowTextInput = !isTagsCountBelowThreshold;

const selectedOptions = useMemo(() => {
if (!selectedTag) {
return [];
}

return [
{
name: selectedTag,
enabled: true,
accountID: null,
},
];
}, [selectedTag]);

const enabledTags = useMemo(() => {
if (!shouldShowDisabledAndSelectedOption) {
return policyTagList;
}
const selectedNames = selectedOptions.map((s) => s.name);
const tags = [...selectedOptions, ...Object.values(policyTagList).filter((policyTag) => policyTag.enabled && !selectedNames.includes(policyTag.name))];

return tags;
}, [selectedOptions, policyTagList, shouldShowDisabledAndSelectedOption]);

//
const sections = useMemo(() => {
const policyRecentlyUsedTagsList = policyRecentlyUsedTags?.tag ?? [];

// TODO: Remove this and one line under once OptionsListUtils (https://github.com/Expensify/App/issues/24921) is migrated to TypeScript.
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return (
// @ts-expect-error TODO: Remove this once OptionsListUtils (https://github.com/Expensify/App/issues/24921) is migrated to TypeScript.
OptionsListUtils.getFilteredOptions({}, {}, [], searchValue, selectedOptions, [], false, false, false, {}, [], true, enabledTags, policyRecentlyUsedTagsList, false).tagOptions
);
}, [searchValue, enabledTags, selectedOptions, policyRecentlyUsedTags?.tag]);

const headerMessage = OptionsListUtils.getHeaderMessageForNonUserList((sections?.[0]?.data?.length ?? 0) > 0, searchValue);

// @ts-expect-error TODO: Remove this once OptionsListUtils (https://github.com/Expensify/App/issues/24921) is migrated to TypeScript.
const selectedOptionKey = sections[0]?.data?.filter((policyTag) => policyTag.searchText === selectedTag)?.[0]?.keyForList;

return (
<OptionsSelector
// @ts-expect-error TODO: Remove this once OptionsSelector (https://github.com/Expensify/App/issues/25125) is migrated to TypeScript.
contentContainerStyles={[{paddingBottom: StyleUtils.getSafeAreaMargins(insets).marginBottom}]}
optionHoveredStyle={styles.hoveredComponentBG}
sectionHeaderStyle={styles.mt5}
sections={sections}
selectedOptions={selectedOptions}
headerMessage={headerMessage}
textInputLabel={translate('common.search')}
boldStyle
highlightSelectedOptions
isRowMultilineSupported
shouldShowTextInput={shouldShowTextInput}
// Focus the first option when searching
focusedIndex={0}
// Focus the selected option on first load
initiallyFocusedOptionKey={selectedOptionKey}
onChangeText={setSearchValue}
onSelectRow={onSubmit}
/>
);
}

TagPicker.displayName = 'TagPicker';

export default withOnyx<TagPickerProps, TagPickerOnyxProps>({
policyTags: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}` as typeof ONYXKEYS.COLLECTION.POLICY_TAGS,
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
},
policyRecentlyUsedTags: {
key: ({policyID}) => `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${policyID}` as typeof ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS,
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
},
})(TagPicker);
94 changes: 0 additions & 94 deletions src/components/TagPicker/index.js
fvlvte marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

41 changes: 0 additions & 41 deletions src/components/TagPicker/tagPickerPropTypes.js

This file was deleted.

5 changes: 2 additions & 3 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,13 @@ function getTagListName(policyTags: OnyxEntry<PolicyTags>) {
/**
* Gets the tags of a policy for a specific key. Defaults to the first tag if no key is provided.
*/
function getTagList(policyTags: OnyxCollection<PolicyTags>, tagKey: string) {
function getTagList(policyTags: OnyxCollection<PolicyTags>, tagKey: string): PolicyTags {
if (Object.keys(policyTags ?? {})?.length === 0) {
return {};
}

const policyTagKey = tagKey ?? Object.keys(policyTags ?? {})[0];

return policyTags?.[policyTagKey]?.tags ?? {};
return (policyTags?.[policyTagKey]?.tags as unknown as PolicyTags) ?? {};
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
}

function isPendingDeletePolicy(policy: OnyxEntry<Policy>): boolean {
Expand Down
8 changes: 8 additions & 0 deletions src/pages/SafeAreaInsetProps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type SafeAreaInsetProps = {
fvlvte marked this conversation as resolved.
Show resolved Hide resolved
top: number;
left: number;
right: number;
bottom: number;
};

export default SafeAreaInsetProps;
Loading