-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathuseCardFeeds.tsx
More file actions
82 lines (68 loc) · 3.63 KB
/
useCardFeeds.tsx
File metadata and controls
82 lines (68 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {useMemo} from 'react';
import type {OnyxCollection, ResultMetadata} from 'react-native-onyx';
import {getCompanyCardFeedWithDomainID} from '@libs/CardUtils';
import ONYXKEYS from '@src/ONYXKEYS';
import type {CardFeeds, CompanyCardFeed, CompanyCardFeedWithDomainID} from '@src/types/onyx';
import type {CardFeedWithDomainID, CardFeedWithNumber, CustomCardFeedData, DirectCardFeedData} from '@src/types/onyx/CardFeeds';
import useOnyx from './useOnyx';
import useWorkspaceAccountID from './useWorkspaceAccountID';
type CombinedCardFeed = CustomCardFeedData &
Partial<DirectCardFeedData> & {
/** Custom feed name, originally coming from settings.companyCardNicknames */
customFeedName?: string;
/** Feed name */
feed: CardFeedWithNumber;
};
type CombinedCardFeeds = Record<CardFeedWithDomainID, CombinedCardFeed>;
/**
* This is a custom hook that combines workspace and domain card feeds for a given policy.
*
* This hook:
* - Gets all available feeds (ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER) from Onyx.
* - Extracts and compiles card feeds data including only feeds where the `preferredPolicy` matches the `policyID`.
*
* @param policyID - The workspace policyID to filter and construct card feeds for.
* @returns -
* A tuple containing:
* 1. Combined workspace and domain card feeds specific to the given policyID (or `undefined` if unavailable).
* 2. The result metadata from the Onyx collection fetch.
* 3. Card feeds specific to the given policyID (or `undefined` if unavailable).
*/
const useCardFeeds = (policyID: string | undefined): [CombinedCardFeeds | undefined, ResultMetadata<OnyxCollection<CardFeeds>>, CardFeeds | undefined] => {
const workspaceAccountID = useWorkspaceAccountID(policyID);
const [allFeeds, allFeedsResult] = useOnyx(ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER, {canBeMissing: true});
const defaultFeed = allFeeds?.[`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`];
const workspaceFeeds = useMemo(() => {
if (!policyID || !allFeeds) {
return undefined;
}
const result: CombinedCardFeeds = {};
return Object.entries(allFeeds).reduce<CombinedCardFeeds>((acc, [onyxKey, feed]) => {
if (!feed?.settings?.companyCards) {
return acc;
}
for (const [key, feedSettings] of Object.entries(feed.settings.companyCards)) {
const feedName = key as CompanyCardFeed;
const feedOAuthAccountDetails = feed.settings.oAuthAccountDetails?.[feedName];
const feedCompanyCardNickname = feed.settings.companyCardNicknames?.[feedName];
const domainID = onyxKey.split('_').at(-1);
const shouldAddFeed = domainID && (feedSettings?.preferredPolicy ? feedSettings.preferredPolicy === policyID : domainID === workspaceAccountID.toString());
if (!shouldAddFeed) {
continue;
}
const combinedFeedKey = getCompanyCardFeedWithDomainID(feedName, domainID);
acc[combinedFeedKey] = {
...feedSettings,
...feedOAuthAccountDetails,
customFeedName: feedCompanyCardNickname,
domainID: Number(domainID),
feed: feedName,
};
}
return acc;
}, result);
}, [allFeeds, policyID, workspaceAccountID]);
return [workspaceFeeds, allFeedsResult, defaultFeed];
};
export default useCardFeeds;
export type {CombinedCardFeeds, CompanyCardFeedWithDomainID, CombinedCardFeed};