Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions components/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useMatrixStore } from '@/stores/useMatrixStore'
import { useSearch } from '@/hooks/useSearch'
import { useT } from '@/i18n'
import { quotaApi } from '@/lib/api'
import { AUTH_REQUIRED_EVENT } from '@/lib/auth-events'
import { getVisitorHeaders } from '@/lib/visitor'

export interface SearchBarHandle {
Expand Down Expand Up @@ -37,6 +38,12 @@ const SearchBar = forwardRef<SearchBarHandle>(function SearchBar(_props, ref) {
// Fetch quota on mount
useEffect(() => { fetchQuota() }, [fetchQuota])

useEffect(() => {
const onAuthRequired = () => setShowLoginModal((prev) => prev || true)
window.addEventListener(AUTH_REQUIRED_EVENT, onAuthRequired)
return () => window.removeEventListener(AUTH_REQUIRED_EVENT, onAuthRequired)
}, [])

// Refetch quota when search completes (isSearching: true → false)
useEffect(() => {
if (prevSearchingRef.current && !isSearching) {
Expand Down
40 changes: 40 additions & 0 deletions hooks/useSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,23 @@ import { useRef, useCallback } from 'react'
import { useMatrixStore } from '@/stores/useMatrixStore'
import { SSEClient } from '@/lib/sse-client'
import { searchApi, extractApi, paperApi } from '@/lib/api'
import { emitAuthRequired, isAuthErrorMessage } from '@/lib/auth-events'
import { getVisitorHeaders } from '@/lib/visitor'
import type { Paper, Column, ColumnSuggestion } from '@/types'

function resolveErrorMessage(payload: unknown): string {
if (typeof payload === 'string') return payload
if (
payload &&
typeof payload === 'object' &&
'message' in payload &&
typeof payload.message === 'string'
) {
return payload.message
}
return ''
}

export function useSearch() {
const {
isSearching,
Expand Down Expand Up @@ -68,6 +82,9 @@ export function useSearch() {
onError: (err) => {
console.error('Extraction SSE error:', err)
setIsExtracting(false)
if (isAuthErrorMessage(err.message)) {
emitAuthRequired()
}
},
}, getVisitorHeaders())
},
Expand Down Expand Up @@ -124,6 +141,13 @@ export function useSearch() {
columnIds.push(col.id)
// 收到 column 时检查是否已有足够论文,立即开始抽取
tryEarlyExtraction()
} else if (event === 'error') {
const message = resolveErrorMessage(data)
console.error('Search stream event error:', message || data)
setIsSearching(false)
if (isAuthErrorMessage(message)) {
emitAuthRequired()
}
}
},
onComplete: (data) => {
Expand All @@ -139,6 +163,9 @@ export function useSearch() {
onError: (err) => {
console.error('Search SSE error:', err)
setIsSearching(false)
if (isAuthErrorMessage(err.message)) {
emitAuthRequired()
}
},
}, getVisitorHeaders())
},
Expand Down Expand Up @@ -172,6 +199,9 @@ export function useSearch() {
onError: (err) => {
console.error('Column extraction error:', err)
setIsExtracting(false)
if (isAuthErrorMessage(err.message)) {
emitAuthRequired()
}
},
}, getVisitorHeaders())
},
Expand Down Expand Up @@ -218,6 +248,13 @@ export function useSearch() {
if (event === 'paper') {
addPaper(data as Paper)
newPaperIds.push(data.id)
} else if (event === 'error') {
const message = resolveErrorMessage(data)
console.error('Load more stream event error:', message || data)
setIsSearching(false)
if (isAuthErrorMessage(message)) {
emitAuthRequired()
}
}
// 加载更多时忽略 session / column 事件
},
Expand All @@ -232,6 +269,9 @@ export function useSearch() {
onError: (err) => {
console.error('Load more error:', err)
setIsSearching(false)
if (isAuthErrorMessage(err.message)) {
emitAuthRequired()
}
},
}, getVisitorHeaders())
},
Expand Down
22 changes: 22 additions & 0 deletions lib/auth-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const AUTH_REQUIRED_EVENT = 'auth-required'

const AUTH_EVENT_DEDUPE_MS = 1500
let lastAuthEventAt = 0

export function emitAuthRequired() {
if (typeof window === 'undefined') return
const now = Date.now()
if (now - lastAuthEventAt < AUTH_EVENT_DEDUPE_MS) return
lastAuthEventAt = now
window.dispatchEvent(new Event(AUTH_REQUIRED_EVENT))
}

export function isAuthErrorMessage(message: string): boolean {
const text = message.toLowerCase()
return (
text.includes('401') ||
text.includes('unauthorized') ||
text.includes('auth token not found in header') ||
text.includes('token not found')
)
}
Loading