Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #4828: [Bug]: Chat interface empty state flickers for a brief moment until messages load #4831

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions frontend/__tests__/components/chat/chat-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ describe("ChatInput", () => {
// Fire paste event with text data
fireEvent.paste(input!, {
clipboardData: {
getData: (type: string) => type === 'text/plain' ? 'test paste' : '',
files: []
}
getData: (type: string) => (type === "text/plain" ? "test paste" : ""),
files: [],
},
});
});

Expand All @@ -187,14 +187,16 @@ describe("ChatInput", () => {
expect(input).toBeTruthy();

// Create a paste event with an image file
const file = new File(["dummy content"], "image.png", { type: "image/png" });
const file = new File(["dummy content"], "image.png", {
type: "image/png",
});

// Fire paste event with image data
fireEvent.paste(input!, {
clipboardData: {
getData: () => '',
files: [file]
}
getData: () => "",
files: [file],
},
});

// Verify image paste was handled
Expand Down
34 changes: 33 additions & 1 deletion frontend/__tests__/components/chat/chat-interface.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ describe("Empty state", () => {
}));

const { useSocket: useSocketMock } = vi.hoisted(() => ({
useSocket: vi.fn(() => ({ send: sendMock, runtimeActive: true })),
useSocket: vi.fn(() => ({
send: sendMock,
runtimeActive: true,
isConnected: true,
})),
}));

beforeAll(() => {
Expand All @@ -31,6 +35,31 @@ describe("Empty state", () => {
vi.clearAllMocks();
});

it("should not render suggestions if socket is not yet connected", () => {
useSocketMock.mockImplementation(() => ({
send: sendMock,
runtimeActive: false,
isConnected: false,
}));

const { rerender } = renderWithProviders(<ChatInterface />, {
preloadedState: {
chat: { messages: [] },
},
});

expect(screen.queryByTestId("suggestions")).not.toBeInTheDocument();

useSocketMock.mockImplementation(() => ({
send: sendMock,
runtimeActive: true,
isConnected: true,
}));
rerender(<ChatInterface />);

expect(screen.getByTestId("suggestions")).toBeInTheDocument();
});

it("should render suggestions if empty", () => {
const { store } = renderWithProviders(<ChatInterface />, {
preloadedState: {
Expand Down Expand Up @@ -80,6 +109,7 @@ describe("Empty state", () => {
useSocketMock.mockImplementation(() => ({
send: sendMock,
runtimeActive: false, // mock an inactive runtime setup
isConnected: false,
}));
const addUserMessageSpy = vi.spyOn(ChatSlice, "addUserMessage");
const user = userEvent.setup();
Expand Down Expand Up @@ -109,6 +139,7 @@ describe("Empty state", () => {
useSocketMock.mockImplementation(() => ({
send: sendMock,
runtimeActive: false, // mock an inactive runtime setup
isConnected: false,
}));
const user = userEvent.setup();
const { rerender } = renderWithProviders(<ChatInterface />, {
Expand All @@ -126,6 +157,7 @@ describe("Empty state", () => {
useSocketMock.mockImplementation(() => ({
send: sendMock,
runtimeActive: true, // mock an active runtime setup
isConnected: true,
}));
rerender(<ChatInterface />);

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/chat-interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const isErrorMessage = (
): message is ErrorMessage => "error" in message;

export function ChatInterface() {
const { send } = useSocket();
const { send, isConnected } = useSocket();
const dispatch = useDispatch();
const scrollRef = React.useRef<HTMLDivElement>(null);
const { scrollDomToBottom, onChatBodyScroll, hitBottom } =
Expand Down Expand Up @@ -74,7 +74,7 @@ export function ChatInterface() {

return (
<div className="h-full flex flex-col justify-between">
{messages.length === 0 && (
{messages.length === 0 && isConnected && (
<div className="flex flex-col gap-6 h-full px-4 items-center justify-center">
<div className="flex flex-col items-center p-4 bg-neutral-700 rounded-xl w-full">
<BuildIt width={45} height={54} />
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/context/socket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,4 @@ function useSocket() {
return context;
}

export { SocketProvider, useSocket };
export { SocketContext, SocketProvider, useSocket };
Loading