|
| 1 | +import { proxy, useSnapshot } from "valtio"; |
| 2 | +import RMarkdown from "react-markdown"; |
| 3 | +import remarkGfm from "remark-gfm"; |
| 4 | +import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; |
| 5 | +import { a11yDark } from "react-syntax-highlighter/dist/esm/styles/prism"; |
| 6 | +import { useCallback, useEffect, useRef, useState } from "react"; |
| 7 | +import { ArrowDownCircleFill } from "react-bootstrap-icons"; |
| 8 | + |
| 9 | +function useLocal<T extends object | undefined>(input: T) { |
| 10 | + return useRef(proxy(input)).current as T; |
| 11 | +} |
| 12 | + |
| 13 | +const history = proxy([ |
| 14 | + { |
| 15 | + type: "system", |
| 16 | + message: ` |
| 17 | +# Hello World |
| 18 | +This is a test |
| 19 | +
|
| 20 | +\`\`\`js |
| 21 | +console.log("Hello World!"); |
| 22 | +return true; |
| 23 | +\`\`\` |
| 24 | + |
| 25 | + `, |
| 26 | + }, |
| 27 | + { type: "user", message: "Hi!" }, |
| 28 | + { type: "system", message: "Hello!" }, |
| 29 | + { type: "user", message: "Hi!" }, |
| 30 | + { type: "system", message: "Hello!" }, |
| 31 | + { type: "user", message: "Hi!" }, |
| 32 | + { type: "system", message: "Hello!" }, |
| 33 | + { type: "user", message: "Hi!" }, |
| 34 | + { type: "system", message: "Hello!" }, |
| 35 | + { type: "user", message: "Hi!" }, |
| 36 | + { type: "system", message: "Hello!" }, |
| 37 | + { type: "user", message: "Hi!" }, |
| 38 | +]); |
| 39 | + |
| 40 | +const Mardown = ({ children }: { children: string }) => ( |
| 41 | + <RMarkdown |
| 42 | + remarkPlugins={[remarkGfm]} |
| 43 | + components={{ |
| 44 | + code: Code, |
| 45 | + }} |
| 46 | + > |
| 47 | + {children} |
| 48 | + </RMarkdown> |
| 49 | +); |
| 50 | + |
| 51 | +const Code = ( |
| 52 | + props: React.DetailedHTMLProps< |
| 53 | + React.HTMLAttributes<HTMLElement>, |
| 54 | + HTMLElement |
| 55 | + >, |
| 56 | +) => { |
| 57 | + const { children, className, ref, ...rest } = props; |
| 58 | + const match = /language-(\w+)/.exec(className || ""); |
| 59 | + |
| 60 | + if (!match) { |
| 61 | + return ( |
| 62 | + <div className="p-3"> |
| 63 | + <code {...rest}>{children}</code> |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <div> |
| 70 | + <SyntaxHighlighter |
| 71 | + {...rest} |
| 72 | + PreTag="div" |
| 73 | + customStyle={{ margin: 0 }} |
| 74 | + language={match[1]} |
| 75 | + style={a11yDark} |
| 76 | + > |
| 77 | + {String(children).replace(/\n$/, "")} |
| 78 | + </SyntaxHighlighter> |
| 79 | + </div> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +const History = ({ history }: { history: any[] }) => { |
| 84 | + const scrollRef = useRef<HTMLDivElement>(null); |
| 85 | + const [displayScrollButton, setDisplayScrollButton] = useState(false); |
| 86 | + |
| 87 | + const onScroll = (e: React.UIEvent<HTMLDivElement>) => { |
| 88 | + const element = e.currentTarget; |
| 89 | + if (element.scrollTop + element.clientHeight < element.scrollHeight) { |
| 90 | + setDisplayScrollButton(true); |
| 91 | + } else { |
| 92 | + setDisplayScrollButton(false); |
| 93 | + } |
| 94 | + }; |
| 95 | + |
| 96 | + const scrollToBottom = useCallback( |
| 97 | + (smooth = true) => { |
| 98 | + if (scrollRef.current) { |
| 99 | + scrollRef.current.scrollTo({ |
| 100 | + top: scrollRef.current.scrollHeight, |
| 101 | + behavior: smooth ? "smooth" : "instant", |
| 102 | + }); |
| 103 | + } |
| 104 | + }, |
| 105 | + [scrollRef], |
| 106 | + ); |
| 107 | + |
| 108 | + // Start at bottom on mount |
| 109 | + useEffect(() => { |
| 110 | + scrollToBottom(false); |
| 111 | + }, []); |
| 112 | + |
| 113 | + // Scroll to bottom when history changes |
| 114 | + useEffect(() => { |
| 115 | + scrollToBottom(); |
| 116 | + }, [history, scrollToBottom]); |
| 117 | + |
| 118 | + return ( |
| 119 | + <div |
| 120 | + className="scroll relative flex flex-1 flex-col overflow-y-scroll px-3" |
| 121 | + onScroll={onScroll} |
| 122 | + ref={scrollRef} |
| 123 | + > |
| 124 | + {history.map((item, index) => { |
| 125 | + switch (item.type) { |
| 126 | + case "system": |
| 127 | + return ( |
| 128 | + <div key={index} className="border-b-2 py-4 text-lg font-bold"> |
| 129 | + <div className="text-l sticky top-0 bg-white">Assistant:</div> |
| 130 | + <div className="prose p-2"> |
| 131 | + <Mardown>{item.message}</Mardown> |
| 132 | + </div> |
| 133 | + </div> |
| 134 | + ); |
| 135 | + case "user": |
| 136 | + return ( |
| 137 | + <div key={index} className="border-b-2 py-4 text-lg font-bold"> |
| 138 | + <div className="sticky top-0 bg-white">You:</div> |
| 139 | + <div className="prose p-2"> |
| 140 | + <Mardown>{item.message}</Mardown> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + ); |
| 144 | + } |
| 145 | + })} |
| 146 | + {displayScrollButton && ( |
| 147 | + <div className="sticky bottom-2 flex w-full justify-center "> |
| 148 | + <button |
| 149 | + onClick={() => scrollToBottom()} |
| 150 | + className="opacity-40 hover:opacity-100" |
| 151 | + > |
| 152 | + <ArrowDownCircleFill size="2rem" /> |
| 153 | + </button> |
| 154 | + </div> |
| 155 | + )} |
| 156 | + </div> |
| 157 | + ); |
| 158 | +}; |
| 159 | + |
| 160 | +const Input = () => { |
| 161 | + return ( |
| 162 | + <form> |
| 163 | + <textarea |
| 164 | + className="textarea textarea-primary w-full rounded-none pl-5" |
| 165 | + placeholder="Bio" |
| 166 | + ></textarea> |
| 167 | + <span className="button"></span> |
| 168 | + </form> |
| 169 | + ); |
| 170 | +}; |
| 171 | + |
| 172 | +export const Chat = () => { |
| 173 | + const historySnap = useSnapshot(history); |
| 174 | + const [value] = useState(""); |
| 175 | + |
| 176 | + const onSizeChange = (size: number) => {}; |
| 177 | + |
| 178 | + return ( |
| 179 | + <div className="flex h-full max-h-full flex-col gap-3 bg-white"> |
| 180 | + <History history={historySnap} /> |
| 181 | + <Input /> |
| 182 | + </div> |
| 183 | + ); |
| 184 | +}; |
0 commit comments