Skip to content

Commit 22981e9

Browse files
committed
refactor: save game sync username in Firestore
1 parent a59f5f2 commit 22981e9

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/hooks/useStore.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ export const useStore = createWithEqualityFn(
528528
gameSyncUsername: undefined,
529529
gameSync: async prefetchedProfile => {
530530
const {
531+
gameSyncUsername,
531532
gameSyncId: accountId,
532533
gameSyncPlatform: platform,
533534
flattenedItems,
@@ -541,7 +542,8 @@ export const useStore = createWithEqualityFn(
541542
steelPath,
542543
steelPathJunctions,
543544
masterNode,
544-
masterJunction
545+
masterJunction,
546+
updateFirestore
545547
} = get();
546548
if (!accountId) return;
547549

@@ -555,12 +557,16 @@ export const useStore = createWithEqualityFn(
555557
)
556558
return;
557559

558-
set({
559-
gameSyncUsername: gameProfile.DisplayName.slice(
560-
0,
561-
gameProfile.DisplayName.length - 1
562-
)
563-
});
560+
const accountUsername = gameProfile.DisplayName.slice(
561+
0,
562+
gameProfile.DisplayName.length - 1
563+
);
564+
if (accountUsername !== gameSyncUsername) {
565+
set({ gameSyncUsername: accountUsername });
566+
updateFirestore({
567+
gameSyncUsername: accountUsername
568+
});
569+
}
564570

565571
const gameProfileItemsXP = new Map();
566572
const gameProfileMissions = new Map();
@@ -640,25 +646,35 @@ export const useStore = createWithEqualityFn(
640646
}, 0)
641647
);
642648
},
643-
setGameSyncInfo: (accountId, platform) => {
644-
set({ gameSyncId: accountId, gameSyncPlatform: platform });
649+
setGameSyncInfo: (accountUsername, accountId, platform) => {
650+
set({
651+
gameSyncUsername: accountUsername,
652+
gameSyncId: accountId,
653+
gameSyncPlatform: platform
654+
});
645655
},
646656
enableGameSync: async (accountId, platform) => {
647657
const response = await getGameProfile(accountId, platform);
648658
const gameProfile = response?.Results?.[0];
649-
const docRef = get().getDocRef();
650-
updateDoc(docRef, {
659+
const accountUsername = gameProfile.DisplayName.slice(
660+
0,
661+
gameProfile.DisplayName.length - 1
662+
);
663+
664+
get().updateFirestore({
665+
gameSyncUsername: accountUsername,
651666
gameSyncId: accountId,
652667
gameSyncPlatform: platform
653668
});
654-
get().setGameSyncInfo(accountId, platform);
669+
get().setGameSyncInfo(accountUsername, accountId, platform);
655670
get().gameSync(gameProfile);
656671
},
657672
disableGameSync: () => {
658673
if (!get().gameSyncId) return;
659674

660675
const docRef = get().getDocRef();
661676
updateDoc(docRef, {
677+
gameSyncUsername: deleteField(),
662678
gameSyncId: deleteField(),
663679
gameSyncPlatform: deleteField()
664680
});
@@ -688,7 +704,7 @@ export const useStore = createWithEqualityFn(
688704
);
689705
},
690706

691-
backupMasteryData: async (attemptedGameSyncId) => {
707+
backupMasteryData: async attemptedGameSyncId => {
692708
try {
693709
const { type, id } = get();
694710
const docRef = get().getDocRef();
@@ -860,3 +876,4 @@ function markMasteryChange(draftState, key, id, mastered) {
860876
});
861877
}
862878
}
879+

src/pages/MasteryChecklist.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function MasteryChecklist(props) {
9696
setNodesMastered(data?.steelPath ?? [], true);
9797
setJunctionsMastered(data?.starChartJunctions ?? [], false);
9898
setJunctionsMastered(data?.steelPathJunctions ?? [], true);
99-
setGameSyncInfo(data?.gameSyncId, data?.gameSyncPlatform);
99+
setGameSyncInfo(data?.gameSyncUsername, data?.gameSyncId, data?.gameSyncPlatform);
100100
setPopupsDismissed(data?.popupsDismissed ?? []);
101101
setAccountLinkErrors(data?.accountLinkErrors ?? 0);
102102

0 commit comments

Comments
 (0)