-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/gra-866-grai-experiment' of github.com:grai-io/…
…grai-core into feature/gra-866-grai-experiment
- Loading branch information
Showing
14 changed files
with
568 additions
and
301 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react" | ||
import { render, screen, waitFor } from "testing" | ||
import ChatWrapper, { FETCH_CHATS } from "./ChatWrapper" | ||
|
||
const workspace = { | ||
id: "1", | ||
} | ||
|
||
test("renders", async () => { | ||
render(<ChatWrapper workspace={workspace} />) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByRole("textbox")).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
test("error", async () => { | ||
const mocks = [ | ||
{ | ||
request: { | ||
query: FETCH_CHATS, | ||
variables: { | ||
workspaceId: "1", | ||
}, | ||
}, | ||
error: new Error("An error occurred"), | ||
}, | ||
] | ||
|
||
render(<ChatWrapper workspace={workspace} />, { mocks }) | ||
|
||
await waitFor(() => { | ||
expect(screen.getByText("An error occurred")).toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { useEffect } from "react" | ||
import { gql, useMutation } from "@apollo/client" | ||
import Loading from "components/layout/Loading" | ||
import GraphError from "components/utils/GraphError" | ||
import { | ||
FetchChats, | ||
FetchChatsVariables, | ||
FetchChats_fetchOrCreateChats_data_messages_data, | ||
} from "./__generated__/FetchChats" | ||
import { Chat } from "./ChatWindow" | ||
import WebsocketChat from "./WebsocketChat" | ||
|
||
export const FETCH_CHATS = gql` | ||
mutation FetchChats($workspaceId: ID!) { | ||
fetchOrCreateChats(workspaceId: $workspaceId) { | ||
data { | ||
id | ||
messages { | ||
data { | ||
id | ||
message | ||
role | ||
created_at | ||
} | ||
} | ||
} | ||
} | ||
} | ||
` | ||
|
||
interface Workspace { | ||
id: string | ||
} | ||
|
||
type ChatWrapperProps = { | ||
workspace: Workspace | ||
} | ||
|
||
const ChatWrapper: React.FC<ChatWrapperProps> = ({ workspace }) => { | ||
const [fetchChats, { data, loading, error }] = useMutation< | ||
FetchChats, | ||
FetchChatsVariables | ||
>(FETCH_CHATS, { | ||
variables: { | ||
workspaceId: workspace.id, | ||
}, | ||
}) | ||
|
||
useEffect(() => { | ||
fetchChats().catch(() => {}) | ||
}, [fetchChats]) | ||
|
||
if (error) return <GraphError error={error} /> | ||
if (loading || !data) return <Loading /> | ||
|
||
const chats: Chat[] = data.fetchOrCreateChats.data | ||
.reduce<FetchChats_fetchOrCreateChats_data_messages_data[]>((acc, chat) => { | ||
return acc.concat(chat.messages.data) | ||
}, []) | ||
.map(message => ({ | ||
message: message.message, | ||
sender: message.role === "USER", | ||
})) | ||
const chatId = data.fetchOrCreateChats.data.at(-1)?.id | ||
|
||
return ( | ||
<WebsocketChat | ||
workspace={workspace} | ||
initialChats={chats} | ||
initialChatId={chatId} | ||
/> | ||
) | ||
} | ||
|
||
export default ChatWrapper |
40 changes: 40 additions & 0 deletions
40
grai-frontend/src/components/chat/__generated__/FetchChats.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.