Skip to content

Commit

Permalink
Merge pull request #19 from dfe-analytical-services/frontend_tidy
Browse files Browse the repository at this point in the history
Frontend tidy up
  • Loading branch information
benoutram authored Sep 29, 2023
2 parents f72e607 + d80cfd2 commit 5aa0620
Show file tree
Hide file tree
Showing 21 changed files with 392 additions and 105 deletions.
24 changes: 12 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
repos:
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
hooks:
- id: black
language_version: python3.11
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8

- repo: https://github.com/psf/black-pre-commit-mirror
rev: 23.9.1
hooks:
- id: black
language_version: python3.11

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
- id: isort
5 changes: 5 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
.next
data/qdrant_storage
next-env.d.ts
node_modules
Pipfile.lock
pnpm-lock.yaml
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "all",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
1 change: 1 addition & 0 deletions chatbot-ui/.env.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NEXT_PUBLIC_CHAT_URL_API=http://localhost:8010/api/chat
4 changes: 2 additions & 2 deletions chatbot-ui/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "next/core-web-vitals"
}
"extends": ["next/core-web-vitals", "plugin:prettier/recommended"]
}
4 changes: 2 additions & 2 deletions chatbot-ui/components/LoadingDots.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import styles from "@/styles/loading-dots.module.css";
import styles from '@/styles/loading-dots.module.css';

const LoadingDots = ({ color = "#000" }: { color: string }) => {
const LoadingDots = ({ color = '#000' }: { color: string }) => {
return (
<span className={styles.loading}>
<span style={{ backgroundColor: color }} />
Expand Down
16 changes: 9 additions & 7 deletions chatbot-ui/components/Page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { ReactNode } from "react";
import PageBanner from "./PageBanner";
import PageFooter from "./PageFooter";
import PageHeader from "./PageHeader";
import PageTitle from "./PageTitle";
import React, { ReactNode } from 'react';
import PageBanner from './PageBanner';
import PageFooter from './PageFooter';
import PageHeader from './PageHeader';
import PageTitle from './PageTitle';

type Props = {
title: string;
Expand All @@ -11,14 +11,16 @@ type Props = {
wide?: boolean;
};

const Page = ({ title, caption = "", children = null }: Props) => {
const Page = ({ title, caption = '', children = null }: Props) => {
return (
<>
<PageHeader />

<div className="govuk-width-container ">
<PageBanner />
<a href="#" className="govuk-back-link">Back</a>
<a href="#" className="govuk-back-link">
Back
</a>
<main className="govuk-main-wrapper" id="main-content" role="main">
<PageTitle title={title} caption={caption} />
{children}
Expand Down
6 changes: 3 additions & 3 deletions chatbot-ui/components/PageBanner.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React from 'react';

const PageBanner = () => {
return (
Expand All @@ -8,10 +8,10 @@ const PageBanner = () => {
Beta
</strong>
<span className="govuk-phase-banner__text">
This is a new service – your{" "}
This is a new service – your{' '}
<a className="govuk-link" href="#">
feedback
</a>{" "}
</a>{' '}
will help us to improve it.
</span>
</p>
Expand Down
2 changes: 1 addition & 1 deletion chatbot-ui/components/PageFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React from 'react';

interface Props {}

Expand Down
2 changes: 1 addition & 1 deletion chatbot-ui/components/PageHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React from 'react';

interface Props {}

Expand Down
2 changes: 1 addition & 1 deletion chatbot-ui/components/PageTitle.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React from 'react';

interface Props {
caption?: string;
Expand Down
7 changes: 4 additions & 3 deletions chatbot-ui/next.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/** @type {import('next').NextConfig} */

const nextConfig = {
poweredByHeader: false,
reactStrictMode: true,
swcMinify: true,
webpack(config) {
// Grab the existing rule that handles SVG imports
const fileLoaderRule = config.module.rules.find((rule) =>
rule.test?.test?.('.svg'),
)
);

config.module.rules.push(
// Reapply the existing rule, but only for svg imports ending in ?url
Expand All @@ -23,10 +24,10 @@ const nextConfig = {
resourceQuery: { not: /url/ }, // exclude if *.svg?url
use: ['@svgr/webpack'],
},
)
);

// Modify the file loader rule to ignore *.svg, since we have it handled now.
fileLoaderRule.exclude = /\.svg$/i
fileLoaderRule.exclude = /\.svg$/i;

config.experiments = { ...config.experiments, topLevelAwait: true };
return config;
Expand Down
6 changes: 2 additions & 4 deletions chatbot-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"dev": "next dev -p 3002",
"build": "next build",
"start": "next start -p 3002",
"lint": "eslint --ignore-path .gitignore \"**/*.+(ts|js|tsx)\"",
"format": "prettier --ignore-path .gitignore \"**/*.+(ts|js|tsx)\" --write"
"lint": "eslint --ignore-path .gitignore \"**/*.+(ts|js|tsx)\""
},
"dependencies": {
"classnames": "^2.3.2",
Expand All @@ -31,7 +30,6 @@
"@typescript-eslint/parser": "^6.7.3",
"eslint": "8.50.0",
"eslint-config-next": "13.5.3",
"prettier": "^3.0.0",
"typescript": "^5.0.0"
}
}
}
4 changes: 2 additions & 2 deletions chatbot-ui/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AppProps } from "next/app";
import "../styles/_all.scss";
import type { AppProps } from 'next/app';
import '../styles/_all.scss';

function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
Expand Down
2 changes: 1 addition & 1 deletion chatbot-ui/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Html, Head, Main, NextScript } from "next/document";
import { Html, Head, Main, NextScript } from 'next/document';

export default function Document() {
return (
Expand Down
77 changes: 39 additions & 38 deletions chatbot-ui/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { useRef, useState, useEffect } from "react";
import Page from "@/components/Page";
import styles from "@/styles/Home.module.css";
import ReactMarkdown from "react-markdown";
import LoadingDots from "@/components/LoadingDots";
import classNames from "classnames";
import React from "react";
import RobotIcon from "../public/assets/images/icons/robot.svg";
import UserIcon from "../public/assets/images/icons/user.svg";
import { useRef, useState, useEffect } from 'react';
import Page from '@/components/Page';
import styles from '@/styles/Home.module.css';
import ReactMarkdown from 'react-markdown';
import LoadingDots from '@/components/LoadingDots';
import classNames from 'classnames';
import React from 'react';
import RobotIcon from '../public/assets/images/icons/robot.svg';
import UserIcon from '../public/assets/images/icons/user.svg';

type Message = {
type: "apiMessage" | "userMessage";
type: 'apiMessage' | 'userMessage';
message: string;
isStreaming?: boolean;
links?: string[];
};

function Home() {
const [query, setQuery] = useState<string>("");
const [query, setQuery] = useState<string>('');
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const [messageState, setMessageState] = useState<{
Expand All @@ -27,13 +27,16 @@ function Home() {
messages: [
{
message:
"Hi, what would you like to know about the latest publications on EES?",
type: "apiMessage",
'Hi, what would you like to know about the latest publications on EES?',
type: 'apiMessage',
},
],
history: [],
});

const api_url =
process.env.NEXT_PUBLIC_CHAT_URL_API ?? 'http://localhost:8010/api/chat';

const { messages } = messageState;

const messageListRef = useRef<HTMLDivElement>(null);
Expand All @@ -48,7 +51,7 @@ function Home() {
e.preventDefault();

if (!query) {
setError("Enter a question");
setError('Enter a question');
return;
}

Expand All @@ -61,20 +64,20 @@ function Home() {
messages: [
...state.messages,
{
type: "userMessage",
type: 'userMessage',
message: question,
},
],
}));

setLoading(true);
setQuery("");
setQuery('');

try {
const response = await fetch("http://localhost:8010/api/chat", {
method: "POST",
const response = await fetch(api_url, {
method: 'POST',
headers: {
"Content-Type": "application/json",
'Content-Type': 'application/json',
},
body: JSON.stringify({
question,
Expand All @@ -88,8 +91,8 @@ function Home() {
messages: [
...state.messages,
{
type: "apiMessage",
message: "",
type: 'apiMessage',
message: '',
},
],
}));
Expand All @@ -102,7 +105,7 @@ function Home() {
const decoder = new TextDecoder();
let done = false;

let pending = "";
let pending = '';
while (!done) {
const { value, done: doneReading } = await reader.read();
done = doneReading;
Expand All @@ -129,15 +132,15 @@ function Home() {
//messageListRef.current?.scrollTo(0, messageListRef.current.scrollHeight);
} catch (error) {
setLoading(false);
setError("An error occurred while fetching the data. Please try again.");
setError('An error occurred while fetching the data. Please try again.');
}
}

//prevent empty submissions
const handleEnter = (e: any) => {
if (e.key === "Enter" && query) {
if (e.key === 'Enter' && query) {
handleSubmit(e);
} else if (e.key == "Enter") {
} else if (e.key == 'Enter') {
e.preventDefault();
}
};
Expand Down Expand Up @@ -167,36 +170,34 @@ function Home() {
</>
)}

<div className={"govuk-body"}>
<div className={'govuk-body'}>
<div className={styles.chat}>
<div ref={messageListRef} className={styles.messagelist}>
{messages.map((message, index) => {
let icon;
let className;

if (message.type === "apiMessage") {
if (message.type === 'apiMessage') {
icon = <RobotIcon height="1.1em" fill="#1d70b8" />;
className = styles.apimessage;
} else {
icon = <UserIcon height="1.1em" fill=" #00703c" />;
icon = <UserIcon height="1.1em" fill="#00703c" />;
// The latest message sent by the user will be animated while waiting for a response
className =
loading && index === messages.length - 1
? styles.usermessagewaiting
: styles.usermessage;
}
return (
<>
<div
key={`chatMessage-${index}`}
className={classNames("govuk-body", className)}
>
{icon}
<div className={styles.markdownanswer}>
<ReactMarkdown>{message.message}</ReactMarkdown>
</div>
<div
key={`chatMessage-${index}`}
className={classNames('govuk-body', className)}
>
{icon}
<div className={styles.markdownanswer}>
<ReactMarkdown>{message.message}</ReactMarkdown>
</div>
</>
</div>
);
})}
</div>
Expand Down
2 changes: 1 addition & 1 deletion chatbot-ui/styles/_all.scss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@import '~govuk-frontend/govuk/all';
@import '~govuk-frontend/govuk/all';
Loading

0 comments on commit 5aa0620

Please sign in to comment.