Skip to content

Commit

Permalink
add profile and fix drawer scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
yxshv committed Apr 14, 2024
1 parent 496849c commit 0ce7c91
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 67 deletions.
72 changes: 38 additions & 34 deletions apps/web/src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,39 +69,43 @@ export async function POST(req: NextRequest) {
});
}

const resp = await fetch(
`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
{
headers: {
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
},
method: "POST",
body: JSON.stringify({
chatHistory: chatHistory.chatHistory ?? [],
}),
},
);

console.log("sourcesOnly", sourcesOnly);

if (sourcesOnly == "true") {
const data = await resp.json();
console.log("data", data);
return new Response(JSON.stringify(data), { status: 200 });
}

if (resp.status !== 200 || !resp.ok) {
const errorData = await resp.json();
console.log(errorData);
return new Response(
JSON.stringify({ message: "Error in CF function", error: errorData }),
{ status: resp.status },
);
}

// Stream the response back to the client
const { readable, writable } = new TransformStream();
resp && resp.body!.pipeTo(writable);
try {
const resp = await fetch(
`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
{
headers: {
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
},
method: "POST",
body: JSON.stringify({
chatHistory: chatHistory.chatHistory ?? [],
}),
},
);

console.log("sourcesOnly", sourcesOnly);

if (sourcesOnly == "true") {
const data = await resp.json();
console.log("data", data);
return new Response(JSON.stringify(data), { status: 200 });
}

if (resp.status !== 200 || !resp.ok) {
const errorData = await resp.json();
console.log(errorData);
return new Response(
JSON.stringify({ message: "Error in CF function", error: errorData }),
{ status: resp.status },
);
}

// Stream the response back to the client
const { readable, writable } = new TransformStream();
resp && resp.body!.pipeTo(writable);

return new Response(readable, { status: 200 });
} catch {
}

return new Response(readable, { status: 200 });
}
4 changes: 2 additions & 2 deletions apps/web/src/app/api/getCount/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from "@/server/db";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, ne, sql } from "drizzle-orm";
import { sessions, storedContent, users } from "@/server/db/schema";
import { type NextRequest, NextResponse } from "next/server";

Expand Down Expand Up @@ -66,7 +66,7 @@ export async function GET(req: NextRequest) {
.where(
and(
eq(storedContent.user, session.user.id),
eq(storedContent.type, "page"),
ne(storedContent.type, "twitter-bookmark"),
),
);

Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ body {
padding-bottom: 15dvh;
}

.bottom-padding {
bottom: 20vh;
bottom: 20dvh;
}

@media (min-width: 768px) {
.bottom-padding {
bottom: 0;
}
}

.chat-answer code {
@apply bg-rgray-3 text-wrap rounded-md border border-rgray-5 p-1 text-sm text-rgray-11;
}
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Metadata } from "next";
import { Roboto, Inter } from "next/font/google";
import "./globals.css";

const roboto = Roboto({ weight: ["300", "400", "500"], subsets: ["latin"] });
const inter = Inter({ weight: ["300", "400", "500"], subsets: ["latin"] });

export const metadata: Metadata = {
Expand Down
84 changes: 75 additions & 9 deletions apps/web/src/components/Main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {

const [selectedSpaces, setSelectedSpaces] = useState<number[]>([]);

const [isStreaming, setIsStreaming] = useState(false)

useEffect(() => {
const search = searchParams.get("q");
if (search && search.trim().length > 0) {
Expand Down Expand Up @@ -241,6 +243,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
return;
}

setIsStreaming(true)

if (response.body) {
let reader = response.body?.getReader();
let decoder = new TextDecoder("utf-8");
Expand All @@ -259,14 +263,24 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {

return reader?.read().then(processText);
});

}
};

const onSend = async () => {
const onSend = () => {
if (value.trim().length < 1) return
setLayout("chat");
await getSearchResults();
getSearchResults();
};

function onValueChange(e: React.ChangeEvent<HTMLTextAreaElement>) {
const value = e.target.value;
setValue(value);
const lines = countLines(e.target);
e.target.rows = Math.min(5, lines);
}


return (
<>
<AnimatePresence mode="wait">
Expand All @@ -284,6 +298,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
/>
) : (
<main
key='intial'
data-sidebar-open={sidebarOpen}
ref={main}
className={cn(
Expand All @@ -302,7 +317,57 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
Ask your second brain
</h1>

<Textarea2
<FilterSpaces
name={"Filter"}
onClose={() => {
textArea.current?.querySelector("textarea")?.focus();
}}
side="top"
align="start"
className="bg-[#252525] mr-auto md:hidden"
selectedSpaces={selectedSpaces}
setSelectedSpaces={setSelectedSpaces}
/>
<Textarea2
ref={textArea}
className="bg-rgray-2 md:hidden h-auto w-full flex-row items-start justify-center overflow-auto px-3 md:items-center md:justify-center"
textAreaProps={{
placeholder: "Ask your SuperMemory...",
className:
"overflow-auto h-auto p-3 md:resize-none text-lg w-auto resize-y text-rgray-11 w-full",
value,
rows: 1,
autoFocus: true,
onChange: onValueChange,
onKeyDown: (e) => {
if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
onSend();
}
},
}}
>
<div className="md:hidden text-rgray-11/70 ml-auto mt-auto flex h-full w-min items-center justify-center pb-3 pr-2">

<FilterSpaces
name={"Filter"}
onClose={() => {
textArea.current?.querySelector("textarea")?.focus();
}}
className="hidden md:flex"
selectedSpaces={selectedSpaces}
setSelectedSpaces={setSelectedSpaces}
/>
<button
onClick={onSend}
disabled={value.trim().length < 1}
className="text-rgray-11/70 bg-rgray-3 focus-visible:ring-rgray-8 hover:bg-rgray-4 mt-auto flex items-center justify-center rounded-full p-2 ring-2 ring-transparent transition-[filter] focus-visible:outline-none disabled:cursor-not-allowed disabled:opacity-50"
>
<ArrowUp className="h-5 w-5" />
</button>
</div>
</Textarea2>

<Textarea2
ref={textArea}
exit={{
opacity: 0,
Expand All @@ -325,6 +390,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
}
},
}}
className="hidden md:flex"
>
<div className="text-rgray-11/70 flex h-full w-fit items-center justify-center pl-0 md:w-full md:p-2">
<FilterSpaces
Expand All @@ -344,10 +410,10 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
<ArrowRight className="h-5 w-5" />
</button>
</div>
</Textarea2>
{width <= 768 && <MemoryDrawer hide={hide} />}
</main>
</Textarea2>
</main>
)}
{width <= 768 && <MemoryDrawer hide={hide} />}
</AnimatePresence>
</>
);
Expand Down Expand Up @@ -388,7 +454,7 @@ export function Chat({
"sidebar relative flex w-full flex-col items-end gap-5 px-5 pt-5 transition-[padding-left,padding-top,padding-right] delay-200 duration-200 md:items-center md:gap-10 md:px-72 [&[data-sidebar-open='true']]:pr-10 [&[data-sidebar-open='true']]:delay-0 md:[&[data-sidebar-open='true']]:pl-[calc(2.5rem+30vw)]",
)}
>
<div className="scrollbar-none h-screen w-full overflow-y-auto px-2 md:px-5">
<div className="scrollbar-none h-[70vh] md:h-screen w-full overflow-y-auto px-2 md:px-5">
{chatHistory.map((msg, i) => (
<ChatMessage index={i} key={i} isLast={i === chatHistory.length - 1}>
<ChatQuestion>{msg.question}</ChatQuestion>
Expand All @@ -404,12 +470,12 @@ export function Chat({
</ChatMessage>
))}
</div>
<div className="from-rgray-2 via-rgray-2 to-rgray-2/0 absolute bottom-0 left-0 h-[30%] w-full bg-gradient-to-t" />
<div className="from-rgray-2 via-rgray-2 to-rgray-2/0 absolute bottom-0 left-0 w-full bg-gradient-to-t" />
<div
data-sidebar-open={sidebarOpen}
className="absolute flex w-full items-center justify-center"
>
<div className="animate-from-top fixed bottom-10 left-1/2 md:left-[auto] md:translate-x-0 -translate-x-1/2 mt-auto flex w-[90%] md:w-[50%] flex-col items-center justify-center gap-2">
<div className="animate-from-top fixed bottom-padding md:bottom-10 left-1/2 md:left-[auto] md:translate-x-0 -translate-x-1/2 mt-auto flex w-[90%] md:w-[50%] flex-col items-center justify-center gap-2">
<FilterSpaces
name={"Filter"}
onClose={() => {
Expand Down
22 changes: 12 additions & 10 deletions apps/web/src/components/MemoryDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ export function MemoryDrawer({ className, hide = false, ...props }: Props) {
)}
handle={false}
>
<button
onClick={() =>
setActiveSnapPoint((prev) => (prev === 0.9 ? 0.1 : 0.9))
}
className="bg-rgray-4 border-rgray-6 text-rgray-11 absolute left-1/2 top-0 flex w-fit -translate-x-1/2 -translate-y-1/2 items-center justify-center gap-2 rounded-md border px-3 py-2"
>
<MemoryIcon className="h-7 w-7" />
Memories
</button>
<MemoriesBar />
<button
onClick={() =>
setActiveSnapPoint((prev) => (prev === 0.9 ? 0.1 : 0.9))
}
className="bg-rgray-4 border-rgray-6 text-rgray-11 absolute left-1/2 top-0 flex w-fit -translate-x-1/2 -translate-y-1/2 items-center justify-center gap-2 rounded-md border px-3 py-2"
>
<MemoryIcon className="h-7 w-7" />
Memories
</button>
<div className="w-full h-full overflow-y-auto">
<MemoriesBar isOpen={true} />
</div>
</DrawerContent>
<DrawerOverlay className="relative bg-transparent" />
</Drawer>
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/components/Sidebar/AddMemoryDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function AddMemoryPage({ closeDialog, defaultSpaces, onAdd }: { closeDial
const [selectedSpacesId, setSelectedSpacesId] = useState<number[]>(defaultSpaces ?? []);

return (
<div className="md:w-[40vw]">
<div className="max-w-[80vw] w-[80vw] md:w-[40vw]">
<DialogHeader>
<DialogTitle>Add a web page to memory</DialogTitle>
<DialogDescription>
Expand Down Expand Up @@ -133,7 +133,7 @@ export function NoteAddPage({ closeDialog, defaultSpaces, onAdd }: { closeDialog
}

return (
<div>
<div className="w-[80vw] md:w-auto">
<Input
ref={inputRef}
data-error="false"
Expand All @@ -152,7 +152,7 @@ export function NoteAddPage({ closeDialog, defaultSpaces, onAdd }: { closeDialog
setContent(editor.storage.markdown.getMarkdown());
}}
extensions={[Markdown]}
className="novel-editor bg-rgray-4 border-rgray-7 dark mt-5 max-h-[60vh] min-h-[40vh] w-[50vw] overflow-y-auto rounded-lg border [&>div>div]:p-5"
className="novel-editor w-full bg-rgray-4 border-rgray-7 dark mt-5 max-h-[60vh] min-h-[40vh] md:w-[50vw] overflow-y-auto rounded-lg border [&>div>div]:p-5"
/>
<DialogFooter>
<FilterSpaces
Expand Down Expand Up @@ -243,7 +243,7 @@ export function SpaceAddPage({ closeDialog, onAdd }: { closeDialog: () => void,
}

return (
<div className="md:w-[40vw]">
<div className="w-[80vw] md:w-[40vw]">
<DialogHeader>
<DialogTitle>Add a space</DialogTitle>
</DialogHeader>
Expand Down Expand Up @@ -376,7 +376,7 @@ export function AddExistingMemoryToSpace({
const [selected, setSelected] = useState<StoredContent[]>([]);

return (
<div className="md:w-[40vw]">
<div className="w-[80vw] md:w-[40vw]">
<DialogHeader>
<DialogTitle>Add an existing memory to {space.title}</DialogTitle>
<DialogDescription>
Expand Down

0 comments on commit 0ce7c91

Please sign in to comment.