import streamlit as st import requests import datetime as DT from helpers.activities import resetActivity, restoreUserActivity, ActivityLogs import constants as C import utils as U def isValidImageUrl(url): if not url: return False try: imageResponse = requests.head(url, timeout=5) contentType = imageResponse.headers.get('Content-Type', '') return imageResponse.status_code == 200 and contentType.startswith('image/') except Exception: return False 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", "https://example.com/default-avatar.png") if not 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 = st.session_state.get("userActivitiesLog", []) 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(log["updated_at"].replace("Z", "+00:00")) localTime = updatedAt.astimezone().strftime("%b %d, %I:%M %p") activityId = log["id"] if activityId == st.session_state.get("activityId"): continue if st.button(f"Saved ⌛ {localTime}", key=f"activity_{log['id']}", use_container_width=True): restoreUserActivity(log["id"])