refactor(web): simplify home page redirect and replace dayjs with native Date API

Move OAuth pending redirect check out of page.tsx since AppInitializer
already handles it globally. Simplify home page to a server-side redirect
and remove dayjs dependency in favor of native Date.now().
This commit is contained in:
yyh
2026-02-10 13:53:27 +08:00
parent 34bff10782
commit 4d439028e5
3 changed files with 10 additions and 40 deletions

View File

@ -7,7 +7,6 @@ import {
RiMailLine,
RiTranslate2,
} from '@remixicon/react'
import dayjs from 'dayjs'
import { useRouter, useSearchParams } from 'next/navigation'
import * as React from 'react'
import { useEffect, useRef } from 'react'
@ -29,7 +28,7 @@ import {
function setItemWithExpiry(key: string, value: string, ttl: number) {
const item = {
value,
expiry: dayjs().add(ttl, 'seconds').unix(),
expiry: Math.floor((Date.now() + ttl * 1000) / 1000),
}
localStorage.setItem(key, JSON.stringify(item))
}

View File

@ -1,35 +1,5 @@
'use client'
import { redirect } from 'next/navigation'
import Link from 'next/link'
import { useRouter } from 'next/navigation'
import { useEffect } from 'react'
import Loading from '@/app/components/base/loading'
import { OAUTH_AUTHORIZE_PENDING_KEY } from '@/app/account/oauth/authorize/constants'
import { getOAuthPendingRedirect } from '@/app/signin/utils/post-login-redirect'
const Home = () => {
const router = useRouter()
useEffect(() => {
const pendingRedirect = getOAuthPendingRedirect(OAUTH_AUTHORIZE_PENDING_KEY)
if (pendingRedirect) {
router.replace(pendingRedirect)
return
}
router.replace('/apps')
}, [router])
return (
<div className="flex min-h-screen flex-col justify-center py-12 sm:px-6 lg:px-8">
<div className="sm:mx-auto sm:w-full sm:max-w-md">
<Loading type="area" />
<div className="mt-10 text-center">
<Link href="/apps">🚀</Link>
</div>
</div>
</div>
)
}
const Home = () => redirect('/apps')
export default Home

View File

@ -1,19 +1,20 @@
import type { ReadonlyURLSearchParams } from 'next/navigation'
import dayjs from 'dayjs'
import { OAUTH_AUTHORIZE_PENDING_KEY, REDIRECT_URL_KEY } from '@/app/account/oauth/authorize/constants'
export function getOAuthPendingRedirect(key: string): string | null {
const itemStr = localStorage.getItem(key)
const getCurrentUnixTimestamp = () => Math.floor(Date.now() / 1000)
function getOAuthPendingRedirect(): string | null {
const itemStr = localStorage.getItem(OAUTH_AUTHORIZE_PENDING_KEY)
if (!itemStr)
return null
try {
const item = JSON.parse(itemStr)
localStorage.removeItem(key)
localStorage.removeItem(OAUTH_AUTHORIZE_PENDING_KEY)
if (!item?.value)
return null
return dayjs().unix() > item.expiry ? null : item.value
return getCurrentUnixTimestamp() > item.expiry ? null : item.value
}
catch {
return null
@ -33,5 +34,5 @@ export const resolvePostLoginRedirect = (searchParams: ReadonlyURLSearchParams)
}
}
return getOAuthPendingRedirect(OAUTH_AUTHORIZE_PENDING_KEY)
return getOAuthPendingRedirect()
}