Skip to content

Commit 910daa1

Browse files
author
ComputelessComputer
committed
show timezone city in today's date header
1 parent 2eb8bb1 commit 910daa1

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/components/layout/AppLayout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import "../editor/Editor.css";
1313
import { listen, } from "@tauri-apps/api/event";
1414
import { watch, } from "@tauri-apps/plugin-fs";
1515
import { useCurrentDate, } from "../../hooks/useCurrentDate";
16+
import { useTimezoneCity, } from "../../hooks/useTimezoneCity";
1617
import { resolveAssetUrl, saveImage, } from "../../services/images";
1718
import type { LibraryItem, } from "../../services/library";
1819
import { getJournalDir, getNotePath, initJournalScope, } from "../../services/paths";
@@ -40,6 +41,7 @@ function insertImageViaView(file: File, view: EditorView,) {
4041

4142
function DateHeader({ date, }: { date: string; },) {
4243
const showToday = isToday(date,);
44+
const city = useTimezoneCity();
4345

4446
return (
4547
<div className="flex items-center gap-4">
@@ -57,6 +59,11 @@ function DateHeader({ date, }: { date: string; },) {
5759
today
5860
</span>
5961
)}
62+
{showToday && city && (
63+
<span className="text-sm text-gray-400 dark:text-gray-500 font-sans">
64+
{city}
65+
</span>
66+
)}
6067
</div>
6168
);
6269
}

src/hooks/useTimezoneCity.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useEffect, useState, } from "react";
2+
3+
function getCity(): string {
4+
const tz = Intl.DateTimeFormat().resolvedOptions().timeZone;
5+
return tz.split("/",).pop()?.replace(/_/g, " ",) ?? "";
6+
}
7+
8+
/**
9+
* Returns the city derived from the system timezone (e.g. "Seoul", "Lisbon"),
10+
* reactively updated when:
11+
* - The window regains focus (user switches back to app after traveling)
12+
* - The document becomes visible (computer wakes from sleep in a new timezone)
13+
*/
14+
export function useTimezoneCity(): string {
15+
const [city, setCity,] = useState(getCity,);
16+
17+
useEffect(() => {
18+
function check() {
19+
setCity((prev,) => {
20+
const next = getCity();
21+
return prev !== next ? next : prev;
22+
},);
23+
}
24+
25+
function onVisibilityChange() {
26+
if (document.visibilityState === "visible") check();
27+
}
28+
29+
document.addEventListener("visibilitychange", onVisibilityChange,);
30+
window.addEventListener("focus", check,);
31+
32+
return () => {
33+
document.removeEventListener("visibilitychange", onVisibilityChange,);
34+
window.removeEventListener("focus", check,);
35+
};
36+
}, [],);
37+
38+
return city;
39+
}

0 commit comments

Comments
 (0)