-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): display alerts for invalid licenses (#3351)
* feat(ui): display alerts for invalid licenses * update * [autofix.ci] apply automated fixes * update: seats exceeded * update: license banner * [autofix.ci] apply automated fixes * Update ee/tabby-ui/lib/hooks/use-license.ts * Apply suggestions from code review --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Meng Zhang <[email protected]>
- Loading branch information
1 parent
48419e6
commit 44d65fb
Showing
10 changed files
with
327 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
'use client' | ||
|
||
import React, { useMemo } from 'react' | ||
import Link from 'next/link' | ||
import { usePathname } from 'next/navigation' | ||
|
||
import { useLicenseValidity } from '@/lib/hooks/use-license' | ||
import { cn } from '@/lib/utils' | ||
import { IconClose, IconNotice } from '@/components/ui/icons' | ||
|
||
import { buttonVariants } from './ui/button' | ||
|
||
export const BANNER_HEIGHT = '3.5rem' | ||
|
||
interface ShowLicenseBannerContextValue { | ||
isShowLicenseBanner: boolean | ||
setIsShowLicenseBanner: React.Dispatch<React.SetStateAction<boolean>> | ||
} | ||
|
||
const ShowLicenseBannerContext = | ||
React.createContext<ShowLicenseBannerContextValue>( | ||
{} as ShowLicenseBannerContextValue | ||
) | ||
|
||
export const ShowLicenseBannerProvider = ({ | ||
children | ||
}: { | ||
children: React.ReactNode | ||
}) => { | ||
const { isExpired, isSeatsExceeded, isLicenseOK } = useLicenseValidity() | ||
|
||
const [isShowLicenseBanner, setIsShowLicenseBanner] = React.useState(false) | ||
|
||
React.useEffect(() => { | ||
const isInIframe = window.self !== window.top | ||
if (isInIframe) return | ||
|
||
if (isExpired || isSeatsExceeded) { | ||
setIsShowLicenseBanner(true) | ||
} else if (isLicenseOK) { | ||
setIsShowLicenseBanner(false) | ||
} | ||
}, [isLicenseOK, isExpired, isSeatsExceeded]) | ||
|
||
return ( | ||
<ShowLicenseBannerContext.Provider | ||
value={{ isShowLicenseBanner, setIsShowLicenseBanner }} | ||
> | ||
{children} | ||
</ShowLicenseBannerContext.Provider> | ||
) | ||
} | ||
|
||
export function useShowLicenseBanner(): [ | ||
boolean, | ||
React.Dispatch<React.SetStateAction<boolean>> | ||
] { | ||
const { isShowLicenseBanner, setIsShowLicenseBanner } = React.useContext( | ||
ShowLicenseBannerContext | ||
) | ||
return [isShowLicenseBanner, setIsShowLicenseBanner] | ||
} | ||
|
||
export function LicenseBanner() { | ||
const [isShowLicenseBanner, setIsShowLicenseBanner] = useShowLicenseBanner() | ||
const { isExpired, isSeatsExceeded } = useLicenseValidity() | ||
const pathname = usePathname() | ||
const style = isShowLicenseBanner ? { height: BANNER_HEIGHT } : { height: 0 } | ||
|
||
const tips = useMemo(() => { | ||
if (isExpired) { | ||
return 'Your subscription is expired.' | ||
} | ||
|
||
if (isSeatsExceeded) { | ||
return 'You have more active users than seats included in your subscription.' | ||
} | ||
|
||
return 'No valid license configured' | ||
}, [isExpired, isSeatsExceeded]) | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
'flex items-center justify-between border-b bg-secondary px-4 text-secondary-foreground transition-all md:px-5', | ||
{ | ||
'opacity-100 pointer-events-auto': isShowLicenseBanner, | ||
'opacity-0 pointer-events-none': !isShowLicenseBanner | ||
} | ||
)} | ||
style={style} | ||
> | ||
<div className="flex items-center gap-1 font-semibold text-destructive"> | ||
<IconNotice /> | ||
{tips} | ||
</div> | ||
|
||
<div className="flex items-center gap-x-4 md:gap-x-8"> | ||
{pathname !== '/settings/subscription' && ( | ||
<Link | ||
href="/settings/subscription" | ||
className={cn(buttonVariants(), 'gap-1')} | ||
> | ||
See more | ||
</Link> | ||
)} | ||
|
||
<IconClose | ||
className="cursor-pointer transition-all hover:opacity-70" | ||
onClick={() => setIsShowLicenseBanner(false)} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.