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

Call check-in route when popup is opened #37

Merged
merged 1 commit into from
May 8, 2024
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
24 changes: 24 additions & 0 deletions src/gkApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,30 @@ export const getAccessToken = async () => {
return cookie?.value;
};

export const checkin = async () => {
const token = await getAccessToken();
if (!token) {
return;
}

// Don't check in more than once every 12 hours to reduce amount of requests
const { lastCheckin } = await storage.local.get('lastCheckin');
if (lastCheckin && Date.now() - lastCheckin < 1000 * 60 * 60) {
return;
Comment on lines +50 to +53
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1: I think this currently checks every hour
2: is there a reason this is different than the gkdev checkin that's once every 24 hours?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.
And I went with 12 instead of 24 for cases such as using it at 10am the first day and 9am the next.

}

const res = await fetch(`${gkApiUrl}/browser-extension/checkin`, {
headers: {
Authorization: `Bearer ${token}`,
},
method: 'POST',
});

if (res.ok) {
void storage.local.set({ lastCheckin: Date.now() });
}
};

export const fetchUser = async () => {
const token = await getAccessToken();
if (!token) {
Expand Down
3 changes: 2 additions & 1 deletion src/popup/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client';
import React from 'react';
import { createRoot } from 'react-dom/client';
import { postEvent } from '../gkApi';
import { checkin, postEvent } from '../gkApi';
import { Popup } from './components/Popup';
import { asyncStoragePersister, queryClient } from './queryClient';

Expand All @@ -17,6 +17,7 @@ function main() {
);

void postEvent('browserExtensionPopupOpened');
void checkin();
}

main();
Loading