|
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): |
|
|
|
fixedTimestamp = re.sub(r'(\d{2}:\d{2}:\d{2}\.\d{5})(?=[+-]|$)', r'\g<1>0', timestamp) |
|
|
|
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"]) |
|
|