Skip to content

Commit

Permalink
feat: add locale
Browse files Browse the repository at this point in the history
  • Loading branch information
bang9 committed Aug 14, 2024
1 parent 1cba826 commit 5e068ab
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
10 changes: 1 addition & 9 deletions src/components/ui/WidgetButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,7 @@ const CloseIconWrapper = styled(IconWrapper)<IconWrapperProps>`
const Icon = {
Open: (props: { url?: string }) => {
const { url } = props;

if (url) {
if (url.endsWith('.svg')) {
return <img src={url} alt={'widget-toggle-button'} data-svg={true} />;
} else {
return <img src={url} alt={'widget-toggle-button'} />;
}
}

if (url) return <img src={url} alt={'widget-toggle-button'} data-svg={url.endsWith('.svg')} />;
return <BotOutlinedIcon />;
},
Close: () => <ChevronDownIcon />,
Expand Down
22 changes: 19 additions & 3 deletions src/components/widget/ProviderContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SendbirdErrorCode } from '@sendbird/chat';
import React, { useMemo } from 'react';
import React, { PropsWithChildren, useEffect, useMemo } from 'react';
import { StyleSheetManager, ThemeProvider } from 'styled-components';

import { useSendbirdStateContext } from '@uikit/index';
import SendbirdProvider from '@uikit/lib/Sendbird';

import { ChatAiWidgetProps } from './ChatAiWidget';
Expand Down Expand Up @@ -33,7 +34,6 @@ const SBComponent = ({ children }: { children: React.ReactElement }) => {
enableHideWidgetForDeactivatedUser,
} = useConstantState();

useAssignGlobalFunction();
const { setIsVisible } = useWidgetState();
const { botStyle } = useWidgetSetting();
const session = useWidgetSession();
Expand Down Expand Up @@ -136,9 +136,25 @@ export default function ProviderContainer(props: ProviderContainerProps) {
<ConstantStateProvider {...props}>
<WidgetSettingProvider>
<WidgetStateProvider>
<SBComponent>{props.children}</SBComponent>
<SBComponent>
<HeadlessComponent>{props.children}</HeadlessComponent>
</SBComponent>
</WidgetStateProvider>
</WidgetSettingProvider>
</ConstantStateProvider>
);
}

const HeadlessComponent = ({ children }: PropsWithChildren) => {
useAssignGlobalFunction();
const { locale } = useConstantState();
const { stores } = useSendbirdStateContext();

useEffect(() => {
if (locale && stores.sdkStore.initialized && stores.sdkStore.sdk) {
stores.sdkStore.sdk.setLocaleForChatbot(locale);
}
}, [locale, stores.sdkStore.initialized, stores.sdkStore.sdk]);

return <>{children}</>;
};
6 changes: 5 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ export interface Constant extends ConstantFeatureFlags {
* @description Whether to open the widget automatically. (only works in desktop)
*/
autoOpen?: boolean;
/**
* @public
* @description Sets the locale for the chatbot.
*/
locale?: string;
/**
* @public
* @description Locale value to be applied to string values of message timestamp and date separator.
Expand Down Expand Up @@ -361,7 +366,6 @@ export const elementIds = {
expandIcon: 'aichatbot-widget-expand-icon',
closeIcon: 'aichatbot-widget-close-icon',
refreshIcon: 'aichatbot-widget-refresh-icon',
uikitModal: 'sendbird-modal-root',
};

export const widgetServiceName = {
Expand Down
1 change: 1 addition & 0 deletions src/context/ConstantContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const ConstantStateProvider = (props: PropsWithChildren<ConstantContextPr
...initialState.messageInputControls,
...props.messageInputControls,
},
locale: props.locale,
dateLocale: props.dateLocale ?? initialState.dateLocale,
// ----- Feature flag props ----- //
autoOpen: props.autoOpen,
Expand Down
2 changes: 2 additions & 0 deletions src/context/WidgetSettingContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const WidgetSettingProvider = ({ children }: React.PropsWithChildren) =>
firstMessageData,
botStudioEditProps,
autoOpen,
locale,
} = useConstantState();

if (!appId || !botId) {
Expand Down Expand Up @@ -90,6 +91,7 @@ export const WidgetSettingProvider = ({ children }: React.PropsWithChildren) =>
appId,
botId,
userId: strategy === 'manual' ? injectedUserId : cachedSession?.userId,
locale,
})
.onGetBotStyle((style) => setBotStyle(style))
.onAutoNonCached(({ user, channel }) => {
Expand Down
5 changes: 4 additions & 1 deletion src/libs/api/widgetSetting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Params = {
createChannel?: boolean;
userId?: string;
// sessionToken?: string;
locale?: string;
};

type Response = {
Expand Down Expand Up @@ -57,12 +58,14 @@ export async function getWidgetSetting({
createUserAndChannel,
createChannel,
userId,
locale,
}: Params): Promise<Response> {
// const headers = sessionToken ? { 'Session-Key': sessionToken } : undefined;
const params = asQueryParams({
create_user_and_channel: asBoolString(createUserAndChannel),
create_channel: asBoolString(createChannel),
user_id: userId,
locale,
});
const path = resolvePath(host, `/v3/bots/${botId}/${appId}/widget_setting?${params}`);

Expand Down Expand Up @@ -218,7 +221,7 @@ function getParamsByStrategy(
if (useCachedSession) {
return { userId: params.userId };
} else {
return { createUserAndChannel: true };
return { createUserAndChannel: true, locale: params.locale };
}
} else {
if (useCachedSession) {
Expand Down

0 comments on commit 5e068ab

Please sign in to comment.