import React, { useEffect, useState } from "react";
import { Button, Icon } from "../../../writer";
import * as cn from "./Banner.styles";
// TODO: move to system?
export interface BannerProps {
children: React.ReactNode;
kind?: "secondary";
label?: string;
href?: string;
icon?: React.ReactNode | string;
store?: number; // seconds until banner can show again
}
export function Banner(props: BannerProps) {
const { isVisible, handleClose } = useBannerStorage(props.store, props.label);
const onCloseHandler = () => {
handleClose();
props.onClose?.();
};
if (!isVisible) {
return null;
}
if (props.kind === "secondary") {
return ;
}
let icon: React.ReactNode = null
if (props.icon) {
if (typeof props.icon === "string") {
icon =
} else {
icon = props.icon
}
}
return
{
(props.store) ? (
) : null
}
}
Banner.Secondary = function BannerSecondary(props: BannerProps & { onClose?: () => void }) {
const { isVisible, handleClose } = useBannerStorage(props.store, props.label);
const onCloseHandler = () => {
handleClose();
props.onClose?.();
};
if (!isVisible) {
return null;
}
let label: React.ReactNode = null
if (props.label) {
label =
}
return
{props.children}
{label}
}
function useBannerStorage(store?: number, label?: string) {
const [isVisible, setIsVisible] = useState(true);
useEffect(() => {
if (store) {
const storageKey = `xyd-banner-closed-${label || 'default'}`;
const closedUntil = localStorage.getItem(storageKey);
if (closedUntil) {
const now = Date.now();
const canShowAgain = parseInt(closedUntil) <= now;
setIsVisible(canShowAgain);
}
}
}, [store, label]);
const handleClose = () => {
if (store) {
const storageKey = `xyd-banner-closed-${label || 'default'}`;
const now = Date.now();
const showAgainAt = now + (store * 1000);
localStorage.setItem(storageKey, showAgainAt.toString());
}
setIsVisible(false);
};
return { isVisible, handleClose };
}
function IconClose(props: { onClick?: () => void }) {
return
}