1- import React from "react" ;
1+ import React , { KeyboardEventHandler } from "react" ;
22import { Layout , Input } from "antd" ;
33import { Link , useHistory , useLocation } from "react-router-dom" ;
44import { StringParam , useQueryParams } from "use-query-params" ;
55import * as qs from "query-string" ;
6+ import { GITHUB_MAIN , GITHUB_FEEDBACK } from "shared/get-env" ;
67import { Auth } from "features" ;
78import { ReactComponent as IcLogo } from "./logo.svg" ;
89import "./index.scss" ;
910
10- const FEEDBACK_URL = "https://github.com/ani-team/github-client/issues/new" ;
11- const GITHUB_URL = "https://github.com/ani-team/github-client " ;
11+ // FIXME: get from `pages`?
12+ const SEARCH_URL = "/search " ;
1213
13- const Header = ( ) => {
14- const { isAuth } = Auth . useAuth ( ) ;
14+ /**
15+ * @hook Логика обработки инпута поиска
16+ */
17+ const useSearchInput = ( ) => {
1518 // !!! FIXME: limit scope of query-params literals
1619 const [ query ] = useQueryParams ( {
1720 q : StringParam ,
@@ -22,6 +25,32 @@ const Header = () => {
2225 const location = useLocation ( ) ;
2326 const history = useHistory ( ) ;
2427
28+ /**
29+ * Обработка инпута поиска
30+ */
31+ const handleKeyDown : KeyboardEventHandler < HTMLInputElement > = ( { key, currentTarget } ) => {
32+ if ( key === "Enter" && currentTarget . value ) {
33+ const q = currentTarget . value ;
34+ history . push ( `${ SEARCH_URL } ?${ qs . stringify ( { ...query , q } ) } ` ) ;
35+ }
36+ } ;
37+ /**
38+ * Поисковой запрос
39+ * @remark Если не страница поиска - обнуляем инпут
40+ */
41+ const searchValue = location . pathname === SEARCH_URL ? query . q ?? "" : "" ;
42+
43+ return { handleKeyDown, searchValue } ;
44+ } ;
45+
46+ /**
47+ * Хедер приложения
48+ * @remark Содержит поисковой инпут с базовой логикой
49+ */
50+ const Header = ( ) => {
51+ const { isAuth } = Auth . useAuth ( ) ;
52+ const { handleKeyDown, searchValue } = useSearchInput ( ) ;
53+
2554 return (
2655 < Layout . Header className = "header" >
2756 < div className = "nav flex flex-grow items-center" >
@@ -33,25 +62,14 @@ const Header = () => {
3362 < Input
3463 className = "header__search"
3564 placeholder = "Search..."
36- defaultValue = { location . pathname === "/search" ? query . q ?? "" : "" }
37- onKeyDown = { ( { key, currentTarget } ) => {
38- if ( key === "Enter" && currentTarget . value ) {
39- history . push (
40- `/search?${ qs . stringify ( {
41- q : currentTarget . value ,
42- type : query . type ,
43- s : query . s ,
44- o : query . o ,
45- } ) } `,
46- ) ;
47- }
48- } }
65+ defaultValue = { searchValue }
66+ onKeyDown = { handleKeyDown }
4967 />
5068 ) }
51- < a className = "m-4 text-gray-600" href = { GITHUB_URL } >
69+ < a className = "m-4 text-gray-600" href = { GITHUB_MAIN } >
5270 GitHub
5371 </ a >
54- < a className = "m-4 text-gray-600" href = { FEEDBACK_URL } >
72+ < a className = "m-4 text-gray-600" href = { GITHUB_FEEDBACK } >
5573 Feedback
5674 </ a >
5775 </ div >
0 commit comments