File size: 6,000 Bytes
b42dfef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04c818a
 
75503d6
04c818a
b42dfef
 
04c818a
b42dfef
04c818a
b42dfef
75503d6
b42dfef
 
 
 
04c818a
b42dfef
 
04c818a
b42dfef
 
 
 
04c818a
b42dfef
 
 
 
 
75503d6
b42dfef
 
 
 
04c818a
b42dfef
 
 
 
 
 
04c818a
b42dfef
 
 
 
04c818a
b42dfef
 
 
 
 
 
 
 
04c818a
b42dfef
 
 
 
 
04c818a
 
 
 
 
 
 
b42dfef
04c818a
b42dfef
 
 
 
 
 
04c818a
b42dfef
04c818a
b42dfef
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
'use client';

import { useState, useEffect } from 'react';
import { 
  initializeOAuth, 
  loginWithHuggingFace, 
  loginDevMode,
  logout, 
  getStoredUserInfo, 
  isAuthenticated,
  isDevelopmentMode 
} from '@/lib/auth';
import { apiClient } from '@/lib/api';
import type { OAuthUserInfo } from '@/lib/auth';

export default function Header() {
  const [userInfo, setUserInfo] = useState<OAuthUserInfo | null>(null);
  const [isLoading, setIsLoading] = useState(true);
  const [showDevLogin, setShowDevLogin] = useState(false);
  const [devUsername, setDevUsername] = useState('');
  const isDevMode = isDevelopmentMode();

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

  const handleOAuthInit = async () => {
    setIsLoading(true);
    try {
      const oauthResult = await initializeOAuth();
      
      if (oauthResult) {
        setUserInfo(oauthResult.userInfo);
        // Set token in API client
        apiClient.setToken(oauthResult.accessToken);
      } else {
        // Check if we have stored user info
        const storedUserInfo = getStoredUserInfo();
        if (storedUserInfo) {
          setUserInfo(storedUserInfo);
        }
      }
    } catch (error) {
      console.error('OAuth initialization error:', error);
    } finally {
      setIsLoading(false);
    }
  };

  const handleLogin = async () => {
    try {
      await loginWithHuggingFace();
    } catch (error) {
      console.error('Login failed:', error);
      alert('Failed to start login process. Please try again.');
    }
  };

  const handleLogout = () => {
    logout();
    apiClient.logout();
    setUserInfo(null);
    // Reload page to clear state
    window.location.reload();
  };

  const handleDevLogin = () => {
    if (!devUsername.trim()) {
      alert('Please enter a username');
      return;
    }
    
    try {
      const result = loginDevMode(devUsername);
      setUserInfo(result.userInfo);
      apiClient.setToken(result.accessToken);
      setShowDevLogin(false);
      setDevUsername('');
    } catch (error) {
      console.error('Dev login failed:', error);
      alert('Failed to login in dev mode');
    }
  };

  return (
    <header className="bg-[#000000]/80 backdrop-blur-xl text-white border-b border-[#424245]/30">
      <div className="flex items-center justify-between px-3 md:px-6 h-12 md:h-14">
          <div className="flex items-center space-x-2 md:space-x-3">
            <h1 className="text-sm md:text-base font-medium text-[#f5f5f7]">AnyCoder</h1>
          </div>
          
          <div className="flex items-center space-x-3">
            {isLoading ? (
              <span className="text-xs text-[#86868b]">Loading...</span>
            ) : userInfo ? (
              <div className="flex items-center space-x-2 md:space-x-3">
                {userInfo.avatarUrl && (
                  <img 
                    src={userInfo.avatarUrl} 
                    alt={userInfo.name}
                    className="w-6 h-6 md:w-7 md:h-7 rounded-full"
                  />
                )}
                <span className="hidden sm:inline text-xs md:text-sm text-[#f5f5f7] font-medium truncate max-w-[100px] md:max-w-none">
                  {userInfo.preferredUsername || userInfo.name}
                </span>
                <button
                  onClick={handleLogout}
                  className="px-3 md:px-3 py-1.5 md:py-1.5 text-[#f5f5f7] text-sm hover:text-white transition-colors"
                >
                  Logout
                </button>
              </div>
            ) : (
              <div className="flex items-center space-x-2 md:space-x-3">
                {/* Dev Mode Login (only on localhost) */}
                {isDevMode && (
                  <>
                    {showDevLogin ? (
                      <div className="flex items-center space-x-2">
                        <input
                          type="text"
                          value={devUsername}
                          onChange={(e) => setDevUsername(e.target.value)}
                          onKeyPress={(e) => e.key === 'Enter' && handleDevLogin()}
                          placeholder="username"
                          className="px-3 py-1.5 rounded-lg text-sm bg-[#1d1d1f] text-[#f5f5f7] border border-[#424245] focus:outline-none focus:border-white/50 w-32 font-medium"
                          autoFocus
                        />
                        <button
                          onClick={handleDevLogin}
                          className="px-3 py-1.5 bg-white text-black rounded-lg text-sm hover:bg-[#f5f5f7] font-medium"
                        >
                          OK
                        </button>
                        <button
                          onClick={() => {
                            setShowDevLogin(false);
                            setDevUsername('');
                          }}
                          className="text-[#86868b] hover:text-[#f5f5f7] text-sm"
                        >

                        </button>
                      </div>
                    ) : (
                    <button
                      onClick={() => setShowDevLogin(true)}
                      className="px-3 py-1.5 text-sm text-[#f5f5f7] hover:text-white transition-colors"
                      title="Dev Mode"
                    >
                      Dev
                    </button>
                    )}
                    <span className="text-[#86868b] text-sm">or</span>
                  </>
                )}
                
                {/* OAuth Login */}
                <button
                  onClick={handleLogin}
                  className="px-3 md:px-4 py-1.5 md:py-2 bg-white text-black rounded-full text-sm hover:bg-[#f5f5f7] transition-all font-medium"
                >
                  Sign in
                </button>
              </div>
            )}
          </div>
        </div>
    </header>
  );
}