From 4ea0bebd92eec021ac0dda5a1fc4ae32038f9877 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Fri, 31 Jan 2025 00:48:03 +0530 Subject: [PATCH 1/5] (refactor:remote uploads) dynamic ingestor types --- frontend/src/components/ToggleSwitch.tsx | 58 +++ frontend/src/upload/Upload.tsx | 449 ++++++++++++++--------- frontend/src/upload/types/ingestor.ts | 43 +++ 3 files changed, 367 insertions(+), 183 deletions(-) create mode 100644 frontend/src/components/ToggleSwitch.tsx create mode 100644 frontend/src/upload/types/ingestor.ts diff --git a/frontend/src/components/ToggleSwitch.tsx b/frontend/src/components/ToggleSwitch.tsx new file mode 100644 index 000000000..02745799d --- /dev/null +++ b/frontend/src/components/ToggleSwitch.tsx @@ -0,0 +1,58 @@ +import React from 'react'; + +type ToggleSwitchProps = { + checked: boolean; + onChange: (checked: boolean) => void; + className?: string; + label?: string; + disabled?: boolean; + activeColor?: string; + inactiveColor?: string; + id?: string; +}; + +const ToggleSwitch: React.FC = ({ + checked, + onChange, + className = '', + label, + disabled = false, + activeColor = 'bg-purple-30', + inactiveColor = 'bg-transparent', + id, +}) => { + return ( + + ); +}; + +export default ToggleSwitch; diff --git a/frontend/src/upload/Upload.tsx b/frontend/src/upload/Upload.tsx index b1c45156e..d2188966e 100644 --- a/frontend/src/upload/Upload.tsx +++ b/frontend/src/upload/Upload.tsx @@ -16,6 +16,23 @@ import { selectSourceDocs, } from '../preferences/preferenceSlice'; import WrapperModal from '../modals/WrapperModal'; +import { + IngestorType, + IngestorConfig, + RedditIngestorConfig, + GithubIngestorConfig, + CrawlerIngestorConfig, + UrlIngestorConfig, +} from './types/ingestor'; + +type IngestorState = { + type: IngestorType; + config: + | RedditIngestorConfig + | GithubIngestorConfig + | CrawlerIngestorConfig + | UrlIngestorConfig; +}; function Upload({ receivedFile = [], @@ -33,18 +50,19 @@ function Upload({ onSuccessfulUpload?: () => void; }) { const [docName, setDocName] = useState(receivedFile[0]?.name); - const [urlName, setUrlName] = useState(''); - const [url, setUrl] = useState(''); - const [repoUrl, setRepoUrl] = useState(''); // P3f93 - const [redditData, setRedditData] = useState({ - client_id: '', - client_secret: '', - user_agent: '', - search_queries: [''], - number_posts: 10, - }); - const [activeTab, setActiveTab] = useState(renderTab); const [files, setfiles] = useState(receivedFile); + const [activeTab, setActiveTab] = useState(renderTab); + + // New unified ingestor state + const [ingestor, setIngestor] = useState({ + type: 'crawler', + name: '', + config: { + name: '', + url: '', + } as CrawlerIngestorConfig, + }); + const [progress, setProgress] = useState<{ type: 'UPLOAD' | 'TRAINING'; percentage: number; @@ -55,12 +73,11 @@ function Upload({ const { t } = useTranslation(); const setTimeoutRef = useRef(); - const urlOptions: { label: string; value: string }[] = [ - { label: `Crawler`, value: 'crawler' }, - // { label: t('modals.uploadDoc.sitemap'), value: 'sitemap' }, - { label: `Link`, value: 'url' }, - { label: `GitHub`, value: 'github' }, - { label: `Reddit`, value: 'reddit' }, + const urlOptions: { label: string; value: IngestorType }[] = [ + { label: 'Crawler', value: 'crawler' }, + { label: 'Link', value: 'url' }, + { label: 'GitHub', value: 'github' }, + { label: 'Reddit', value: 'reddit' }, ]; const [urlType, setUrlType] = useState<{ label: string; value: string }>({ @@ -284,22 +301,23 @@ function Upload({ const uploadRemote = () => { const formData = new FormData(); - formData.append('name', urlName); + formData.append('name', ingestor.name); formData.append('user', 'local'); - if (urlType !== null) { - formData.append('source', urlType?.value); - } - formData.append('data', url); - if ( - redditData.client_id.length > 0 && - redditData.client_secret.length > 0 - ) { - formData.set('name', 'other'); - formData.set('data', JSON.stringify(redditData)); - } - if (urlType.value === 'github') { - formData.append('repo_url', repoUrl); // Pdeac + formData.append('source', ingestor.type); + + if (ingestor.type === 'reddit') { + formData.set('data', JSON.stringify(ingestor.config)); + } else if (ingestor.type === 'github') { + const githubConfig = ingestor.config as GithubIngestorConfig; + formData.append('repo_url', githubConfig.repo_url); + formData.append('data', githubConfig.repo_url); + } else { + const urlBasedConfig = ingestor.config as + | CrawlerIngestorConfig + | UrlIngestorConfig; + formData.append('data', urlBasedConfig.url); } + const apiHost = import.meta.env.VITE_API_HOST; const xhr = new XMLHttpRequest(); xhr.upload.addEventListener('progress', (event) => { @@ -346,20 +364,158 @@ function Upload({ }, }); + const isUploadDisabled = () => { + if (activeTab !== 'remote') return false; + + switch (ingestor.type) { + case 'reddit': { + const redditConfig = ingestor.config as RedditIngestorConfig; + return ( + !redditConfig.client_id || + !redditConfig.client_secret || + !redditConfig.user_agent || + !redditConfig.search_queries.length || + !redditConfig.number_posts + ); + } + case 'github': + return !(ingestor.config as GithubIngestorConfig).repo_url; + default: { + const urlConfig = ingestor.config as + | CrawlerIngestorConfig + | UrlIngestorConfig; + return !urlConfig.url || !ingestor.name; + } + } + }; + + const handleIngestorChange = ( + e: React.ChangeEvent, + ) => { + const { name, value } = e.target; + + if (ingestor.type === 'reddit') { + const redditConfig = ingestor.config as RedditIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...redditConfig, + [name]: + name === 'search_queries' + ? value.split(',').map((item) => item.trim()) + : name === 'number_posts' + ? parseInt(value) + : value, + }, + }); + } else if (ingestor.type === 'github') { + const githubConfig = ingestor.config as GithubIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...githubConfig, + [name]: value, + }, + }); + } else { + const urlConfig = ingestor.config as + | CrawlerIngestorConfig + | UrlIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...urlConfig, + [name]: value, + }, + }); + } + }; + const handleChange = ( e: React.ChangeEvent, ) => { const { name, value } = e.target; - if (name === 'search_queries' && value.length > 0) { - setRedditData({ - ...redditData, - [name]: value.split(',').map((item) => item.trim()), + + if (ingestor.type === 'reddit') { + const redditConfig = ingestor.config as RedditIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...redditConfig, + [name]: + name === 'search_queries' + ? value.split(',').map((item) => item.trim()) + : name === 'number_posts' + ? parseInt(value) + : value, + }, }); - } else - setRedditData({ - ...redditData, - [name]: name === 'number_posts' ? parseInt(value) : value, + } else if (ingestor.type === 'github') { + const githubConfig = ingestor.config as GithubIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...githubConfig, + [name]: value, + }, }); + } else { + const urlConfig = ingestor.config as + | CrawlerIngestorConfig + | UrlIngestorConfig; + setIngestor({ + ...ingestor, + config: { + ...urlConfig, + [name]: value, + }, + }); + } + }; + + const handleIngestorTypeChange = (type: IngestorType) => { + let newConfig: + | RedditIngestorConfig + | GithubIngestorConfig + | CrawlerIngestorConfig + | UrlIngestorConfig; + + switch (type) { + case 'reddit': + newConfig = { + name: ingestor.name, + client_id: '', + client_secret: '', + user_agent: '', + search_queries: [], + number_posts: 10, + }; + break; + case 'github': + newConfig = { + name: ingestor.name, + repo_url: '', + }; + break; + case 'crawler': + case 'url': + newConfig = { + name: ingestor.name, + url: '', + }; + break; + default: + newConfig = { + name: ingestor.name, + url: '', + } as CrawlerIngestorConfig; + } + + setIngestor({ + type, + name: ingestor.name, + config: newConfig, + }); }; let view; @@ -455,145 +611,96 @@ function Upload({ - setUrlType(value) + selectedValue={ingestor.type} + onSelect={(selected: { label: string; value: string }) => + handleIngestorTypeChange(selected.value as IngestorType) } size="w-full" rounded="3xl" /> - {urlType.label !== 'Reddit' && urlType.label !== 'GitHub' ? ( + {ingestor.type === 'reddit' ? ( <> + + setUrlName(e.target.value)} + name="user_agent" + value={(ingestor.config as RedditIngestorConfig).user_agent} + onChange={handleIngestorChange} borderVariant="thin" - > -
- - {t('modals.uploadDoc.name')} - -
+ /> setUrl(e.target.value)} + name="search_queries" + value={ + (ingestor.config as RedditIngestorConfig).search_queries + } + onChange={handleIngestorChange} + borderVariant="thin" + /> + -
- - {t('modals.uploadDoc.link')} - -
+ /> - ) : urlType.label === 'GitHub' ? ( // P3f93 + ) : ingestor.type === 'github' ? ( + + ) : ( <> setUrlName(e.target.value)} + name="name" + value={ingestor.name} + onChange={(e) => + setIngestor({ ...ingestor, name: e.target.value }) + } borderVariant="thin" - > -
- - {t('modals.uploadDoc.name')} - -
+ /> setRepoUrl(e.target.value)} + name="url" + value={ + ( + ingestor.config as + | CrawlerIngestorConfig + | UrlIngestorConfig + ).url + } + onChange={handleIngestorChange} borderVariant="thin" - > -
- - {t('modals.uploadDoc.repoUrl')} - -
+ /> - ) : ( -
-
- -
- - {t('modals.uploadDoc.reddit.id')} - -
-
-
- -
- - {t('modals.uploadDoc.reddit.secret')} - -
-
-
- -
- - {t('modals.uploadDoc.reddit.agent')} - -
-
-
- -
- - {t('modals.uploadDoc.reddit.searchQueries')} - -
-
-
- -
- - {t('modals.uploadDoc.reddit.numberOfPosts')} - -
-
-
)} )} @@ -615,33 +722,9 @@ function Upload({ uploadRemote(); } }} - disabled={ - (activeTab === 'file' && (!files.length || !docName)) || - (activeTab === 'remote' && - ((urlType.label !== 'Reddit' && - urlType.label !== 'GitHub' && - (!url || !urlName)) || - (urlType.label === 'GitHub' && !repoUrl) || - (urlType.label === 'Reddit' && - (!redditData.client_id || - !redditData.client_secret || - !redditData.user_agent || - !redditData.search_queries || - !redditData.number_posts)))) - } + disabled={isUploadDisabled()} className={`rounded-3xl px-4 py-2 font-medium ${ - (activeTab === 'file' && (!files.length || !docName)) || - (activeTab === 'remote' && - ((urlType.label !== 'Reddit' && - urlType.label !== 'GitHub' && - (!url || !urlName)) || - (urlType.label === 'GitHub' && !repoUrl) || - (urlType.label === 'Reddit' && - (!redditData.client_id || - !redditData.client_secret || - !redditData.user_agent || - !redditData.search_queries || - !redditData.number_posts)))) + isUploadDisabled() ? 'cursor-not-allowed bg-gray-300 text-gray-500' : 'cursor-pointer bg-purple-30 text-white hover:bg-purple-40' }`} diff --git a/frontend/src/upload/types/ingestor.ts b/frontend/src/upload/types/ingestor.ts new file mode 100644 index 000000000..6a4db8692 --- /dev/null +++ b/frontend/src/upload/types/ingestor.ts @@ -0,0 +1,43 @@ +export interface BaseIngestorConfig { + name: string; +} + +export interface RedditIngestorConfig extends BaseIngestorConfig { + client_id: string; + client_secret: string; + user_agent: string; + search_queries: string[]; + number_posts: number; +} + +export interface GithubIngestorConfig extends BaseIngestorConfig { + repo_url: string; +} + +export interface CrawlerIngestorConfig extends BaseIngestorConfig { + url: string; +} + +export interface UrlIngestorConfig extends BaseIngestorConfig { + url: string; +} + +export type IngestorType = 'crawler' | 'github' | 'reddit' | 'url'; + +export interface IngestorConfig { + type: IngestorType; + name: string; + config: + | RedditIngestorConfig + | GithubIngestorConfig + | CrawlerIngestorConfig + | UrlIngestorConfig + | string; +} + +export type IngestorFormData = { + name: string; + user: string; + source: IngestorType; + data: string; +}; From a69e81076a45830e162c1fd7c21525a899f8368a Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Fri, 31 Jan 2025 06:23:54 +0530 Subject: [PATCH 2/5] (feat:components) add label props --- frontend/src/components/Input.tsx | 40 +++++++++++++++--------- frontend/src/components/ToggleSwitch.tsx | 8 ++--- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/frontend/src/components/Input.tsx b/frontend/src/components/Input.tsx index 17e601900..f7b1765e9 100644 --- a/frontend/src/components/Input.tsx +++ b/frontend/src/components/Input.tsx @@ -7,6 +7,7 @@ const Input = ({ value, isAutoFocused = false, placeholder, + label, maxLength, className, colorVariant = 'silver', @@ -26,21 +27,30 @@ const Input = ({ thick: 'border-2', }; return ( - - {children} - +
+ + {children} + + {label && ( +
+ + {label} + +
+ )} +
); }; diff --git a/frontend/src/components/ToggleSwitch.tsx b/frontend/src/components/ToggleSwitch.tsx index 02745799d..061d25632 100644 --- a/frontend/src/components/ToggleSwitch.tsx +++ b/frontend/src/components/ToggleSwitch.tsx @@ -23,9 +23,12 @@ const ToggleSwitch: React.FC = ({ }) => { return ( ); }; From 4b83fa35499d80f6df4d06d19e86517ec11dd22a Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Fri, 31 Jan 2025 06:29:35 +0530 Subject: [PATCH 3/5] (refactor:remote types) enhance abstraction --- frontend/src/components/types/index.ts | 1 + frontend/src/upload/Upload.tsx | 245 ++++++++++++------------- frontend/src/upload/types/ingestor.ts | 118 ++++++++++++ 3 files changed, 234 insertions(+), 130 deletions(-) diff --git a/frontend/src/components/types/index.ts b/frontend/src/components/types/index.ts index 7af1c5458..a7ff5405a 100644 --- a/frontend/src/components/types/index.ts +++ b/frontend/src/components/types/index.ts @@ -8,6 +8,7 @@ export type InputProps = { maxLength?: number; name?: string; placeholder?: string; + label?: string; className?: string; children?: React.ReactElement; onChange: ( diff --git a/frontend/src/upload/Upload.tsx b/frontend/src/upload/Upload.tsx index d2188966e..0dfcc89af 100644 --- a/frontend/src/upload/Upload.tsx +++ b/frontend/src/upload/Upload.tsx @@ -8,6 +8,7 @@ import FileUpload from '../assets/file_upload.svg'; import WebsiteCollect from '../assets/website_collect.svg'; import Dropdown from '../components/Dropdown'; import Input from '../components/Input'; +import ToggleSwitch from '../components/ToggleSwitch'; import { ActiveState, Doc } from '../models/misc'; import { getDocs } from '../preferences/preferenceApi'; import { @@ -23,7 +24,9 @@ import { GithubIngestorConfig, CrawlerIngestorConfig, UrlIngestorConfig, + IngestorFormSchemas, } from './types/ingestor'; +import { IngestorDefaultConfigs } from '../upload/types/ingestor'; type IngestorState = { type: IngestorType; @@ -53,14 +56,111 @@ function Upload({ const [files, setfiles] = useState(receivedFile); const [activeTab, setActiveTab] = useState(renderTab); + const renderFormFields = () => { + const schema = IngestorFormSchemas[ingestor.type]; + + return schema.map((field) => { + switch (field.type) { + case 'string': + return ( +
+ +
+ ); + case 'number': + return ( +
+ +
+ ); + case 'enum': + return ( +
+ { + const syntheticEvent = { + target: { + name: field.name, + value: + typeof value === 'string' + ? value + : JSON.stringify(value), + }, + } as React.ChangeEvent; + handleIngestorChange(syntheticEvent); + }} + size="w-full" + rounded="3xl" + placeholder={field.label} + border="border" + borderColor="gray-5000" + /> +
+ ); + case 'boolean': + return ( +
+ { + const syntheticEvent = { + target: { + name: field.name, + value: checked, + }, + } as unknown as React.ChangeEvent; + handleIngestorChange(syntheticEvent); + }} + className="mt-2" + /> +
+ ); + default: + return null; + } + }); + }; + // New unified ingestor state - const [ingestor, setIngestor] = useState({ - type: 'crawler', - name: '', - config: { - name: '', - url: '', - } as CrawlerIngestorConfig, + const [ingestor, setIngestor] = useState(() => { + const defaultType: IngestorType = 'crawler'; + const defaultConfig = IngestorDefaultConfigs[defaultType]; + return { + type: defaultType, + name: defaultConfig.name, + config: defaultConfig.config, + }; }); const [progress, setProgress] = useState<{ @@ -474,47 +574,12 @@ function Upload({ }; const handleIngestorTypeChange = (type: IngestorType) => { - let newConfig: - | RedditIngestorConfig - | GithubIngestorConfig - | CrawlerIngestorConfig - | UrlIngestorConfig; - - switch (type) { - case 'reddit': - newConfig = { - name: ingestor.name, - client_id: '', - client_secret: '', - user_agent: '', - search_queries: [], - number_posts: 10, - }; - break; - case 'github': - newConfig = { - name: ingestor.name, - repo_url: '', - }; - break; - case 'crawler': - case 'url': - newConfig = { - name: ingestor.name, - url: '', - }; - break; - default: - newConfig = { - name: ingestor.name, - url: '', - } as CrawlerIngestorConfig; - } + const defaultConfig = IngestorDefaultConfigs[type]; setIngestor({ type, - name: ingestor.name, - config: newConfig, + name: defaultConfig.name, + config: defaultConfig.config, }); }; @@ -611,97 +676,17 @@ function Upload({ opt.value === ingestor.type) || null + } onSelect={(selected: { label: string; value: string }) => handleIngestorTypeChange(selected.value as IngestorType) } size="w-full" rounded="3xl" /> - {ingestor.type === 'reddit' ? ( - <> - - - - - - - ) : ingestor.type === 'github' ? ( - - ) : ( - <> - - setIngestor({ ...ingestor, name: e.target.value }) - } - borderVariant="thin" - /> - - - )} + {/* Dynamically render form fields based on schema */} + {renderFormFields()} )}
diff --git a/frontend/src/upload/types/ingestor.ts b/frontend/src/upload/types/ingestor.ts index 6a4db8692..f9a4f8798 100644 --- a/frontend/src/upload/types/ingestor.ts +++ b/frontend/src/upload/types/ingestor.ts @@ -41,3 +41,121 @@ export type IngestorFormData = { source: IngestorType; data: string; }; + +export type FieldType = 'string' | 'number' | 'enum' | 'boolean'; + +export interface FormField { + name: keyof BaseIngestorConfig | string; + label: string; + type: FieldType; + options?: { label: string; value: string }[]; +} + +export const IngestorFormSchemas: Record = { + crawler: [ + { + name: 'name', + label: 'Name', + type: 'string', + }, + { + name: 'url', + label: 'URL', + type: 'string', + }, + ], + url: [ + { + name: 'name', + label: 'Name', + type: 'string', + }, + { + name: 'url', + label: 'URL', + type: 'string', + }, + ], + reddit: [ + { + name: 'name', + label: 'Name', + type: 'string', + }, + { + name: 'client_id', + label: 'Client ID', + type: 'string', + }, + { + name: 'client_secret', + label: 'Client Secret', + type: 'string', + }, + { + name: 'user_agent', + label: 'User Agent', + type: 'string', + }, + { + name: 'search_queries', + label: 'Search Queries', + type: 'string', + }, + { + name: 'number_posts', + label: 'Number of Posts', + type: 'number', + }, + ], + github: [ + { + name: 'name', + label: 'Name', + type: 'string', + }, + { + name: 'repo_url', + label: 'Repository URL', + type: 'string', + }, + ], +}; + +export const IngestorDefaultConfigs: Record< + IngestorType, + Omit +> = { + crawler: { + name: '', + config: { + name: '', + url: '', + } as CrawlerIngestorConfig, + }, + url: { + name: '', + config: { + name: '', + url: '', + } as UrlIngestorConfig, + }, + reddit: { + name: '', + config: { + name: '', + client_id: '', + client_secret: '', + user_agent: '', + search_queries: [], + number_posts: 10, + } as RedditIngestorConfig, + }, + github: { + name: '', + config: { + name: '', + repo_url: '', + } as GithubIngestorConfig, + }, +}; From b9ec6b4315eaccf567311be678642e4dc47d1dc2 Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Sat, 1 Feb 2025 00:08:01 +0530 Subject: [PATCH 4/5] (refactor:upload) separate name from the configurations --- frontend/src/upload/Upload.tsx | 140 +++++++++++--------------- frontend/src/upload/types/ingestor.ts | 31 +----- 2 files changed, 62 insertions(+), 109 deletions(-) diff --git a/frontend/src/upload/Upload.tsx b/frontend/src/upload/Upload.tsx index 0dfcc89af..9ea10114f 100644 --- a/frontend/src/upload/Upload.tsx +++ b/frontend/src/upload/Upload.tsx @@ -25,11 +25,13 @@ import { CrawlerIngestorConfig, UrlIngestorConfig, IngestorFormSchemas, + FormField, } from './types/ingestor'; import { IngestorDefaultConfigs } from '../upload/types/ingestor'; type IngestorState = { type: IngestorType; + name: string; config: | RedditIngestorConfig | GithubIngestorConfig @@ -59,18 +61,19 @@ function Upload({ const renderFormFields = () => { const schema = IngestorFormSchemas[ingestor.type]; - return schema.map((field) => { + return schema.map((field: FormField) => { switch (field.type) { case 'string': return (
+ handleIngestorChange(field.name, e.target.value) + } borderVariant="thin" label={field.label} colorVariant="gray" @@ -81,12 +84,13 @@ function Upload({ return (
+ handleIngestorChange(field.name, parseInt(e.target.value)) + } borderVariant="thin" label={field.label} colorVariant="gray" @@ -101,22 +105,11 @@ function Upload({ options={field.options || []} selectedValue={(ingestor.config as any)[field.name]} onSelect={( - value: - | string - | { name: string; id: string; type: string } - | { label: string; value: string } - | { value: number; description: string }, + selected: { label: string; value: string } | string, ) => { - const syntheticEvent = { - target: { - name: field.name, - value: - typeof value === 'string' - ? value - : JSON.stringify(value), - }, - } as React.ChangeEvent; - handleIngestorChange(syntheticEvent); + const value = + typeof selected === 'string' ? selected : selected.value; + handleIngestorChange(field.name, value); }} size="w-full" rounded="3xl" @@ -130,7 +123,6 @@ function Upload({ return (
{ @@ -140,7 +132,7 @@ function Upload({ value: checked, }, } as unknown as React.ChangeEvent; - handleIngestorChange(syntheticEvent); + handleIngestorChange(field.name, syntheticEvent.target.value); }} className="mt-2" /> @@ -381,7 +373,8 @@ function Upload({ files.forEach((file) => { formData.append('file', file); }); - formData.append('name', docName); + + formData.append('name', activeTab === 'file' ? docName : ingestor.name); formData.append('user', 'local'); const apiHost = import.meta.env.VITE_API_HOST; const xhr = new XMLHttpRequest(); @@ -406,15 +399,19 @@ function Upload({ formData.append('source', ingestor.type); if (ingestor.type === 'reddit') { - formData.set('data', JSON.stringify(ingestor.config)); + const redditConfig = ingestor.config as RedditIngestorConfig; + redditConfig.name = ingestor.name; + formData.set('data', JSON.stringify(redditConfig)); } else if (ingestor.type === 'github') { const githubConfig = ingestor.config as GithubIngestorConfig; + githubConfig.name = ingestor.name; formData.append('repo_url', githubConfig.repo_url); formData.append('data', githubConfig.repo_url); } else { const urlBasedConfig = ingestor.config as | CrawlerIngestorConfig | UrlIngestorConfig; + urlBasedConfig.name = ingestor.name; formData.append('data', urlBasedConfig.url); } @@ -465,70 +462,40 @@ function Upload({ }); const isUploadDisabled = () => { + if (activeTab === 'file') { + return !docName || files.length === 0; + } + if (activeTab !== 'remote') return false; - switch (ingestor.type) { - case 'reddit': { - const redditConfig = ingestor.config as RedditIngestorConfig; - return ( - !redditConfig.client_id || - !redditConfig.client_secret || - !redditConfig.user_agent || - !redditConfig.search_queries.length || - !redditConfig.number_posts - ); - } - case 'github': - return !(ingestor.config as GithubIngestorConfig).repo_url; - default: { - const urlConfig = ingestor.config as - | CrawlerIngestorConfig - | UrlIngestorConfig; - return !urlConfig.url || !ingestor.name; + if (!ingestor.name) return true; + + return Object.values(ingestor.config).some((value) => { + if (Array.isArray(value)) { + return value.length === 0; } - } + return !value; + }); }; - const handleIngestorChange = ( - e: React.ChangeEvent, - ) => { - const { name, value } = e.target; + const handleIngestorChange = (key: string, value: any) => { + setIngestor((prevState: IngestorConfig): IngestorConfig => { + if (key === 'name') { + return { + ...prevState, + name: value, + }; + } - if (ingestor.type === 'reddit') { - const redditConfig = ingestor.config as RedditIngestorConfig; - setIngestor({ - ...ingestor, + return { + ...prevState, config: { - ...redditConfig, - [name]: - name === 'search_queries' - ? value.split(',').map((item) => item.trim()) - : name === 'number_posts' - ? parseInt(value) - : value, + ...(prevState.config as any), + [key]: value, }, - }); - } else if (ingestor.type === 'github') { - const githubConfig = ingestor.config as GithubIngestorConfig; - setIngestor({ - ...ingestor, - config: { - ...githubConfig, - [name]: value, - }, - }); - } else { - const urlConfig = ingestor.config as - | CrawlerIngestorConfig - | UrlIngestorConfig; - setIngestor({ - ...ingestor, - config: { - ...urlConfig, - [name]: value, - }, - }); - } + }; + }); + console.log(ingestor); }; const handleChange = ( @@ -686,6 +653,18 @@ function Upload({ rounded="3xl" /> {/* Dynamically render form fields based on schema */} + + + setIngestor({ ...ingestor, name: e.target.value }) + } + borderVariant="thin" + placeholder="Name" + label="Name" + /> {renderFormFields()} )} @@ -707,7 +686,6 @@ function Upload({ uploadRemote(); } }} - disabled={isUploadDisabled()} className={`rounded-3xl px-4 py-2 font-medium ${ isUploadDisabled() ? 'cursor-not-allowed bg-gray-300 text-gray-500' diff --git a/frontend/src/upload/types/ingestor.ts b/frontend/src/upload/types/ingestor.ts index f9a4f8798..83c74bfd9 100644 --- a/frontend/src/upload/types/ingestor.ts +++ b/frontend/src/upload/types/ingestor.ts @@ -6,7 +6,7 @@ export interface RedditIngestorConfig extends BaseIngestorConfig { client_id: string; client_secret: string; user_agent: string; - search_queries: string[]; + search_queries: string; number_posts: number; } @@ -31,8 +31,7 @@ export interface IngestorConfig { | RedditIngestorConfig | GithubIngestorConfig | CrawlerIngestorConfig - | UrlIngestorConfig - | string; + | UrlIngestorConfig; } export type IngestorFormData = { @@ -53,11 +52,6 @@ export interface FormField { export const IngestorFormSchemas: Record = { crawler: [ - { - name: 'name', - label: 'Name', - type: 'string', - }, { name: 'url', label: 'URL', @@ -65,11 +59,6 @@ export const IngestorFormSchemas: Record = { }, ], url: [ - { - name: 'name', - label: 'Name', - type: 'string', - }, { name: 'url', label: 'URL', @@ -77,11 +66,6 @@ export const IngestorFormSchemas: Record = { }, ], reddit: [ - { - name: 'name', - label: 'Name', - type: 'string', - }, { name: 'client_id', label: 'Client ID', @@ -109,11 +93,6 @@ export const IngestorFormSchemas: Record = { }, ], github: [ - { - name: 'name', - label: 'Name', - type: 'string', - }, { name: 'repo_url', label: 'Repository URL', @@ -129,32 +108,28 @@ export const IngestorDefaultConfigs: Record< crawler: { name: '', config: { - name: '', url: '', } as CrawlerIngestorConfig, }, url: { name: '', config: { - name: '', url: '', } as UrlIngestorConfig, }, reddit: { name: '', config: { - name: '', client_id: '', client_secret: '', user_agent: '', - search_queries: [], + search_queries: '', number_posts: 10, } as RedditIngestorConfig, }, github: { name: '', config: { - name: '', repo_url: '', } as GithubIngestorConfig, }, From 5c5b730bb8b1b20c62009e281eeb284509a5d9ce Mon Sep 17 00:00:00 2001 From: ManishMadan2882 Date: Sat, 1 Feb 2025 00:40:30 +0530 Subject: [PATCH 5/5] purge logs, unused --- frontend/src/upload/Upload.tsx | 43 ---------------------------------- 1 file changed, 43 deletions(-) diff --git a/frontend/src/upload/Upload.tsx b/frontend/src/upload/Upload.tsx index 9ea10114f..725ecdb18 100644 --- a/frontend/src/upload/Upload.tsx +++ b/frontend/src/upload/Upload.tsx @@ -495,49 +495,6 @@ function Upload({ }, }; }); - console.log(ingestor); - }; - - const handleChange = ( - e: React.ChangeEvent, - ) => { - const { name, value } = e.target; - - if (ingestor.type === 'reddit') { - const redditConfig = ingestor.config as RedditIngestorConfig; - setIngestor({ - ...ingestor, - config: { - ...redditConfig, - [name]: - name === 'search_queries' - ? value.split(',').map((item) => item.trim()) - : name === 'number_posts' - ? parseInt(value) - : value, - }, - }); - } else if (ingestor.type === 'github') { - const githubConfig = ingestor.config as GithubIngestorConfig; - setIngestor({ - ...ingestor, - config: { - ...githubConfig, - [name]: value, - }, - }); - } else { - const urlConfig = ingestor.config as - | CrawlerIngestorConfig - | UrlIngestorConfig; - setIngestor({ - ...ingestor, - config: { - ...urlConfig, - [name]: value, - }, - }); - } }; const handleIngestorTypeChange = (type: IngestorType) => {