Skip to content

Commit c38c952

Browse files
authored
Merge pull request #559 from GooeyAI/recipe_last_updated_time
show last saved time
2 parents 3c2623e + e7dd76e commit c38c952

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

daras_ai_v2/base.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
from routers.root import RecipeTabs
7171
from workspaces.models import Workspace, WorkspaceMembership
7272
from workspaces.widgets import get_current_workspace, set_current_workspace
73+
from daras_ai_v2.utils import get_relative_time
7374

7475
DEFAULT_META_IMG = (
7576
# Small
@@ -350,12 +351,28 @@ def render(self):
350351

351352
header_placeholder = gui.div()
352353
gui.newline()
354+
with gui.div(className="position-relative"):
355+
with gui.nav_tabs():
356+
for tab in self.get_tabs():
357+
url = self.current_app_url(tab)
358+
with gui.nav_item(url, active=tab == self.tab):
359+
gui.html(tab.title)
353360

354-
with gui.nav_tabs():
355-
for tab in self.get_tabs():
356-
url = self.current_app_url(tab)
357-
with gui.nav_item(url, active=tab == self.tab):
358-
gui.html(tab.title)
361+
if (
362+
self.current_pr
363+
and not self.current_pr.is_root()
364+
and self.tab == RecipeTabs.run
365+
):
366+
with gui.div(
367+
className="container-margin-reset d-none d-md-block",
368+
style=dict(
369+
position="absolute",
370+
top="50%",
371+
right="0",
372+
transform="translateY(-50%)",
373+
),
374+
):
375+
self._render_saved_timestamp(self.current_pr)
359376
with gui.nav_tab_content():
360377
self.render_selected_tab()
361378

@@ -472,6 +489,10 @@ def _render_unpublished_changes_indicator(self):
472489
):
473490
gui.html("Unpublished changes")
474491

492+
def _render_saved_timestamp(self, pr: PublishedRun):
493+
with gui.tag("span", className="text-muted"):
494+
gui.write(f"{get_relative_time(pr.updated_at)}")
495+
475496
def _render_options_button_with_dialog(self):
476497
ref = gui.use_alert_dialog(key="options-modal")
477498
if gui.button(label=icons.more_options, className="mb-0", type="tertiary"):

daras_ai_v2/utils.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from django.utils import timezone
2+
from datetime import timedelta, datetime
3+
4+
THRESHOLDS = [
5+
(timedelta(days=365), "y"),
6+
(timedelta(days=30), "mo"),
7+
(timedelta(days=1), "d"),
8+
(timedelta(hours=1), "h"),
9+
(timedelta(minutes=1), "m"),
10+
(timedelta(seconds=3), "s"),
11+
]
12+
13+
14+
def get_relative_time(timestamp: datetime) -> str:
15+
diff = timezone.now() - timestamp
16+
17+
if abs(diff) < timedelta(seconds=3):
18+
return "Just now"
19+
20+
for threshold, unit in THRESHOLDS:
21+
if abs(diff) >= threshold:
22+
value = round(diff / threshold)
23+
return (
24+
f"{value}{unit} ago" if diff > timedelta() else f"in {abs(value)}{unit}"
25+
)
26+
27+
return "Just now"

0 commit comments

Comments
 (0)