Skip to content

Commit 77513c8

Browse files
committed
Added character counter and fixed text select issue in popup.
1 parent ecad4c3 commit 77513c8

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

frontend/src/components/DraftCards/SectionCard.tsx

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { useContext, useState } from 'react'
22
import { Stack } from '@fluentui/react'
33
import { AppStateContext } from '../../state/AppProvider'
44
import { sectionGenerate, SectionGenerateRequest } from '../../api'
@@ -54,7 +54,10 @@ const useStyles = makeStyles({
5454
borderRadius: '4px',
5555
// margin top and bottom
5656
margin: '0.5rem 0',
57-
padding: '0.5rem'
57+
padding: '0.5rem',
58+
'&::selection': {
59+
backgroundColor: '#FFD6A5',
60+
}
5861
},
5962

6063
popoverGenerateButton: {
@@ -87,8 +90,6 @@ const useStyles = makeStyles({
8790
height: '100%',
8891
padding: '0.5rem',
8992
minHeight: '150px',
90-
91-
// selection
9293
'&::selection': {
9394
backgroundColor: '#FFD6A5'
9495
}
@@ -99,14 +100,22 @@ const useStyles = makeStyles({
99100
textAlign: 'left',
100101
fontSize: '12px',
101102
color: '#888'
103+
},
104+
105+
characterCounter: {
106+
marginRight: '5px',
107+
fontSize: '12px',
108+
color: '#888',
109+
alignSelf: 'end'
102110
}
103111
})
104112

105113
const SectionCard = ({ sectionIdx }: SectionCardProps) => {
106114
const classes = useStyles()
107-
const [isLoading, setIsLoading] = React.useState(false)
108-
const [isPopoverOpen, setIsPopoverOpen] = React.useState(false)
109-
const appStateContext = React.useContext(AppStateContext)
115+
const [isLoading, setIsLoading] = useState(false)
116+
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
117+
const appStateContext = useContext(AppStateContext)
118+
const [charCount, setCharCount] = useState(0)
110119

111120
if (!appStateContext) {
112121
throw new Error('useAppState must be used within a AppStateProvider')
@@ -120,6 +129,7 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
120129
const sectionTitle = section.title
121130
const sectionDescription = section.description
122131
const sectionContent = section.content
132+
const sectionCharacterLimit = 2000
123133

124134
const handleOpenChange: PopoverProps['onOpenChange'] = (e, data) => setIsPopoverOpen(data.open || false)
125135

@@ -136,6 +146,8 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
136146
content: responseBody.section_content
137147
}
138148
appStateContext?.dispatch({ type: 'UPDATE_SECTION', payload: { sectionIdx: sectionIdx, section: updatedSection } })
149+
const content = updatedSection.content || ''
150+
setCharCount(content.length)
139151

140152
setIsLoading(false)
141153
}
@@ -172,7 +184,8 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
172184
appearance="outline"
173185
size="large"
174186
defaultValue={sectionDescription}
175-
className={classes.popoverTextarea}
187+
className={ classes.popoverTextarea }
188+
textarea={{ className: classes.popoverTextarea }}
176189
/>
177190

178191
<Stack horizontal style={{ justifyContent: 'space-between' }}>
@@ -206,21 +219,26 @@ const SectionCard = ({ sectionIdx }: SectionCardProps) => {
206219
appearance="outline"
207220
size="large"
208221
defaultValue={sectionContent}
222+
maxLength={sectionCharacterLimit}
209223
onChange={(e, data) => {
224+
const content = data.value || ''
225+
setCharCount(content.length)
210226
const updatedSection: Section = {
211227
title: sectionTitle,
212228
description: sectionDescription,
213-
content: data.value || ''
229+
content: content
214230
}
215231
appStateContext?.dispatch({
216232
type: 'UPDATE_SECTION',
217233
payload: { sectionIdx: sectionIdx, section: updatedSection }
218234
})
219235
}}
220236
textarea={{ className: classes.sectionContentTextarea }}
221-
// style={{ width: '100%', height: '100%' }}
222237
/>
223-
<Text className={classes.disclaimerText}>AI-generated content may be incorrect</Text>
238+
<Stack horizontal horizontalAlign="space-between" verticalAlign="center">
239+
<Text className={classes.disclaimerText}>AI-generated content may be incorrect</Text>
240+
<Text className={classes.characterCounter}>{sectionCharacterLimit - charCount} characters remaining</Text>
241+
</Stack>
224242
</>
225243
)}
226244
</Stack>

0 commit comments

Comments
 (0)