Skip to content

Commit

Permalink
Call check-in route when popup is opened (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdgarcia authored May 8, 2024
1 parent 066fc68 commit 3877545
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
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;
}

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();

0 comments on commit 3877545

Please sign in to comment.