-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crawler): add multiple urls support (#1112)
* feat(Field): add icon support * feat(Crawler): replace submit button with send icon * feat(crawler): add multiple urls support * feat: add <FeedItems/> component to display adding items * feat(FeedItems): add remove icon * feat: add url displayer * feat: add invalid url message * fix: add crawler to upload page * feat: clean sueCrawler * feat: rename Feed to KnowledgeToFeed * feat: add tracking
- Loading branch information
1 parent
02964c4
commit f230bc1
Showing
31 changed files
with
289 additions
and
46 deletions.
There are no files selected for viewing
8 changes: 4 additions & 4 deletions
8
frontend/app/chat/[chatId]/components/ActionsBar/ActionsBar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
...t/[chatId]/components/ActionsBar/components/Feed/components/Crawler/helpers/isValidUrl.ts
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
frontend/app/chat/[chatId]/components/ActionsBar/components/Feed/index.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...components/ActionsBar/components/KnowledgeToFeed/components/Crawler/helpers/isValidUrl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export const isValidUrl = (urlString: string): boolean => { | ||
const urlPattern = new RegExp( | ||
"^(https?:\\/\\/)?" + // validate protocol | ||
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // validate domain name | ||
"((\\d{1,3}\\.){3}\\d{1,3}))" + // validate OR ip (v4) address | ||
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // validate port and path | ||
"(\\?[;&a-z\\d%_.~+=-]*)?" + // validate query string | ||
"(\\#[-a-z\\d_]*)?$", | ||
"i" | ||
); // validate fragment locator | ||
|
||
return !!urlPattern.test(urlString); | ||
}; |
57 changes: 57 additions & 0 deletions
57
...]/components/ActionsBar/components/KnowledgeToFeed/components/Crawler/hooks/useCrawler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"use client"; | ||
import { useRef, useState } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { useSupabase } from "@/lib/context/SupabaseProvider"; | ||
import { useToast } from "@/lib/hooks"; | ||
import { redirectToLogin } from "@/lib/router/redirectToLogin"; | ||
import { useEventTracking } from "@/services/analytics/useEventTracking"; | ||
|
||
import { FeedItemType } from "../../../types"; | ||
import { isValidUrl } from "../helpers/isValidUrl"; | ||
|
||
type UseCrawlerProps = { | ||
addContent: (content: FeedItemType) => void; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const useCrawler = ({ addContent }: UseCrawlerProps) => { | ||
const urlInputRef = useRef<HTMLInputElement | null>(null); | ||
const { session } = useSupabase(); | ||
const { publish } = useToast(); | ||
const { t } = useTranslation(["translation", "upload"]); | ||
const [urlToCrawl, setUrlToCrawl] = useState<string>(""); | ||
const { track } = useEventTracking(); | ||
|
||
if (session === null) { | ||
redirectToLogin(); | ||
} | ||
|
||
const handleSubmit = () => { | ||
if (urlToCrawl === "") { | ||
return; | ||
} | ||
if (!isValidUrl(urlToCrawl)) { | ||
void track("URL_INVALID"); | ||
publish({ | ||
variant: "danger", | ||
text: t("invalidUrl"), | ||
}); | ||
|
||
return; | ||
} | ||
void track("URL_CRAWLED"); | ||
addContent({ | ||
source: "crawl", | ||
url: urlToCrawl, | ||
}); | ||
setUrlToCrawl(""); | ||
}; | ||
|
||
return { | ||
urlInputRef, | ||
urlToCrawl, | ||
setUrlToCrawl, | ||
handleSubmit, | ||
}; | ||
}; |
49 changes: 49 additions & 0 deletions
49
...at/[chatId]/components/ActionsBar/components/KnowledgeToFeed/components/Crawler/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use client"; | ||
import { useTranslation } from "react-i18next"; | ||
import { MdSend } from "react-icons/md"; | ||
|
||
import Button from "@/lib/components/ui/Button"; | ||
import Field from "@/lib/components/ui/Field"; | ||
|
||
import { useCrawler } from "./hooks/useCrawler"; | ||
import { FeedItemType } from "../../types"; | ||
|
||
type CrawlerProps = { | ||
addContent: (content: FeedItemType) => void; | ||
}; | ||
|
||
export const Crawler = ({ addContent }: CrawlerProps): JSX.Element => { | ||
const { urlInputRef, urlToCrawl, handleSubmit, setUrlToCrawl } = useCrawler({ | ||
addContent, | ||
}); | ||
const { t } = useTranslation(["translation", "upload"]); | ||
|
||
return ( | ||
<div className="w-full flex justify-center items-center"> | ||
<div className="max-w-xl w-full"> | ||
<form | ||
onSubmit={(e) => { | ||
e.preventDefault(); | ||
handleSubmit(); | ||
}} | ||
className="w-full" | ||
> | ||
<Field | ||
name="crawlurl" | ||
ref={urlInputRef} | ||
type="text" | ||
placeholder={t("webSite", { ns: "upload" })} | ||
className="w-full" | ||
value={urlToCrawl} | ||
onChange={(e) => setUrlToCrawl(e.target.value)} | ||
icon={ | ||
<Button variant={"tertiary"}> | ||
<MdSend /> | ||
</Button> | ||
} | ||
/> | ||
</form> | ||
</div> | ||
</div> | ||
); | ||
}; |
40 changes: 40 additions & 0 deletions
40
...atId]/components/ActionsBar/components/KnowledgeToFeed/components/FeedItems/FeedItems.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Fragment } from "react"; | ||
import { IoMdCloseCircle } from "react-icons/io"; | ||
import { MdLink } from "react-icons/md"; | ||
|
||
import { UrlDisplay } from "./components"; | ||
import { FeedItemType } from "../../types"; | ||
|
||
type FeedItemsProps = { | ||
contents: FeedItemType[]; | ||
removeContent: (index: number) => void; | ||
}; | ||
|
||
export const FeedItems = ({ | ||
contents, | ||
removeContent, | ||
}: FeedItemsProps): JSX.Element => { | ||
if (contents.length === 0) { | ||
return <Fragment />; | ||
} | ||
|
||
return ( | ||
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-2 gap-4 mt-5 shadow-md shadow-md dark:shadow-primary/25 hover:shadow-xl transition-shadow rounded-xl bg-white dark:bg-black border border-black/10 dark:border-white/25 p-6"> | ||
{contents.map((item, index) => ( | ||
<div | ||
key={item.url} | ||
className="relative bg-gray-100 p-4 rounded-lg shadow-sm" | ||
> | ||
<IoMdCloseCircle | ||
className="absolute top-2 right-2 cursor-pointer text-gray-400 text-2xl" | ||
onClick={() => removeContent(index)} | ||
/> | ||
<div className="flex items-center"> | ||
<MdLink className="mr-2 text-2xl" /> | ||
<UrlDisplay url={item.url} /> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
); | ||
}; |
23 changes: 23 additions & 0 deletions
23
...sBar/components/KnowledgeToFeed/components/FeedItems/components/UrlDisplay/UrlDisplay.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useState } from "react"; | ||
|
||
import { enhanceUrlDisplay } from "./utils/enhanceUrlDisplay"; | ||
|
||
type UrlDisplayProps = { | ||
url: string; | ||
}; | ||
|
||
export const UrlDisplay = ({ url }: UrlDisplayProps): JSX.Element => { | ||
const [showFullUrl, setShowFullUrl] = useState(false); | ||
|
||
const toggleShowFullUrl = () => { | ||
setShowFullUrl(!showFullUrl); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<span className="cursor-pointer" onClick={toggleShowFullUrl}> | ||
{showFullUrl ? url : enhanceUrlDisplay(url)} | ||
</span> | ||
</div> | ||
); | ||
}; |
1 change: 1 addition & 0 deletions
1
...ActionsBar/components/KnowledgeToFeed/components/FeedItems/components/UrlDisplay/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./UrlDisplay"; |
24 changes: 24 additions & 0 deletions
24
...nts/KnowledgeToFeed/components/FeedItems/components/UrlDisplay/utils/enhanceUrlDisplay.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export const enhanceUrlDisplay = (url: string): string => { | ||
const parts = url.split("/"); | ||
|
||
// Check if the URL has at least 3 parts (protocol, domain, and one more segment) | ||
if (parts.length >= 3) { | ||
const domain = parts[2]; | ||
const path = parts.slice(3).join("/"); | ||
|
||
// Split the domain by "." to check for subdomains and remove "www" | ||
const domainParts = domain.split("."); | ||
if (domainParts[0] === "www") { | ||
domainParts.shift(); // Remove "www" | ||
} | ||
|
||
// Combine the beginning (subdomain/domain) and the end (trimmed path) | ||
const beginning = domainParts.join("."); | ||
const trimmedPath = path.slice(0, 5) + "..." + path.slice(-5); // Display the beginning and end of the path | ||
|
||
return `${beginning}/${trimmedPath}`; | ||
} | ||
|
||
// If the URL doesn't have enough parts, return it as is | ||
return url; | ||
}; |
1 change: 1 addition & 0 deletions
1
...components/ActionsBar/components/KnowledgeToFeed/components/FeedItems/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./UrlDisplay"; |
1 change: 1 addition & 0 deletions
1
...t/[chatId]/components/ActionsBar/components/KnowledgeToFeed/components/FeedItems/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./FeedItems"; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions
1
...nsBar/components/Feed/components/index.ts → ...nents/KnowledgeToFeed/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./Crawler"; | ||
export * from "./FeedItems"; | ||
export * from "./FileUploader"; |
22 changes: 22 additions & 0 deletions
22
...hat/[chatId]/components/ActionsBar/components/KnowledgeToFeed/hooks/useKnowledgeToFeed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useState } from "react"; | ||
|
||
import { FeedItemType } from "../types"; | ||
|
||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
export const useKnowledgeToFeed = () => { | ||
const [contents, setContents] = useState<FeedItemType[]>([]); | ||
|
||
const addContent = (content: FeedItemType) => { | ||
setContents((prevContents) => [...prevContents, content]); | ||
}; | ||
const removeContent = (index: number) => { | ||
setContents((prevContents) => prevContents.filter((_, i) => i !== index)); | ||
}; | ||
|
||
return { | ||
addContent, | ||
contents, | ||
setContents, | ||
removeContent, | ||
}; | ||
}; |
1 change: 1 addition & 0 deletions
1
frontend/app/chat/[chatId]/components/ActionsBar/components/KnowledgeToFeed/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./KnowledgeToFeed"; |
6 changes: 6 additions & 0 deletions
6
frontend/app/chat/[chatId]/components/ActionsBar/components/KnowledgeToFeed/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export type FeedItemSource = "crawl" | "upload"; | ||
|
||
export type FeedItemType = { | ||
source: FeedItemSource; | ||
url: string; | ||
}; |
2 changes: 1 addition & 1 deletion
2
frontend/app/chat/[chatId]/components/ActionsBar/components/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from "./ChatInput"; | ||
export * from "./Feed"; | ||
export * from "./KnowledgeToFeed"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.