11import React , { useEffect , useState , useContext } from 'react'
22import { Stack , Text } from '@fluentui/react'
3- import { Book28Regular , Book32Regular , BookRegular , News28Regular , NewsRegular , Notepad28Regular , Notepad32Regular } from '@fluentui/react-icons'
3+ import {
4+ Book28Regular ,
5+ Book32Regular ,
6+ BookRegular ,
7+ News28Regular ,
8+ NewsRegular ,
9+ Notepad28Regular ,
10+ Notepad32Regular
11+ } from '@fluentui/react-icons'
412import { Button , Avatar } from '@fluentui/react-components'
513import styles from './Sidebar.module.css'
614import { AppStateContext } from '../../state/AppProvider'
715import { getUserInfo } from '../../api'
816import { useNavigate , useLocation } from 'react-router-dom'
917
10-
1118enum NavigationButtonStates {
1219 Active = 'active' ,
1320 Inactive = 'inactive' ,
@@ -28,9 +35,24 @@ const NavigationButton = ({ text, buttonState, onClick }: NavigationButtonProps)
2835 } [ buttonState ]
2936
3037 const iconElements : { [ key : string ] : JSX . Element } = {
31- 'Browse' : < News28Regular color = { fontColor } /> ,
32- 'Generate' : < Book28Regular color = { fontColor } /> ,
33- 'Draft' : < Notepad28Regular color = { fontColor } />
38+ Browse : (
39+ < News28Regular
40+ color = { fontColor }
41+ cursor = { buttonState === NavigationButtonStates . Disabled ? 'not-allowed' : 'pointer' }
42+ />
43+ ) ,
44+ Generate : (
45+ < Book28Regular
46+ color = { fontColor }
47+ cursor = { buttonState === NavigationButtonStates . Disabled ? 'not-allowed' : 'pointer' }
48+ />
49+ ) ,
50+ Draft : (
51+ < Notepad28Regular
52+ color = { fontColor }
53+ cursor = { buttonState === NavigationButtonStates . Disabled ? 'not-allowed' : 'pointer' }
54+ />
55+ )
3456 }
3557
3658 const buttonStyle = {
@@ -42,37 +64,46 @@ const NavigationButton = ({ text, buttonState, onClick }: NavigationButtonProps)
4264 const icon = iconElements [ text ]
4365
4466 return (
45- < Stack onClick = { buttonState === NavigationButtonStates . Inactive ? onClick : ( ) => { } } className = { buttonStyle } >
46- < Button appearance = "transparent"
47- size = "large"
48- icon = { icon }
49- style = { { padding : '0' } }
50- />
51- < Text style = { { color : fontColor } } > { text } </ Text >
67+ < Stack
68+ onClick = { buttonState === NavigationButtonStates . Inactive ? onClick : ( ) => { } }
69+ className = { buttonStyle }
70+ style = { { cursor : buttonState === NavigationButtonStates . Disabled ? 'not-allowed' : 'pointer' } } >
71+ < Button appearance = "transparent" size = "large" icon = { icon } style = { { padding : '0' } } />
72+ < Text
73+ style = { {
74+ color : fontColor ,
75+ cursor : buttonState === NavigationButtonStates . Disabled ? 'not-allowed' : 'pointer'
76+ } } >
77+ { text }
78+ </ Text >
5279 </ Stack >
5380 )
5481}
5582
5683const Sidebar = ( ) : JSX . Element => {
5784 const appStateContext = useContext ( AppStateContext )
5885 const navigate = useNavigate ( )
59- const location = useLocation ( ) ;
60- const [ name , setName ] = useState < string > ( "" )
86+ const location = useLocation ( )
87+ const [ name , setName ] = useState < string > ( '' )
6188
6289 useEffect ( ( ) => {
63- if ( ! appStateContext ) { throw new Error ( 'useAppState must be used within a AppStateProvider' ) }
90+ if ( ! appStateContext ) {
91+ throw new Error ( 'useAppState must be used within a AppStateProvider' )
92+ }
6493
6594 if ( appStateContext . state . frontendSettings ?. auth_enabled ) {
66- getUserInfo ( ) . then ( ( res ) => {
67- const name : string = res [ 0 ] . user_claims . find ( ( claim : any ) => claim . typ === 'name' ) ?. val ?? ''
68- setName ( name )
69- } ) . catch ( ( err ) => {
70- console . error ( 'Error fetching user info: ' , err )
71- } )
95+ getUserInfo ( )
96+ . then ( res => {
97+ const name : string = res [ 0 ] . user_claims . find ( ( claim : any ) => claim . typ === 'name' ) ?. val ?? ''
98+ setName ( name )
99+ } )
100+ . catch ( err => {
101+ console . error ( 'Error fetching user info: ' , err )
102+ } )
72103 }
73- } , [ ] )
104+ } , [ appStateContext ] )
74105
75- // determine url from react-router-dom
106+ // determine url from react-router-dom
76107 const determineView = ( ) => {
77108 const url = location . pathname
78109
@@ -82,20 +113,59 @@ const Sidebar = (): JSX.Element => {
82113 }
83114
84115 const currentView = determineView ( )
85-
86- // inactive, disabled, active
87- var draftButtonState = NavigationButtonStates . Disabled
88- if ( appStateContext ?. state . draftedDocument ) { draftButtonState = currentView === 'draft' ? NavigationButtonStates . Active : NavigationButtonStates . Inactive }
116+ const isGenerating = appStateContext ?. state . isGenerating
89117
90118 return (
91119 < Stack className = { styles . sidebarContainer } >
92120 < Stack horizontal className = { styles . avatarContainer } >
93121 < Avatar color = "colorful" name = { name } />
94122 </ Stack >
95123 < Stack className = { styles . sidebarNavigationContainer } >
96- < NavigationButton text = { "Browse" } buttonState = { currentView === 'chat' ? NavigationButtonStates . Active : NavigationButtonStates . Inactive } onClick = { ( ) => { navigate ( "/chat" ) } } />
97- < NavigationButton text = { "Generate" } buttonState = { currentView === 'generate' ? NavigationButtonStates . Active : NavigationButtonStates . Inactive } onClick = { ( ) => { navigate ( "/generate" ) } } />
98- < NavigationButton text = { "Draft" } buttonState = { draftButtonState } onClick = { ( ) => { navigate ( "/draft" ) } } />
124+ < NavigationButton
125+ text = { 'Browse' }
126+ buttonState = {
127+ currentView === 'chat'
128+ ? NavigationButtonStates . Active
129+ : appStateContext ?. state . isGenerating
130+ ? NavigationButtonStates . Disabled
131+ : NavigationButtonStates . Inactive
132+ }
133+ onClick = { ( ) => {
134+ if ( ! isGenerating ) {
135+ navigate ( '/chat' )
136+ }
137+ } }
138+ />
139+ < NavigationButton
140+ text = { 'Generate' }
141+ buttonState = {
142+ currentView === 'generate'
143+ ? NavigationButtonStates . Active
144+ : appStateContext ?. state . isGenerating
145+ ? NavigationButtonStates . Disabled
146+ : NavigationButtonStates . Inactive
147+ }
148+ onClick = { ( ) => {
149+ if ( ! isGenerating ) {
150+ navigate ( '/generate' )
151+ }
152+ } }
153+ />
154+ < NavigationButton
155+ text = { 'Draft' }
156+ buttonState = {
157+ currentView === 'draft'
158+ ? NavigationButtonStates . Active
159+ : appStateContext ?. state . isGenerating
160+ ? NavigationButtonStates . Disabled
161+ : NavigationButtonStates . Inactive
162+ }
163+ onClick = { ( ) => {
164+ if ( ! isGenerating ) {
165+ navigate ( '/draft' )
166+ }
167+ } }
168+ />
99169 </ Stack >
100170 </ Stack >
101171 )
0 commit comments