Skip to content

Commit

Permalink
refactor: replacing mobx with jotai (janhq#160)
Browse files Browse the repository at this point in the history
* refactor: replacing mobx with jotai

Signed-off-by: James <[email protected]>
Co-authored-by: James <[email protected]>
Co-authored-by: Louis <[email protected]>
  • Loading branch information
3 people authored Sep 14, 2023
1 parent cc39664 commit d55a838
Show file tree
Hide file tree
Showing 115 changed files with 3,755 additions and 7,722 deletions.
3 changes: 1 addition & 2 deletions conf/sample.env_web-client
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ NEXT_PUBLIC_DOWNLOAD_APP_IOS=#
NEXT_PUBLIC_DOWNLOAD_APP_ANDROID=#
NEXT_PUBLIC_GRAPHQL_ENGINE_URL=http://localhost:8080/v1/graphql
NEXT_PUBLIC_GRAPHQL_ENGINE_WEB_SOCKET_URL=ws://localhost:8080/v1/graphql
OPENAPI_ENDPOINT=http://host.docker.internal:8000/v1
OPENAPI_KEY=openapikey
NEXT_PUBLIC_OPENAPI_ENDPOINT=http://localhost:8000/v1/chat/completions
KEYCLOAK_CLIENT_ID=hasura
KEYCLOAK_CLIENT_SECRET=oMtCPAV7diKpE564SBspgKj4HqlKM4Hy
AUTH_ISSUER=http://localhost:8088/realms/$KEYCLOAK_CLIENT_ID
Expand Down
4 changes: 1 addition & 3 deletions web-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ Replace above configuration with your actual infrastructure.
| [@tailwindcss/typography](https://tailwindcss.com/docs/typography-plugin) | UI | ^0.5.9 |
| [embla-carousel](https://www.embla-carousel.com/) | UI | ^8.0.0-rc11 |
| [@apollo/client](https://www.apollographql.com/docs/react/) | State management | ^3.8.1 |
| [mobx](https://mobx.js.org/README.html) | State management | ^6.10.0 |
| [mobx-react-lite](https://www.npmjs.com/package/mobx-react-lite) | State management | ^4.0.3 |
| [mobx-state-tree](https://mobx-state-tree.js.org/) | State management | ^5.1.8 |
| [jotai](https://jotai.org/) | State management | ^2.4.0 |


## Deploy to Netlify
Expand Down
98 changes: 29 additions & 69 deletions web-client/app/_components/AdvancedPrompt/index.tsx
Original file line number Diff line number Diff line change
@@ -1,69 +1,29 @@
"use client";
import { useCallback } from "react";
import Image from "next/image";
import { useStore } from "@/_models/RootStore";
import { observer } from "mobx-react-lite";
import { MenuAdvancedPrompt } from "../MenuAdvancedPrompt";
import { useForm } from "react-hook-form";
import { useMutation } from "@apollo/client";
import { CreateMessageDocument, CreateMessageMutation } from "@/graphql";

export const AdvancedPrompt: React.FC = observer(() => {
const { register, handleSubmit } = useForm();
const { historyStore } = useStore();

const onAdvancedPrompt = useCallback(() => {
historyStore.toggleAdvancedPrompt();
}, []);

const [createMessageMutation] = useMutation<CreateMessageMutation>(
CreateMessageDocument
);
const onSubmit = (data: any) => {
historyStore.sendControlNetPrompt(
createMessageMutation,
data.prompt,
data.negativePrompt,
data.fileInput[0]
);
};

return (
<form
className={`${
historyStore.showAdvancedPrompt ? "w-[288px]" : "hidden"
} h-screen flex flex-col border-r border-gray-200`}
onSubmit={handleSubmit(onSubmit)}
>
<button
onClick={onAdvancedPrompt}
className="flex items-center mx-2 mt-3 mb-[10px] flex-none gap-1 text-xs leading-[18px] text-[#6B7280]"
>
<Image src={"/icons/chevron-left.svg"} width={20} height={20} alt="" />
<span className="font-semibold text-gray-500 text-xs">
BASIC PROMPT
</span>
</button>
<div className="flex flex-col justify-start flex-1 p-3 gap-[10px] overflow-x-hidden scroll">
<MenuAdvancedPrompt register={register} />
</div>
<div className="py-3 px-2 flex flex-none gap-3 items-center justify-between border-t border-gray-200">
<button className="w-1/2 flex items-center text-gray-900 py-2 px-3 rounded-lg gap-1 justify-center bg-gray-100 text-sm leading-5">
<Image
src={"/icons/unicorn_arrow-random.svg"}
width={16}
height={16}
alt=""
/>
Random
</button>
<button
className="w-1/2 flex items-center text-gray-900 justify-center py-2 px-3 rounded-lg gap-1 bg-yellow-300 text-sm leading-5"
onClick={(e) => handleSubmit(onSubmit)(e)}
>
Generate
</button>
</div>
</form>
);
});
import { MenuAdvancedPrompt } from "../MenuAdvancedPrompt";
import { useForm } from "react-hook-form";
import BasicPromptButton from "../BasicPromptButton";
import PrimaryButton from "../PrimaryButton";

const AdvancedPrompt: React.FC = () => {
const { register, handleSubmit } = useForm();

const onSubmit = (data: any) => {};

return (
<form
className="w-[288px] h-screen flex flex-col border-r border-gray-200"
onSubmit={handleSubmit(onSubmit)}
>
<BasicPromptButton />
<MenuAdvancedPrompt register={register} />
<div className="py-3 px-2 flex flex-none gap-3 items-center justify-between border-t border-gray-200">
<PrimaryButton
fullWidth={true}
title="Generate"
onClick={() => handleSubmit(onSubmit)}
/>
</div>
</form>
);
};

export default AdvancedPrompt;
54 changes: 27 additions & 27 deletions web-client/app/_components/AdvancedTextArea/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { FieldValues, UseFormRegister } from "react-hook-form";

type Props = {
formId?: string;
height: number;
title: string;
placeholder: string;
register: UseFormRegister<FieldValues>;
};

export const AdvancedTextArea: React.FC<Props> = ({
formId = "",
height,
placeholder,
title,
register,
}) => (
<div className="w-full flex flex-col pt-3 gap-1">
<label className="text-sm leading-5 text-gray-800">{title}</label>
<textarea
style={{ height: `${height}px` }}
className="rounded-lg py-[13px] px-5 border outline-none resize-none border-gray-300 bg-gray-50 placeholder:gray-400 text-sm font-normal"
placeholder={placeholder}
{...register(formId, { required: formId === "prompt" ? true : false })}
/>
</div>
);
import { FieldValues, UseFormRegister } from "react-hook-form";

type Props = {
formId?: string;
height: number;
title: string;
placeholder: string;
register: UseFormRegister<FieldValues>;
};

export const AdvancedTextArea: React.FC<Props> = ({
formId = "",
height,
placeholder,
title,
register,
}) => (
<div className="w-full flex flex-col pt-3 gap-1">
<label className="text-sm leading-5 text-gray-800">{title}</label>
<textarea
style={{ height: `${height}px` }}
className="rounded-lg py-[13px] px-5 border outline-none resize-none border-gray-300 bg-gray-50 placeholder:gray-400 text-sm font-normal"
placeholder={placeholder}
{...register(formId, { required: formId === "prompt" ? true : false })}
/>
</div>
);
156 changes: 78 additions & 78 deletions web-client/app/_components/ApiPane/index.tsx
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
import Image from "next/image";
import { useState } from "react";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import js from "react-syntax-highlighter/dist/esm/languages/hljs/javascript";
import { atomOneDark } from "react-syntax-highlighter/dist/esm/styles/hljs";
import useGetModelApiInfo from "@/_hooks/useGetModelApiInfo";

SyntaxHighlighter.registerLanguage("javascript", js);

const ApiPane: React.FC = () => {
const [expend, setExpend] = useState(true);
const { data } = useGetModelApiInfo();
const [highlightCode, setHighlightCode] = useState(data[0]);

return (
<div className="h-full flex flex-col relative">
<div className="absolute top-0 left-0 h-full w-full overflow-x-hidden scroll">
<button
onClick={() => setExpend(!expend)}
className="flex items-center flex-none"
>
<Image
src={"/icons/unicorn_angle-down.svg"}
width={24}
height={24}
alt=""
/>
<span>Request</span>
</button>
<div
className={`${
expend ? "block" : "hidden"
} bg-[#1F2A37] rounded-lg w-full flex-1`}
>
<div className="p-2 flex justify-between flex-1">
<div className="flex">
{data.map((item, index) => (
<button
className={`py-1 text-xs text-[#9CA3AF] px-2 flex gap-[10px] rounded ${
highlightCode?.type === item.type
? "bg-[#374151] text-white"
: ""
}`}
key={index}
onClick={() => setHighlightCode(item)}
>
{item.type}
</button>
))}
</div>
<button
onClick={() =>
navigator.clipboard.writeText(highlightCode?.stringCode)
}
>
<Image
src={"/icons/unicorn_clipboard-alt.svg"}
width={24}
height={24}
alt=""
/>
</button>
</div>
<SyntaxHighlighter
className="w-full bg-transparent overflow-x-hidden scroll resize-none"
language="jsx"
style={atomOneDark}
customStyle={{ padding: "12px", background: "transparent" }}
wrapLongLines={true}
>
{highlightCode?.stringCode}
</SyntaxHighlighter>
</div>
</div>
</div>
);
};

import Image from "next/image";
import { useState } from "react";
import { Light as SyntaxHighlighter } from "react-syntax-highlighter";
import js from "react-syntax-highlighter/dist/esm/languages/hljs/javascript";
import { atomOneDark } from "react-syntax-highlighter/dist/esm/styles/hljs";
import useGetModelApiInfo from "@/_hooks/useGetModelApiInfo";

SyntaxHighlighter.registerLanguage("javascript", js);

const ApiPane: React.FC = () => {
const [expend, setExpend] = useState(true);
const { data } = useGetModelApiInfo();
const [highlightCode, setHighlightCode] = useState(data[0]);

return (
<div className="h-full flex flex-col relative">
<div className="absolute top-0 left-0 h-full w-full overflow-x-hidden scroll">
<button
onClick={() => setExpend(!expend)}
className="flex items-center flex-none"
>
<Image
src={"/icons/unicorn_angle-down.svg"}
width={24}
height={24}
alt=""
/>
<span>Request</span>
</button>
<div
className={`${
expend ? "block" : "hidden"
} bg-[#1F2A37] rounded-lg w-full flex-1`}
>
<div className="p-2 flex justify-between flex-1">
<div className="flex">
{data.map((item, index) => (
<button
className={`py-1 text-xs text-[#9CA3AF] px-2 flex gap-[10px] rounded ${
highlightCode?.type === item.type
? "bg-[#374151] text-white"
: ""
}`}
key={index}
onClick={() => setHighlightCode(item)}
>
{item.type}
</button>
))}
</div>
<button
onClick={() =>
navigator.clipboard.writeText(highlightCode?.stringCode)
}
>
<Image
src={"/icons/unicorn_clipboard-alt.svg"}
width={24}
height={24}
alt=""
/>
</button>
</div>
<SyntaxHighlighter
className="w-full bg-transparent overflow-x-hidden scroll resize-none"
language="jsx"
style={atomOneDark}
customStyle={{ padding: "12px", background: "transparent" }}
wrapLongLines={true}
>
{highlightCode?.stringCode}
</SyntaxHighlighter>
</div>
</div>
</div>
);
};

export default ApiPane;
30 changes: 15 additions & 15 deletions web-client/app/_components/ApiStep/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
type Props = {
title: string;
description: string;
};

export const ApiStep: React.FC<Props> = ({ description, title }) => {
return (
<div className="gap-2 flex flex-col">
<span className="text-[#8A8A8A]">{title}</span>
<div className="flex flex-col gap-[10px] p-[18px] bg-[#F9F9F9] overflow-y-hidden">
<pre className="text-sm leading-5 text-black">{description}</pre>
</div>
</div>
);
};
type Props = {
title: string;
description: string;
};

export const ApiStep: React.FC<Props> = ({ description, title }) => {
return (
<div className="gap-2 flex flex-col">
<span className="text-[#8A8A8A]">{title}</span>
<div className="flex flex-col gap-[10px] p-[18px] bg-[#F9F9F9] overflow-y-hidden">
<pre className="text-sm leading-5 text-black">{description}</pre>
</div>
</div>
);
};
Loading

0 comments on commit d55a838

Please sign in to comment.