Skip to content

Full Toolkit for Next-Level Domain Analysis

Clean up search suggestions 🧹

Felix Wotschofskyc260b0f73ef7
diff --git a/lib/search.ts b/lib/search.ts
index 18e3f3a..825e2ec 100644
--- a/lib/search.ts
+++ b/lib/search.ts
@@ -43,19 +43,22 @@ export const recordLookup = async (payload: LookupLogPayload) => {
};
export const getSearchSuggestions = async (query: string) => {
- const segments = query.split('.').filter(Boolean);
+ const primarySuggestions = await getSearchSuggestionsForPrefix(query);
- const suggestions = await Promise.all(
- // Limit to 2 segments to avoid abuse and keep results sensible
- segments.slice(0, 2).map(async (_, i) => {
- const querySegment = segments.slice(i).join('.');
- const suggestions = await getSearchSuggestionsForPrefix(querySegment);
- const prefix = i === 0 ? '' : segments.slice(0, i).join('.');
- return suggestions.map((s) => (prefix ? `${prefix}.${s}` : s));
- }),
- );
+ if (primarySuggestions.length > 0) {
+ return primarySuggestions;
+ }
+
+ const segments = query.split('.');
+ if (segments.length < 2) {
+ return [];
+ }
+
+ const secondarySuggestions = await getSearchSuggestionsForPrefix(
+ segments.slice(1).join('.'),
+ ).then((suggestions) => suggestions.map((s) => `${segments[0]}.${s}`));
- return Array.from(new Set(suggestions.flat()));
+ return secondarySuggestions;
};
export const getSearchSuggestionsForPrefix = async (prefix: string) => {