import { useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { ActiveState } from '../models/misc'; import { setSelectedDocs, setSourceDocs, selectSourceDocs, } from './preferenceSlice'; import { getDocs, Doc, getLocalRecentDocs, setLocalRecentDocs, } from './preferenceApi'; export default function APIKeyModal({ modalState, setModalState, isCancellable = true, }: { modalState: ActiveState; setModalState: (val: ActiveState) => void; isCancellable?: boolean; }) { const dispatch = useDispatch(); const docs = useSelector(selectSourceDocs); const [localSelectedDocs, setLocalSelectedDocs] = useState(null); const [isDocsListOpen, setIsDocsListOpen] = useState(false); const [isError, setIsError] = useState(false); function handleSubmit() { if (!localSelectedDocs) { setIsError(true); } else { setLocalRecentDocs(localSelectedDocs); dispatch(setSelectedDocs(localSelectedDocs)); setModalState('INACTIVE'); setIsError(false); } } function handleCancel() { async function getRecentDocs() { const response = await getLocalRecentDocs(); console.log('response'); if (response) { const parsedResponse = JSON.parse(response) as Doc; setLocalSelectedDocs(parsedResponse); } } getRecentDocs(); console.log('cancel'); setIsError(false); setModalState('INACTIVE'); } useEffect(() => { async function getRecentDocs() { const response = await getLocalRecentDocs(); if (response) { const parsedResponse = JSON.parse(response) as Doc; dispatch(setSelectedDocs(parsedResponse)); setLocalSelectedDocs(parsedResponse); setModalState('INACTIVE'); } } async function requestDocs() { const data = await getDocs(); dispatch(setSourceDocs(data)); } getRecentDocs(); requestDocs(); }, []); return (

Select Source Documentation

Please select the library of documentation that you would like to use with our app.

setIsDocsListOpen(!isDocsListOpen)} > {!localSelectedDocs ? (

Select

) : (

{localSelectedDocs.name} {localSelectedDocs.version}

)}
{isDocsListOpen && (
{docs ? ( docs.map((doc, index) => { if (doc.model) { return (
{ setLocalSelectedDocs(doc); setIsDocsListOpen(false); }} className="h-10 w-full cursor-pointer border-x-2 border-b-2 hover:bg-gray-100" >

{doc.name} {doc.version}

); } }) ) : (

No default documentation.

)}
)}
{isCancellable && ( )} {isError && (

Please select source documentation.

)}
); }