import React, { useCallback, useMemo, useRef, useState } from 'react'; import Icon from '../common/Icon'; import Modal from '../common/Modal'; import SelectField from '../common/SelectField'; import countries from '../../utils/countries'; import { useAddKeywords } from '../../services/keywords'; type AddKeywordsProps = { keywords: KeywordType[], scraperName: string, allowsCity: boolean, closeModal: Function, domain: string } type KeywordsInput = { keywords: string, device: string, country: string, domain: string, tags: string, city?:string, } const AddKeywords = ({ closeModal, domain, keywords, scraperName = '', allowsCity = false }: AddKeywordsProps) => { const inputRef = useRef(null); const defCountry = localStorage.getItem('default_country') || 'US'; const [error, setError] = useState(''); const [showTagSuggestions, setShowTagSuggestions] = useState(false); const [newKeywordsData, setNewKeywordsData] = useState({ keywords: '', device: 'desktop', country: defCountry, domain, tags: '' }); const { mutate: addMutate, isLoading: isAdding } = useAddKeywords(() => closeModal(false)); const existingTags: string[] = useMemo(() => { const allTags = keywords.reduce((acc: string[], keyword) => [...acc, ...keyword.tags], []).filter((t) => t && t.trim() !== ''); return [...new Set(allTags)]; }, [keywords]); const setDeviceType = useCallback((input:string) => { let updatedDevice = ''; if (newKeywordsData.device.includes(input)) { updatedDevice = newKeywordsData.device.replace(',', '').replace(input, ''); } else { updatedDevice = newKeywordsData.device ? `${newKeywordsData.device},${input}` : input; } setNewKeywordsData({ ...newKeywordsData, device: updatedDevice }); }, [newKeywordsData]); const addKeywords = () => { const nkwrds = newKeywordsData; if (nkwrds.keywords) { const devices = nkwrds.device.split(','); const multiDevice = nkwrds.device.includes(',') && devices.length > 1; const keywordsArray = [...new Set(nkwrds.keywords.split('\n').map((item) => item.trim()).filter((item) => !!item))]; const currentKeywords = keywords.map((k) => `${k.keyword}-${k.device}-${k.country}${k.city ? `-${k.city}` : ''}`); const keywordExist = keywordsArray.filter((k) => devices.some((device) => currentKeywords.includes(`${k}-${device}-${nkwrds.country}${nkwrds.city ? `-${nkwrds.city}` : ''}`)), ); if (!multiDevice && (keywordsArray.length === 1 || currentKeywords.length === keywordExist.length) && keywordExist.length > 0) { setError(`Keywords ${keywordExist.join(',')} already Exist`); setTimeout(() => { setError(''); }, 3000); } else { const newKeywords = keywordsArray.flatMap((k) => devices.filter((device) => !currentKeywords.includes(`${k}-${device}-${nkwrds.country}${nkwrds.city ? `-${nkwrds.city}` : ''}`), ).map((device) => ({ keyword: k, device, country: nkwrds.country, domain: nkwrds.domain, tags: nkwrds.tags, city: nkwrds.city, })), ); addMutate(newKeywords); } } else { setError('Please Insert a Keyword'); setTimeout(() => { setError(''); }, 3000); } }; const deviceTabStyle = 'cursor-pointer px-2 py-2 rounded'; return ( { closeModal(false); }} title={'Add New Keywords'} width="[420px]">
{ return { label: countries[countryISO][0], value: countryISO }; })} defaultLabel='All Countries' updateField={(updated:string[]) => { setNewKeywordsData({ ...newKeywordsData, country: updated[0] }); localStorage.setItem('default_country', updated[0]); }} rounded='rounded' maxHeight={48} flags={true} />
  • setDeviceType('desktop')}> Desktop
  • setDeviceType('mobile')}> Mobile
setNewKeywordsData({ ...newKeywordsData, tags: e.target.value })} /> setShowTagSuggestions(!showTagSuggestions)}> {showTagSuggestions && (
    {existingTags.length > 0 && existingTags.map((tag, index) => { return newKeywordsData.tags.split(',').map((t) => t.trim()).includes(tag) === false &&
  • { const tagInput = newKeywordsData.tags; // eslint-disable-next-line no-nested-ternary const tagToInsert = tagInput + (tagInput.trim().slice(-1) === ',' ? '' : (tagInput.trim() ? ', ' : '')) + tag; setNewKeywordsData({ ...newKeywordsData, tags: tagToInsert }); setShowTagSuggestions(false); if (inputRef?.current) (inputRef.current as HTMLInputElement).focus(); }}> {tag}
  • ; })} {existingTags.length === 0 &&

    No Existing Tags Found...

    }
)}
setNewKeywordsData({ ...newKeywordsData, city: e.target.value })} />
{error &&
{error}
}
); }; export default AddKeywords;