fix: no console api

This commit is contained in:
yyh 2026-06-24 16:18:00 +08:00
parent fe274fc28c
commit a656e514aa
No known key found for this signature in database
2 changed files with 64 additions and 5 deletions

View File

@ -1,7 +1,19 @@
import type { ReactNode } from 'react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { renderHook } from '@testing-library/react'
import { createElement } from 'react'
import { userProfileQueryOptions } from '@/features/account-profile/client'
import { createAccountProfileQueryWrapper } from '@/test/account-profile-query'
import useTimestamp from './use-timestamp'
const navigationMocks = vi.hoisted(() => ({
pathname: '/apps',
}))
vi.mock('@/next/navigation', () => ({
usePathname: () => navigationMocks.pathname,
}))
vi.mock('@/context/app-context', () => ({
useAppContext: vi.fn(() => ({
userProfile: {
@ -21,7 +33,27 @@ vi.mock('@/context/app-context', () => ({
})),
}))
const createEmptyQueryWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
function EmptyQueryWrapper({ children }: { children: ReactNode }) {
return createElement(QueryClientProvider, { client: queryClient }, children)
}
return { queryClient, wrapper: EmptyQueryWrapper }
}
describe('useTimestamp', () => {
beforeEach(() => {
navigationMocks.pathname = '/apps'
})
describe('formatTime', () => {
it('should format unix timestamp correctly', () => {
const { result } = renderHook(() => useTimestamp(), { wrapper: createAccountProfileQueryWrapper() })
@ -63,4 +95,15 @@ describe('useTimestamp', () => {
.toBe('20:00')
})
})
it('should not request account profile on public webapp routes', () => {
navigationMocks.pathname = '/chatbot/share-token'
const { queryClient, wrapper } = createEmptyQueryWrapper()
const { result } = renderHook(() => useTimestamp(), { wrapper })
expect(result.current.formatTime(1704132000, 'YYYY')).toBe('2024')
expect(queryClient.isFetching({ queryKey: userProfileQueryOptions().queryKey })).toBe(0)
expect(queryClient.getQueryState(userProfileQueryOptions().queryKey)?.fetchStatus).toBe('idle')
})
})

View File

@ -5,23 +5,39 @@ import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import { useCallback } from 'react'
import { userProfileQueryOptions } from '@/features/account-profile/client'
import { usePathname } from '@/next/navigation'
dayjs.extend(utc)
dayjs.extend(timezone)
const PUBLIC_WEBAPP_ROUTE_SEGMENTS = new Set(['agent', 'chat', 'chatbot', 'completion', 'workflow'])
const isPublicWebAppPath = (pathname: string | null) => {
const segment = pathname?.split('/').find(Boolean)
return segment ? PUBLIC_WEBAPP_ROUTE_SEGMENTS.has(segment) : false
}
const getBrowserTimezone = () => {
return Intl.DateTimeFormat().resolvedOptions().timeZone
}
const useTimestamp = () => {
const { data: timezone } = useQuery({
const pathname = usePathname()
const shouldUseAccountTimezone = !isPublicWebAppPath(pathname)
const { data: accountTimezone } = useQuery({
...userProfileQueryOptions(),
select: data => data.profile.timezone ?? undefined,
enabled: shouldUseAccountTimezone,
})
const resolvedTimezone = accountTimezone ?? getBrowserTimezone()
const formatTime = useCallback((value: number, format: string) => {
return dayjs.unix(value).tz(timezone).format(format)
}, [timezone])
return dayjs.unix(value).tz(resolvedTimezone).format(format)
}, [resolvedTimezone])
const formatDate = useCallback((value: string, format: string) => {
return dayjs(value).tz(timezone).format(format)
}, [timezone])
return dayjs(value).tz(resolvedTimezone).format(format)
}, [resolvedTimezone])
return { formatTime, formatDate }
}