File size: 3,523 Bytes
21999ba
 
618e6a8
21999ba
 
 
4a987af
 
 
 
 
618e6a8
24e5a85
618e6a8
21999ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8511c9
 
21999ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8511c9
 
 
 
21999ba
 
 
 
 
 
 
 
 
 
 
4a987af
 
 
618e6a8
21999ba
397dfe3
21999ba
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
import streamlit as st
import datetime as DT
from zoneinfo import ZoneInfo
from helpers.activities import resetActivity, restoreUserActivity, ActivityLogs
import constants as C
import utils as U
import re


def __fixTimestamp(timestamp: str):
    # Add a trailing zero to microseconds if needed
    fixedTimestamp = re.sub(r'(\d{2}:\d{2}:\d{2}\.\d{5})(?=[+-]|$)', r'\g<1>0', timestamp)
    # U.pprint(f"{timestamp=} | {fixedTimestamp=}")
    return fixedTimestamp


def showSidebar():
    with st.sidebar:
        if not ("user" in st.session_state and "token" in st.session_state):
            return

        st.markdown("""
            <style>
            .user-info {
                display: flex;
                align-items: center;
                padding: 10px;
                background-color: rgba(255, 255, 255, 0.1);
                border-radius: 5px;
                margin-bottom: 10px;
                margin-top: -2rem;
            }
            .user-avatar {
                width: 40px;
                height: 40px;
                border-radius: 50%;
                margin-right: 10px;
            }
            .user-details {
                flex-grow: 1;
            }
            .user-name {
                font-weight: bold;
                margin: 0;
            }
            .user-email {
                font-size: 0.8em;
                color: #888;
                margin: 0;
            }
            </style>
            """, unsafe_allow_html=True)

        userAvatar = st.session_state["user"].get("picture")
        if not U.isValidImageUrl(userAvatar):
            userAvatar = C.AVATAR_ICON
        userName = st.session_state["user"].get("name", "User")
        userEmail = st.session_state["user"].get("email", "")

        st.markdown(f"""
            <div class="user-info">
                <img src="{userAvatar}" class="user-avatar">
                <div class="user-details">
                    <p class="user-name">{userName}</p>
                    <p class="user-email">{userEmail}</p>
                </div>
            </div>
            """, unsafe_allow_html=True)

        if st.button("Logout", key="logout_button", type="secondary", use_container_width=True):
            for key in ["token", "user"]:
                if key in st.session_state:
                    del st.session_state[key]
            st.rerun()

        st.markdown("---")

        if st.button("\\+ New Story", help="Your current story will be auto-saved 😊", key="save_activities_button", type="primary", use_container_width=True):
            resetActivity()

        userActivityLogs: ActivityLogs = [
            log for log in st.session_state.get("userActivitiesLog", [])
            if log["id"] != st.session_state.get("activityId")
        ]
        U.pprint(f"{userActivityLogs=}")

        if not userActivityLogs:
            return

        st.markdown("""
            ---
            ### Your Past Stories
        """)
        with st.container(height=300, border=False):
            for log in userActivityLogs:
                updatedAt = DT.datetime.fromisoformat(__fixTimestamp(
                    log["updated_at"].replace("Z", "+00:00")
                ))
                localTime = updatedAt.astimezone(ZoneInfo("Asia/Kolkata")).strftime("%b %d, %I:%M %p IST")
                activityId = log["id"]
                if st.button(f"Saved ⏱ {localTime}", key=f"activity_{activityId}", use_container_width=True):
                    restoreUserActivity(log["id"])