fix(ui): fix useHash hook, do not use next/router (#2503)

* fix(ui): fix useHash hook, do not use next/router

* update

* update

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Meng Zhang 2024-06-25 19:07:22 +08:00 committed by GitHub
parent b3be6bbc1d
commit 9d2a193914
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 13 deletions

View File

@ -59,7 +59,7 @@ export const ChatSideBar: React.FC<ChatSideBarProps> = ({
),
{
hash: lineHash,
replace: true
replace: false
}
)
return

View File

@ -1,9 +1,10 @@
import React from 'react'
import Router from 'next/router'
import { useParams } from 'next/navigation'
import { useLatest } from './use-latest'
export function useHash(): [string, (hash: string) => void] {
const param = useParams()
const [hash, setHash] = React.useState<string>('')
const hashRef = useLatest(hash)
@ -11,22 +12,22 @@ export function useHash(): [string, (hash: string) => void] {
window.location.hash = hash
}, [])
React.useEffect(() => {
const handleHashChange = () => {
const newHash = window.location.hash
if (hashRef.current !== newHash) {
setHash(newHash)
}
const handleHashChange = () => {
const newHash = window.location.hash
if (hashRef.current !== newHash) {
setHash(newHash)
}
}
handleHashChange()
Router.events.on('hashChangeComplete', handleHashChange)
React.useEffect(() => {
window.addEventListener('hashchange', handleHashChange)
return () => {
Router.events.off('hashChangeComplete', handleHashChange)
window.removeEventListener('hashchange', handleHashChange)
}
}, [])
// Handle hash changes from next/navigation
React.useEffect(handleHashChange, [param])
return [hash, changeHash]
}

View File

@ -0,0 +1,8 @@
id: do-not-use-next-pages
message: Don't use next pages routing as we're fully commited to app router.
severity: error
language: typescript
files:
- ./ee/tabby-ui/**
rule:
pattern: import $$$ from 'next/router'