aboutsummaryrefslogtreecommitdiff
path: root/apps/extension/src/App.tsx
blob: dbba707e8eb4799533fa39385d0d668b96149874 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { useEffect, useState } from 'react';
import { z } from 'zod';
import { userObj } from './types/zods';

function App() {
  const [userData, setUserData] = useState<z.infer<typeof userObj> | null>(
    null,
  );

  const doStuff = () => {
    chrome.runtime.sendMessage({ type: 'getJwt' }, (response) => {
      const jwt = response.jwt;
      const loginButton = document.getElementById('login');

      if (loginButton) {
        if (jwt) {
          fetch('https://anycontext.dhr.wtf/api/me', {
            headers: {
              Authorization: `Bearer ${jwt}`,
            },
          })
            .then((res) => res.json())
            .then((data) => {
              const d = userObj.safeParse(data);
              if (d.success) {
                setUserData(d.data);
              } else {
                console.error(d.error);
              }
            });
          loginButton.style.display = 'none';
        }
      }
    });
  };

  useEffect(() => {
    doStuff();
  }, []);

  return (
    <div className="p-8">
      <button
        onClick={() =>
          chrome.tabs.create({
            url: 'https://anycontext.dhr.wtf/api/auth/signin',
          })
        }
        id="login"
      >
        Log in
      </button>
      <div>
        {userData && (
          <div className="flex items-center">
            <img
              width={40}
              className="rounded-full"
              src={userData.data.user.image!}
              alt=""
            />
            <div>
              <h3>{userData.data.user.name}</h3>
              <p>{userData.data.user.email}</p>
            </div>
          </div>
        )}
      </div>
    </div>
  );
}

export default App;