From 178c1fd594dd3686331a7bc8293671bcbdc95bbd Mon Sep 17 00:00:00 2001 From: Jens Claes Date: Fri, 4 Nov 2022 11:44:27 +0100 Subject: [PATCH 1/3] chore: yarn lint --fix --- components/Autocomplete.tsx | 219 +++++++++--------- components/Html.tsx | 2 +- components/PackageReadMePlaceholder.tsx | 38 +-- lib/utils.ts | 2 +- pages/_document.tsx | 5 +- pages/index.tsx | 29 ++- .../packages/[package]/versions/[version].tsx | 26 +-- .../versions/[version]/topics/[topic].tsx | 4 +- 8 files changed, 154 insertions(+), 171 deletions(-) diff --git a/components/Autocomplete.tsx b/components/Autocomplete.tsx index f0e076c..1f2d47e 100644 --- a/components/Autocomplete.tsx +++ b/components/Autocomplete.tsx @@ -1,127 +1,118 @@ -import { Heading, Paragraph } from "@datacamp/waffles-text"; -import router from "next/router"; -import { useEffect, useState } from "react"; -import { API_URL } from "../lib/utils"; +import { Heading, Paragraph } from '@datacamp/waffles-text'; +import router from 'next/router'; +import { useEffect, useState } from 'react'; + +import { API_URL } from '../lib/utils'; type Props = { - searchInput: string + searchInput: string; }; export default function AutoComplete({ searchInput }: Props) { - const [packageSuggestions, setPackageSuggestions] = useState([]); const [topicSuggestions, setTopicSuggestions] = useState([]); - function onClick(query) { - router.push(`/search?q=${encodeURIComponent(query)}`); - }; + function onClick(query) { + router.push(`/search?q=${encodeURIComponent(query)}`); + } + + async function autoComplete(query) { + try { + // fetch the data + const [resPackages, resTopics] = await Promise.all([ + fetch(`${API_URL}/search_packages?q=${query}&page=1&latest=1`, { + headers: { + Accept: 'application/json', + }, + }), + fetch(`${API_URL}/search_functions?q=${query}&page=1&latest=1`, { + headers: { + Accept: 'application/json', + }, + }), + ]); - async function autoComplete(query) { - try { - // fetch the data - const [resPackages, resTopics] = await Promise.all([ - fetch( - `${API_URL}/search_packages?q=${query}&page=1&latest=1`, - { - headers: { - Accept: 'application/json', - }, - }, - ), - fetch( - `${API_URL}/search_functions?q=${query}&page=1&latest=1`, - { - headers: { - Accept: 'application/json', - }, - }, - ) - ]); - - const { packages } = await resPackages?.json(); - const functions = await resTopics?.json(); - const topics = functions?.functions; - const relevantPackages = packages?.filter((p)=>(p?.score>1)); - const relevantTopics = topics?.filter((p)=>(p?.score>1)); - setPackageSuggestions(relevantPackages?.slice(0, Math.min(relevantPackages?.length, 5))); - setTopicSuggestions(relevantTopics?.slice(0, Math.min(relevantTopics?.length, 5))); - - } catch (err) { - console.error(err); - } - }; + const { packages } = await resPackages?.json(); + const functions = await resTopics?.json(); + const topics = functions?.functions; + const relevantPackages = packages?.filter((p) => p?.score > 1); + const relevantTopics = topics?.filter((p) => p?.score > 1); + setPackageSuggestions( + relevantPackages?.slice(0, Math.min(relevantPackages?.length, 5)), + ); + setTopicSuggestions( + relevantTopics?.slice(0, Math.min(relevantTopics?.length, 5)), + ); + } catch (err) { + console.error(err); + } + } - useEffect(()=>{ - autoComplete(searchInput); - }, [searchInput]) + useEffect(() => { + autoComplete(searchInput); + }, [searchInput]); - return ( -
- { - searchInput - && -
onClick(searchInput)} - className="flex items-center px-4 py-4 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5" - > - {`View results for "${searchInput}"`} -
- } -
- { - packageSuggestions?.length>0 - && - searchInput - && -
    -
  • - PACKAGES + return ( +
    + {searchInput && ( +
    onClick(searchInput)} + > + {`View results for "${searchInput}"`} +
    + )} +
    + {packageSuggestions?.length > 0 && searchInput && ( +
      +
    • + + PACKAGES + +
    • + {packageSuggestions?.map((p) => { + return ( +
    • onClick(p?.fields?.package_name)} + > + + {p?.fields?.package_name} +
    • - { - packageSuggestions?.map((p)=>{ - return ( -
    • onClick(p?.fields?.package_name)} - className="flex items-center px-4 py-2 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5" - > - {p?.fields?.package_name} -
    • - ) - })} -
    - } -
    -
    - { - topicSuggestions?.length>0 - && - searchInput - && -
      -
    • - FUNCTIONS + ); + })} +
    + )} +
    +
    + {topicSuggestions?.length > 0 && searchInput && ( +
      +
    • + + FUNCTIONS + +
    • + {topicSuggestions?.map((t) => { + return ( +
    • onClick(t?.fields?.name)} + > +
      + {`${t?.fields?.name}`} +
      +
      + {`(${t?.fields?.package_name})`} +
    • - { - topicSuggestions?.map((t)=>{ - return ( -
    • onClick(t?.fields?.name)} - className="flex items-center px-4 py-2 cursor-pointer hover:bg-dc-beige200 hover:opacity-0.5" - > -
      - {`${t?.fields?.name}`} -
      -
      - {`(${t?.fields?.package_name})`} -
      -
    • - ) - })} -
    - } -
    -
    - ) -} \ No newline at end of file + ); + })} +
+ )} +
+
+ ); +} diff --git a/components/Html.tsx b/components/Html.tsx index f9492bc..d99d63d 100644 --- a/components/Html.tsx +++ b/components/Html.tsx @@ -1,4 +1,4 @@ -import { MathJax, MathJaxContext } from "better-react-mathjax"; +import { MathJax, MathJaxContext } from 'better-react-mathjax'; type Props = { children: string; diff --git a/components/PackageReadMePlaceholder.tsx b/components/PackageReadMePlaceholder.tsx index 46ade4b..2d25928 100644 --- a/components/PackageReadMePlaceholder.tsx +++ b/components/PackageReadMePlaceholder.tsx @@ -1,23 +1,17 @@ import Html from './Html'; type Props = { - packageName: string, - version: string, - title: string, - description: string -} + description: string; + packageName: string; + title: string; + version: string; +}; export default function PackageReadMePlaceholder(data: Props) { - const { - packageName, - version, - title, - description - } = data; + const { description, packageName, title, version } = data; return ( -
+
@@ -25,25 +19,17 @@ export default function PackageReadMePlaceholder(data: Props) { {`${packageName} (version ${version})`}
- { - title - && - ( + {title && (
-

{title}

+

{title}

- ) - } - { - description - && - ( + )} + {description && (

Description

{description}
- ) - } + )}
diff --git a/lib/utils.ts b/lib/utils.ts index d0f5392..2f1193c 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -41,4 +41,4 @@ export function copyTextToClipboard(text: string) { navigator.clipboard.writeText(text); } -export const API_URL = 'https://api.rdocumentation.org'; \ No newline at end of file +export const API_URL = 'https://api.rdocumentation.org'; diff --git a/pages/_document.tsx b/pages/_document.tsx index 6829c2c..8f462f1 100644 --- a/pages/_document.tsx +++ b/pages/_document.tsx @@ -20,7 +20,10 @@ class MyDocument extends Document { - +