|
1 | | -import { useEffect, useMemo, useState } from "react" |
| 1 | +import { Dispatch, useEffect, useMemo, useState } from "react" |
2 | 2 |
|
3 | 3 | import { ContextInfo } from "components/ui/context-info" |
4 | 4 | import { FilterInput } from "components/ui/filter-input" |
@@ -70,6 +70,42 @@ interface Filters { |
70 | 70 | untilDate?: string |
71 | 71 | } |
72 | 72 |
|
| 73 | +const SearchFilters = ({ onChange }: { onChange: Dispatch<Filters> }) => ( |
| 74 | + <FilterInput |
| 75 | + className="block" |
| 76 | + placeholder="Search for time entries" |
| 77 | + onChange={({ text, tags }) => { |
| 78 | + onChange({ |
| 79 | + description: text, |
| 80 | + category: tags.category, |
| 81 | + fromDate: tags.from, |
| 82 | + untilDate: tags.until, |
| 83 | + }) |
| 84 | + }} |
| 85 | + tagConfigs={{ |
| 86 | + category: {}, |
| 87 | + until: { |
| 88 | + validate: value => value.length < 4 || dateHelpers.isValid(value), |
| 89 | + format: value => { |
| 90 | + if (value.length < 4) return "" |
| 91 | + const isYear = /^\d{4}$/.test(value) |
| 92 | + const date = dateHelpers.parse(isYear ? `${value}-12-31` : value) |
| 93 | + return dateHelpers.stringify(date) |
| 94 | + }, |
| 95 | + }, |
| 96 | + from: { |
| 97 | + validate: value => value.length < 4 || dateHelpers.isValid(value), |
| 98 | + format: value => { |
| 99 | + if (value.length < 4) return "" |
| 100 | + const isYear = /^\d{4}$/.test(value) |
| 101 | + const date = dateHelpers.parse(isYear ? `${value}-01-01` : value) |
| 102 | + return dateHelpers.stringify(date) |
| 103 | + }, |
| 104 | + }, |
| 105 | + }} |
| 106 | + /> |
| 107 | +) |
| 108 | + |
73 | 109 | export const SearchRoute = () => { |
74 | 110 | const raw = useAtomValue(timeEntriesData) |
75 | 111 | const allFlat = useMemo( |
@@ -131,43 +167,7 @@ export const SearchRoute = () => { |
131 | 167 | return ( |
132 | 168 | <div className={cn(vstack({}), "h-full px-10 pt-6")}> |
133 | 169 | <div className="mb-4"> |
134 | | - <FilterInput |
135 | | - className="block" |
136 | | - placeholder="Search for time entries" |
137 | | - onChange={({ text, tags }) => { |
138 | | - setFilter({ |
139 | | - description: text, |
140 | | - category: tags.category, |
141 | | - fromDate: tags.from, |
142 | | - untilDate: tags.until, |
143 | | - }) |
144 | | - }} |
145 | | - tagConfigs={{ |
146 | | - category: {}, |
147 | | - until: { |
148 | | - validate: value => value.length < 4 || dateHelpers.isValid(value), |
149 | | - format: value => { |
150 | | - if (value.length < 4) return "" |
151 | | - const isYear = /^\d{4}$/.test(value) |
152 | | - const date = dateHelpers.parse( |
153 | | - isYear ? `${value}-12-31` : value |
154 | | - ) |
155 | | - return dateHelpers.stringify(date) |
156 | | - }, |
157 | | - }, |
158 | | - from: { |
159 | | - validate: value => value.length < 4 || dateHelpers.isValid(value), |
160 | | - format: value => { |
161 | | - if (value.length < 4) return "" |
162 | | - const isYear = /^\d{4}$/.test(value) |
163 | | - const date = dateHelpers.parse( |
164 | | - isYear ? `${value}-01-01` : value |
165 | | - ) |
166 | | - return dateHelpers.stringify(date) |
167 | | - }, |
168 | | - }, |
169 | | - }} |
170 | | - /> |
| 170 | + <SearchFilters onChange={setFilter} /> |
171 | 171 | </div> |
172 | 172 |
|
173 | 173 | {filtered.length === 0 ? ( |
|
0 commit comments