-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Display timestamp of last usage for each Passkey device in PAsskey de…
…vice selector (#2680) * start replacing DeviceData with DeviceWithUsage * finish replacing DeviceData with DeviceWithUsage, display last_usage formatted in devices list if available * remove unnecessary console log * BigInt in types -> bigint * adjust formatting * rearrange and use relative time format * format * create time format helper utility and tests * format
- Loading branch information
Showing
6 changed files
with
130 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"; | ||
import { formatLastUsage } from "./time"; | ||
|
||
describe("formatLastUsage", () => { | ||
const NOW = new Date(); | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
vi.setSystemTime(NOW); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
test("formats time within the last hour", () => { | ||
const timestamp = new Date(NOW.getTime() - 30 * 60 * 1000); // 30 minutes ago | ||
expect(formatLastUsage(timestamp)).toBe( | ||
`today at ${timestamp.toLocaleTimeString("en-US", { | ||
hour: "numeric", | ||
minute: "numeric", | ||
})}` | ||
); | ||
}); | ||
|
||
test("formats time from earlier today", () => { | ||
const timestamp = new Date(NOW.getTime() - 7 * 60 * 60 * 1000); // 7 hours ago | ||
expect(formatLastUsage(timestamp)).toBe( | ||
`today at ${timestamp.toLocaleTimeString("en-US", { | ||
hour: "numeric", | ||
minute: "numeric", | ||
})}` | ||
); | ||
}); | ||
|
||
test("formats time from yesterday", () => { | ||
const timestamp = new Date(NOW.getTime() - 24 * 60 * 60 * 1000); // 24 hours ago | ||
expect(formatLastUsage(timestamp)).toBe("yesterday"); | ||
}); | ||
|
||
test("formats time from several days ago", () => { | ||
const timestamp = new Date(NOW.getTime() - 5 * 24 * 60 * 60 * 1000); // 5 days ago | ||
expect(formatLastUsage(timestamp)).toBe("5 days ago"); | ||
}); | ||
|
||
test("formats time from last month", () => { | ||
const timestamp = new Date(NOW.getTime() - 30 * 24 * 60 * 60 * 1000); // ~1 month ago | ||
expect(formatLastUsage(timestamp)).toBe("last month"); | ||
}); | ||
|
||
test("formats time from 5 months ago", () => { | ||
const timestamp = new Date(NOW.getTime() - 5 * 30 * 24 * 60 * 60 * 1000); // ~1 month ago | ||
expect(formatLastUsage(timestamp)).toBe("5 months ago"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const formatLastUsage = (timestamp: Date): string => { | ||
const now = new Date(); | ||
const diffInMillis = timestamp.getTime() - now.getTime(); | ||
const diffInDays = Math.round(diffInMillis / (1000 * 60 * 60 * 24)); | ||
|
||
// If more than 25 days, use months | ||
if (Math.abs(diffInDays) >= 25) { | ||
const diffInMonths = Math.round(diffInDays / 30); | ||
return new Intl.RelativeTimeFormat("en", { | ||
numeric: "auto", | ||
style: "long", | ||
}).format(diffInMonths, "month"); | ||
} | ||
|
||
const relativeTime = new Intl.RelativeTimeFormat("en", { | ||
numeric: "auto", | ||
style: "long", | ||
}).format(diffInDays, "day"); | ||
|
||
// If within last 24 hours, append the time | ||
if (Math.abs(diffInDays) < 1) { | ||
const timeString = new Intl.DateTimeFormat("en", { | ||
hour: "numeric", | ||
minute: "numeric", | ||
}).format(timestamp); | ||
return `${relativeTime} at ${timeString}`; | ||
} | ||
|
||
return relativeTime; | ||
}; |