Skip to content

Commit 68c491c

Browse files
committed
chore: Refactoring
1 parent 1bbf47b commit 68c491c

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/app-layout/runtime-drawer/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ export const mapRuntimeConfigToAiDrawer = (
149149
onToggle?: NonCancelableEventHandler<DrawerStateChangeParams>;
150150
headerActions?: ReadonlyArray<ButtonGroupProps.Item>;
151151
exitExpandedModeTrigger?: React.ReactNode;
152+
onToggleFocusMode?: NonCancelableEventHandler<{ isExpanded: boolean }>;
152153
} => {
153154
const { mountContent, unmountContent, trigger, exitExpandedModeTrigger, ...runtimeDrawer } = runtimeConfig;
154155

src/app-layout/visual-refresh-toolbar/state/use-ai-drawer.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
3-
import { useEffect, useRef, useState } from 'react';
3+
import { useRef, useState } from 'react';
44

55
import { fireNonCancelableEvent } from '../../../internal/events';
6-
import { usePrevious } from '../../../internal/hooks/use-previous';
76
import { DrawerPayload as RuntimeAiDrawerConfig, WidgetMessage } from '../../../internal/plugins/widget/interfaces';
87
import { getLimitedValue } from '../../../split-panel/utils/size-utils';
98
import { mapRuntimeConfigToAiDrawer } from '../../runtime-drawer';
@@ -34,18 +33,9 @@ export function useAiDrawer({
3433
const [size, setSize] = useState<number | null>(null);
3534
const aiDrawerWasOpenRef = useRef(false);
3635
aiDrawerWasOpenRef.current = aiDrawerWasOpenRef.current || !!activeAiDrawerId;
37-
const prevExpandedDrawerId = usePrevious(expandedDrawerId);
3836

3937
const aiDrawer = runtimeDrawer && mapRuntimeConfigToAiDrawer(runtimeDrawer);
4038

41-
useEffect(() => {
42-
if (prevExpandedDrawerId !== expandedDrawerId && (expandedDrawerId === aiDrawer?.id || expandedDrawerId === null)) {
43-
fireNonCancelableEvent(runtimeDrawer?.onToggleFocusMode, {
44-
isExpanded: !!expandedDrawerId,
45-
});
46-
}
47-
}, [runtimeDrawer?.onToggleFocusMode, expandedDrawerId, prevExpandedDrawerId, aiDrawer]);
48-
4939
function onActiveAiDrawerResize(size: number) {
5040
const limitedSize = getLimitedValue(minAiDrawerSize, size, getMaxAiDrawerSize());
5141
setSize(limitedSize);

src/app-layout/visual-refresh-toolbar/state/use-app-layout.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export const useAppLayout = (
7171
const [navigationAnimationDisabled, setNavigationAnimationDisabled] = useState(true);
7272
const [splitPanelAnimationDisabled, setSplitPanelAnimationDisabled] = useState(true);
7373
const [isNested, setIsNested] = useState(false);
74-
const [expandedDrawerId, setExpandedDrawerId] = useState<string | null>(null);
74+
const [expandedDrawerId, setInternalExpandedDrawerId] = useState<string | null>(null);
7575
const rootRefInternal = useRef<HTMLDivElement>(null);
7676
// This workaround ensures the ref is defined before checking if the app layout is nested.
7777
// On initial render, the ref might be undefined because this component loads asynchronously via the widget API.
@@ -90,6 +90,16 @@ export const useAppLayout = (
9090
fireNonCancelableEvent(onToolsChange, { open });
9191
};
9292

93+
const setExpandedDrawerId = (value: string | null) => {
94+
setInternalExpandedDrawerId(value);
95+
96+
if (aiDrawer?.onToggleFocusMode && (value === aiDrawer?.id || value === null)) {
97+
fireNonCancelableEvent(aiDrawer.onToggleFocusMode, {
98+
isExpanded: !!value,
99+
});
100+
}
101+
};
102+
93103
const onGlobalDrawerFocus = (drawerId: string, open: boolean) => {
94104
globalDrawersFocusControl.setFocus({ force: true, drawerId, open });
95105
};

0 commit comments

Comments
 (0)