{"version":3,"sources":["../src/notice.tsx"],"sourcesContent":["import type { AlertProps } from \"@yamada-ui/alert\"\nimport type {\n  CSSUIObject,\n  FC,\n  NoticeComponentProps,\n  NoticeConfigOptions,\n  NoticePlacement,\n  StyledTheme,\n} from \"@yamada-ui/core\"\nimport type { ReactNode } from \"react\"\nimport {\n  Alert,\n  AlertDescription,\n  AlertIcon,\n  AlertTitle,\n} from \"@yamada-ui/alert\"\nimport { CloseButton } from \"@yamada-ui/close-button\"\nimport { ui, useTheme } from \"@yamada-ui/core\"\nimport { cx, merge } from \"@yamada-ui/utils\"\nimport { useMemo } from \"react\"\n\nexport interface UseNoticeOptions extends NoticeConfigOptions {}\n\nexport interface NoticeOptions {\n  id: number | string\n  duration: UseNoticeOptions[\"duration\"]\n  message: (props: NoticeComponentProps) => ReactNode\n  placement: NoticePlacement\n  status: UseNoticeOptions[\"status\"]\n  onDelete: () => void\n  style?: CSSUIObject\n  isDelete?: boolean\n  onCloseComplete?: () => void\n}\n\nconst findId = (\n  options: NoticeOptions[],\n  id: number | string,\n): NoticeOptions | undefined => options.find((notice) => notice.id === id)\n\nconst findNotice = (\n  state: State,\n  id: number | string,\n): {\n  index: number\n  placement: NoticePlacement | undefined\n} => {\n  const placement = getNoticePlacement(state, id)\n\n  const index = placement\n    ? state[placement].findIndex((notice) => notice.id === id)\n    : -1\n\n  return { index, placement }\n}\n\nconst getNoticePlacement = (\n  state: State,\n  id: number | string,\n): NoticePlacement | undefined => {\n  for (const [placement, values] of Object.entries(state)) {\n    if (findId(values, id)) return placement as NoticePlacement\n  }\n}\n\ninterface CreateNoticeOptions\n  extends Partial<\n    Pick<\n      NoticeOptions,\n      \"duration\" | \"id\" | \"onCloseComplete\" | \"placement\" | \"status\" | \"style\"\n    >\n  > {}\n\nlet counter = 0\n\nconst createNotice = (\n  message: (props: NoticeComponentProps) => ReactNode,\n  {\n    id,\n    style,\n    duration,\n    placement = \"top\",\n    status,\n    onCloseComplete,\n  }: CreateNoticeOptions,\n) => {\n  counter += 1\n\n  id ??= counter\n\n  return {\n    id,\n    style,\n    duration,\n    isDelete: false,\n    message,\n    placement,\n    status,\n    onCloseComplete,\n    onDelete: () => noticeStore.remove(String(id), placement),\n  }\n}\n\nconst createRender = (options: UseNoticeOptions): FC<NoticeComponentProps> => {\n  const { component } = options\n\n  const Render: FC<NoticeComponentProps> = (props) => {\n    if (typeof component === \"function\") {\n      return component({ ...props, ...options })\n    } else {\n      return <Notice {...props} {...options} />\n    }\n  }\n\n  return Render\n}\n\nconst createNoticeFunc = (\n  defaultOptions: UseNoticeOptions,\n  theme: StyledTheme,\n) => {\n  const themeOptions = theme.__config?.notice?.options ?? {}\n\n  const computedOptions = (options: UseNoticeOptions) =>\n    merge(themeOptions, merge(defaultOptions, options))\n\n  const notice = (options: UseNoticeOptions = {}) => {\n    options = computedOptions(options)\n\n    const message = createRender(options)\n\n    return noticeStore.create(message, options)\n  }\n\n  notice.update = (\n    id: number | string,\n    options: Omit<UseNoticeOptions, \"id\">,\n  ) => {\n    options = computedOptions(options)\n\n    noticeStore.update(id, options)\n  }\n\n  notice.closeAll = noticeStore.closeAll\n  notice.close = noticeStore.close\n  notice.isActive = noticeStore.isActive\n\n  return notice\n}\n\ntype CreateNoticeReturn = ReturnType<typeof createNoticeFunc>\n\n/**\n * `useNotice` is a custom hook that controls the notifications of the application.\n *\n * @see Docs https://yamada-ui.com/hooks/use-notice\n */\nexport const useNotice = (\n  defaultOptions?: UseNoticeOptions,\n): CreateNoticeReturn => {\n  const { theme } = useTheme()\n\n  return useMemo(\n    () => createNoticeFunc(defaultOptions ?? {}, theme),\n    [defaultOptions, theme],\n  )\n}\n\ntype State = {\n  [K in NoticePlacement]: NoticeOptions[]\n}\n\ninterface Store {\n  close: (id: number | string) => void\n  closeAll: (options?: { placement?: NoticePlacement[] }) => void\n  create: (\n    message: (props: NoticeComponentProps) => ReactNode,\n    options: UseNoticeOptions,\n  ) => number | string\n  getSnapshot: () => State\n  isActive: (id: number | string) => boolean\n  remove: (id: number | string, placement: NoticePlacement) => void\n  subscribe: (onStoreChange: () => void) => () => void\n  update: (id: number | string, options: Omit<UseNoticeOptions, \"id\">) => void\n}\n\nconst initialState = {\n  bottom: [],\n  \"bottom-left\": [],\n  \"bottom-right\": [],\n  top: [],\n  \"top-left\": [],\n  \"top-right\": [],\n}\n\nconst createNoticeStore = (initialState: State): Store => {\n  let state = initialState\n  const storeChangeCache = new Set<() => void>()\n\n  const setState = (setStateFunc: (values: State) => State) => {\n    state = setStateFunc(state)\n    storeChangeCache.forEach((onStoreChange) => onStoreChange())\n  }\n\n  return {\n    close: (id) => {\n      setState((prev) => {\n        const placement = getNoticePlacement(prev, id)\n\n        if (!placement) return prev\n\n        return {\n          ...prev,\n          [placement]: prev[placement].map((notice) =>\n            notice.id == id ? { ...notice, isDelete: true } : notice,\n          ),\n        }\n      })\n    },\n\n    closeAll: ({ placement } = {}) => {\n      setState((prev) => {\n        let placements: NoticePlacement[] = [\n          \"bottom\",\n          \"bottom-right\",\n          \"bottom-left\",\n          \"top\",\n          \"top-left\",\n          \"top-right\",\n        ]\n\n        if (placement) placements = placement\n\n        return placements.reduce(\n          (acc, placement) => {\n            acc[placement] = prev[placement].map((notice) => ({\n              ...notice,\n              isDelete: true,\n            }))\n\n            return acc\n          },\n          { ...prev },\n        )\n      })\n    },\n\n    create: (message, options) => {\n      const limit = options.limit\n\n      const notice = createNotice(message, options)\n      const { id, placement } = notice\n\n      setState((prev) => {\n        let prevNotices = prev[placement]\n\n        if (\n          limit !== undefined &&\n          limit > 0 &&\n          prevNotices.length > limit - 1\n        ) {\n          const n = prevNotices.length - (limit - 1)\n          const notices = placement.includes(\"top\")\n            ? prevNotices.slice(n * -1)\n            : prevNotices.slice(0, n)\n\n          const ids = notices.map(({ id }) => id)\n\n          prevNotices = prevNotices.map((notice) =>\n            ids.includes(notice.id) ? { ...notice, isDelete: true } : notice,\n          )\n        }\n\n        const notices = placement.includes(\"top\")\n          ? [notice, ...prevNotices]\n          : [...prevNotices, notice]\n\n        return { ...prev, [placement]: notices }\n      })\n\n      return id\n    },\n\n    getSnapshot: () => state,\n\n    isActive: (id) =>\n      Boolean(findNotice(noticeStore.getSnapshot(), id).placement),\n\n    remove: (id, placement) => {\n      setState((prevState) => ({\n        ...prevState,\n        [placement]: prevState[placement].filter((notice) => notice.id != id),\n      }))\n    },\n\n    subscribe: (onStoreChange) => {\n      storeChangeCache.add(onStoreChange)\n\n      return () => {\n        setState(() => initialState)\n        storeChangeCache.delete(onStoreChange)\n      }\n    },\n\n    update: (id, options) => {\n      setState((prev) => {\n        const next = { ...prev }\n        const { index, placement } = findNotice(next, id)\n\n        if (placement && index !== -1 && next[placement][index]) {\n          next[placement][index] = {\n            ...next[placement][index],\n            ...options,\n            message: createRender(options),\n          }\n        }\n\n        return next\n      })\n    },\n  }\n}\n\nexport const noticeStore = createNoticeStore(initialState)\n\nexport interface NoticeProps\n  extends Omit<AlertProps, keyof UseNoticeOptions>,\n    UseNoticeOptions {\n  onClose?: () => void\n}\n\nconst Notice: FC<NoticeProps> = ({\n  className,\n  colorScheme,\n  variant = \"basic\",\n  closeStrategy = \"button\",\n  description,\n  icon,\n  isClosable,\n  status,\n  title,\n  onClose,\n}) => {\n  const isButtonClosable =\n    isClosable && (closeStrategy === \"button\" || closeStrategy === \"both\")\n  const isElementClosable =\n    isClosable && (closeStrategy === \"element\" || closeStrategy === \"both\")\n\n  return (\n    <Alert\n      className={cx(\"ui-notice\", className)}\n      colorScheme={colorScheme}\n      variant={variant}\n      alignItems=\"start\"\n      boxShadow=\"fallback(lg, 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05))\"\n      pe={isButtonClosable ? 8 : undefined}\n      status={status}\n      onClick={isElementClosable ? onClose : undefined}\n    >\n      <AlertIcon\n        className=\"ui-notice__icon\"\n        variant={icon?.variant}\n        {...(icon?.color ? { color: icon.color } : {})}\n      >\n        {icon?.children}\n      </AlertIcon>\n\n      <ui.div flex=\"1\">\n        {title ? (\n          <AlertTitle className=\"ui-notice__title\" lineClamp={1}>\n            {title}\n          </AlertTitle>\n        ) : null}\n        {description ? (\n          <AlertDescription className=\"ui-notice__desc\" lineClamp={3}>\n            {description}\n          </AlertDescription>\n        ) : null}\n      </ui.div>\n\n      {isButtonClosable ? (\n        <CloseButton\n          className=\"ui-notice__close-button\"\n          size=\"sm\"\n          position=\"absolute\"\n          right={2}\n          top={2}\n          onClick={(ev) => {\n            ev.stopPropagation()\n\n            onClose?.()\n          }}\n        />\n      ) : null}\n    </Alert>\n  )\n}\n\nNotice.displayName = \"Notice\"\nNotice.__ui__ = \"Notice\"\n"],"mappings":";;;AAUA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,mBAAmB;AAC5B,SAAS,IAAI,gBAAgB;AAC7B,SAAS,IAAI,aAAa;AAC1B,SAAS,eAAe;AA2FX,cAiQP,YAjQO;AA3Eb,IAAM,SAAS,CACb,SACA,OAC8B,QAAQ,KAAK,CAAC,WAAW,OAAO,OAAO,EAAE;AAEzE,IAAM,aAAa,CACjB,OACA,OAIG;AACH,QAAM,YAAY,mBAAmB,OAAO,EAAE;AAE9C,QAAM,QAAQ,YACV,MAAM,SAAS,EAAE,UAAU,CAAC,WAAW,OAAO,OAAO,EAAE,IACvD;AAEJ,SAAO,EAAE,OAAO,UAAU;AAC5B;AAEA,IAAM,qBAAqB,CACzB,OACA,OACgC;AAChC,aAAW,CAAC,WAAW,MAAM,KAAK,OAAO,QAAQ,KAAK,GAAG;AACvD,QAAI,OAAO,QAAQ,EAAE,EAAG,QAAO;AAAA,EACjC;AACF;AAUA,IAAI,UAAU;AAEd,IAAM,eAAe,CACnB,SACA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA,YAAY;AAAA,EACZ;AAAA,EACA;AACF,MACG;AACH,aAAW;AAEX,yBAAO;AAEP,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA,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