Skip to content

Commit

Permalink
fix agent age for very new browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
nonrational committed Oct 25, 2024
1 parent e16334d commit 607c1f4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"lock": false,
"version": "0.0.2",
"version": "0.0.3",
"tasks": {
"check": "deno fmt --check && deno lint && deno check **/*.ts && deno check **/*.tsx",
"cli": "echo \"import '\\$fresh/src/dev/cli.ts'\" | deno run --unstable -A -",
Expand Down
4 changes: 2 additions & 2 deletions lib/agent_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface AgentVersion extends SemVer {
}

// SemVer compatibility requires that we return a 0 for unspecified parts.
const safeParseInt = (s?: string): number => {
export const safeParseInt = (s?: string): number => {
const num = s ? parseInt(s, 10) : 0

if (isNaN(num)) {
Expand All @@ -49,7 +49,7 @@ const parse = (value: string): AgentVersion => {
}

const toSemVer = (version: AgentVersion): SemVer => {
return parseSemVer(version.parts.join('.'))
return parseSemVer(version.parts.map((v) => v ? v : '0').join('.'))
}

const format = (version: AgentVersion, length?: number): string => {
Expand Down
29 changes: 20 additions & 9 deletions routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Handlers, PageProps } from '$fresh/server.ts'
import { type Agent, getAgentReleaseInfo } from '../lib/agent.ts'
import { formatDateYearMonth, humanizeDurationSince, randInterjection, toOrdinal } from '../lib/utils.ts'
import { getFamilyName, getGlobalUsageStats, getNorthAmericaUsageStats } from '../lib/family.ts'
import AgVer from '../lib/agent_version.ts'
import AgVer, { safeParseInt } from '../lib/agent_version.ts'
import UaInputSubmit from '../islands/ua_input_submit.tsx'
import type { UserAgent } from '$std/http/user_agent.ts'

Expand Down Expand Up @@ -48,17 +48,28 @@ const AgentIdentification = ({ ok, version, userAgent, name, source }: RenderDat
)
}

const AgentReleaseAge = ({ name, releaseDate }: RenderData) => {
if (!releaseDate) return null

const { date, version } = releaseDate
const AgentReleaseAge = ({ name, version, releaseDate, asOf, currentVersion }: RenderData) => {
if (!releaseDate?.date) {
// if the agent version is beyond the current version, it's likely just been released.
const veryRecent = (currentVersion && version) ? safeParseInt(currentVersion) < version.major : false

// if the current date formatted matches the asOf date formatted, we're still in the same month, so use "in" not "after"
const likelyReleased = formatDateYearMonth(asOf || new Date())
const inOrAfter = formatDateYearMonth(new Date()) === likelyReleased

return (
<p>
{veryRecent
? <>That version is brand new! It was likely released {inOrAfter ? 'in' : 'after'} {formatDateYearMonth(asOf || new Date())}.</>
: <>That version is either very old or very new. That's all we know.</>}
</p>
)
}

return (
<p>
{name} {version}
{date
? ` was released in ${formatDateYearMonth(date)}; it's ${humanizeDurationSince(date)} old.`
: ` hasn't officially been released yet. Far out.`}
{name} {releaseDate.version} was released in {formatDateYearMonth(releaseDate.date)};{' '}
<>it's {humanizeDurationSince(releaseDate.date)} old.</>
</p>
)
}
Expand Down

0 comments on commit 607c1f4

Please sign in to comment.