File size: 2,877 Bytes
bbea1cc
 
cb5b71d
 
db55b72
bbea1cc
 
 
7f781f8
993d03b
dc92053
7f781f8
dc92053
8c11dd4
bbea1cc
cb5b71d
 
 
 
bbea1cc
 
041af8a
 
8c11dd4
041af8a
 
993d03b
 
 
bbea1cc
 
041af8a
 
 
8c11dd4
041af8a
 
 
7f781f8
bbea1cc
 
 
 
 
 
db55b72
bbea1cc
 
 
 
cb5b71d
 
7f781f8
cb5b71d
 
 
041af8a
 
 
6a31b9a
dc92053
041af8a
 
dc92053
cb5b71d
db55b72
 
 
 
dc92053
db55b72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cb5b71d
6a31b9a
 
 
cb5b71d
 
dc92053
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
import urllib.parse

import streamlit as st

from components.flex import st_flex
from core.constants import OAUTH_CLIENT_ID
from core.constants import OAUTH_STATE
from core.constants import REDIRECT_URI
from core.query_params import clear_query_params
from core.query_params import get_code
from core.query_params import get_project_timestamp
from core.query_params import get_state
from core.state import CurrentProject
from core.state import get_user
from core.state import User
from utils import init_state
from views.splash import render_splash
from views.wizard import render_editor

st.set_page_config(page_title="Croissant Editor", page_icon="🥐", layout="wide")

init_state()

user = get_user()

if OAUTH_CLIENT_ID and not user:
    state = get_state()
    if state and state == OAUTH_STATE:
        code = get_code()
        if not code:
            st.stop()
        try:
            st.session_state[User] = User.connect(code)
            # Clear the cache to force retrieving the new user.
            get_user()
        except:
            raise
        finally:
            clear_query_params()
    else:
        redirect_uri = urllib.parse.quote(REDIRECT_URI, safe="")
        client_id = urllib.parse.quote(OAUTH_CLIENT_ID, safe="")
        state = urllib.parse.quote(OAUTH_STATE, safe="")
        scope = urllib.parse.quote("openid profile", safe="")
        url = f"https://huggingface.co/oauth/authorize?response_type=code&redirect_uri={redirect_uri}&scope={scope}&client_id={client_id}&state={state}"
        st.header("Croissant Editor")
        st.link_button("🤗 Login with Hugging Face", url)
        st.stop()


def _back_to_menu():
    """Sends the user back to the menu."""
    clear_query_params()
    init_state(force=True)


def _logout():
    """Logs the user out."""
    st.cache_data.clear()
    st.session_state[User] = None
    _back_to_menu()


timestamp = get_project_timestamp()

button_width = 73  # This is the best value for the current content of the buttons.
buttons_widths = []
if OAUTH_CLIENT_ID:
    buttons_widths.append(button_width)
if timestamp:
    buttons_widths.append(button_width)
widths = [200, sum(buttons_widths) + 10]  # 10 being the space between elements.

with st_flex(
    flex_direction="row",
    justify_content="space-between",
    align_items="center",
    widths=widths,
):
    st.header("Croissant Editor")
    if OAUTH_CLIENT_ID or timestamp:
        with st_flex(
            flex_direction="row",
            justify_content="space-between",
            widths=buttons_widths,
        ):
            if OAUTH_CLIENT_ID:
                st.button("Log out", on_click=_logout)
            if timestamp:
                st.button("Home", on_click=_back_to_menu)

should_display_editor = bool(st.session_state.get(CurrentProject))

if should_display_editor:
    render_editor()
else:
    render_splash()