mirror of
https://github.com/arc53/DocsGPT.git
synced 2025-11-29 16:43:16 +00:00
shifted to parcel, styled-components
This commit is contained in:
@@ -1,17 +1,250 @@
|
||||
"use client";
|
||||
import { Fragment, useEffect, useRef, useState } from 'react'
|
||||
import { PaperPlaneIcon, RocketIcon, ExclamationTriangleIcon } from '@radix-ui/react-icons';
|
||||
import { Input } from './ui/input';
|
||||
import { Button } from './ui/button';
|
||||
import { ScrollArea } from './ui/scroll-area'
|
||||
import { Alert, AlertTitle, AlertDescription } from './ui/alert';
|
||||
import Dragon from '../assets/cute-docsgpt.svg'
|
||||
import { PaperPlaneIcon, RocketIcon, ExclamationTriangleIcon, Cross1Icon, WidthIcon } from '@radix-ui/react-icons';
|
||||
import { MESSAGE_TYPE } from '../models/types';
|
||||
import { Query, Status } from '../models/types';
|
||||
import MessageIcon from '../assets/message.svg'
|
||||
import Cancel from '../assets/cancel.svg'
|
||||
import { Query, Status } from '@/models/customTypes';
|
||||
import { fetchAnswerStreaming } from '@/requests/streamingApi';
|
||||
import Response from './Response';
|
||||
import { fetchAnswerStreaming } from '../requests/streamingApi';
|
||||
import styled, { keyframes } from 'styled-components';
|
||||
const WidgetContainer = styled.div`
|
||||
position: fixed;
|
||||
right: 10px;
|
||||
bottom: 10px;
|
||||
z-index: 1000;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
text-align: left;
|
||||
width: 356px;
|
||||
height: 405px;
|
||||
`;
|
||||
const StyledContainer = styled.div`
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
border-radius: 0.375rem;
|
||||
background-color: rgb(34, 35, 39);
|
||||
border: 1px solid gray;
|
||||
font-family: sans-serif;
|
||||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 2px 4px rgba(0, 0, 0, 0.1);
|
||||
transition: visibility 0.3s, opacity 0.3s;
|
||||
`;
|
||||
const FloatingButton = styled.div`
|
||||
position: absolute;
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
bottom: 1rem;
|
||||
right: 1rem;
|
||||
width: 5rem;
|
||||
height: 5rem;
|
||||
border-radius: 9999px;
|
||||
overflow: hidden;
|
||||
background-image: linear-gradient(to bottom right, #5AF0EC, #E80D9D);
|
||||
background-color: #5AF0EC;
|
||||
background-color: rgba(0, 0, 0, 0.8);
|
||||
font-family: sans-serif;
|
||||
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
&:hover {
|
||||
transform: scale(1.1);
|
||||
transition: transform 0.2s ease-in-out;
|
||||
}
|
||||
`;
|
||||
const CancelButton = styled.button`
|
||||
cursor: pointer;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
margin: 0.5rem;
|
||||
padding: 0;
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
outline: none;
|
||||
color: inherit;
|
||||
transition: opacity 0.3s ease;
|
||||
&:hover {
|
||||
opacity: 0.5;
|
||||
}
|
||||
.white-filter {
|
||||
filter: invert(100%);
|
||||
}
|
||||
`;
|
||||
|
||||
const Header = styled.div`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 0.75rem;
|
||||
`;
|
||||
|
||||
const IconWrapper = styled.div`
|
||||
padding: 0.5rem;
|
||||
`;
|
||||
|
||||
const ContentWrapper = styled.div`
|
||||
flex: 1;
|
||||
margin-left: 0.5rem;
|
||||
`;
|
||||
|
||||
const Title = styled.h3`
|
||||
font-size: 0.875rem;
|
||||
font-weight: normal;
|
||||
color: #FAFAFA;
|
||||
margin-top: 0;
|
||||
margin-bottom: 0.25rem;
|
||||
`;
|
||||
|
||||
const Description = styled.p`
|
||||
font-size: 0.75rem;
|
||||
color: #A1A1AA;
|
||||
margin-top: 0;
|
||||
`;
|
||||
const Conversation = styled.div`
|
||||
height: 18rem;
|
||||
padding: 0.5rem;
|
||||
border-radius: 0.375rem;
|
||||
text-align: left;
|
||||
overflow-y: auto;
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: #4a4a4a transparent; /* thumb color track color */
|
||||
`;
|
||||
|
||||
const MessageBubble = styled.div<{ type: MESSAGE_TYPE }>`
|
||||
display: flex;
|
||||
justify-content: ${props => props.type === 'QUESTION' ? 'flex-end' : 'flex-start'};
|
||||
margin: 0.5rem;
|
||||
`;
|
||||
const Message = styled.p<{ type: MESSAGE_TYPE }>`
|
||||
background: ${props => props.type === 'QUESTION' ?
|
||||
'linear-gradient(to bottom right, #8860DB, #6D42C5)' :
|
||||
props => props.type === 'ANSWER' ?
|
||||
'#38383b' :
|
||||
''};
|
||||
|
||||
color: ${props => props.type != 'ERROR' ? '#ffff' : '#b91c1c'};
|
||||
border:${props => props.type !== 'ERROR' ? 'none' : '1px solid #b91c1c'};
|
||||
max-width: 80%;
|
||||
display: block;
|
||||
padding: 0.75rem;
|
||||
border-radius: 0.375rem;
|
||||
`;
|
||||
const ErrorAlert = styled.div`
|
||||
color: #b91c1c;
|
||||
border:0.1px solid #b91c1c;
|
||||
display: flex;
|
||||
padding:4px;
|
||||
opacity: 90%;
|
||||
max-width: 70%;
|
||||
font-weight: 400;
|
||||
border-radius: 0.375rem;
|
||||
justify-content: space-evenly;
|
||||
`
|
||||
//dot loading animation
|
||||
const dotBounce = keyframes`
|
||||
0%, 80%, 100% {
|
||||
transform: translateY(0);
|
||||
}
|
||||
40% {
|
||||
transform: translateY(-5px);
|
||||
}
|
||||
`;
|
||||
|
||||
const DotAnimation = styled.div`
|
||||
display: inline-block;
|
||||
animation: ${dotBounce} 1s infinite ease-in-out;
|
||||
`;
|
||||
|
||||
// delay classes as styled components
|
||||
const Delay = styled(DotAnimation) <{ delay: number }>`
|
||||
animation-delay: ${props => props.delay + 'ms'};
|
||||
`;
|
||||
const PromptContainer = styled.form`
|
||||
background-color: transparent;
|
||||
padding: 12px 8px;
|
||||
opacity: 1;
|
||||
width: 340px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
`;
|
||||
const StyledInput = styled.input`
|
||||
width: 80%;
|
||||
border: 1px solid #686877;
|
||||
height: 36px;
|
||||
background-color: transparent;
|
||||
font-size: 14px;
|
||||
border-radius: 6px;
|
||||
color: #ffff;
|
||||
outline: none;
|
||||
padding: 6px;
|
||||
`;
|
||||
const StyledButton = styled.button`
|
||||
color: #ccc;
|
||||
background-image: linear-gradient(to bottom right, #5AF0EC, #E80D9D);
|
||||
font-size: 14px;
|
||||
padding: 0 8px;
|
||||
border-radius: 6px;
|
||||
margin: 2px;
|
||||
width: 36px;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
outline: none;
|
||||
&:hover{
|
||||
opacity: 80%;
|
||||
}
|
||||
&:disabled {
|
||||
opacity: 60%;
|
||||
}`
|
||||
const HeroContainer = styled.div`
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
width: 80%;
|
||||
background-image: linear-gradient(to bottom right, #5AF0EC, #ff1bf4);
|
||||
border-radius: 10px;
|
||||
margin: 0 auto;
|
||||
padding: 1px;
|
||||
`;
|
||||
const HeroWrapper = styled.div`
|
||||
background-color: #222327;
|
||||
border-radius: 10px;
|
||||
font-weight: normal;
|
||||
padding: 2px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
`
|
||||
const HeroTitle = styled.h3`
|
||||
color: #fff;
|
||||
font-size: 15px;
|
||||
margin-bottom: 5px;
|
||||
padding: 3px;
|
||||
`;
|
||||
|
||||
const HeroDescription = styled.p`
|
||||
color: #fff;
|
||||
font-size: 13px;
|
||||
`;
|
||||
const Hero = () => {
|
||||
return (
|
||||
<>
|
||||
<HeroContainer>
|
||||
<HeroWrapper>
|
||||
<IconWrapper style={{ marginTop: '8px' }}>
|
||||
<RocketIcon color='white' width={20} height={20} />
|
||||
</IconWrapper>
|
||||
<div>
|
||||
<HeroTitle>Welcome to DocsGPT !</HeroTitle>
|
||||
<HeroDescription>
|
||||
This is a chatbot that uses GPT-3, Faiss, and LangChain to answer questions.
|
||||
</HeroDescription>
|
||||
</div>
|
||||
</HeroWrapper>
|
||||
</HeroContainer>
|
||||
</>
|
||||
);
|
||||
};
|
||||
export const DocsGPTWidget = ({ apiHost = 'https://gptcloud.arc53.com', selectDocs = 'default', apiKey = 'docsgpt-public' }) => {
|
||||
|
||||
const [prompt, setPrompt] = useState('');
|
||||
@@ -61,8 +294,8 @@ export const DocsGPTWidget = ({ apiHost = 'https://gptcloud.arc53.com', selectDo
|
||||
setConversationId(data.id)
|
||||
} else {
|
||||
const result = data.answer;
|
||||
let streamingResponse = queries[queries.length - 1].response ? queries[queries.length - 1].response : '';
|
||||
let updatedQueries = [...queries];
|
||||
const streamingResponse = queries[queries.length - 1].response ? queries[queries.length - 1].response : '';
|
||||
const updatedQueries = [...queries];
|
||||
updatedQueries[updatedQueries.length - 1].response = streamingResponse + result;
|
||||
setQueries(updatedQueries);
|
||||
}
|
||||
@@ -72,7 +305,7 @@ export const DocsGPTWidget = ({ apiHost = 'https://gptcloud.arc53.com', selectDo
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
|
||||
let updatedQueries = [...queries];
|
||||
const updatedQueries = [...queries];
|
||||
updatedQueries[updatedQueries.length - 1].error = 'error'
|
||||
setQueries(updatedQueries);
|
||||
setStatus('idle')
|
||||
@@ -89,113 +322,91 @@ export const DocsGPTWidget = ({ apiHost = 'https://gptcloud.arc53.com', selectDo
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="dark text-left widget-container font-sans">
|
||||
<div onClick={() => setOpen(true)}
|
||||
className={`${open ? 'hidden' : ''} cursor-pointer`}>
|
||||
<div className="mr-2 mb-2 bottom-2 right-2 absolute w-20 h-20 rounded-full overflow-hidden dark:divide-gray-700 border dark:border-gray-100 bg-gradient-to-br dark:from-[#5AF0EC] dark:to-[#E80D9D] from-gray-900/80 via-gray-900 to-gray-900 font-sans shadow backdrop-blur-sm flex items-center justify-center">
|
||||
<img
|
||||
src={MessageIcon}
|
||||
alt="DocsGPT"
|
||||
className="cursor-pointer hover:opacity-50 w-12"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className={`${open ? '' : 'hidden'} absolute bottom-0 dark:divide-gray-700 rounded-md dark:bg-[#222327] dark:border-gray-700 font-sans shadow backdrop-blur-sm w-full`} style={{ transform: 'translateY(0%) translateZ(0px)' }}>
|
||||
<WidgetContainer>
|
||||
<FloatingButton onClick={() => setOpen(true)} hidden={open}>
|
||||
<MessageIcon/>
|
||||
</FloatingButton>
|
||||
{open && <StyledContainer>
|
||||
<div>
|
||||
<img
|
||||
src={Cancel}
|
||||
alt="Exit"
|
||||
className="cursor-pointer hover:opacity-50 absolute top-0 right-0 m-2 white-filter"
|
||||
onClick={(event) => {
|
||||
event.stopPropagation();
|
||||
setOpen(false);
|
||||
}}
|
||||
/>
|
||||
<div className="flex items-center gap-2 p-3">
|
||||
<div className='p-2 flex justify-between'>
|
||||
<img src={Dragon} />
|
||||
<div className='mx-2 w-full'>
|
||||
<h3 className="text-sm font-normal text-gray-700 dark:text-[#FAFAFA] ">Get AI assistance</h3>
|
||||
<p className="mt-1 text-xs text-gray-400 dark:text-[#A1A1AA]">DocsGPT's AI Chatbot is here to help</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<CancelButton onClick={() => setOpen(false)}>
|
||||
<Cross1Icon style={{ color: 'white' }} />
|
||||
</CancelButton>
|
||||
|
||||
<Header>
|
||||
<IconWrapper>
|
||||
<img width={48} height={48} src='https://d3dg1063dc54p9.cloudfront.net/cute-docsgpt.png' alt='docs-gpt'/>
|
||||
</IconWrapper>
|
||||
<ContentWrapper>
|
||||
<Title>Get AI assistance</Title>
|
||||
<Description>DocsGPT's AI Chatbot is here to help</Description>
|
||||
</ContentWrapper>
|
||||
</Header>
|
||||
</div>
|
||||
<div className="w-full">
|
||||
{open && (
|
||||
<div className='h-full'>
|
||||
<ScrollArea className='h-72 p-2 rounded-md text-left'>
|
||||
{
|
||||
queries.length > 0 ? queries?.map((query, index) => {
|
||||
return (
|
||||
<Fragment key={index}>
|
||||
{
|
||||
query.prompt && <div className='flex justify-end m-2 '>
|
||||
<p ref={(!(query.response || query.error) && index === queries.length - 1) ? scrollRef : null} className='bg-gradient-to-br dark:from-[#8860DB] dark:to-[#6D42C5] max-w-[80%] dark:text-white block px-3 py-2 rounded-lg '>
|
||||
{query.prompt}
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
{
|
||||
query.response ? <div className='flex justify-start m-2 '>
|
||||
<div ref={(index === queries.length - 1) ? scrollRef : null} className='dark:bg-[#38383B] max-w-[80%] dark:text-white block px-3 py-2 rounded-lg'>
|
||||
<Response message={query.response} />
|
||||
</div>
|
||||
</div>
|
||||
: <div className='m-2'>
|
||||
{
|
||||
query.error ? <Alert className='border-red-700 text-red-700 max-w-[80%]' variant="destructive">
|
||||
<ExclamationTriangleIcon color='red' className="h-4 w-4" />
|
||||
<AlertTitle>Network Error</AlertTitle>
|
||||
<AlertDescription>
|
||||
Something went wrong !
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
: <div className='flex justify-start m-2 '>
|
||||
<p className='dark:bg-[#38383B] text-xl max-w-[80%] font-extrabold dark:text-white block px-3 py-2 rounded-lg justify-center text-gray-800 transition duration-300 rounded-b '>
|
||||
<span className="dot-animation">.</span>
|
||||
<span className="dot-animation delay-200">.</span>
|
||||
<span className="dot-animation delay-400">.</span>
|
||||
</p>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</Fragment>)
|
||||
})
|
||||
: <div className='absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 w-5/6 bg-gradient-to-br dark:from-[#5AF0EC] dark:to-[#ff1bf4] rounded-lg mx-2 p-[1px]'>
|
||||
<Alert className='dark:bg-[#222327] mx-0'>
|
||||
<RocketIcon className="h-4 w-4" />
|
||||
<AlertTitle>Welcome to DocsGPT !</AlertTitle>
|
||||
<AlertDescription>
|
||||
This is a chatbot that uses the GPT-3, Faiss and LangChain to answer questions.
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
</div>
|
||||
}
|
||||
</ScrollArea>
|
||||
<form
|
||||
onSubmit={handleSubmit}
|
||||
className="relative w-full m-0 bg-transparent p-2" style={{ opacity: 1 }}>
|
||||
<div className='p-2 flex justify-between bg-transparent'>
|
||||
<Input
|
||||
value={prompt} onChange={(event) => setPrompt(event.target.value)}
|
||||
type='text'
|
||||
className="w-[85%] border border-[#686877] h-8 bg-transparent px-5 py-4 text-sm text-gray-700 dark:text-white focus:outline-none" placeholder="What do you want to do?" />
|
||||
<Button
|
||||
className="text-gray-400 dark:text-gray-500 bg-gradient-to-br dark:from-[#5AF0EC] dark:to-[#E80D9D] disabled:bg-black text-sm inset-y-0 px-2"
|
||||
type="submit"
|
||||
disabled={prompt.length == 0 || status !== 'idle'}>
|
||||
<PaperPlaneIcon className='text-white' />
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
)}
|
||||
<div style={{ width: '100%' }}>
|
||||
<Conversation>
|
||||
{
|
||||
queries.length > 0 ? queries?.map((query, index) => {
|
||||
return (
|
||||
<Fragment key={index}>
|
||||
{
|
||||
query.prompt && <MessageBubble type='QUESTION'>
|
||||
<Message
|
||||
type='QUESTION'
|
||||
ref={(!(query.response || query.error) && index === queries.length - 1) ? scrollRef : null}>
|
||||
{query.prompt}
|
||||
</Message>
|
||||
</MessageBubble>
|
||||
}
|
||||
{
|
||||
query.response ? <MessageBubble type='ANSWER'>
|
||||
<Message
|
||||
type='ANSWER'
|
||||
ref={(index === queries.length - 1) ? scrollRef : null}
|
||||
>
|
||||
{query.response}
|
||||
</Message>
|
||||
</MessageBubble>
|
||||
: <div>
|
||||
{
|
||||
query.error ? <ErrorAlert>
|
||||
<IconWrapper>
|
||||
<ExclamationTriangleIcon style={{ marginTop: '4px' }} width={22} height={22} color='#b91c1c' />
|
||||
</IconWrapper>
|
||||
<div>
|
||||
<h5 style={{ margin: 2 }}>Network Error</h5>
|
||||
<span style={{ margin: 2, fontSize: '13px' }}>Something went wrong !</span>
|
||||
</div>
|
||||
</ErrorAlert>
|
||||
: <MessageBubble type='ANSWER'>
|
||||
<Message type='ANSWER' style={{ fontWeight: 600 }}>
|
||||
<DotAnimation>.</DotAnimation>
|
||||
<Delay delay={200}>.</Delay>
|
||||
<Delay delay={400}>.</Delay>
|
||||
</Message>
|
||||
</MessageBubble>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</Fragment>)
|
||||
})
|
||||
: <Hero />
|
||||
}
|
||||
</Conversation>
|
||||
<PromptContainer
|
||||
onSubmit={handleSubmit}>
|
||||
<StyledInput
|
||||
value={prompt} onChange={(event) => setPrompt(event.target.value)}
|
||||
type='text' placeholder="What do you want to do?" />
|
||||
<StyledButton
|
||||
disabled={prompt.length == 0 || status !== 'idle'}>
|
||||
<PaperPlaneIcon color='white' />
|
||||
</StyledButton>
|
||||
</PromptContainer>
|
||||
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</StyledContainer>}
|
||||
</WidgetContainer>
|
||||
</>
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user