Skip to content

Commit 3877545

Browse files
authored
Call check-in route when popup is opened (#37)
1 parent 066fc68 commit 3877545

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/gkApi.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,30 @@ export const getAccessToken = async () => {
4141
return cookie?.value;
4242
};
4343

44+
export const checkin = async () => {
45+
const token = await getAccessToken();
46+
if (!token) {
47+
return;
48+
}
49+
50+
// Don't check in more than once every 12 hours to reduce amount of requests
51+
const { lastCheckin } = await storage.local.get('lastCheckin');
52+
if (lastCheckin && Date.now() - lastCheckin < 1000 * 60 * 60) {
53+
return;
54+
}
55+
56+
const res = await fetch(`${gkApiUrl}/browser-extension/checkin`, {
57+
headers: {
58+
Authorization: `Bearer ${token}`,
59+
},
60+
method: 'POST',
61+
});
62+
63+
if (res.ok) {
64+
void storage.local.set({ lastCheckin: Date.now() });
65+
}
66+
};
67+
4468
export const fetchUser = async () => {
4569
const token = await getAccessToken();
4670
if (!token) {

src/popup/main.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client';
44
import React from 'react';
55
import { createRoot } from 'react-dom/client';
6-
import { postEvent } from '../gkApi';
6+
import { checkin, postEvent } from '../gkApi';
77
import { Popup } from './components/Popup';
88
import { asyncStoragePersister, queryClient } from './queryClient';
99

@@ -17,6 +17,7 @@ function main() {
1717
);
1818

1919
void postEvent('browserExtensionPopupOpened');
20+
void checkin();
2021
}
2122

2223
main();

0 commit comments

Comments
 (0)