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

[NoQA] Adjust useOnyx selector for collections #49918

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions src/hooks/useReportIDs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ONYXKEYS from '@src/ONYXKEYS';
import type * as OnyxTypes from '@src/types/onyx';
import type {Message} from '@src/types/onyx/ReportAction';
import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems';
import useActiveWorkspace from './useActiveWorkspace';
import useCurrentReportID from './useCurrentReportID';
import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails';
Expand Down Expand Up @@ -42,7 +43,7 @@
Object.values(reportActions)
.filter(Boolean)
.map((reportAction) => {
const {reportActionID, actionName, errors = [], originalMessage} = reportAction;

Check failure on line 46 in src/hooks/useReportIDs.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

'originalMessage' is deprecated. Used in old report actions before migration. Replaced by using getOriginalMessage function
const message = ReportActionsUtils.getReportActionMessage(reportAction);
const decision = message?.moderationDecision?.decision;

Expand Down Expand Up @@ -81,8 +82,8 @@
}: ReportIDsContextProviderProps) {
const [priorityMode] = useOnyx(ONYXKEYS.NVP_PRIORITY_MODE, {initialValue: CONST.PRIORITY_MODE.DEFAULT});
const [chatReports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: policySelector});
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {selector: reportActionsSelector});
const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: (c) => mapOnyxCollectionItems(c, policySelector)});

Check failure on line 85 in src/hooks/useReportIDs.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'OnyxEntry<{ id: string; name: string; role: ValueOf<{ readonly ADMIN: "admin"; readonly AUDITOR: "auditor"; readonly USER: "user"; }>; type: ValueOf<{ readonly PERSONAL: "personal"; readonly CORPORATE: "corporate"; readonly TEAM: "team"; }>; ... 70 more ...; workspaceAccountID?: number | undefined; } & Partial<...> ...' is not assignable to parameter of type 'OnyxCollection<OnyxEntry<{ id: string; name: string; role: ValueOf<{ readonly ADMIN: "admin"; readonly AUDITOR: "auditor"; readonly USER: "user"; }>; type: ValueOf<{ readonly PERSONAL: "personal"; readonly CORPORATE: "corporate"; readonly TEAM: "team"; }>; ... 70 more ...; workspaceAccountID?: number | undefined; } ...'.
const [allReportActions] = useOnyx(ONYXKEYS.COLLECTION.REPORT_ACTIONS, {selector: (c) => mapOnyxCollectionItems(c, reportActionsSelector)});

Check failure on line 86 in src/hooks/useReportIDs.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'OnyxEntry<ReportActions>' is not assignable to parameter of type 'OnyxCollection<OnyxEntry<ReportActions>>'.
const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS);
const [reportsDrafts] = useOnyx(ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT);
const [betas] = useOnyx(ONYXKEYS.BETAS);
Expand All @@ -101,7 +102,7 @@
currentReportID ?? null,
chatReports,
betas,
policies,

Check failure on line 105 in src/hooks/useReportIDs.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'OnyxCollection<Record<string, PolicySelector | undefined>>' is not assignable to parameter of type 'OnyxCollection<PolicySelector>'.
priorityMode,
allReportActions,
transactionViolations,
Expand Down
3 changes: 2 additions & 1 deletion src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import type SCREENS from '@src/SCREENS';
import type {PersonalDetails, Report} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems';

type ProfilePageProps = StackScreenProps<ProfileNavigatorParamList, typeof SCREENS.PROFILE_ROOT>;

Expand Down Expand Up @@ -76,7 +77,7 @@
};

function ProfilePage({route}: ProfilePageProps) {
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: chatReportSelector});
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT, {selector: (c) => mapOnyxCollectionItems(c, chatReportSelector)});

Check failure on line 80 in src/pages/ProfilePage.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'OnyxEntry<{ avatarUrl?: string | undefined; avatarFileName?: string | undefined; chatType?: ValueOf<{ readonly POLICY_ANNOUNCE: "policyAnnounce"; readonly POLICY_ADMINS: "policyAdmins"; readonly TRIP_ROOM: "tripRoom"; ... 6 more ...; readonly SYSTEM: "system"; }> | undefined; ... 71 more ...; visibleChatMemberAccoun...' is not assignable to parameter of type 'OnyxCollection<OnyxEntry<{ avatarUrl?: string | undefined; avatarFileName?: string | undefined; chatType?: ValueOf<{ readonly POLICY_ANNOUNCE: "policyAnnounce"; readonly POLICY_ADMINS: "policyAdmins"; readonly TRIP_ROOM: "tripRoom"; ... 6 more ...; readonly SYSTEM: "system"; }> | undefined; ... 71 more ...; visibleC...'.
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const [personalDetailsMetadata] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_METADATA);
const [session] = useOnyx(ONYXKEYS.SESSION);
Expand All @@ -90,7 +91,7 @@
const reportKey = useMemo(() => {
const reportID = isCurrentUser
? ReportUtils.findSelfDMReportID()
: ReportUtils.getChatByParticipants(session?.accountID ? [accountID, session.accountID] : [], reports)?.reportID ?? '-1';

Check failure on line 94 in src/pages/ProfilePage.tsx

View workflow job for this annotation

GitHub Actions / typecheck

Argument of type 'OnyxCollection<Record<string, OnyxEntry<{ avatarUrl?: string | undefined; avatarFileName?: string | undefined; chatType?: ValueOf<{ readonly POLICY_ANNOUNCE: "policyAnnounce"; readonly POLICY_ADMINS: "policyAdmins"; ... 7 more ...; readonly SYSTEM: "system"; }> | undefined; ... 71 more ...; visibleChatMemberAccountI...' is not assignable to parameter of type 'OnyxCollection<{ avatarUrl?: string | undefined; avatarFileName?: string | undefined; chatType?: ValueOf<{ readonly POLICY_ANNOUNCE: "policyAnnounce"; readonly POLICY_ADMINS: "policyAdmins"; readonly TRIP_ROOM: "tripRoom"; ... 6 more ...; readonly SYSTEM: "system"; }> | undefined; ... 71 more ...; visibleChatMemberA...'.

if (SessionActions.isAnonymousUser() || !reportID) {
return `${ONYXKEYS.COLLECTION.REPORT}0` as const;
Expand Down
8 changes: 8 additions & 0 deletions src/utils/mapOnyxCollectionItems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {OnyxCollection} from 'react-native-onyx';

export default function mapOnyxCollectionItems<Item, Collection extends OnyxCollection<Item>, RT>(collection: Collection, mapper: (item: Item) => RT) {
return Object.entries(collection ?? {}).reduce((acc: NonNullable<OnyxCollection<RT>>, [key, item]) => {
acc[key] = mapper(item as Item);
return acc;
}, {});
}
Loading