Skip to content

Commit c4c1046

Browse files
authored
Revision 3 (#4)
* fix: improve ttl relative time formatting * fix: make refresh button refresh the items list too * make refresh button invalidate dbsize
1 parent b03e2f9 commit c4c1046

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/components/databrowser/components/sidebar/index.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { IconRefresh } from "@tabler/icons-react"
22

3+
import { queryClient } from "@/lib/clients"
34
import { Button } from "@/components/ui/button"
45
import { Spinner } from "@/components/ui/spinner"
56

7+
import { FETCH_LIST_ITEMS_QUERY_KEY } from "../../hooks"
68
import { useKeys } from "../../hooks/use-keys"
79
import { AddKeyModal } from "../add-key-modal"
8-
import { DisplayDbSize } from "./db-size"
10+
import { DisplayDbSize, FETCH_DB_SIZE_QUERY_KEY } from "./db-size"
911
import { Empty } from "./empty"
1012
import { InfiniteScroll } from "./infinite-scroll"
1113
import { KeysList } from "./keys-list"
@@ -23,7 +25,18 @@ export function Sidebar() {
2325
<div className="flex h-10 items-center justify-between pl-1">
2426
<DisplayDbSize />
2527
<div className="flex gap-1">
26-
<Button className="h-7 w-7 px-0" onClick={refetch}>
28+
<Button
29+
className="h-7 w-7 px-0"
30+
onClick={() => {
31+
refetch()
32+
queryClient.invalidateQueries({
33+
queryKey: [FETCH_LIST_ITEMS_QUERY_KEY],
34+
})
35+
queryClient.invalidateQueries({
36+
queryKey: [FETCH_DB_SIZE_QUERY_KEY],
37+
})
38+
}}
39+
>
2740
<Spinner isLoading={query.isFetching}>
2841
<IconRefresh size={16} />
2942
</Spinner>

src/lib/utils.ts

+18-7
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,26 @@ const units = {
2424
second: 1000,
2525
} as const
2626

27-
// 130 -> 2 minutes
28-
// 7800 -> 2 hours
29-
/** 130 is "2 minutes", 5 is "5 seconds" */
27+
// 2h 10m, 1d 2h, 1 year 2 months, 3 years 4 months etc.
3028
export function formatTime(seconds: number) {
29+
let milliseconds = seconds * 1000
30+
const parts = []
31+
3132
for (const [unit, value] of Object.entries(units)) {
32-
const interval = (seconds * 1000) / value
33-
if (interval >= 1) {
34-
return `${Math.floor(interval)} ${unit}${interval > 1 && unit !== "min" ? "s" : ""}`
33+
if (milliseconds >= value) {
34+
const amount = Math.floor(milliseconds / value)
35+
const plural = amount > 1 ? "s" : ""
36+
const label =
37+
unit === "month" ? ` month${plural}` : unit === "year" ? ` year${plural}` : unit[0]
38+
parts.push(`${amount}${label}`)
39+
milliseconds %= value
3540
}
3641
}
37-
return "just now"
42+
43+
// If no parts (e.g., 0ms), default to "0s"
44+
if (parts.length === 0) {
45+
parts.push("0s")
46+
}
47+
48+
return parts.slice(0, 2).join(" ")
3849
}

0 commit comments

Comments
 (0)