UNPKG

@awsui/components-react

Version:

On July 19th, 2022, we launched [Cloudscape Design System](https://cloudscape.design). Cloudscape is an evolution of AWS-UI. It consists of user interface guidelines, front-end components, design resources, and development tools for building intuitive, en

72 lines 3.98 kB
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 import { useRef, useState } from 'react'; import { fireNonCancelableEvent } from '../../../internal/events'; import { getLimitedValue } from '../../../split-panel/utils/size-utils'; import { mapRuntimeConfigToAiDrawer } from '../../runtime-drawer'; const DEFAULT_ON_CHANGE_PARAMS = { initiatedByUserAction: true }; const MIN_DRAWER_SIZE = 400; export function useAiDrawer({ onAiDrawerFocus, expandedDrawerId, setExpandedDrawerId, getMaxAiDrawerSize, }) { var _a, _b; const [runtimeDrawer, setRuntimeDrawer] = useState(null); const [activeAiDrawerId, setActiveAiDrawerId] = useState(null); const [size, setSize] = useState(null); const aiDrawerWasOpenRef = useRef(false); aiDrawerWasOpenRef.current = aiDrawerWasOpenRef.current || !!activeAiDrawerId; function onActiveAiDrawerResize(size) { const limitedSize = getLimitedValue(minAiDrawerSize, size, getMaxAiDrawerSize()); setSize(limitedSize); fireNonCancelableEvent(activeAiDrawer === null || activeAiDrawer === void 0 ? void 0 : activeAiDrawer.onResize, { id: activeAiDrawerId, size: limitedSize }); } function onActiveAiDrawerChange(newDrawerId, { initiatedByUserAction } = DEFAULT_ON_CHANGE_PARAMS) { setActiveAiDrawerId(newDrawerId); if (newDrawerId) { fireNonCancelableEvent(runtimeDrawer === null || runtimeDrawer === void 0 ? void 0 : runtimeDrawer.onToggle, { isOpen: true, initiatedByUserAction }); } if (activeAiDrawerId) { fireNonCancelableEvent(runtimeDrawer === null || runtimeDrawer === void 0 ? void 0 : runtimeDrawer.onToggle, { isOpen: false, initiatedByUserAction }); if (activeAiDrawerId === expandedDrawerId) { setExpandedDrawerId === null || setExpandedDrawerId === void 0 ? void 0 : setExpandedDrawerId(null); } } onAiDrawerFocus === null || onAiDrawerFocus === void 0 ? void 0 : onAiDrawerFocus(); } function aiDrawerMessageHandler(event) { if (event.type === 'registerLeftDrawer') { setRuntimeDrawer(event.payload); if (!aiDrawerWasOpenRef.current && event.payload.defaultActive) { onActiveAiDrawerChange(event.payload.id, { initiatedByUserAction: false }); } return; } switch (event.type) { case 'updateDrawerConfig': setRuntimeDrawer(current => (current ? { ...current, ...event.payload } : current)); break; case 'openDrawer': onActiveAiDrawerChange(event.payload.id, { initiatedByUserAction: false }); break; case 'closeDrawer': onActiveAiDrawerChange(null, { initiatedByUserAction: false }); break; case 'resizeDrawer': onActiveAiDrawerResize(event.payload.size); break; } } const aiDrawer = runtimeDrawer && mapRuntimeConfigToAiDrawer(runtimeDrawer); const activeAiDrawer = activeAiDrawerId && activeAiDrawerId === (aiDrawer === null || aiDrawer === void 0 ? void 0 : aiDrawer.id) ? aiDrawer : null; const activeAiDrawerSize = activeAiDrawerId ? ((_a = size !== null && size !== void 0 ? size : activeAiDrawer === null || activeAiDrawer === void 0 ? void 0 : activeAiDrawer.defaultSize) !== null && _a !== void 0 ? _a : MIN_DRAWER_SIZE) : 0; const minAiDrawerSize = Math.min((_b = activeAiDrawer === null || activeAiDrawer === void 0 ? void 0 : activeAiDrawer.defaultSize) !== null && _b !== void 0 ? _b : MIN_DRAWER_SIZE, MIN_DRAWER_SIZE); return { aiDrawer, aiDrawerMessageHandler, onActiveAiDrawerChange, activeAiDrawer, activeAiDrawerId, activeAiDrawerSize, minAiDrawerSize, onActiveAiDrawerResize, }; } //# sourceMappingURL=use-ai-drawer.js.map