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

Merge last visited data. #1975

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading