From 07fbeb6cf0656133c4f1dfc4b74dcda5a41ca3aa Mon Sep 17 00:00:00 2001 From: Rozstone <42225395+wststone@users.noreply.github.com> Date: Tue, 27 Feb 2024 15:58:57 +0800 Subject: [PATCH] enhancement: improve client-side code (#2568) --- web/app/components/base/button/index.tsx | 14 +++++++++----- .../components/billing/billing-page/index.tsx | 18 +++++++----------- .../header/account-setting/index.tsx | 12 ++++-------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/web/app/components/base/button/index.tsx b/web/app/components/base/button/index.tsx index e617a5d12d..24d58c6ea5 100644 --- a/web/app/components/base/button/index.tsx +++ b/web/app/components/base/button/index.tsx @@ -3,13 +3,16 @@ import React from 'react' import Spinner from '../spinner' export type IButtonProps = { - type?: string + /** + * The style of the button + */ + type?: 'primary' | 'warning' | (string & {}) className?: string disabled?: boolean loading?: boolean tabIndex?: number children: React.ReactNode - onClick?: MouseEventHandler + onClick?: MouseEventHandler } const Button: FC = ({ @@ -35,15 +38,16 @@ const Button: FC = ({ } return ( -
{children} {/* Spinner is hidden when loading is false */} -
+ ) } diff --git a/web/app/components/billing/billing-page/index.tsx b/web/app/components/billing/billing-page/index.tsx index 494851ea5c..843d0995e5 100644 --- a/web/app/components/billing/billing-page/index.tsx +++ b/web/app/components/billing/billing-page/index.tsx @@ -1,7 +1,8 @@ 'use client' import type { FC } from 'react' -import React, { useEffect } from 'react' +import React from 'react' import { useTranslation } from 'react-i18next' +import useSWR from 'swr' import PlanComp from '../plan' import { ReceiptList } from '../../base/icons/src/vender/line/financeAndECommerce' import { LinkExternal01 } from '../../base/icons/src/vender/line/general' @@ -12,17 +13,11 @@ import { useProviderContext } from '@/context/provider-context' const Billing: FC = () => { const { t } = useTranslation() const { isCurrentWorkspaceManager } = useAppContext() - const [billingUrl, setBillingUrl] = React.useState('') const { enableBilling } = useProviderContext() - - useEffect(() => { - if (!enableBilling || !isCurrentWorkspaceManager) - return - (async () => { - const { url } = await fetchBillingUrl() - setBillingUrl(url) - })() - }, [isCurrentWorkspaceManager]) + const { data: billingUrl } = useSWR( + (!enableBilling || !isCurrentWorkspaceManager) ? null : ['/billing/invoices'], + () => fetchBillingUrl().then(data => data.url), + ) return (
@@ -39,4 +34,5 @@ const Billing: FC = () => {
) } + export default React.memo(Billing) diff --git a/web/app/components/header/account-setting/index.tsx b/web/app/components/header/account-setting/index.tsx index a83542ef05..d0f5db243a 100644 --- a/web/app/components/header/account-setting/index.tsx +++ b/web/app/components/header/account-setting/index.tsx @@ -138,16 +138,12 @@ export default function AccountSetting({ ] const scrollRef = useRef(null) const [scrolled, setScrolled] = useState(false) - const scrollHandle = (e: Event) => { - if ((e.target as HTMLDivElement).scrollTop > 0) - setScrolled(true) - - else - setScrolled(false) - } useEffect(() => { const targetElement = scrollRef.current - + const scrollHandle = (e: Event) => { + const userScrolled = (e.target as HTMLDivElement).scrollTop > 0 + setScrolled(userScrolled) + } targetElement?.addEventListener('scroll', scrollHandle) return () => { targetElement?.removeEventListener('scroll', scrollHandle)