Skip to content

Commit

Permalink
Merge pull request #559 from GooeyAI/recipe_last_updated_time
Browse files Browse the repository at this point in the history
show last saved time
  • Loading branch information
anish-work authored Jan 7, 2025
2 parents 3c2623e + e7dd76e commit c38c952
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
31 changes: 26 additions & 5 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
from routers.root import RecipeTabs
from workspaces.models import Workspace, WorkspaceMembership
from workspaces.widgets import get_current_workspace, set_current_workspace
from daras_ai_v2.utils import get_relative_time

DEFAULT_META_IMG = (
# Small
Expand Down Expand Up @@ -350,12 +351,28 @@ def render(self):

header_placeholder = gui.div()
gui.newline()
with gui.div(className="position-relative"):
with gui.nav_tabs():
for tab in self.get_tabs():
url = self.current_app_url(tab)
with gui.nav_item(url, active=tab == self.tab):
gui.html(tab.title)

with gui.nav_tabs():
for tab in self.get_tabs():
url = self.current_app_url(tab)
with gui.nav_item(url, active=tab == self.tab):
gui.html(tab.title)
if (
self.current_pr
and not self.current_pr.is_root()
and self.tab == RecipeTabs.run
):
with gui.div(
className="container-margin-reset d-none d-md-block",
style=dict(
position="absolute",
top="50%",
right="0",
transform="translateY(-50%)",
),
):
self._render_saved_timestamp(self.current_pr)
with gui.nav_tab_content():
self.render_selected_tab()

Expand Down Expand Up @@ -472,6 +489,10 @@ def _render_unpublished_changes_indicator(self):
):
gui.html("Unpublished changes")

def _render_saved_timestamp(self, pr: PublishedRun):
with gui.tag("span", className="text-muted"):
gui.write(f"{get_relative_time(pr.updated_at)}")

def _render_options_button_with_dialog(self):
ref = gui.use_alert_dialog(key="options-modal")
if gui.button(label=icons.more_options, className="mb-0", type="tertiary"):
Expand Down
27 changes: 27 additions & 0 deletions daras_ai_v2/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.utils import timezone
from datetime import timedelta, datetime

THRESHOLDS = [
(timedelta(days=365), "y"),
(timedelta(days=30), "mo"),
(timedelta(days=1), "d"),
(timedelta(hours=1), "h"),
(timedelta(minutes=1), "m"),
(timedelta(seconds=3), "s"),
]


def get_relative_time(timestamp: datetime) -> str:
diff = timezone.now() - timestamp

if abs(diff) < timedelta(seconds=3):
return "Just now"

for threshold, unit in THRESHOLDS:
if abs(diff) >= threshold:
value = round(diff / threshold)
return (
f"{value}{unit} ago" if diff > timedelta() else f"in {abs(value)}{unit}"
)

return "Just now"

0 comments on commit c38c952

Please sign in to comment.