Skip to content

Commit

Permalink
fix: reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
shafin-deriv committed Sep 4, 2023
1 parent 934f354 commit e743e5e
Showing 1 changed file with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ const FAQ = ({ type, content, src }: TDescription) => {
);
};

const scrollToElement = (wrapper_element: HTMLElement, offset: number) => {
if (wrapper_element) {
wrapper_element.scrollTo({
top: offset,
behavior: 'smooth',
});
}
};

const FAQContent = observer(({ faq_list, hide_header = false }: TFAQContent) => {
const { dashboard } = useDBotStore();
const { faq_search_value } = dashboard;
Expand All @@ -43,21 +52,12 @@ const FAQContent = observer(({ faq_list, hide_header = false }: TFAQContent) =>
// Scroll to the top of the open accordion item.
// Need timer to first close the accordion item then scroll the new item to top.
timer_id.current = setTimeout(() => {
if (faq_wrapper_element?.current) {
const open_accordion_element: HTMLElement | null =
faq_wrapper_element?.current?.querySelector('.dc-accordion__item--open');
const previous_sibling_element = open_accordion_element?.previousElementSibling as HTMLElement;
if (previous_sibling_element) {
faq_wrapper_element.current.scrollTo({
top: previous_sibling_element.offsetTop - 80,
behavior: 'smooth',
});
} else if (open_accordion_element) {
faq_wrapper_element.current.scrollTo({
top: 0,
behavior: 'smooth',
});
}
const open_accordion_element: HTMLElement | null | undefined =
faq_wrapper_element.current?.querySelector('.dc-accordion__item--open');
const previous_sibling_element = open_accordion_element?.previousElementSibling as HTMLElement;
if (faq_wrapper_element?.current && open_accordion_element) {
const offset = previous_sibling_element ? previous_sibling_element.offsetTop - 80 : 0;
scrollToElement(faq_wrapper_element?.current, offset);
}
if (timer_id?.current) clearTimeout(timer_id.current);
}, 5);
Expand Down

0 comments on commit e743e5e

Please sign in to comment.