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

feat: support custom widget button rendering #97

Merged
merged 1 commit into from
Feb 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
6 changes: 6 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ interface Props extends Partial<Constant> {
botId?: string;
hashedKey?: string;
autoOpen?: boolean;
renderWidgetToggleButton?: (props: {
onClick: () => void;
accentColor: string;
isOpen: boolean;
}) => React.ReactElement;
}

const App = (props: Props) => {
Expand Down Expand Up @@ -33,6 +38,7 @@ const App = (props: Props) => {
enableMention={props.enableMention}
customUserAgentParam={props.customUserAgentParam}
autoOpen={props.autoOpen}
renderWidgetToggleButton={props.renderWidgetToggleButton}
/>
);
};
Expand Down
40 changes: 21 additions & 19 deletions src/components/ChatAiWidget.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import '@sendbird/uikit-react/dist/index.css';
import '../css/index.css';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { useEffect, useRef, useState } from 'react';
import styled, { css } from 'styled-components';
Expand Down Expand Up @@ -101,22 +100,17 @@ const StyledArrowIcon = styled.span<{ isOpen: boolean }>`
`;
}}
`;
export interface Props extends Partial<Constant> {
applicationId: string;
botId: string;
hashedKey?: string;
autoOpen?: boolean;
}

interface ToggleButtonProps {
onClick: () => void;
accentColor: string;
isOpen: boolean;
}
const WidgetToggleButton = ({
onClick,
accentColor,
isOpen,
}: {
onClick: () => void;
accentColor: string;
isOpen: boolean;
}) => {
}: ToggleButtonProps) => {
return (
<StyledWidgetButtonWrapper
id="aichatbot-widget-button"
Expand All @@ -133,6 +127,14 @@ const WidgetToggleButton = ({
);
};

export interface Props extends Partial<Constant> {
applicationId: string;
botId: string;
hashedKey?: string;
autoOpen?: boolean;
renderWidgetToggleButton?: (props: ToggleButtonProps) => React.ReactElement;
}

const Component = (props: Props) => {
const { isFetching, ...channelStyle } = useChannelStyle({
appId: props.applicationId,
Expand Down Expand Up @@ -164,20 +166,20 @@ const Component = (props: Props) => {
}
}, [channelStyle.autoOpen, props.autoOpen]);

const toggleButtonProps = {
onClick: buttonClickHandler,
accentColor: channelStyle.accentColor,
isOpen,
};
return isMobile && isOpen ? (
<MobileContainer width={mobileContainerWidth}>
<Chat {...props} isOpen={isOpen} setIsOpen={setIsOpen} />
</MobileContainer>
) : (
<>
<WidgetWindow isOpen={isOpen} setIsOpen={setIsOpen} {...props} />
{!isFetching && (
<WidgetToggleButton
onClick={buttonClickHandler}
accentColor={channelStyle.accentColor}
isOpen={isOpen}
/>
)}
{props.renderWidgetToggleButton?.(toggleButtonProps) ||
(!isFetching && <WidgetToggleButton {...toggleButtonProps} />)}
</>
);
};
Expand Down