Skip to content

Commit

Permalink
feat(frontend): add a chatbot for users (#2144)
Browse files Browse the repository at this point in the history
# Description

Please include a summary of the changes and the related issue. Please
also include relevant motivation and context.

## Checklist before requesting a review

Please delete options that are not relevant.

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my code
- [ ] I have commented hard-to-understand areas
- [ ] I have ideally added tests that prove my fix is effective or that
my feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] Any dependent changes have been merged

## Screenshots (if appropriate):
  • Loading branch information
Zewed authored Feb 5, 2024
1 parent d246ad1 commit 04d6c34
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
27 changes: 15 additions & 12 deletions frontend/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ChatsProvider } from "@/lib/context/ChatsProvider";
import { MenuProvider } from "@/lib/context/MenuProvider/Menu-provider";
import { SearchModalProvider } from "@/lib/context/SearchModalProvider/search-modal-provider";
import { useSupabase } from "@/lib/context/SupabaseProvider";
import { IntercomProvider } from "@/lib/helpers/intercom/IntercomProvider";
import { UpdateMetadata } from "@/lib/helpers/updateMetadata";
import { usePageTracking } from "@/services/analytics/june/usePageTracking";
import "../lib/config/LocaleConfig/i18n";
Expand Down Expand Up @@ -55,19 +56,21 @@ const App = ({ children }: PropsWithChildren): JSX.Element => {

return (
<PostHogProvider client={posthog}>
<div className="flex flex-1 flex-col overflow-auto">
<SearchModalProvider>
<SearchModal />
<NotificationBanner />
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-auto">
<Menu />
<div onClick={onClickOutside} className="flex-1">
{children}
<IntercomProvider>
<div className="flex flex-1 flex-col overflow-auto">
<SearchModalProvider>
<SearchModal />
<NotificationBanner />
<div className="relative h-full w-full flex justify-stretch items-stretch overflow-auto">
<Menu />
<div onClick={onClickOutside} className="flex-1">
{children}
</div>
<UpdateMetadata />
</div>
<UpdateMetadata />
</div>
</SearchModalProvider>
</div>
</SearchModalProvider>
</div>
</IntercomProvider>
</PostHogProvider>
);
};
Expand Down
35 changes: 35 additions & 0 deletions frontend/lib/helpers/intercom/IntercomProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { usePathname } from "next/navigation";
import { useEffect } from "react";

import { useUserData } from "@/lib/hooks/useUserData";

import {
boot as bootIntercom,
load as loadIntercom,
update as updateIntercom,
} from "./intercom";

export const IntercomProvider = ({
children,
}: {
children: React.ReactNode;
}): React.ReactNode => {
const pathname = usePathname();
const { userData } = useUserData();

if (
typeof window !== "undefined" &&
process.env.NEXT_PUBLIC_INTERCOM_APP_ID
) {
loadIntercom();
bootIntercom(userData?.email ?? "");
}

useEffect(() => {
if (process.env.NEXT_PUBLIC_INTERCOM_APP_ID) {
updateIntercom();
}
}, [pathname]);

return children;
};
17 changes: 17 additions & 0 deletions frontend/lib/helpers/intercom/intercom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export const APP_ID = process.env.NEXT_PUBLIC_INTERCOM_APP_ID;

// prettier-ignore
export const load = (): void => {
(function(){const w=window;const ic=(w as any).Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',(w as any).intercomSettings);}else{const d=document;const i=function(){i.c(arguments);};i.q=[] as any[];i.c=function(args: any){i.q.push(args);};(w as any).Intercom=i;const l=function(){const s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/' + APP_ID;const x=d.getElementsByTagName('script')[0];x.parentNode?.insertBefore(s, x);};if(document.readyState==='complete'){l();}else if((w as any).attachEvent){(window as any).attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})();
}

// Initializes Intercom
export const boot = (email: string): void => {
(window as any).Intercom &&
(window as any).Intercom("boot", { app_id: APP_ID, email });
};

export const update = (): void => {
window && (window as any).Intercom && (window as any).Intercom("update");
};
8 changes: 8 additions & 0 deletions frontend/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const ContentSecurityPolicy = {
"https://api.june.so",
"https://us.posthog.com",
"https://preview.quivr.app",
"*.intercom.io",
"*.intercomcdn.com",
"https://*.vercel.app",
process.env.NEXT_PUBLIC_FRONTEND_URL,
],
Expand All @@ -58,6 +60,8 @@ const ContentSecurityPolicy = {
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.NEXT_PUBLIC_BACKEND_URL,
process.env.NEXT_PUBLIC_CMS_URL,
"*.intercom.io",
"*.intercomcdn.com",
"https://api.june.so",
"https://api.openai.com",
"https://cdn.growthbook.io",
Expand All @@ -67,6 +71,8 @@ const ContentSecurityPolicy = {
"img-src": [
"'self'",
"https://www.gravatar.com",
"*.intercom.io",
"*.intercomcdn.com",
"https://quivr-cms.s3.eu-west-3.amazonaws.com",
"data:",
"*",
Expand All @@ -83,6 +89,8 @@ const ContentSecurityPolicy = {
"'unsafe-inline'",
"'unsafe-eval'",
"https://va.vercel-scripts.com/",
"*.intercom.io",
"*.intercomcdn.com",
process.env.NEXT_PUBLIC_FRONTEND_URL,
"https://preview.quivr.app",
"https://*.vercel.app",
Expand Down

0 comments on commit 04d6c34

Please sign in to comment.