Skip to content

Commit

Permalink
feat: tawk to bubble location
Browse files Browse the repository at this point in the history
  • Loading branch information
arrocke committed Nov 7, 2024
1 parent a04a9b8 commit d37f3de
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/[locale]/(authenticated)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default async function AuthenticatedLayout({ children, params }: { childr
<div className="flex-grow relative flex flex-col w-full">
{children}
</div>
<footer className="absolute bottom-0 w-full p-2 flex flex-row z-10 justify-end">
<footer className="absolute bottom-0 w-full p-2 flex flex-row z-10 justify-start">
<LanguageDialog />
</footer>
</div>
Expand Down
26 changes: 26 additions & 0 deletions app/[locale]/ChatBubble.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Script from "next/script";

export default function ChatBubble({ placement }: { placement: 'bl' | 'br' }) {
if (process.env.NODE_ENV !== 'production') return null

return <Script id="show-banner" strategy="lazyOnload">
{`
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
Tawk_API.customStyle = {
visibility: {
desktop: {
position: '${placement}',
}
},
};
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/671d701f4304e3196ad8d17a/1ib5frv2r';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
`}
</Script>
}
18 changes: 2 additions & 16 deletions app/[locale]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { headFontClass } from "../fonts";
import languages from "../../languages.json";
import { FlashProvider } from "../flash";
import Script from "next/script";
import ChatBubble from "./ChatBubble";

export async function generateMetadata(): Promise<Metadata> {
const t = await getTranslations("RootLayout")
Expand Down Expand Up @@ -49,22 +50,7 @@ export default function RootLayout({
{children}
</FlashProvider>
</NextIntlClientProvider>
{
process.env.NODE_ENV === 'production' &&
<Script id="show-banner">
{`
var Tawk_API=Tawk_API||{}, Tawk_LoadStart=new Date();
(function(){
var s1=document.createElement("script"),s0=document.getElementsByTagName("script")[0];
s1.async=true;
s1.src='https://embed.tawk.to/671d701f4304e3196ad8d17a/1ib5frv2r';
s1.charset='UTF-8';
s1.setAttribute('crossorigin','*');
s0.parentNode.insertBefore(s1,s0);
})();
`}
</Script>
}
<ChatBubble placement={language.dir === 'rtl' ? 'bl' : 'br'} />
</body>
</html>
);
Expand Down
7 changes: 5 additions & 2 deletions app/components/LanguageDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function LanguageDialog() {
<dialog
ref={dialog}
className="
rounded-lg shadow-md border border-gray-200 bg-white mx-auto p-8 focus-visible:outline outline-green-300 outline-2 end-2 bottom-2 start-auto
rounded-lg shadow-md border border-gray-200 bg-white mx-auto p-8 focus-visible:outline outline-green-300 outline-2 start-2 bottom-2 end-auto
dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200
"
>
Expand All @@ -36,7 +36,10 @@ export default function LanguageDialog() {
<ComboboxInput
className="block min-w-[150px]"
value={locale}
onChange={(language) => router.push(pathName.replace(new RegExp(`/${locale}(?=/|$)`), `/${language}`))}
onChange={(language) => {
// We need to do a hard refresh so that the tawk.to chat widget will reload with settings for the new language.
window.location.href = new URL(pathName.replace(new RegExp(`/${locale}(?=/|$)`), `/${language}`), window.location.origin).toString()
}}
aria-label="Interface Language"
up
items={Object.entries(languages).map(([value, config]) => ({
Expand Down

0 comments on commit d37f3de

Please sign in to comment.