Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable open on mount #6

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function App() {
}}
onOpen={(dimensions) => {
setFrameSize(dimensions);
open(dimensions);
open();
}}
onResize={(dimensions) => {
setFrameSize(dimensions);
Expand Down
2 changes: 1 addition & 1 deletion src/components/IframeGuard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const IframeGuard: FC<IframeGuardProps> = ({
}) => {
const [isIframe, setIsIframe] = useState(false);
const close = useLayoutStore((store) => store.close);

const hasMounted = useRef(false);

useEffect(() => {
Expand All @@ -24,7 +25,6 @@ export const IframeGuard: FC<IframeGuardProps> = ({
useEffect(() => {
if (!hasMounted.current) {
parentDispatch(WIDGET_IFRAME_IS_READY_ACTION);
close();
hasMounted.current = true;
}
}, [close]);
Expand Down
27 changes: 16 additions & 11 deletions src/store/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export enum LayoutState {
interface LayoutStore {
state: LayoutState;
frameSize: WindowDimensions;
open: (dimensions: WindowDimensions) => void;
open: () => void;
setFrameSize: (dimensions: WindowDimensions) => void;
close: () => void;
}
Expand All @@ -28,24 +28,29 @@ const DEFAULT_FRAME_WIDTH = 400;
const DEFAULT_FRAME_HEIGHT = 300;
const FRAME_MARGIN_X = 48;

export const useLayoutStore = create<LayoutStore>()((set) => {
function getFrameSize(dimensions: WindowDimensions) {
return {
width:
dimensions.width - FRAME_MARGIN_X < DEFAULT_FRAME_WIDTH
? dimensions.width - FRAME_MARGIN_X
: DEFAULT_FRAME_WIDTH,
height: DEFAULT_FRAME_HEIGHT,
};
}

export const useLayoutStore = create<LayoutStore>()((set, get) => {
return {
state: LayoutState.CLOSED,
frameSize: { width: DEFAULT_FRAME_WIDTH, height: DEFAULT_FRAME_HEIGHT },
setFrameSize: (parentDimensions: WindowDimensions) => {
const frameSize = {
width:
parentDimensions.width - FRAME_MARGIN_X < DEFAULT_FRAME_WIDTH
? parentDimensions.width - FRAME_MARGIN_X
: DEFAULT_FRAME_WIDTH,
height: DEFAULT_FRAME_HEIGHT,
};
const frameSize = getFrameSize(parentDimensions);
set({ frameSize });
parentDispatch(RESIZE_FRAME_ACTION(frameSize));
},
open: (dimensions: WindowDimensions) => {
open: () => {
clearTimeout(timeout);
parentDispatch(RESIZE_FRAME_ACTION(dimensions));
const frameSize = get().frameSize;
parentDispatch(RESIZE_FRAME_ACTION(frameSize));
parentDispatch(EXPAND_WIDGET_ACTION);
return set({ state: LayoutState.OPEN });
},
Expand Down
3 changes: 1 addition & 2 deletions src/views/Views.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const Views: FC<ViewsProps> = () => {
const layoutState = useLayoutStore((store) => store.state);
const open = useLayoutStore((store) => store.open);
const close = useLayoutStore((store) => store.close);
const frameSize = useLayoutStore((store) => store.frameSize);

const { connect, disconnect, status, error } = useVoice();

Expand All @@ -24,7 +23,7 @@ export const Views: FC<ViewsProps> = () => {
<OpenButton
status={status.value}
onPress={() => {
open(frameSize);
open();
}}
/>
</>
Expand Down