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(""" """, 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"""
""", 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"])