Skip to content

Commit 06cd925

Browse files
musaleMnickii
andauthored
fix: implement caching on the card state for person card (#3266)
* Cache the user card state * Make the batch calls if person has person-card support The calls will respond with network data or cache data. If no cache is found, it loads from network then creates the cache * fix build * fix lint issues --------- Co-authored-by: Nickii Miaro <[email protected]>
1 parent ba87c32 commit 06cd925

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

packages/mgt-components/src/components/mgt-person-card/mgt-person-card.graph.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { BatchResponse, IBatch, IGraph, prepScopes } from '@microsoft/mgt-element';
8+
import { BatchResponse, CacheItem, CacheService, CacheStore, IBatch, IGraph, prepScopes } from '@microsoft/mgt-element';
99
import { Chat, ChatMessage } from '@microsoft/microsoft-graph-types';
1010
import { Profile } from '@microsoft/microsoft-graph-types-beta';
1111

@@ -15,6 +15,7 @@ import { MgtPersonCardState } from './mgt-person-card.types';
1515
import { MgtPersonCardConfig } from './MgtPersonCardConfig';
1616
import { validUserByIdScopes } from '../../graph/graph.user';
1717
import { validInsightScopes } from '../../graph/graph.files';
18+
import { schemas } from '../../graph/cacheStores';
1819

1920
const userProperties =
2021
'businessPhones,companyName,department,displayName,givenName,jobTitle,mail,mobilePhone,officeLocation,preferredLanguage,surname,userPrincipalName,id,accountEnabled';
@@ -27,6 +28,11 @@ const batchKeys = {
2728
person: 'person'
2829
};
2930

31+
interface CacheCardState extends MgtPersonCardState, CacheItem {}
32+
33+
export const getCardStateInvalidationTime = (): number =>
34+
CacheService.config.users.invalidationPeriod || CacheService.config.defaultInvalidationPeriod;
35+
3036
/**
3137
* Get data to populate the person card
3238
*
@@ -44,6 +50,15 @@ export const getPersonCardGraphData = async (
4450
): Promise<MgtPersonCardState> => {
4551
const userId = personDetails.id;
4652
const email = getEmailFromGraphEntity(personDetails);
53+
const cache: CacheStore<CacheCardState> = CacheService.getCache<CacheCardState>(
54+
schemas.users,
55+
schemas.users.stores.cardState
56+
);
57+
const cardState = await cache.getValue(userId);
58+
59+
if (cardState && getCardStateInvalidationTime() > Date.now() - cardState.timeCached) {
60+
return cardState;
61+
}
4762

4863
const isContactOrGroup =
4964
'classification' in personDetails ||
@@ -101,6 +116,8 @@ export const getPersonCardGraphData = async (
101116
data.directReports = data.directReports.filter(report => report.accountEnabled);
102117
}
103118

119+
await cache.putValue(userId, data);
120+
104121
return data;
105122
};
106123

packages/mgt-components/src/components/mgt-person/mgt-person.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { personCardConverter, type PersonCardInteraction } from './../PersonCard
3838
import { styles } from './mgt-person-css';
3939
import { AvatarType, MgtPersonConfig, avatarTypeConverter } from './mgt-person-types';
4040
import { strings } from './strings';
41+
import { getPersonCardGraphData } from '../mgt-person-card/mgt-person-card.graph';
4142

4243
/**
4344
* Person properties part of original set provided by graph by default
@@ -1202,6 +1203,12 @@ export class MgtPerson extends MgtTemplatedTaskComponent {
12021203

12031204
details = this.personDetailsInternal || this.personDetails || this.fallbackDetails;
12041205

1206+
// load card data at this point
1207+
if (this.personCardInteraction !== 'none') {
1208+
// perform the batch requests and cache
1209+
void getPersonCardGraphData(graph, details, this.personQuery === 'me');
1210+
}
1211+
12051212
// populate presence
12061213
const defaultPresence: Presence = {
12071214
activity: 'Offline',

packages/mgt-components/src/graph/cacheStores.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ export const schemas = {
2121
stores: {
2222
users: 'users',
2323
usersQuery: 'usersQuery',
24-
userFilters: 'userFilters'
24+
userFilters: 'userFilters',
25+
cardState: 'cardState'
2526
},
26-
version: 3
27+
version: 4
2728
},
2829
photos: {
2930
name: 'photos',

0 commit comments

Comments
 (0)