Skip to content

Full Toolkit for Next-Level Domain Analysis

Extract search suggestions logic 🔍

Felix Wotschofsky3bb5f08718be
diff --git a/app/api/search-suggestions/route.ts b/app/api/search-suggestions/route.ts
index 43bcd83..8ec4cdd 100644
--- a/app/api/search-suggestions/route.ts
+++ b/app/api/search-suggestions/route.ts
@@ -1,7 +1,7 @@
import { NextResponse } from 'next/server';
import { applyRateLimit } from '@/lib/api';
-import bigquery from '@/lib/bigquery';
+import { getSearchSuggestions } from '@/lib/search';
export const runtime = 'edge';
export const preferredRegion = 'home';
@@ -31,26 +31,7 @@ export const GET = async (request: Request) => {
});
}
- if (!bigquery) {
- return NextResponse.json([]);
- }
-
- const tableName = `\`${bigquery.projectId}.${process.env.BIGQUERY_DATASET}.lookups\``;
- const results = await bigquery.query({
- query: `
- SELECT baseDomain
- FROM ${tableName}
- WHERE baseDomain LIKE @query
- GROUP BY baseDomain
- ORDER BY COUNT(*) ASC
- LIMIT 5
- `,
- params: {
- query: `${query}%`,
- },
- });
-
- const suggestions = results.map((row: any) => row.baseDomain) as string[];
+ const suggestions = await getSearchSuggestions(query);
return NextResponse.json(suggestions, {
headers: {
diff --git a/app/lookup/[domain]/layout.tsx b/app/lookup/[domain]/layout.tsx
index 3b6e888..b332e23 100644
--- a/app/lookup/[domain]/layout.tsx
+++ b/app/lookup/[domain]/layout.tsx
@@ -1,12 +1,10 @@
import { ExternalLinkIcon } from 'lucide-react';
import type { Metadata } from 'next';
import dynamic from 'next/dynamic';
-import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { type FC, type ReactNode } from 'react';
-import { getDomain } from 'tldts';
-import bigquery from '@/lib/bigquery';
+import { recordLookup } from '@/lib/search';
import { isValidDomain } from '@/lib/utils';
import SearchForm from '../../_components/SearchForm';
@@ -46,35 +44,7 @@ const LookupLayout: FC<LookupLayoutProps> = ({
return notFound();
}
- if (bigquery) {
- const baseDomain = getDomain(domain);
-
- const forwardedFor = headers().get('x-forwarded-for');
- const ip = (forwardedFor ?? '127.0.0.1').split(',')[0];
-
- bigquery
- .insertRows({
- datasetName: process.env.BIGQUERY_DATASET!,
- tableName: 'lookups',
- rows: [
- {
- domain,
- baseDomain,
- timestamp: Math.floor(new Date().getTime() / 1000),
- ip,
- },
- ],
- })
- .catch((error) => {
- if ('errors' in error) {
- for (const err of error.errors) {
- console.error(err);
- }
- } else {
- console.error(error);
- }
- });
- }
+ recordLookup(domain);
return (
<>
diff --git a/app/lookup/[domain]/map/_components/ResultsGlobe.tsx b/app/lookup/[domain]/map/_components/ResultsGlobe.tsx
index 829d9ee..fb424fc 100644
--- a/app/lookup/[domain]/map/_components/ResultsGlobe.tsx
+++ b/app/lookup/[domain]/map/_components/ResultsGlobe.tsx
@@ -5,7 +5,7 @@ import naturalCompare from 'natural-compare-lite';
import { useTheme } from 'next-themes';
import dynamic from 'next/dynamic';
import { useRouter } from 'next/navigation';
-import { type FC, useEffect, useRef, useState } from 'react';
+import { type FC, useCallback, useEffect, useRef, useState } from 'react';
import { cn } from '@/lib/utils';
@@ -13,7 +13,71 @@ import styles from './ResultsGlobe.module.css';
const Globe = dynamic(() => import('react-globe.gl'), { ssr: false });
-export const runtime = 'edge';
+const createMarkerResults = (results: string[]) => {
+ if (results.length === 0) {
+ return undefined;
+ }
+
+ const sortedResults = results.slice().sort(naturalCompare);
+ const isTruncated = sortedResults.length > 5;
+ const shownResults = sortedResults.slice(0, isTruncated ? 4 : 5);
+
+ return h(
+ 'div',
+ shownResults.map((value) => h('p.text-xs.text-muted-foreground', value)),
+ isTruncated
+ ? h(
+ 'p.text-xs.text-muted-foreground.italic',
+ `and ${results.length - 4} more`
+ )
+ : undefined
+ );
+};
+
+const createMarker = (
+ label: string,
+ results: {
+ A: string[];
+ AAAA: string[];
+ CNAME: string[];
+ },
+ href: string,
+ onClick: () => void
+) =>
+ h(
+ 'div.marker-wrapper',
+
+ h(
+ 'div.flex.flex-col.gap-2.rounded-lg.bg-background.p-2.shadow-md',
+
+ h('h3.text-sm.font-bold', label),
+
+ createMarkerResults(results.A),
+ createMarkerResults(results.AAAA),
+ createMarkerResults(results.CNAME),
+
+ results.A.length === 0 &&
+ results.AAAA.length === 0 &&
+ results.CNAME.length === 0
+ ? h('p.text-xs.text-muted-foreground.italic', 'No records found!')
+ : undefined,
+
+ h(
+ 'p.text-xs.text-muted-foreground.italic',
+ h(
+ 'a.underline.decoration-dotted',
+ {
+ href,
+ onclick: (event: MouseEvent) => {
+ event.preventDefault();
+ onClick();
+ },
+ },
+ 'View full results'
+ )
+ )
+ )
+ );
type ResultsGlobeProps = {
domain: string;
@@ -51,6 +115,15 @@ const ResultsGlobe: FC<ResultsGlobeProps> = ({ domain, markers }) => {
return () => observer.disconnect();
}, [wrapperRef]);
+ const htmlElementHandler = useCallback(
+ (data: ResultsGlobeProps['markers'][number]) => {
+ const href = `/lookup/${domain}?resolver=cloudflare&location=${data.code}`;
+ const onClick = () => router.push(href);
+ return createMarker(data.name, data.results, href, onClick);
+ },
+ [domain, router]
+ );
+
return (
<div ref={wrapperRef} className={cn(styles.wrapper, 'w-full')}>
<Globe
@@ -64,92 +137,7 @@ const ResultsGlobe: FC<ResultsGlobeProps> = ({ domain, markers }) => {
width={width}
htmlElementsData={markers}
// @ts-expect-error
- htmlElement={(d: ResultsGlobeProps['markers'][number]) => {
- return h(
- 'div.marker-wrapper',
-
- h(
- 'div.flex.flex-col.gap-2.rounded-lg.bg-background.p-2.shadow-md',
-
- h('h3.text-sm.font-bold', d.name),
- d.results.A.length
- ? h(
- 'div',
- d.results.A.sort(naturalCompare)
- .slice(0, d.results.A.length > 5 ? 4 : 5)
- .map((value) =>
- h('p.text-xs.text-muted-foreground', value)
- ),
- d.results.A.length > 5
- ? h(
- 'p.text-xs.text-muted-foreground.italic',
- `and ${d.results.A.length - 4} more`
- )
- : undefined
- )
- : undefined,
-
- d.results.AAAA.length
- ? h(
- 'div',
- d.results.AAAA.sort(naturalCompare)
- .slice(0, d.results.AAAA.length > 5 ? 4 : 5)
- .map((value) =>
- h('p.text-xs.text-muted-foreground', value)
- ),
- d.results.AAAA.length > 5
- ? h(
- 'p.text-xs.text-muted-foreground.italic',
- `and ${d.results.AAAA.length - 4} more`
- )
- : undefined
- )
- : undefined,
-
- d.results.CNAME.length
- ? h(
- 'div',
- d.results.CNAME.sort(naturalCompare)
- .slice(0, d.results.CNAME.length > 5 ? 4 : 5)
- .map((value) =>
- h('p.text-xs.text-muted-foreground', value)
- ),
- d.results.CNAME.length > 5
- ? h(
- 'p.text-xs.text-muted-foreground.italic',
- `and ${d.results.CNAME.length - 4} more`
- )
- : undefined
- )
- : undefined,
-
- d.results.A.length === 0 &&
- d.results.AAAA.length === 0 &&
- d.results.CNAME.length === 0
- ? h(
- 'p.text-xs.text-muted-foreground.italic',
- 'No records found!'
- )
- : undefined,
-
- h(
- 'p.text-xs.text-muted-foreground.italic',
- h(
- 'a.underline.decoration-dotted',
- {
- href: `/lookup/${domain}?resolver=cloudflare&location=${d.code}`,
- onclick: (event: MouseEvent) => {
- event.preventDefault();
- const el = event.target as HTMLAnchorElement;
- router.push(el.href);
- },
- },
- 'View full results'
- )
- )
- )
- );
- }}
+ htmlElement={htmlElementHandler}
/>
</div>
);
diff --git a/lib/search.ts b/lib/search.ts
new file mode 100644
index 0000000..220acd3
--- /dev/null
+++ b/lib/search.ts
@@ -0,0 +1,61 @@
+import { headers } from 'next/headers';
+import { getDomain } from 'tldts';
+
+import bigquery from '@/lib/bigquery';
+
+export const recordLookup = (domain: string) => {
+ if (!bigquery) {
+ return;
+ }
+
+ const baseDomain = getDomain(domain);
+
+ const forwardedFor = headers().get('x-forwarded-for');
+ const ip = (forwardedFor ?? '127.0.0.1').split(',')[0];
+
+ bigquery
+ .insertRows({
+ datasetName: process.env.BIGQUERY_DATASET!,
+ tableName: 'lookups',
+ rows: [
+ {
+ domain,
+ baseDomain,
+ timestamp: Math.floor(new Date().getTime() / 1000),
+ ip,
+ },
+ ],
+ })
+ .catch((error) => {
+ if ('errors' in error) {
+ for (const err of error.errors) {
+ console.error(err);
+ }
+ } else {
+ console.error(error);
+ }
+ });
+};
+
+export const getSearchSuggestions = async (query: string) => {
+ if (!bigquery) {
+ return [];
+ }
+
+ const tableName = `\`${bigquery.projectId}.${process.env.BIGQUERY_DATASET}.lookups\``;
+ const results = await bigquery.query({
+ query: `
+ SELECT baseDomain
+ FROM ${tableName}
+ WHERE baseDomain LIKE @query
+ GROUP BY baseDomain
+ ORDER BY COUNT(*) ASC
+ LIMIT 5
+ `,
+ params: {
+ query: `${query}%`,
+ },
+ });
+
+ return results.map((row: any) => row.baseDomain) as string[];
+};