Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
6b6b965e
Commit
6b6b965e
authored
Jan 17, 2023
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
search result redirects
parent
77e0febf
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
61 additions
and
5 deletions
+61
-5
nodeFetch.ts
lib/api/nodeFetch.ts
+3
-1
resources.ts
lib/api/resources.ts
+3
-0
getServerSideProps.ts
lib/next/getServerSideProps.ts
+2
-2
search-results.tsx
pages/search-results.tsx
+47
-2
search.ts
types/api/search.ts
+6
-0
No files found.
lib/api/nodeFetch.ts
View file @
6b6b965e
import
type
{
IncomingMessage
}
from
'
http
'
;
import
type
{
NextApiRequest
}
from
'
next
'
;
import
type
{
NextApiRequestCookies
}
from
'
next/dist/server/api-utils
'
;
import
type
{
RequestInit
,
Response
}
from
'
node-fetch
'
;
import
nodeFetch
from
'
node-fetch
'
;
...
...
@@ -6,7 +8,7 @@ import { httpLogger } from 'lib/api/logger';
import
*
as
cookies
from
'
lib/cookies
'
;
export
default
function
fetchFactory
(
_req
:
NextApiRequest
,
_req
:
NextApiRequest
|
(
IncomingMessage
&
{
cookies
:
NextApiRequestCookies
})
,
)
{
// first arg can be only a string
// FIXME migrate to RequestInfo later if needed
...
...
lib/api/resources.ts
View file @
6b6b965e
...
...
@@ -240,6 +240,9 @@ export const RESOURCES = {
],
filterFields
:
[
'
q
'
],
},
search_check_redirect
:
{
path
:
'
/api/v2/search/check-redirect
'
,
},
// DEPRECATED
old_api
:
{
...
...
lib/next/getServerSideProps.ts
View file @
6b6b965e
import
type
{
GetServerSideProps
,
GetServerSidePropsResult
}
from
'
next
'
;
import
type
{
GetServerSideProps
}
from
'
next
'
;
export
type
Props
=
{
cookies
:
string
;
...
...
@@ -6,7 +6,7 @@ export type Props = {
id
?:
string
;
}
export
const
getServerSideProps
:
GetServerSideProps
=
async
({
req
,
query
}):
Promise
<
GetServerSidePropsResult
<
Props
>>
=>
{
export
const
getServerSideProps
:
GetServerSideProps
<
Props
>
=
async
({
req
,
query
})
=>
{
return
{
props
:
{
cookies
:
req
.
headers
.
cookie
||
''
,
...
...
pages/search-results.tsx
View file @
6b6b965e
import
type
{
NextPage
}
from
'
next
'
;
import
type
{
GetServerSideProps
,
NextPage
}
from
'
next
'
;
import
Head
from
'
next/head
'
;
import
React
from
'
react
'
;
import
type
{
SearchRedirectResult
}
from
'
types/api/search
'
;
import
buildUrlNode
from
'
lib/api/buildUrlNode
'
;
import
fetchFactory
from
'
lib/api/nodeFetch
'
;
import
link
from
'
lib/link/link
'
;
import
getNetworkTitle
from
'
lib/networks/getNetworkTitle
'
;
import
type
{
Props
}
from
'
lib/next/getServerSideProps
'
;
import
{
getServerSideProps
as
getServerSidePropsBase
}
from
'
lib/next/getServerSideProps
'
;
import
SearchResults
from
'
ui/pages/SearchResults
'
;
const
SearchResultsPage
:
NextPage
=
()
=>
{
...
...
@@ -19,4 +26,42 @@ const SearchResultsPage: NextPage = () => {
export
default
SearchResultsPage
;
export
{
getServerSideProps
}
from
'
lib/next/getServerSideProps
'
;
export
const
getServerSideProps
:
GetServerSideProps
<
Props
>
=
async
({
req
,
res
,
resolvedUrl
,
query
})
=>
{
try
{
const
q
=
String
(
query
.
q
);
const
url
=
buildUrlNode
(
'
search_check_redirect
'
,
undefined
,
{
q
});
const
redirectsResponse
=
await
fetchFactory
(
req
)(
url
);
const
payload
=
await
redirectsResponse
.
json
()
as
SearchRedirectResult
;
if
(
!
payload
||
typeof
payload
!==
'
object
'
||
!
payload
.
redirect
)
{
throw
Error
();
}
const
redirectUrl
=
(()
=>
{
switch
(
payload
.
type
)
{
case
'
block
'
:
{
return
link
(
'
block
'
,
{
id
:
q
});
}
case
'
address
'
:
{
return
link
(
'
address_index
'
,
{
id
:
payload
.
parameter
||
q
});
}
case
'
transaction
'
:
{
return
link
(
'
tx
'
,
{
id
:
q
});
}
}
})();
if
(
!
redirectUrl
)
{
throw
Error
();
}
return
{
redirect
:
{
destination
:
redirectUrl
,
permanent
:
false
,
},
};
}
catch
(
error
)
{}
return
getServerSidePropsBase
({
req
,
res
,
resolvedUrl
,
query
});
};
types/api/search.ts
View file @
6b6b965e
...
...
@@ -49,3 +49,9 @@ export interface SearchResult {
export
interface
SearchResultFilters
{
q
:
string
;
}
export
interface
SearchRedirectResult
{
parameter
:
string
|
null
;
redirect
:
boolean
;
type
:
'
address
'
|
'
block
'
|
'
transaction
'
|
null
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment