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

契約書の要約ボタンをとりあえず設置 #5

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
93 changes: 93 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"eslint": "8.39.0",
"eslint-config-next": "13.3.1",
"next": "13.3.1",
"openai": "^3.2.1",
"pdfjs-dist": "^3.5.141",
"postcss": "8.4.23",
"react": "18.2.0",
Expand Down
38 changes: 38 additions & 0 deletions src/components/elements/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { FC, MouseEvent, CSSProperties } from "react";

import { ButtonColorType, ButtonVariantType, ButtonColor } from "./constants";

type PropsType = {
style?: CSSProperties;
text: string;
outline?: boolean;
disabled?: boolean;
className?: string;
color: ButtonColorType;
variant: ButtonVariantType;
onClick: (e: MouseEvent<HTMLButtonElement>) => void;
};

export const Button: FC<PropsType> = ({
style,
text,
className = "",
disabled = false,
color,
variant,
onClick,
}: PropsType) => {
return (
<button
style={style}
type="button"
disabled={disabled}
onClick={onClick}
className={`${ButtonColor[variant][color]} ${
disabled ? "cursor-not-allowed opacity-80" : "cursor-pointer"
} px-4 py-2 rounded ${className}`}
>
{text}
</button>
);
};
18 changes: 18 additions & 0 deletions src/components/elements/Button/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export type ButtonColorType = "primary" | "gray";
export type ButtonVariantType = "outlined" | "contained";

export const ButtonColor: Record<
ButtonVariantType,
Record<ButtonColorType, string>
> = {
contained: {
primary:
"text-white border border-solid bg-primary-500 hover:bg-primary-600",
gray: "text-white border border-solid bg-gray-500 hover:bg-gray-600",
},
outlined: {
primary:
"border border-solid bg-white border-primary-500 hover:bg-primary-100 text-primary-500",
gray: "border border-solid bg-white border-gray-500 hover:bg-gray-100 text-gray-600",
},
};
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { DropzoneFileField } from "./elements/DropzoneFileField/DropzoneFileField";
export { Icon } from "./elements/Icon/Icon";
export { Button } from "./elements/Button/Button";
39 changes: 33 additions & 6 deletions src/features/home/components/HomeLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import { useEffect } from "react";

import { DropzoneFileField, Icon } from "@keiyomi/components";
import { useFile, usePdfLoad } from "@keiyomi/hooks";
import { Button, DropzoneFileField, Icon } from "@keiyomi/components";
import { useContractSummaryRequest } from "@keiyomi/features/home";
import { useFile, usePdfLoad, useInput } from "@keiyomi/hooks";

export const HomeLayout = () => {
const { file, handleDropFile, fileUrl } = useFile();
const { pdfText, loadPdfUrl } = usePdfLoad();
const [{ value: summaryText }, setSummaryText] = useInput("");
const { doSummaryRequest, isChatRequesting } = useContractSummaryRequest("");

useEffect(() => {
if (fileUrl) loadPdfUrl(fileUrl);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [file]);

const handleAiRequest = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault();
doSummaryRequest({
message: "This is a contract between John and Acme.",
onSuccess: (data) => setSummaryText(data.chatMessage.content),
});
};

return (
<main
className={
Expand All @@ -37,14 +48,30 @@ export const HomeLayout = () => {
className="mx-auto"
/>
<p className="mt-2 font-semibold">
{file ? file.name : "契約書をPDFをインポート"}{" "}
{file ? file.name : "契約書をPDFをインポート"}
</p>
</div>
</DropzoneFileField>
{file && (
<p className="bg-white shadow mt-8 max-h-[60vh] overflow-y-auto mx-auto border border-solid border-gray-200 rounded p-2 whitespace-pre-wrap ">
{pdfText}
</p>
<div className="mt-8">
<div className="text-center">
<Button
text="契約書を要約"
onClick={handleAiRequest}
color="primary"
variant="outlined"
disabled={isChatRequesting}
/>
</div>
{summaryText && (
<p className="mt-8 bg-white shadow max-h-[60vh] overflow-y-auto mx-auto border border-solid border-gray-200 rounded p-2 whitespace-pre-wrap ">
{summaryText}
</p>
)}
<p className="mt-8 bg-white shadow max-h-[60vh] overflow-y-auto mx-auto border border-solid border-gray-200 rounded p-2 whitespace-pre-wrap ">
{pdfText}
</p>
</div>
)}
</div>
</main>
Expand Down
Empty file removed src/features/home/hooks/index.ts
Empty file.
31 changes: 31 additions & 0 deletions src/features/home/hooks/useContractSummaryRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { OpenAiOnSuccessType, useOpenAiRequest } from "@keiyomi/hooks";

type ApiRequestType = {
message: string;
onSuccess?: ({ data, chatMessage }: OpenAiOnSuccessType) => void;
};

type ReturnType = {
doSummaryRequest: ({ message, onSuccess }: ApiRequestType) => void;
isChatRequesting: boolean;
};

export const useContractSummaryRequest = (
currentEmployeeId: string
): ReturnType => {
const { openAiRequest, isChatRequesting } =
useOpenAiRequest(currentEmployeeId);

const doSummaryRequest = ({ message, onSuccess }: ApiRequestType) => {
openAiRequest({
prompt: message,
systemMessage: "あなたは10歳にもわかる言葉で説明するAIです。",
onSuccess,
});
};

return {
doSummaryRequest,
isChatRequesting,
};
};
1 change: 1 addition & 0 deletions src/features/home/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { HomeLayout } from "./components/HomeLayout";
export { useContractSummaryRequest } from "./hooks/useContractSummaryRequest";
4 changes: 4 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
export { useFile } from "./useFile";
export { usePdfLoad } from "./usePdfLoad";
export { useBoolean } from "./useBoolean";
export { useInput } from "./useInput";
export { useOpenAiRequest } from "./useOpenAiRequest";
export type { OpenAiOnSuccessType } from "./useOpenAiRequest";
27 changes: 27 additions & 0 deletions src/hooks/useBoolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useCallback, useState, ChangeEvent } from "react";

type ReturnType = {
isChecked: boolean;
setTrue: () => void;
setFalse: () => void;
toggle: () => void;
handleChange: (value: boolean) => void;
onChange: (e: ChangeEvent<HTMLInputElement>) => void;
};

export const useBoolean = (initial = false): ReturnType => {
const [isChecked, setIsChecked] = useState(initial);

const setTrue = useCallback(() => setIsChecked(true), []);
const setFalse = useCallback(() => setIsChecked(false), []);
const toggle = useCallback(() => setIsChecked((state) => !state), []);
const handleChange = useCallback((value: boolean) => setIsChecked(value), []);
const onChange = useCallback(
(e: ChangeEvent<HTMLInputElement>) => {
handleChange(e.target.checked);
},
[handleChange]
);

return { isChecked, setTrue, setFalse, toggle, handleChange, onChange };
};
21 changes: 21 additions & 0 deletions src/hooks/useInput.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState, useCallback, ChangeEvent } from "react";

type ReturnType = [
{
value: string;
onChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
},
(value?: string) => void
];

export const useInput = (initialValue: string): ReturnType => {
const [value, setValue] = useState(initialValue || "");
return [
{
value,
onChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) =>
setValue(e.target.value),
},
useCallback((value?: string) => setValue(value || ""), []),
];
};
Loading