mirror of
https://github.com/netbirdio/docs.git
synced 2026-04-16 15:36:36 +00:00
added banner (#484)
This commit is contained in:
committed by
GitHub
parent
fe9d74b566
commit
36b8eb060a
30
src/hooks/useCustomQueryURL.js
Normal file
30
src/hooks/useCustomQueryURL.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useMemo } from 'react'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
export function useCustomQueryURL(target = '') {
|
||||
let router = useRouter()
|
||||
|
||||
return useMemo(() => {
|
||||
if (!target) {
|
||||
return ''
|
||||
}
|
||||
if (target.startsWith('http')) {
|
||||
return target
|
||||
}
|
||||
|
||||
let asPath = router?.asPath ?? ''
|
||||
let queryIndex = asPath.indexOf('?')
|
||||
|
||||
if (queryIndex === -1) {
|
||||
return target
|
||||
}
|
||||
|
||||
let currentQuery = asPath.slice(queryIndex + 1)
|
||||
if (!currentQuery) {
|
||||
return target
|
||||
}
|
||||
|
||||
let separator = target.includes('?') ? '&' : '?'
|
||||
return `${target}${separator}${currentQuery}`
|
||||
}, [router?.asPath, target])
|
||||
}
|
||||
51
src/hooks/useLocalStorage.js
Normal file
51
src/hooks/useLocalStorage.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
export function useLocalStorage(key, initialValue) {
|
||||
let [storedValue, setStoredValue] = useState(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return initialValue
|
||||
}
|
||||
|
||||
try {
|
||||
let item = window.localStorage.getItem(key)
|
||||
return item ? JSON.parse(item) : initialValue
|
||||
} catch {
|
||||
return initialValue
|
||||
}
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
let item = window.localStorage.getItem(key)
|
||||
setStoredValue(item ? JSON.parse(item) : initialValue)
|
||||
} catch {
|
||||
setStoredValue(initialValue)
|
||||
}
|
||||
}, [initialValue, key])
|
||||
|
||||
let setValue = useCallback(
|
||||
(value) => {
|
||||
setStoredValue((previousValue) => {
|
||||
let valueToStore =
|
||||
typeof value === 'function' ? value(previousValue) : value
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
if (valueToStore === undefined) {
|
||||
window.localStorage.removeItem(key)
|
||||
} else {
|
||||
window.localStorage.setItem(key, JSON.stringify(valueToStore))
|
||||
}
|
||||
}
|
||||
|
||||
return valueToStore
|
||||
})
|
||||
},
|
||||
[key]
|
||||
)
|
||||
|
||||
return [storedValue, setValue]
|
||||
}
|
||||
Reference in New Issue
Block a user