Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
sdream-ai-fe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
侯明涛
sdream-ai-fe
Commits
67fc813e
Commit
67fc813e
authored
Aug 08, 2024
by
HoMeTown
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: login tip处理
parent
a42cb84d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
71 deletions
+40
-71
src/auth/AuthContext.tsx
+5
-2
src/auth/withAuth.tsx
+3
-2
src/config/nav.ts
+16
-0
src/layouts/Navbar/Navbar.tsx
+16
-67
No files found.
src/auth/AuthContext.tsx
View file @
67fc813e
...
...
@@ -6,6 +6,7 @@ import { useLocalStorageState } from 'ahooks'
interface
AuthContextType
{
isLoggedIn
:
boolean
showLoginModal
:
boolean
showLoginTip
:
boolean
login
:
()
=>
void
logout
:
()
=>
void
toggleLoginModal
:
()
=>
void
...
...
@@ -24,8 +25,9 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
defaultValue
:
''
,
},
)
const
[
isLoggedIn
,
setIsLoggedIn
]
=
useState
(
Boolean
(
token
)
)
const
[
isLoggedIn
,
setIsLoggedIn
]
=
useState
(
!!
token
)
const
[
showLoginModal
,
setShowLoginModal
]
=
useState
(
false
)
const
[
showLoginTip
,
setShowLoginTip
]
=
useState
(
!
token
)
const
login
=
()
=>
{
setIsLoggedIn
(
true
)
...
...
@@ -38,11 +40,12 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
const
toggleLoginModal
=
()
=>
{
setShowLoginModal
(
!
showLoginModal
)
setShowLoginTip
(
showLoginModal
)
}
return
(
// eslint-disable-next-line react/no-unstable-context-value
<
AuthContext
.
Provider
value=
{
{
isLoggedIn
,
showLoginModal
,
login
,
logout
,
toggleLoginModal
}
}
>
<
AuthContext
.
Provider
value=
{
{
isLoggedIn
,
showLogin
Tip
,
showLogin
Modal
,
login
,
logout
,
toggleLoginModal
}
}
>
{
children
}
</
AuthContext
.
Provider
>
)
...
...
src/auth/withAuth.tsx
View file @
67fc813e
...
...
@@ -4,13 +4,14 @@ import { useAuth } from './AuthContext'
export
interface
WithAuthProps
{
checkAuth
:
()
=>
boolean
showLoginTip
:
boolean
}
export
function
withAuth
<
T
extends
WithAuthProps
=
WithAuthProps
>
(
WrappedComponent
:
React
.
ComponentType
<
T
>
,
)
{
return
(
props
:
Omit
<
T
,
keyof
WithAuthProps
>
)
=>
{
const
{
isLoggedIn
,
toggleLoginModal
}
=
useAuth
()
const
{
isLoggedIn
,
toggleLoginModal
,
showLoginTip
}
=
useAuth
()
const
checkAuth
=
()
=>
{
if
(
!
isLoggedIn
)
{
...
...
@@ -20,6 +21,6 @@ export function withAuth<T extends WithAuthProps = WithAuthProps>(
return
true
}
return
<
WrappedComponent
{
...
(
props
as
T
)}
checkAuth=
{
checkAuth
}
/>
return
<
WrappedComponent
{
...
(
props
as
T
)}
showLoginTip=
{
showLoginTip
}
checkAuth=
{
checkAuth
}
/>
}
}
src/config/nav.ts
0 → 100644
View file @
67fc813e
import
Tools
from
'@/assets/svg/tools.svg?react'
import
Logo
from
'@/assets/svg/logo.svg?react'
import
AddNewChat
from
'@/assets/svg/addNewChat.svg?react'
import
HistoryChat
from
'@/assets/svg/historyChat.svg?react'
import
Collect
from
'@/assets/svg/collect.svg?react'
export
const
NAV_BAR_ITEMS
=
[
{
icon
:
Logo
,
label
:
''
,
key
:
'logo'
},
{
icon
:
''
,
label
:
''
,
key
:
'line1'
},
{
icon
:
AddNewChat
,
label
:
'新建对话'
,
key
:
'add'
},
{
icon
:
HistoryChat
,
label
:
'历史对话'
,
key
:
'history'
},
{
icon
:
Collect
,
label
:
'收藏'
,
key
:
'collect'
},
{
icon
:
''
,
label
:
''
,
key
:
'line2'
},
{
icon
:
Tools
,
label
:
'工具'
,
key
:
'tools'
},
{
icon
:
''
,
label
:
''
,
key
:
'line3'
},
]
src/layouts/Navbar/Navbar.tsx
View file @
67fc813e
import
type
React
from
'react'
import
{
motion
}
from
'framer-motion'
import
{
Button
,
Tooltip
}
from
'@nextui-org/react'
import
{
useLocalStorageState
,
useToggle
}
from
'ahooks'
import
{
useEffect
,
useRef
}
from
'react'
import
styles
from
'./Navbar.module.less'
import
{
NavBarItem
}
from
'./components/NavBarItem'
import
Logo
from
'@/assets/svg/logo.svg?react'
import
AddNewChat
from
'@/assets/svg/addNewChat.svg?react'
import
HistoryChat
from
'@/assets/svg/historyChat.svg?react'
import
Collect
from
'@/assets/svg/collect.svg?react'
import
Tools
from
'@/assets/svg/tools.svg?react'
import
UserIcon
from
'@/assets/svg/user.svg?react'
import
{
LoginModal
}
from
'@/components/LoginModal'
import
type
{
WithAuthProps
}
from
'@/auth/withAuth'
import
{
withAuth
}
from
'@/auth/withAuth'
const
NAV_BAR_ITEMS
=
[
{
icon
:
Logo
,
label
:
''
,
key
:
'logo'
},
{
icon
:
''
,
label
:
''
,
key
:
'line1'
},
{
icon
:
AddNewChat
,
label
:
'新建对话'
,
key
:
'add'
},
{
icon
:
HistoryChat
,
label
:
'历史对话'
,
key
:
'history'
},
{
icon
:
Collect
,
label
:
'收藏'
,
key
:
'collect'
},
{
icon
:
''
,
label
:
''
,
key
:
'line2'
},
{
icon
:
Tools
,
label
:
'工具'
,
key
:
'tools'
},
{
icon
:
''
,
label
:
''
,
key
:
'line3'
},
]
import
{
NAV_BAR_ITEMS
}
from
'@/config/nav'
interface
NavbarProps
{
isCollapsed
:
boolean
onToggle
:
()
=>
void
}
const
NavbarBase
:
React
.
FC
<
NavbarProps
&
WithAuthProps
>
=
({
checkAuth
,
onToggle
})
=>
{
const
[
token
]
=
useLocalStorageState
<
string
|
undefined
>
(
'__TOKEN__'
,
{
defaultValue
:
''
,
listenStorageChange
:
true
,
})
const
[
isOpenLogTip
,
isOpenLogTipActions
]
=
useToggle
()
const
[
isOpenLoginModal
,
isOpenLoginModalActions
]
=
useToggle
()
const
intervalRef
=
useRef
<
NodeJS
.
Timeout
|
null
>
(
null
)
const
handleShowLoginTip
=
()
=>
{
intervalRef
.
current
=
setTimeout
(()
=>
{
if
(
!
token
)
{
isOpenLogTipActions
.
setRight
()
}
if
(
intervalRef
.
current
)
{
clearInterval
(
intervalRef
.
current
)
intervalRef
.
current
=
null
}
},
1000
)
}
const
handleCloseLoginModal
=
()
=>
{
isOpenLoginModalActions
.
setLeft
()
handleShowLoginTip
()
}
const
NavbarBase
:
React
.
FC
<
NavbarProps
&
WithAuthProps
>
=
({
showLoginTip
,
checkAuth
,
onToggle
})
=>
{
const
handleClick
=
(
type
:
string
|
undefined
)
=>
{
if
(
!
checkAuth
())
return
...
...
@@ -67,28 +23,21 @@ const NavbarBase: React.FC<NavbarProps & WithAuthProps> = ({ checkAuth, onToggle
}
}
useEffect
(()
=>
{
handleShowLoginTip
()
},
[])
return
(
<>
<
motion
.
nav
className=
"h-full flex-shrink-0 flex flex-col items-center justify-center"
>
<
motion
.
div
className=
{
`${styles.layoutNavBarAgent} sm:flex hidden w-[64px] bg-white gap-[24px]`
}
>
{
NAV_BAR_ITEMS
.
map
((
item
)
=>
{
return
(
<
NavBarItem
onClick=
{
handleClick
}
icon=
{
item
.
icon
}
label=
{
item
.
label
}
key=
{
item
.
key
}
type=
{
item
.
key
}
/>
)
})
}
<
Tooltip
isOpen=
{
isOpenLogTip
}
color=
"foreground"
content=
"登录体验更多功能"
placement=
"right"
>
<
Button
onClick=
{
checkAuth
}
variant=
"light"
isIconOnly
aria
-
label=
"Like"
>
<
UserIcon
/>
</
Button
>
</
Tooltip
>
</
motion
.
div
>
</
motion
.
nav
>
<
LoginModal
onClose=
{
handleCloseLoginModal
}
isOpen=
{
isOpenLoginModal
}
/>
</>
<
motion
.
nav
className=
"h-full flex-shrink-0 flex flex-col items-center justify-center"
>
<
motion
.
div
className=
{
`${styles.layoutNavBarAgent} sm:flex hidden w-[64px] bg-white gap-[24px]`
}
>
{
NAV_BAR_ITEMS
.
map
((
item
)
=>
{
return
(
<
NavBarItem
onClick=
{
handleClick
}
icon=
{
item
.
icon
}
label=
{
item
.
label
}
key=
{
item
.
key
}
type=
{
item
.
key
}
/>
)
})
}
<
Tooltip
isOpen=
{
showLoginTip
}
color=
"foreground"
content=
"登录体验更多功能"
placement=
"right"
>
<
Button
onClick=
{
checkAuth
}
variant=
"light"
isIconOnly
aria
-
label=
"Like"
>
<
UserIcon
/>
</
Button
>
</
Tooltip
>
</
motion
.
div
>
</
motion
.
nav
>
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment