|
| 1 | +import axios from 'axios'; |
| 2 | +import ToolName from '@/components/ui/ToolCard/Tool.Name'; |
| 3 | +import Title from '@/components/ui/ToolCard/Tool.Title'; |
| 4 | +import ToolFooter from '@/components/ui/ToolCard/Tool.Footer'; |
| 5 | +import Image from 'next/image'; |
| 6 | +import ProductHuntCard from '@/components/ui/ProductHuntCard'; |
| 7 | +import request from 'request'; |
| 8 | + |
| 9 | +function extractHostAndPath(url: string): { host: string; path: string } { |
| 10 | + const regex = /^(?:https?:\/\/)?([^\/?#]+)(\/[^?#]*)?/; |
| 11 | + const matches = url.match(regex); |
| 12 | + |
| 13 | + if (matches && matches.length >= 2) { |
| 14 | + const host = matches[1]; |
| 15 | + const path = matches[2] || '/'; // Default to '/' if path is not provided |
| 16 | + return { host, path }; |
| 17 | + } else { |
| 18 | + throw new Error('Invalid URL format'); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +function extractLink(url: string) { |
| 23 | + // Regular expression to match URLs with "www." or without any protocol |
| 24 | + const regex = /^(?:https?:\/\/)?(?:www\.)?(.*)/; |
| 25 | + |
| 26 | + // Extract the domain from the URL using regex |
| 27 | + const matches = url.match(regex); |
| 28 | + |
| 29 | + // If matches found, construct the link with "https://" prefix |
| 30 | + if (matches && matches.length > 1) { |
| 31 | + const domain = matches[1]; |
| 32 | + return `https://${domain}`; |
| 33 | + } |
| 34 | + |
| 35 | + // If no matches found, return the original URL |
| 36 | + return url; |
| 37 | +} |
| 38 | + |
| 39 | +type Product = { |
| 40 | + node: { |
| 41 | + id: string; |
| 42 | + name: string; |
| 43 | + description: string; |
| 44 | + slug: string; |
| 45 | + tagline: string; |
| 46 | + votesCount: number; |
| 47 | + thumbnail: { |
| 48 | + url: string; |
| 49 | + }; |
| 50 | + productLinks: { |
| 51 | + url: string; |
| 52 | + }; |
| 53 | + website: string; |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +export const metadata = { |
| 58 | + title: 'Best dev tools this week on Product Hunt - Dev Hunt', |
| 59 | + metadataBase: new URL('https://devhunt.org'), |
| 60 | + alternates: { |
| 61 | + canonical: '/best-dev-tools-this-week-on-product-hunt', |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +export default async () => { |
| 66 | + const origin = process.env.NODE_ENV == 'development' ? 'http://localhost:3001' : 'https://devhunt.org'; |
| 67 | + const { |
| 68 | + data: { posts }, |
| 69 | + } = await axios.get(`${origin}/api/ph-dev-tools`); |
| 70 | + |
| 71 | + // Create an array to store all the promises for the requests |
| 72 | + const requests = posts.map((item: Product) => { |
| 73 | + // Return a promise for each request |
| 74 | + return new Promise((resolve, reject) => { |
| 75 | + // Perform the request asynchronously |
| 76 | + request({ url: item.node.website, followRedirect: false }, function (err, res, body) { |
| 77 | + if (err) { |
| 78 | + reject(err); |
| 79 | + } else { |
| 80 | + resolve(extractLink(extractHostAndPath(res.headers.location as string).host)); |
| 81 | + } |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + // Wait for all promises to resolve |
| 87 | + const websites = (await Promise.all(requests)) || []; |
| 88 | + |
| 89 | + return ( |
| 90 | + <section className="max-w-4xl mt-20 mx-auto px-4 md:px-8"> |
| 91 | + <div> |
| 92 | + <h1 className="text-slate-50 text-3xl font-semibold">Best dev tools this week on Product Hunt</h1> |
| 93 | + </div> |
| 94 | + <ul className="mt-10 mb-12 divide-y divide-slate-800/60"> |
| 95 | + {posts?.map((tool: Product, idx: number) => ( |
| 96 | + <li key={idx} className="py-3"> |
| 97 | + <ProductHuntCard href={websites[idx] ? `${websites[idx]}/?ref=devhunt` : tool.node.website}> |
| 98 | + {/* {console.log(tool.node)} */} |
| 99 | + <div className="w-full flex items-center gap-x-4"> |
| 100 | + <Image |
| 101 | + src={tool.node.thumbnail.url} |
| 102 | + alt={tool.node.name} |
| 103 | + width={64} |
| 104 | + height={64} |
| 105 | + className="rounded-full object-cover flex-none" |
| 106 | + /> |
| 107 | + <div className="w-full space-y-1"> |
| 108 | + <ToolName href={websites[idx] ? websites[idx] : tool.node.website}>{tool.node.name}</ToolName> |
| 109 | + <Title className="line-clamp-2">{tool.node.tagline}</Title> |
| 110 | + <ToolFooter> |
| 111 | + {/* <Tags items={[tool.product_pricing_types?.title ?? 'Free', ...(tool.product_categories || []).map(c => c.name)]} /> */} |
| 112 | + </ToolFooter> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <div className="px-4 py-1 text-center active:scale-[1.5] duration-200 rounded-md border bg-[linear-gradient(180deg,_#1E293B_0%,_rgba(30,_41,_59,_0.00)_100%)] border-slate-700 text-orange-300"> |
| 116 | + <span className="text-sm pointer-events-none">#{idx + 1}</span> |
| 117 | + </div> |
| 118 | + </ProductHuntCard> |
| 119 | + </li> |
| 120 | + ))} |
| 121 | + </ul> |
| 122 | + </section> |
| 123 | + ); |
| 124 | +}; |
0 commit comments