Skip to content

Commit

Permalink
create chat section
Browse files Browse the repository at this point in the history
  • Loading branch information
caroluchoa committed Nov 30, 2023
1 parent 5987e07 commit c8cf93e
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 35 deletions.
119 changes: 119 additions & 0 deletions components/chat/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { useSignal } from "@preact/signals";
import { useCallback, useEffect, useRef } from "preact/hooks";

function Chat() {
const messageEl = useRef<HTMLDivElement>(null);
const userInput = useSignal("");
const ws = useSignal<WebSocket | null>(null);
const messageList = useSignal<{ content: string; role: "user" | "bot" }[]>(
[],
);

useEffect(() => {
const host = window.location.host;
const websocket = window.location.protocol === "https:" ? "wss" : "ws";
ws.value = new WebSocket(
`${websocket}://${host}/live/invoke/ai-assistants/actions/chat.ts?assistant=brand`,
);
ws.value.onmessage = (event: MessageEvent) => {
updateMessages(event.data);
};
}, []);

const updateMessages = useCallback((data: string) => {
const newMessageObject: { content: string; role: "user" | "bot" } = {
content: data,
role: "user",
};
messageList.value = [...messageList.value, newMessageObject];
}, []);

useEffect(() => {
// For automatic srolling
const messageElement = messageEl.current;

if (messageElement) {
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
messageElement.scrollTop = messageElement.scrollHeight;
}
}
});

observer.observe(messageElement, { childList: true });

return () => observer.disconnect();
}
}, []);

const send = useCallback((text: string) => {
if (ws.value) {
ws.value.send(text);
}
}, []);

const handleSubmit = () => {
send(userInput.value);
const newMessageObject: { content: string; role: "user" | "bot" } = {
content: userInput.value,
role: "bot",
};
messageList.value = [...messageList.value, newMessageObject];
userInput.value = "";
};

const handleKeydown = (e: KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();
}
};

return (
<>
<div class="w-96 mb-4 mt-2 shadow-lg rounded-lg h-3/4 flex flex-col justify-end z-50 bg-white fixed bottom-1 right-4">
<div class="bg-blue-500 flex justify-center p-3 rounded-t-lg text-white">
DecoBot
</div>
<div
ref={messageEl}
class="h-full overflow-y-auto pt-4 flex flex-col mx-5"
>
{messageList.value.map((message, index) => (
<div
key={index}
class={`p-2 rounded-2xl mb-3 w-fit text-sm max-w-xs ${
message.role === "user"
? "bg-gray-200 text-black self-start"
: "bg-blue-600 text-white self-end"
}`}
>
{message.content}
</div>
))}
</div>
<div class="flex flex-row items-center bg-gray-100 rounded-xl relative mb-4 p-4 mt-4 mx-4">
<textarea
id="userInput"
placeholder="Ask..."
class="w-full grow h-16 outline-none relative resize-none pr-6 bg-gray-100 text-sm"
value={userInput.value}
onInput={(e: Event) =>
userInput.value = (e.target as HTMLTextAreaElement).value}
onKeyDown={handleKeydown}
/>
<button
type="button"
class="bg-blue-600 hover:bg-blue-700 absolute rounder-md font-light text-white py-1 px-4 rounded-lg text-sm bottom-3 right-3"
onClick={handleSubmit}
>
Send
</button>
</div>
</div>
</>
);
}

export default Chat;
4 changes: 2 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"imports": {
"$store/": "./",
"deco/": "https://denopkg.com/deco-cx/[email protected].4/",
"apps/": "https://denopkg.com/deco-cx/apps@0.23.13/",
"deco/": "https://denopkg.com/deco-cx/[email protected].6/",
"apps/": "https://denopkg.com/deco-cx/apps@9dd6db949718be381b9338f8167037b9edaddd63/",
"$fresh/": "https://denopkg.com/denoland/fresh@7ad4610e3a42aba42638cbc1041b96ee58a9b29e/",
"preact": "https://esm.sh/[email protected]",
"preact/": "https://esm.sh/[email protected]/",
Expand Down
66 changes: 34 additions & 32 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ import * as $$2 from "./islands/AddToCartButton/shopify.tsx";
import * as $$3 from "./islands/AddToCartButton/vnda.tsx";
import * as $$4 from "./islands/AddToCartButton/vtex.tsx";
import * as $$5 from "./islands/AddToCartButton/wake.tsx";
import * as $$6 from "./islands/Header/Buttons.tsx";
import * as $$7 from "./islands/Header/Cart/linx.tsx";
import * as $$8 from "./islands/Header/Cart/nuvemshop.tsx";
import * as $$9 from "./islands/Header/Cart/shopify.tsx";
import * as $$10 from "./islands/Header/Cart/vnda.tsx";
import * as $$11 from "./islands/Header/Cart/vtex.tsx";
import * as $$12 from "./islands/Header/Cart/wake.tsx";
import * as $$13 from "./islands/Header/Drawers.tsx";
import * as $$14 from "./islands/Header/Searchbar.tsx";
import * as $$15 from "./islands/Newsletter.tsx";
import * as $$16 from "./islands/OutOfStock.tsx";
import * as $$17 from "./islands/ProductImageZoom.tsx";
import * as $$18 from "./islands/SearchControls.tsx";
import * as $$19 from "./islands/ShippingSimulation.tsx";
import * as $$20 from "./islands/SliderJS.tsx";
import * as $$21 from "./islands/WishlistButton.tsx";
import * as $$6 from "./islands/Chat.tsx";
import * as $$7 from "./islands/Header/Buttons.tsx";
import * as $$8 from "./islands/Header/Cart/linx.tsx";
import * as $$9 from "./islands/Header/Cart/nuvemshop.tsx";
import * as $$10 from "./islands/Header/Cart/shopify.tsx";
import * as $$11 from "./islands/Header/Cart/vnda.tsx";
import * as $$12 from "./islands/Header/Cart/vtex.tsx";
import * as $$13 from "./islands/Header/Cart/wake.tsx";
import * as $$14 from "./islands/Header/Drawers.tsx";
import * as $$15 from "./islands/Header/Searchbar.tsx";
import * as $$16 from "./islands/Newsletter.tsx";
import * as $$17 from "./islands/OutOfStock.tsx";
import * as $$18 from "./islands/ProductImageZoom.tsx";
import * as $$19 from "./islands/SearchControls.tsx";
import * as $$20 from "./islands/ShippingSimulation.tsx";
import * as $$21 from "./islands/SliderJS.tsx";
import * as $$22 from "./islands/WishlistButton.tsx";

const manifest = {
routes: {
Expand All @@ -37,22 +38,23 @@ const manifest = {
"./islands/AddToCartButton/vnda.tsx": $$3,
"./islands/AddToCartButton/vtex.tsx": $$4,
"./islands/AddToCartButton/wake.tsx": $$5,
"./islands/Header/Buttons.tsx": $$6,
"./islands/Header/Cart/linx.tsx": $$7,
"./islands/Header/Cart/nuvemshop.tsx": $$8,
"./islands/Header/Cart/shopify.tsx": $$9,
"./islands/Header/Cart/vnda.tsx": $$10,
"./islands/Header/Cart/vtex.tsx": $$11,
"./islands/Header/Cart/wake.tsx": $$12,
"./islands/Header/Drawers.tsx": $$13,
"./islands/Header/Searchbar.tsx": $$14,
"./islands/Newsletter.tsx": $$15,
"./islands/OutOfStock.tsx": $$16,
"./islands/ProductImageZoom.tsx": $$17,
"./islands/SearchControls.tsx": $$18,
"./islands/ShippingSimulation.tsx": $$19,
"./islands/SliderJS.tsx": $$20,
"./islands/WishlistButton.tsx": $$21,
"./islands/Chat.tsx": $$6,
"./islands/Header/Buttons.tsx": $$7,
"./islands/Header/Cart/linx.tsx": $$8,
"./islands/Header/Cart/nuvemshop.tsx": $$9,
"./islands/Header/Cart/shopify.tsx": $$10,
"./islands/Header/Cart/vnda.tsx": $$11,
"./islands/Header/Cart/vtex.tsx": $$12,
"./islands/Header/Cart/wake.tsx": $$13,
"./islands/Header/Drawers.tsx": $$14,
"./islands/Header/Searchbar.tsx": $$15,
"./islands/Newsletter.tsx": $$16,
"./islands/OutOfStock.tsx": $$17,
"./islands/ProductImageZoom.tsx": $$18,
"./islands/SearchControls.tsx": $$19,
"./islands/ShippingSimulation.tsx": $$20,
"./islands/SliderJS.tsx": $$21,
"./islands/WishlistButton.tsx": $$22,
},
baseUrl: import.meta.url,
};
Expand Down
1 change: 1 addition & 0 deletions islands/Chat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from "apps/brand-assistant/sections/Chat.tsx";
3 changes: 3 additions & 0 deletions loaders/List/Sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ interface Props {
sections: Section[] | null;
}

/**
* @ignoreAI true
*/
function Sections({ sections }: Props): VNode[] | null {
if (sections === null) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion static/tailwind.css

Large diffs are not rendered by default.

0 comments on commit c8cf93e

Please sign in to comment.