Skip to content

Commit 3668584

Browse files
authored
Merge pull request #333 from lara-learning/feat/search-highlight
feat: Highlighter
2 parents 39f9a64 + b629326 commit 3668584

File tree

10 files changed

+71
-12
lines changed

10 files changed

+71
-12
lines changed

packages/components/src/entry-input-layout.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import styled from 'styled-components'
44
import { Spacings } from './spacing'
55
import { FontSizes } from './font-size'
66
import { BorderRadii } from './border-radius'
7+
import Highlighter from './highlighter'
78

89
const ActionWrapper = styled.div`
910
display: flex;
@@ -118,13 +119,6 @@ const TextWrapper = styled(ValueBase)`
118119
padding-left: 0;
119120
`
120121

121-
const Text = styled.p`
122-
margin: 0;
123-
padding: 0;
124-
white-space: pre-wrap;
125-
word-break: break-word;
126-
`
127-
128122
const Time = styled(ValueBase)`
129123
width: 100px;
130124
text-align: right;
@@ -193,17 +187,26 @@ interface EntryInputLayoutProps extends HTMLAttributes<HTMLElement>, EntryContai
193187
text: string
194188
time: string
195189
actions: JSX.Element
190+
term: string
196191
clickHandler: React.Dispatch<React.SetStateAction<boolean>>
197192
}
198193

199-
export const EntryInputLayout: React.FC<EntryInputLayoutProps> = ({ text, time, actions, clickHandler, ...rest }) => {
194+
export const EntryInputLayout: React.FC<EntryInputLayoutProps> = ({
195+
text,
196+
time,
197+
actions,
198+
term,
199+
clickHandler,
200+
...rest
201+
}) => {
202+
console.log(term)
200203
return (
201204
<>
202205
<StyledEntryContainer {...rest}>
203206
<StyledEntryValueWrapper>
204207
<StyledInputWrapper onClick={() => clickHandler(true)}>
205208
<TextWrapper>
206-
<Text>{text}</Text>
209+
<Highlighter term={term}>{text}</Highlighter>
207210
</TextWrapper>
208211
<Time>{time}</Time>
209212
</StyledInputWrapper>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
interface highlighterProps {
2+
term: string
3+
children: string
4+
}
5+
6+
const Highlighter: React.FC<highlighterProps> = ({ term, children }) => {
7+
if (!term) {
8+
return <>{children}</>
9+
}
10+
const regex = new RegExp(`(${term})`, 'gi')
11+
const parts = children.split(regex)
12+
console.log(parts)
13+
14+
return (
15+
<>
16+
<div>test</div>
17+
{parts.map((part: string, index: number) =>
18+
regex.test(part) ? (
19+
<span
20+
key={index}
21+
style={{
22+
margin: 0,
23+
backgroundColor: 'rgb(84 198 247)',
24+
padding: 0,
25+
whiteSpace: 'pre-wrap',
26+
wordBreak: 'break-word',
27+
fontWeight: 'bold',
28+
}}
29+
>
30+
{part}
31+
</span>
32+
) : (
33+
part
34+
)
35+
)}
36+
</>
37+
)
38+
}
39+
40+
export default Highlighter

packages/frontend/src/components/day-input.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ interface DayInputProps {
6666
disabled?: boolean
6767
reportStatus?: ReportStatus
6868
primary?: boolean
69+
term: string
6970
updateMessageDay?: (message: string, commentId: string) => void
7071
updateMessageEntry?: (
7172
message: string,
@@ -86,6 +87,7 @@ const DayInput: React.FunctionComponent<DayInputProps> = ({
8687
primary,
8788
updateMessageDay,
8889
updateMessageEntry,
90+
term,
8991
}) => {
9092
const { getTotalMinutes } = useDayHelper()
9193
const { addToast } = useToastContext()
@@ -232,6 +234,7 @@ const DayInput: React.FunctionComponent<DayInputProps> = ({
232234
}
233235
>
234236
<EntriesInput
237+
term={term}
235238
handleStatusChange={handleStatusChange}
236239
day={day}
237240
reportStatus={reportStatus ?? ReportStatus.Todo}

packages/frontend/src/components/entries-input.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useToastContext } from '../hooks/use-toast-context'
2020
import { useDayHelper } from '../helper/day-helper'
2121

2222
interface EntriesInputProps {
23+
term: string
2324
day?: Pick<Day, 'id' | 'date'> & {
2425
entries: (Pick<Entry, 'id' | 'text' | 'time' | 'orderId'> & {
2526
comments: (Pick<Comment, 'id' | 'text' | 'published'> & {
@@ -49,6 +50,7 @@ const EntriesInput: React.FC<EntriesInputProps> = ({
4950
handleStatusChange,
5051
trainee,
5152
updateMessage,
53+
term,
5254
}) => {
5355
const [createEntryMutation] = useCreateEntryMutation()
5456
const { addToast } = useToastContext()
@@ -107,6 +109,7 @@ const EntriesInput: React.FC<EntriesInputProps> = ({
107109
.sort((a, b) => a.orderId - b.orderId)
108110
.map((entry) => (
109111
<EntryInput
112+
term={term}
110113
handleStatusChange={handleStatusChange}
111114
key={entry.id}
112115
disabled={disabled}

packages/frontend/src/components/entry-input.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ interface EntryDisplayFieldProps {
5353
showContextMenu?: string
5454
setShowContextMenu?: React.Dispatch<React.SetStateAction<string>>
5555
updateMessage?: (message: string, commentId: string) => void
56+
term: string
5657
}
5758

5859
const EntryInput: React.FC<EntryDisplayFieldProps> = ({
@@ -65,6 +66,7 @@ const EntryInput: React.FC<EntryDisplayFieldProps> = ({
6566
showContextMenu,
6667
setShowContextMenu,
6768
updateMessage,
69+
term,
6870
}) => {
6971
const { loading, data } = useEntryInputDataQuery()
7072

@@ -301,6 +303,7 @@ const EntryInput: React.FC<EntryDisplayFieldProps> = ({
301303
<TextTimeInput autoFocus entry={entry} onDelete={handleDelete} onSave={handleSave} />
302304
) : (
303305
<EntryInputLayout
306+
term={term}
304307
clickable
305308
disabled={disabled}
306309
draggable={!disabled}

packages/frontend/src/pages/archive-page.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ const ArchivePage: React.FunctionComponent = () => {
123123

124124
const [allChecked, setAllChecked] = React.useState(false)
125125
const [selectedTrainee, setSelectedTrainee] = React.useState<string | undefined>(undefined)
126+
const [term, setTerm] = React.useState<string | undefined>(undefined)
126127

127128
const getArchivedReports = React.useCallback(() => {
128129
if (!data) return []
@@ -215,6 +216,8 @@ const ArchivePage: React.FunctionComponent = () => {
215216
let weekEnd: number | undefined = undefined
216217
let searchText: string | undefined = undefined
217218

219+
setTerm(value)
220+
218221
const yearMatch = yearMonthRegex.exec(value)
219222
if (yearMonthRegex.test(value) && yearMatch) {
220223
year = parseInt(yearMatch[1], 10)
@@ -346,8 +349,8 @@ const ArchivePage: React.FunctionComponent = () => {
346349

347350
const link =
348351
data?.currentUser?.__typename === UserTypeEnum.Trainer
349-
? `/report/${traineeId}/${year}/${week}`
350-
: `/report/${year}/${week}`
352+
? `/report/${traineeId}/${year}/${week}/${term ? term : ''}`
353+
: `/report/${year}/${week}/${term ? term : ''}`
351354

352355
return (
353356
<StyledArchiveTableRow key={report.id}>

packages/frontend/src/pages/dashboard-page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const DashboardPage: React.FunctionComponent = () => {
6363
<Container overflow={'visible'}>
6464
<DayInput
6565
primary
66+
term=""
6667
heading={getTodayHeading()}
6768
reportStatus={reportForYearAndWeek?.status}
6869
day={day}

packages/frontend/src/pages/report-page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import { useReportHelper } from '../helper/report-helper'
4545
const ReportPage: React.FunctionComponent = () => {
4646
const navigate = useNavigate()
4747
const { getFinishedDays } = useReportHelper()
48-
const { trainee, year, week } = useParams()
48+
const { trainee, year, week, term } = useParams()
4949

5050
const variables: ReportPageDataQueryVariables = {
5151
year: parseInt(year ?? '', 10),
@@ -282,6 +282,7 @@ const ReportPage: React.FunctionComponent = () => {
282282
{report?.days.map((day) => (
283283
<StyledTopBorderWrapper key={day.id}>
284284
<DayInput
285+
term={term ? term : ''}
285286
day={day}
286287
disabled={reportArchived || reportReview || day.status === DayStatusEnum.Holiday}
287288
reportStatus={report.status}

packages/frontend/src/pages/report-review-page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ const ReportReviewPage: React.FunctionComponent = () => {
388388
<>
389389
{day.entries.map((entry, entryIndex) => (
390390
<EntryInput
391+
term=""
391392
key={entryIndex}
392393
entry={entry}
393394
day={day}

packages/frontend/src/routes.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ const AppRoutes: React.FunctionComponent<RoutesProps> = ({ currentUser }) => {
7171
<>
7272
<Route path="/" element={<DashboardPage />} />
7373
<Route path="/archive" element={<ArchivePage />} />
74+
<Route path="/report/:year/:week/:term" element={<ReportPage />} />
7475
<Route path="/report/:year/:week" element={<ReportPage />} />
7576
<Route path="/report/missing" element={<MissingPage />} />
7677
<Route path="/alexa" element={<AlexaPage />} />

0 commit comments

Comments
 (0)