import { NextPage, NextPageContext } from 'next' import type { FC } from 'react' import dynamic from 'next/dynamic' import { ParsedUrlQuery } from 'querystring' import { List, Bot, Server } from '@types' import { get } from '@utils/Query' import { SearchQuerySchema } from '@utils/Yup' import { KoreanbotsEndPoints } from '@utils/Constants' const Hero = dynamic(() => import('@components/Hero')) const Advertisement = dynamic(() => import('@components/Advertisement')) const BotCard = dynamic(() => import('@components/BotCard')) const ServerCard = dynamic(() => import('@components/ServerCard')) const Container = dynamic(() => import('@components/Container')) const ResponsiveGrid = dynamic(() => import('@components/ResponsiveGrid')) const LongButton = dynamic(() => import('@components/LongButton')) const Redirect = dynamic(() => import('@components/Redirect')) const SearchComponent: FC<{data: List, query: URLQuery, type: 'bot' | 'server' }> = ({ data, query, type }) => { return

{type === 'bot' ? '봇' : '서버'}

{ !data || data.data.length === 0 ?

검색 결과가 없습니다.

: <> { data.data.map(el => ( type === 'bot' ? : )) } { data.totalPage !== 1 && 더보기 } }
} const Search:NextPage = ({ botData, serverData, priority, query }) => { if(!query?.q) return const list: ('bot'|'server')[] = [ 'bot', 'server' ] return <>
{ (priority === 'server' ? list.reverse() : list).map(el => ) }
} export const getServerSideProps = async(ctx: Context) => { if(ctx.query.query && !ctx.query.q) ctx.query.q = ctx.query.query if(!ctx.query?.q) { ctx.res.statusCode = 301 ctx.res.setHeader('Location', '/') return { props: {} } } if(!ctx.query.page) ctx.query.page = '1' const validate = await SearchQuerySchema.validate(ctx.query).then(el => el).catch(() => null) if(!validate || isNaN(Number(ctx.query.page))) return { props: { query: ctx.query } } else { return { props: { botData: await get.list.search.load(JSON.stringify({ query: ctx.query.q || '', page: ctx.query.page })).then(el => el).catch(() => null), serverData: await get.serverList.search.load(JSON.stringify({ query: ctx.query.q || '', page: ctx.query.page })).then(el => el).catch(() => null), query: ctx.query, priority: validate.priority || null } } } } interface SearchProps { botData?: List serverData?: List priority?: 'bot' | 'server' query: URLQuery } interface Context extends NextPageContext { query: URLQuery } interface URLQuery extends ParsedUrlQuery { q?: string query?: string page?: string } export default Search