shifted to parcel, styled-components

This commit is contained in:
ManishMadan2882
2024-03-05 21:15:58 +05:30
parent 5fffa8e9db
commit ae929438a5
33 changed files with 8187 additions and 9855 deletions

View File

@@ -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>
</>
)
}
}

View File

@@ -1,102 +0,0 @@
import { vscDarkPlus } from 'react-syntax-highlighter/dist/cjs/styles/prism';
import remarkGfm from 'remark-gfm';
import ReactMarkdown from 'react-markdown'
import SyntaxHighlighter from 'react-syntax-highlighter/dist/esm/light-async';
interface typeProps {
message: string
}
const classes = {
list: {
p: {
display: 'inline'
},
li: {
':not(:first-child)': {
marginTop: '1em'
},
'> .list': {
marginTop: '1em'
}
}
}
};
const Response = (props: typeProps) => {
return (
<ReactMarkdown
className="whitespace-pre-wrap break-words max-w-72"
remarkPlugins={[remarkGfm]}
components={{
code({ node, className, children, ...props }) {
const match = /language-(\w+)/.exec(className || '');
return match ? (
<SyntaxHighlighter
PreTag="div"
wrapLines={true}
lineProps={{ style: { width: '', overflowX: 'scroll' } }}
language={match[1]}
style={vscDarkPlus}
>
{String(children).replace(/\n$/, '')}
</SyntaxHighlighter>
) : (
<code className={className ? className : ''} {...props}>
{children}
</code>
);
},
ul({ children }) {
return (
<ul
className={`list-inside list-disc whitespace-normal pl-4 ${classes.list}`}
>
{children}
</ul>
);
},
ol({ children }) {
return (
<ol
className={`list-inside list-decimal whitespace-normal pl-4 ${classes.list}`}
>
{children}
</ol>
);
},
table({ children }) {
return (
<div className="relative overflow-x-auto rounded-lg border">
<table className="w-full text-left text-sm text-gray-700">
{children}
</table>
</div>
);
},
thead({ children }) {
return (
<thead className="text-xs uppercase text-gray-900 [&>.table-row]:bg-gray-50">
{children}
</thead>
);
},
tr({ children }) {
return (
<tr className="table-row border-b odd:bg-white even:bg-gray-50">
{children}
</tr>
);
},
td({ children }) {
return <td className="px-6 py-3">{children}</td>;
},
th({ children }) {
return <th className="px-6 py-3">{children}</th>;
},
}}
>
{props.message}
</ReactMarkdown>
)
}
export default Response

View File

@@ -1,59 +0,0 @@
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const alertVariants = cva(
"relative w-full rounded-lg border px-4 py-3 text-sm [&>svg+div]:translate-y-[-3px] [&>svg]:absolute [&>svg]:left-4 [&>svg]:top-4 [&>svg]:text-foreground [&>svg~*]:pl-7",
{
variants: {
variant: {
default: "bg-background text-foreground",
destructive:
"border-destructive/50 text-destructive dark:border-destructive [&>svg]:text-destructive",
},
},
defaultVariants: {
variant: "default",
},
}
)
const Alert = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
>(({ className, variant, ...props }, ref) => (
<div
ref={ref}
role="alert"
className={cn(alertVariants({ variant }), className)}
{...props}
/>
))
Alert.displayName = "Alert"
const AlertTitle = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLHeadingElement>
>(({ className, ...props }, ref) => (
<h5
ref={ref}
className={cn("mb-1 font-medium leading-none tracking-tight", className)}
{...props}
/>
))
AlertTitle.displayName = "AlertTitle"
const AlertDescription = React.forwardRef<
HTMLParagraphElement,
React.HTMLAttributes<HTMLParagraphElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn("text-sm [&_p]:leading-relaxed", className)}
{...props}
/>
))
AlertDescription.displayName = "AlertDescription"
export { Alert, AlertTitle, AlertDescription }

View File

@@ -1,57 +0,0 @@
import * as React from "react"
import { Slot } from "@radix-ui/react-slot"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
const buttonVariants = cva(
"inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50",
{
variants: {
variant: {
default:
"bg-primary text-primary-foreground shadow hover:bg-primary/90",
destructive:
"bg-destructive text-destructive-foreground shadow-sm hover:bg-destructive/90",
outline:
"border border-input bg-background shadow-sm hover:bg-accent hover:text-accent-foreground",
secondary:
"bg-secondary text-secondary-foreground shadow-sm hover:bg-secondary/80",
ghost: "hover:bg-accent hover:text-accent-foreground",
link: "text-primary underline-offset-4 hover:underline",
},
size: {
default: "h-9 px-4 py-2",
sm: "h-8 rounded-md px-3 text-xs",
lg: "h-10 rounded-md px-8",
icon: "h-9 w-9",
},
},
defaultVariants: {
variant: "default",
size: "default",
},
}
)
export interface ButtonProps
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
VariantProps<typeof buttonVariants> {
asChild?: boolean
}
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
({ className, variant, size, asChild = false, ...props }, ref) => {
const Comp = asChild ? Slot : "button"
return (
<Comp
className={cn(buttonVariants({ variant, size, className }))}
ref={ref}
{...props}
/>
)
}
)
Button.displayName = "Button"
export { Button, buttonVariants }

View File

@@ -1,25 +0,0 @@
import * as React from "react"
import { cn } from "@/lib/utils"
export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {}
const Input = React.forwardRef<HTMLInputElement, InputProps>(
({ className, type, ...props }, ref) => {
return (
<input
type={type}
className={cn(
"flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors file:border-0 file:bg-transparent file:text-sm file:font-medium placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Input.displayName = "Input"
export { Input }

View File

@@ -1,48 +0,0 @@
"use client"
import * as React from "react"
import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area"
import { cn } from "@/lib/utils"
const ScrollArea = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.Root>
>(({ className, children, ...props }, ref) => (
<ScrollAreaPrimitive.Root
ref={ref}
className={cn("relative overflow-hidden", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport className="h-full w-full rounded-[inherit]">
{children}
</ScrollAreaPrimitive.Viewport>
<ScrollBar />
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
))
ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName
const ScrollBar = React.forwardRef<
React.ElementRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>,
React.ComponentPropsWithoutRef<typeof ScrollAreaPrimitive.ScrollAreaScrollbar>
>(({ className, orientation = "vertical", ...props }, ref) => (
<ScrollAreaPrimitive.ScrollAreaScrollbar
ref={ref}
orientation={orientation}
className={cn(
"flex touch-none select-none transition-colors",
orientation === "vertical" &&
"h-full w-2.5 border-l border-l-transparent p-[1px]",
orientation === "horizontal" &&
"h-2.5 flex-col border-t border-t-transparent p-[1px]",
className
)}
{...props}
>
<ScrollAreaPrimitive.ScrollAreaThumb className="relative flex-1 rounded-full bg-border" />
</ScrollAreaPrimitive.ScrollAreaScrollbar>
))
ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName
export { ScrollArea, ScrollBar }

View File

@@ -1,24 +0,0 @@
import * as React from "react"
import { cn } from "@/lib/utils"
export interface TextareaProps
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {}
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>(
({ className, ...props }, ref) => {
return (
<textarea
className={cn(
"flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-sm shadow-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50",
className
)}
ref={ref}
{...props}
/>
)
}
)
Textarea.displayName = "Textarea"
export { Textarea }