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