Skip to content

Commit

Permalink
Merge pull request #1975 from Hyperkid123/merge-last-visited-data
Browse files Browse the repository at this point in the history
Merge last visited data.
  • Loading branch information
Hyperkid123 authored Feb 27, 2024
2 parents 58ee841 + 07e12d4 commit 0356fed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/chrome/src/ChromeProvider/ChromeProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('ChromeProvider', () => {
});
}, 10000);

test('should not update state on mount if received error response', async () => {
test('should update state on mount with fallback data if received error response', async () => {
const errorResponse = { errors: [{ status: 404, meta: { response_by: 'gateway' }, detail: 'Undefined Insights application' }] };
getSpy.mockRejectedValue(errorResponse);
postSpy.mockRejectedValue(errorResponse);
Expand Down Expand Up @@ -118,7 +118,8 @@ describe('ChromeProvider', () => {
// There is some race condition that mismatches local runs vs travis runs.
// We're going to handle the base case and use Cypress to test the component.
expect(consoleSpy).toHaveBeenCalled();
expect(getSpy).toHaveBeenCalledTimes(1);
expect(getSpy).toHaveBeenCalledTimes(2);
expect(getSpy).toHaveBeenCalledWith('/api/chrome-service/v1/last-visited');
expect(getSpy).toHaveBeenCalledWith('/api/chrome-service/v1/user');
});
});
49 changes: 21 additions & 28 deletions packages/chrome/src/ChromeProvider/ChromeProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,35 +78,28 @@ const useLastVisitedLocalStorage = (providerState: ReturnType<typeof chromeState
const { pathname } = useLocation();
const scalprum = useScalprum();
const titleTarget = document.querySelector('title');
const lastVisitedLocal = localStorage.getItem(LAST_VISITED_FLAG);
useEffect(() => {
const getInitialPages = async () => {
try {
if (!lastVisitedLocal) {
const firstPages: LastVisitedPage[] = await get<LastVisitedPage[]>(LAST_VISITED_URL);
if (firstPages) {
providerState.setLastVisited(firstPages);
try {
localStorage.setItem(LAST_VISITED_FLAG, JSON.stringify(firstPages));
} catch (error) {
console.error('Unable to load initial last visited pages!', error);
}
}
} else {
const lastVisited: LastVisitedPage[] = JSON.parse(localStorage.getItem(LAST_VISITED_FLAG) ?? '[]');
if (!Array.isArray(lastVisited)) {
localStorage.setItem(LAST_VISITED_FLAG, JSON.stringify([]));
providerState.setLastVisited([]);
} else {
providerState.setLastVisited(lastVisited);
}
}
} catch (error: any) {
console.error('Unable to parse last visited pages from localStorage!', error);
providerState.setLastVisited([]);
localStorage.setItem(LAST_VISITED_FLAG, JSON.stringify([]));
const getInitialPages = async () => {
let localVisited: LastVisitedPage[] = [];
try {
localVisited = JSON.parse(localStorage.getItem(LAST_VISITED_FLAG) ?? '[]');
if (!Array.isArray(localVisited)) {
localVisited = [];
}
};
} catch (error) {
console.error('Unable to parse last visited pages from localStorage!', error);
localVisited = [];
}
try {
const firstPages: LastVisitedPage[] = [...((await get<LastVisitedPage[]>(LAST_VISITED_URL)) ?? []), ...localVisited].slice(0, 10);
providerState.setLastVisited(firstPages);
localStorage.setItem(LAST_VISITED_FLAG, JSON.stringify(firstPages));
} catch (error: any) {
console.error('Unable to load initial last visited pages!', error);
providerState.setLastVisited(localVisited);
localStorage.setItem(LAST_VISITED_FLAG, JSON.stringify(localVisited));
}
};
useEffect(() => {
getInitialPages();
}, []);

Expand Down
4 changes: 2 additions & 2 deletions packages/chrome/src/ChromeProvider/chromeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ const chromeState = () => {
}

// initializes state with new identity and should trigger all updates
function setIdentity(userIdentity: UserIdentity) {
state = { ...userIdentity, initialized: true };
function setIdentity({ lastVisitedPages, ...userIdentity }: UserIdentity) {
state = { ...state, ...userIdentity, initialized: true };
Object.values(subscriptions)
.flat()
.forEach((event) => {
Expand Down

0 comments on commit 0356fed

Please sign in to comment.