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

Ask-AI feature without Logo and NativeLanguage page #19

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Dev
  • Loading branch information
kitanoyoru committed Dec 15, 2023
commit d43cd38b42a489db84cd58fdcc671135f63bdc29
5 changes: 2 additions & 3 deletions src/api/requests/getDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const getWhatIsGraph = async () => {
};

export const getWhatIsSingleton = async () => {
const { singleton } = await scUtils.findKeynodes('singleton');
return getDescriptionByAddr(singleton.value);
const { singletone } = await scUtils.findKeynodes('singletone');
return getDescriptionByAddr(singletone.value);
};

export const getWhatIsSetTheory = async () => {
Expand All @@ -56,7 +56,6 @@ export const getDescriptionByAddr = async (elementAddr: number) => {
};

export const getDescriptionById = async (id: string, lang: TLanguage) => {
console.log(id, lang);
const { uiMenuSummary } = await scUtils.findKeynodes('ui_menu_summary');

const elementAddr = await searchAddrById(id);
Expand Down
26 changes: 22 additions & 4 deletions src/components/AskAnswer/AskAnswer.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import axios from 'axios';

import { useTranslate } from 'ostis-ui-lib';
import { useToast, useTranslate } from 'ostis-ui-lib';

import { useLocation } from 'react-router-dom';
import { ChangeEvent, useEffect, useReducer, useState, useRef } from 'react';
import { ChangeEvent, useEffect, useReducer, useState, useRef, useCallback } from 'react';

import { Spinner, useLanguage } from 'ostis-ui-lib';
import { SPINER_COLOR } from '@constants';
Expand All @@ -16,6 +14,7 @@ import { getHintButtonHandler } from 'src/constants/hintButtons';
import { getDescriptionById } from '@api/requests/getDescription';
import { useDispatch, useSelector } from 'react-redux';
import { addInHistory, selectRequests } from '@store/requestDialogHistorySlice';
import { Notification } from '@components/Notification';

interface NavigateState {
query?: string;
Expand All @@ -34,6 +33,8 @@ export const AskAnswer = () => {

const [query, setQuery] = useState<string>();

const { addToast } = useToast();

const scrollRef = useRef<HTMLDivElement>(null);

const translate = useTranslate();
Expand Down Expand Up @@ -88,6 +89,22 @@ export const AskAnswer = () => {
}
};

const onEmptySubmit = useCallback(() => {
addToast(
<Notification
type="warning"
title={{
ru: 'Вы не можете отправить пустой запрос',
en: `It's impossible to save an empty fragment`,
}}
/>,
{
position: 'bottomRight',
duration: 2000,
},
);
}, [addToast]);

if (!state) return null;

if (isLoading) return <Spinner className={styles.spinner} appearance={SPINER_COLOR} />;
Expand All @@ -104,6 +121,7 @@ export const AskAnswer = () => {
className={styles.inputQueryBar}
onChange={onInputChange}
onSubmit={onInputSubmit}
onEmptySubmit={onEmptySubmit}
/>
</div>
);
Expand Down
28 changes: 16 additions & 12 deletions src/components/AskInput/AskInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { refSetter, useTranslate } from 'ostis-ui-lib';
interface IProps
extends Omit<InputHTMLAttributes<HTMLInputElement>, 'onChange' | 'onFocus' | 'onBlur'> {
className?: string;
onEmptySubmit?: () => void;
onEmptySubmit: () => void;
onSubmit: () => void;
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
onFocus?: (e: React.FocusEvent<HTMLDivElement>) => void;
Expand All @@ -27,7 +27,15 @@ interface IProps

export const AskInput = forwardRef<HTMLInputElement, IProps>(
(
{ className, onChange, onSubmit, onFocus: onFocusFromProps, onBlur: onBlurFromProps, ...props },
{
className,
onChange,
onSubmit,
onEmptySubmit,
onFocus: onFocusFromProps,
onBlur: onBlurFromProps,
...props
},
ref,
) => {
const [searchValue, setSearchValue] = useState('');
Expand All @@ -42,16 +50,15 @@ export const AskInput = forwardRef<HTMLInputElement, IProps>(
};

const onInputChange = (e: ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
setSearchValue(e.currentTarget.value);
onChange(e);
};

const handleKeyUp = (e: KeyboardEvent<HTMLInputElement>) => {
const code = e.code;

switch (code) {
case 'Enter': {
onSubmit();
handleSubmit();
break;
}
}
Expand All @@ -73,7 +80,6 @@ export const AskInput = forwardRef<HTMLInputElement, IProps>(

const onBlur = (e: React.FocusEvent<HTMLInputElement>) => {
setIsFocused(false);

if (e.currentTarget.contains(e.relatedTarget)) {
onBlurFromProps?.(e);
}
Expand All @@ -86,11 +92,6 @@ export const AskInput = forwardRef<HTMLInputElement, IProps>(
);
});

const emptyMessage = translate({
ru: '🪄 Спросите IMS',
en: '🪄 Ask IMS',
});

const handleSubmit = () => {
onSubmit();
resetInput();
Expand All @@ -111,7 +112,10 @@ export const AskInput = forwardRef<HTMLInputElement, IProps>(
>
<input
className={styles.dialogInput}
placeholder={emptyMessage}
placeholder={translate({
ru: '🪄 Спросите IMS',
en: '🪄 Ask IMS',
})}
ref={refSetter<HTMLInputElement>(ref, innerRef)}
value={searchValue}
onKeyDown={onInputKeyDown}
Expand Down
11 changes: 8 additions & 3 deletions src/constants/hintButtons.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { getHistoryOfIMS, getWhatIsGraph, getWhatIsIMS } from '@api/requests/getDescription';
import {
getHistoryOfIMS,
getWhatIsGraph,
getWhatIsIMS,
getWhatIsSingleton,
} from '@api/requests/getDescription';
import { TLanguage, TTexts } from 'ostis-ui-lib';

export const hintButtons: { text: TTexts; handler: () => Promise<string | null> }[] = [
{
text: { ru: 'Что ты можешь?', en: 'What can you do?' },
handler: () => Promise.resolve(null),
text: { ru: 'Что такое синглтон?', en: 'What is singleton?' },
handler: getWhatIsSingleton,
},
{
text: { ru: 'Что такое IMS?', en: 'What is IMS' },
Expand Down