From 740ff941a57351d6200700b3e45aee112e720a3a Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Fri, 30 May 2025 08:35:15 +0530 Subject: [PATCH] feat(discover): add preview mode --- src/app/api/discover/route.ts | 59 ++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/app/api/discover/route.ts b/src/app/api/discover/route.ts index 8c1f470..b1c761d 100644 --- a/src/app/api/discover/route.ts +++ b/src/app/api/discover/route.ts @@ -16,28 +16,43 @@ const topics = ['AI', 'tech']; /* TODO: Add UI to customize this */ export const GET = async (req: Request) => { try { - const data = ( - await Promise.all([ - ...new Array(articleWebsites.length * topics.length) - .fill(0) - .map(async (_, i) => { - return ( - await searchSearxng( - `site:${articleWebsites[i % articleWebsites.length]} ${ - topics[i % topics.length] - }`, - { - engines: ['bing news'], - pageno: 1, - }, - ) - ).results; - }), - ]) - ) - .map((result) => result) - .flat() - .sort(() => Math.random() - 0.5); + const params = new URL(req.url).searchParams; + const mode: 'normal' | 'preview' = + (params.get('mode') as 'normal' | 'preview') || 'normal'; + + let data = []; + + if (mode === 'normal') { + data = ( + await Promise.all([ + ...new Array(articleWebsites.length * topics.length) + .fill(0) + .map(async (_, i) => { + return ( + await searchSearxng( + `site:${articleWebsites[i % articleWebsites.length]} ${ + topics[i % topics.length] + }`, + { + engines: ['bing news'], + pageno: 1, + }, + ) + ).results; + }), + ]) + ) + .map((result) => result) + .flat() + .sort(() => Math.random() - 0.5); + } else { + data = ( + await searchSearxng( + `site:${articleWebsites[Math.floor(Math.random() * articleWebsites.length)]} ${topics[Math.floor(Math.random() * topics.length)]}`, + { engines: ['bing news'], pageno: 1 }, + ) + ).results; + } return Response.json( {