Skip to content

Commit

Permalink
hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
shivankacker committed Jan 23, 2024
1 parent a9970f8 commit 09c7594
Show file tree
Hide file tree
Showing 9 changed files with 4,408 additions and 6,423 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'audio-recorder-polyfill';
3 changes: 2 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
distDir: 'build'
distDir: 'build',
reactStrictMode: false,
};

module.exports = nextConfig;
Expand Down
34 changes: 24 additions & 10 deletions src/app/(main)/project/[project_id]/chat/[chat_id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export default function Chat(params: {
}) {
const { project_id, chat_id } = params.params;
const searchParams = useSearchParams();
const shouldAutoPlay = searchParams?.get("autoplay") !== null;
const autoplayParam = searchParams?.get("autoplay") !== null;
const [shouldAutoPlay, setShouldAutoPlay] = useState<boolean>(false);
const [newChat, setNewChat] = useState("");
const [chatMessage, setChatMessage] = useState<string>("");
const [storage] = useAtom(storageAtom);
Expand All @@ -42,21 +43,24 @@ export default function Chat(params: {

const chat: Chat | undefined = chatQuery.data;
const [projectData, setProjectData] = useState<Project>();
const [autoPlayIndex, setAutoPlayIndex] = useState<number>(-1);

const openai_key =
!storage?.user?.allow_key || storage?.override_api_key
? storage?.openai_api_key
: undefined;

useEffect(() => {
if (!chat?.chats) return;
const uri = window.location.toString();
if (uri.indexOf("?") > 0)
window.history.replaceState(
{},
document.title,
uri.substring(0, uri.indexOf("?")),
);
}, []);
setShouldAutoPlay(true);
window.history.replaceState(
{},
document.title,
uri.substring(0, uri.indexOf("?")),
);
}, [chat, autoplayParam]);

useEffect(() => {
const prevTitle = document.title;
Expand Down Expand Up @@ -101,6 +105,7 @@ export default function Chat(params: {
{
retry: false,
onSuccess: async (data, vars) => {
setAutoPlayIndex((chat?.chats?.length || 0) + 1);
if (!project?.assistant_id) await chatQuery.refetch();
},
onError: async (error, vars) => {
Expand All @@ -111,8 +116,8 @@ export default function Chat(params: {
);

const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
setIsTyping(true);
e.preventDefault();
setIsTyping(true);
const fd = await getFormData(storage, undefined, newChat);
converseMutation.mutate({ formdata: fd });
};
Expand All @@ -131,6 +136,15 @@ export default function Chat(params: {
}
}, [chatMessage]);

useEffect(() => {
setShouldAutoPlay(false);
}, [shouldAutoPlay]);

useEffect(() => {
console.log(autoPlayIndex);
if (autoPlayIndex !== -1) setTimeout(() => setAutoPlayIndex(-1), 1000);
}, [autoPlayIndex]);

return (
<div className="h-screen flex flex-col flex-1">
<div className="flex-1 overflow-auto" ref={messagesContainerRef}>
Expand All @@ -139,8 +153,8 @@ export default function Chat(params: {
message={message}
key={message.external_id}
autoplay={
(!!chatMessage || shouldAutoPlay) &&
i === (chat?.chats?.length || 0) - 1
(shouldAutoPlay && i === 1) ||
i === autoPlayIndex
}
/>
))}
Expand Down
11 changes: 4 additions & 7 deletions src/components/chatbar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from "react";
import { Input } from "./ui/interactive";
import Loading from "./ui/loading";
import LangDialog from "./ui/langdialog";
import { supportedLanguages } from "@/utils/constants";
import { storageAtom } from "@/store";
import { useAtom } from "jotai";
Expand Down Expand Up @@ -30,9 +29,8 @@ export default function ChatBar(props: {
return (
<>
<div
className={`fixed inset-0 flex items-center justify-center transition-all ${
status === "recording" ? "visible opacity-100" : "invisible opacity-0"
}`}
className={`fixed inset-0 flex items-center justify-center transition-all ${status === "recording" ? "visible opacity-100" : "invisible opacity-0"
}`}
>
<div className="bg-black/40 absolute inset-0 -z-10" />
<div className="md:min-w-[300px] md:min-h-[300px] bg-white rounded-xl p-4 flex items-center flex-col gap-4">
Expand Down Expand Up @@ -122,9 +120,8 @@ export default function ChatBar(props: {
<button
title="Search by Voice"
type="button"
className={`w-12 h-12 p-1 ml-2 text-xl bg-gray-50 text-gray-500 rounded-lg hover:bg-gray-100 transition ${
status === "recording" ? "text-green-500" : ""
}`}
className={`w-12 h-12 p-1 ml-2 text-xl bg-gray-50 text-gray-500 rounded-lg hover:bg-gray-100 transition ${status === "recording" ? "text-green-500" : ""
}`}
onClick={
status === "recording" ? stopRecording : startRecording
}
Expand Down
25 changes: 13 additions & 12 deletions src/components/chatblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default function ChatBlock(props: {
const audio = new Audio(message?.audio);
setAudio(audio);
setAudioStatus("playing");
console.log("playing");
audio.play();
audio.addEventListener("ended", () => {
setAudioStatus("stopped");
Expand All @@ -106,6 +107,7 @@ export default function ChatBlock(props: {
};

const togglePlay = () => {
console.log(audioStatus);
if (audioStatus === "loading") return;
if (audioStatus === "unloaded") loadAudio();
if (audioStatus === "playing") {
Expand Down Expand Up @@ -133,9 +135,8 @@ export default function ChatBlock(props: {

return (
<div
className={`flex flex-col gap-4 p-4 pt-8 md:p-6 md:pt-8 ${
message?.messageType === ChatMessageType.USER ? "bg-gray-500/5" : ""
}`}
className={`flex flex-col gap-4 p-4 pt-8 md:p-6 md:pt-8 ${message?.messageType === ChatMessageType.USER ? "bg-gray-500/5" : ""
}`}
>
<div className="flex gap-6 max-w-4xl mx-auto w-full">
<div>
Expand Down Expand Up @@ -187,7 +188,7 @@ export default function ChatBlock(props: {
>
{audioStatus === "unloaded"
? (message?.message || message?.original_message) +
cursorText || ""
cursorText || ""
: `<span className="text-green-600">${highlightText}</span><span>${blackText}</span>`}
</ReactMarkdown>
)}
Expand All @@ -206,13 +207,13 @@ export default function ChatBlock(props: {
</button>
{(audioStatus === "paused" ||
audioStatus === "playing") && (
<button
onClick={stopAudio}
className="flex items-center justify-center text-red-500 rounded-lg transition bg-gray-200 hover:text-gray-700 hover:bg-gray-300 p-2 "
>
<i className="fa-regular fa-circle-stop text-xl"></i>
</button>
)}
<button
onClick={stopAudio}
className="flex items-center justify-center text-red-500 rounded-lg transition bg-gray-200 hover:text-gray-700 hover:bg-gray-300 p-2 "
>
<i className="fa-regular fa-circle-stop text-xl"></i>
</button>
)}
</div>
)}
{storage?.show_english &&
Expand Down Expand Up @@ -303,7 +304,7 @@ const ChatFeedback = ({
"This is harmful / unsafe.",
],
});
const { chat_id } = useParams();
const { chat_id }: any = useParams();

const createChatFeedbackMutation = useMutation(
(feedback: Partial<ChatFeedback>) => API.feedback.create(feedback),
Expand Down
2 changes: 1 addition & 1 deletion src/components/recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const useAudioRecorder = (
const audioUrl = URL.createObjectURL(audioBlob);
setMediaBlobUrl(audioUrl);
onStop?.(audioUrl);
setStatus("stopped");
setStatus("idle");
});

recorder.start();
Expand Down
78 changes: 36 additions & 42 deletions src/components/sidebar/chatSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ export default function ChatSideBar(props: { project_id?: string }) {
},
...(storage?.user?.is_staff
? [
{
icon: "user-shield",
text: "Admin",
onclick: () => {
router.push("/admin");
},
{
icon: "user-shield",
text: "Admin",
onclick: () => {
router.push("/admin");
},
]
},
]
: []),
{
icon: "sign-out-alt",
Expand Down Expand Up @@ -149,9 +149,8 @@ export default function ChatSideBar(props: { project_id?: string }) {
threshold={10}
loader={
<div
className={`${
chatsQuery.isFetching ? "" : "hidden"
} flex justify-center items-center mt-2 h-full`}
className={`${chatsQuery.isFetching ? "" : "hidden"
} flex justify-center items-center mt-2 h-full`}
>
<div
className="inline-block h-8 w-8 animate-spin rounded-full border-4 border-solid border-current border-r-transparent align-[-0.125em] motion-reduce:animate-[spin_1.5s_linear_infinite]"
Expand All @@ -164,39 +163,34 @@ export default function ChatSideBar(props: { project_id?: string }) {
</div>
}
>
{project_id ? (
chatsQuery.data?.pages.map((group, index) => (
<div key={index} className="flex flex-col gap-2">
{group.results.map((chat: Chat) => (
<div
key={chat.external_id}
className="w-full group hover:bg-gray-100 rounded-lg overflow-hidden flex gap-2 justify-between"
<div className="flex flex-col gap-2">
{project_id && (
chatsQuery.fullData?.map((chat: Chat, j: number) => (
<div
key={j}
className="w-full group hover:bg-gray-100 rounded-lg overflow-hidden flex gap-2 justify-between"
>
<Link
href={`/project/${project_id}/chat/${chat.external_id}`}
className="w-full py-2 px-4 text-left truncate"
title={chat.title}
>
<Link
href={`/project/${project_id}/chat/${chat.external_id}`}
className="w-full py-2 px-4 text-left truncate"
title={chat.title}
>
{chat.title}
</Link>
<button
className="py-2 px-2 hidden group-hover:block"
onClick={() =>
setDeleteModal({
open: true,
external_id: chat.external_id,
})
}
>
<i className="fal fa-trash-alt" />
</button>
</div>
))}
</div>
))
) : (
<></>
)}
{chat.title}
</Link>
<button
className="py-2 px-2 hidden group-hover:block"
onClick={() =>
setDeleteModal({
open: true,
external_id: chat.external_id,
})
}
>
<i className="fal fa-trash-alt" />
</button>
</div>
)))}
</div>
</InfiniteScroll>
</div>
</div>
Expand Down
5 changes: 4 additions & 1 deletion src/utils/hooks/useInfiQuery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ export const useInfiQuery = (
const loadMore = () => {
if (query.hasNextPage) query.fetchNextPage();
};
return { ...query, loadMore };

const fullData = query.data?.pages.flatMap((page) => page.results);

return { ...query, loadMore, fullData };
};
Loading

0 comments on commit 09c7594

Please sign in to comment.