quivr/frontend/lib/components/PageHeader/PageHeader.tsx
Antoine Dewez e7ce2fa54b
feat(frontend): show remaining credits (#2495)
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
2024-04-27 04:44:48 -07:00

81 lines
2.3 KiB
TypeScript

import { useEffect, useState } from "react";
import { useUserApi } from "@/lib/api/user/useUserApi";
import { useMenuContext } from "@/lib/context/MenuProvider/hooks/useMenuContext";
import { useUserSettingsContext } from "@/lib/context/UserSettingsProvider/hooks/useUserSettingsContext";
import { ButtonType } from "@/lib/types/QuivrButton";
import styles from "./PageHeader.module.scss";
import { Icon } from "../ui/Icon/Icon";
import { QuivrButton } from "../ui/QuivrButton/QuivrButton";
type Props = {
iconName: string;
label: string;
buttons: ButtonType[];
};
export const PageHeader = ({
iconName,
label,
buttons,
}: Props): JSX.Element => {
const { isOpened } = useMenuContext();
const { isDarkMode, setIsDarkMode } = useUserSettingsContext();
const [lightModeIconName, setLightModeIconName] = useState("sun");
const { remainingCredits, setRemainingCredits } = useUserSettingsContext();
const { getUserCredits } = useUserApi();
const toggleTheme = () => {
setIsDarkMode(!isDarkMode);
};
useEffect(() => {
setLightModeIconName(isDarkMode ? "sun" : "moon");
}, [isDarkMode]);
useEffect(() => {
void (async () => {
const res = await getUserCredits();
setRemainingCredits(res);
})();
}, []);
return (
<div className={styles.page_header_wrapper}>
<div className={`${styles.left} ${!isOpened ? styles.menu_closed : ""}`}>
<Icon name={iconName} size="large" color="primary" />
<span>{label}</span>
</div>
<div className={styles.buttons_wrapper}>
{buttons.map((button, index) => (
<QuivrButton
key={index}
label={button.label}
onClick={button.onClick}
color={button.color}
iconName={button.iconName}
hidden={button.hidden}
/>
))}
{remainingCredits !== null && (
<div className={styles.credits}>
<span className={styles.number}>{remainingCredits}</span>
<Icon name="coin" color="gold" size="normal"></Icon>
</div>
)}
<Icon
name={lightModeIconName}
color="black"
handleHover={true}
size="small"
onClick={toggleTheme}
/>
</div>
</div>
);
};
export default PageHeader;