Skip to content

Commit

Permalink
refactor: appContext to modals (#174)
Browse files Browse the repository at this point in the history
Signed-off-by: Petr Kadlec <[email protected]>
  • Loading branch information
kapetr authored Jan 8, 2025
1 parent ed7727b commit b81bbbd
Show file tree
Hide file tree
Showing 78 changed files with 244 additions and 477 deletions.
3 changes: 2 additions & 1 deletion .env.local.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ API_URL=http://localhost:4000/
SCHEMA_API_URL=http://localhost:4000/docs/json?showHidden=true
SCHEMA_OBSERVE_URL=http://localhost:4000/docs/json

FEATURE_FLAGS='{"Knowledge":true,"TextExtraction":true,"Files":true,"FunctionTools":true,"Observe":true}'

NEXT_PUBLIC_APP_NAME="BeeAI"
NEXT_PUBLIC_FEATURE_FLAGS='{"Knowledge":true,"TextExtraction":true,"Files":true,"FunctionTools":true,"Observe":true}'

NEXT_PUBLIC_TOU_TEXT=
NEXT_PUBLIC_PRIVACY_URL=
Expand Down
5 changes: 1 addition & 4 deletions src/app/(main)/[projectId]/chat/[assistantId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ export default async function AssistantChatPage({

return (
<LayoutInitializer layout={{ navbarProps: { type: 'chat', assistant } }}>
<VectorStoreFilesUploadProvider
projectId={projectId}
organizationId={organizationId}
>
<VectorStoreFilesUploadProvider>
<FilesUploadProvider>
<ChatProvider
assistant={{
Expand Down
5 changes: 1 addition & 4 deletions src/app/(main)/[projectId]/thread/[threadId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,7 @@ export default async function ThreadPage({
navbarProps: { type: 'chat', assistant: threadAssistant.data },
}}
>
<VectorStoreFilesUploadProvider
projectId={projectId}
organizationId={organizationId}
>
<VectorStoreFilesUploadProvider>
<FilesUploadProvider>
<ChatProvider
thread={thread}
Expand Down
5 changes: 1 addition & 4 deletions src/app/(main)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

import { ModalProvider } from '@/layout/providers/ModalProvider';
import { UserProfileInitializer } from '@/store/user-profile/UserProfileInitializer';
import { SessionProvider } from 'next-auth/react';
import { PropsWithChildren } from 'react';
Expand All @@ -27,9 +26,7 @@ export default async function MainLayout({ children }: PropsWithChildren) {
return (
<SessionProvider>
<UserProfileInitializer userProfile={session.userProfile}>
<QueryProvider>
<ModalProvider>{children}</ModalProvider>
</QueryProvider>
<QueryProvider>{children}</QueryProvider>
</UserProfileInitializer>
</SessionProvider>
);
Expand Down
3 changes: 1 addition & 2 deletions src/components/UploadDataset/UploadDataset.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import { createFile } from '@/app/api/files';
import { CloudUpload } from '@carbon/react/icons';
import mimeType from 'mime-types';
import { useAppContext } from '@/layout/providers/AppProvider';
import { useProjectContext } from '@/layout/providers/ProjectProvider';

interface Props<
TFieldValues extends FieldValues = FieldValues,
Expand Down Expand Up @@ -85,7 +84,7 @@ export function UploadDataset<
}: Props<TFieldValues, TName>) {
const prefix = usePrefix();
const id = useId();
const { project, organization } = useProjectContext();
const { project, organization } = useAppContext();

const { field } = useController({ control, name });

Expand Down
9 changes: 1 addition & 8 deletions src/layout/providers/AppProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
useRef,
useState,
} from 'react';
import { ProjectProvider } from './ProjectProvider';
import { FeatureName } from '@/utils/parseFeatureFlags';

export interface AppContextValue {
Expand Down Expand Up @@ -124,13 +123,7 @@ export function AppProvider({
onPageLeaveRef,
}}
>
{/* TODO: remove duplicity of props in AppContext */}
<ProjectProvider
project={projectData ?? project}
organization={organization}
>
{children}
</ProjectProvider>
{children}
</AppContext.Provider>
</AppApiContext.Provider>
);
Expand Down
54 changes: 0 additions & 54 deletions src/layout/providers/ProjectProvider.tsx

This file was deleted.

15 changes: 9 additions & 6 deletions src/layout/shell/AppShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { AppProvider } from '../providers/AppProvider';
import { AppHeader } from './AppHeader';
import classes from './AppShell.module.scss';
import { ensureDefaultOrganizationId } from '@/app/auth/rsc';
import { ModalProvider } from '../providers/ModalProvider';

interface Props {
projectId: string;
Expand All @@ -43,13 +44,15 @@ export async function AppShell({
organization={{ id: organizationId }}
featureFlags={featureFlags}
>
<div className={classes.root}>
<AppHeader />
<ModalProvider>
<div className={classes.root}>
<AppHeader />

<main id={MAIN_ELEMENT_ID} className={classes.content}>
{children}
</main>
</div>
<main id={MAIN_ELEMENT_ID} className={classes.content}>
{children}
</main>
</div>
</ModalProvider>
</AppProvider>
);
}
3 changes: 1 addition & 2 deletions src/layout/shell/MainNav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ import { usePrefetchArtifacts } from '@/modules/apps/hooks/usePrefetchArtifacts'
import { usePathname } from 'next/navigation';
import { useAppContext } from '../providers/AppProvider';
import classes from './MainNav.module.scss';
import { useProjectContext } from '../providers/ProjectProvider';

export function MainNav() {
const pathname = usePathname();

const { project } = useProjectContext();
const { project } = useAppContext();

const artifactsCount = useArtifactsCount();

Expand Down
4 changes: 2 additions & 2 deletions src/layout/shell/SidebarButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import { Button, ButtonBaseProps } from '@carbon/react';
import { Close, Menu } from '@carbon/react/icons';
import { MouseEvent } from 'react';
import { UserSetting } from '../hooks/useUserSetting';
import { useProjectContext } from '../providers/ProjectProvider';
import { SidebarProps } from './Sidebar';
import classes from './SidebarButton.module.scss';
import { useAppContext } from '../providers/AppProvider';

interface Props extends Omit<ButtonBaseProps, 'kind' | 'size' | 'className'> {
sidebarId: SidebarProps['id'];
Expand All @@ -38,7 +38,7 @@ export function SidebarButton({
onMouseEnter,
...props
}: Props) {
const { project } = useProjectContext();
const { project } = useAppContext();

const prefetchThreads = usePrefetchThreads();
const prefetchArtifacts = usePrefetchArtifacts({ useDefaultParams: true });
Expand Down
4 changes: 2 additions & 2 deletions src/layout/shell/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { KeyboardEventHandler, useId, useMemo, useRef, useState } from 'react';
import { useOnClickOutside } from 'usehooks-ts';
import { TermsOfUseModal } from './TermsOfUseModal';
import classes from './UserProfile.module.scss';
import { useProjectContext } from '../providers/ProjectProvider';
import { useAppContext } from '../providers/AppProvider';

interface Props {
className?: string;
Expand All @@ -40,7 +40,7 @@ export function UserProfile({ className }: Props) {
const [open, setOpen] = useState(false);
const ref = useRef<HTMLElement>(null);
const buttonRef = useRef<HTMLButtonElement>(null);
const { project } = useProjectContext();
const { project } = useAppContext();
const id = useId();

const { openModal } = useModal();
Expand Down
65 changes: 25 additions & 40 deletions src/modules/apps/builder/AppBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ import { decodeMetadata, encodeMetadata } from '@/app/api/utils';
import { Tooltip } from '@/components/Tooltip/Tooltip';
import { useBreakpoint } from '@/hooks/useBreakpoint';
import { useModal } from '@/layout/providers/ModalProvider';
import {
ProjectProvider,
useProjectContext,
} from '@/layout/providers/ProjectProvider';
import { NavbarHeading } from '@/layout/shell/Navbar';
import { ChatProvider, useChat } from '@/modules/chat/providers/ChatProvider';
import {
Expand Down Expand Up @@ -54,6 +50,7 @@ import classes from './AppBuilder.module.scss';
import { useAppBuilder, useAppBuilderApi } from './AppBuilderProvider';
import { ArtifactSharedIframe } from './ArtifactSharedIframe';
import { SourceCodeEditor } from './SourceCodeEditor';
import { useAppContext } from '@/layout/providers/AppProvider';

interface Props {
thread?: Thread;
Expand All @@ -62,7 +59,7 @@ interface Props {
}

export function AppBuilder({ assistant, thread, initialMessages }: Props) {
const { project, organization } = useProjectContext();
const { project, organization } = useAppContext();
const queryClient = useQueryClient();
const { setCode, getCode } = useAppBuilderApi();
const { artifact, code } = useAppBuilder();
Expand Down Expand Up @@ -160,7 +157,7 @@ function AppBuilderContent() {
const [selectedTab, setSelectedTab] = useState(TabsKeys.Preview);

const router = useRouter();
const { project, organization } = useProjectContext();
const { project, organization } = useAppContext();
const { openModal } = useModal();
const { getMessages, sendMessage, thread } = useChat();
const { setArtifact, setMobilePreviewOpen } = useAppBuilderApi();
Expand Down Expand Up @@ -203,17 +200,15 @@ function AppBuilderContent() {

if (isFirstNewApp || hasUnsavedChanges) {
openModal((props) => (
<ProjectProvider project={project} organization={organization}>
<SaveAppModal
artifact={artifact}
messageId={message?.id}
code={code ?? undefined}
onSaveSuccess={setArtifact}
isConfirmation
additionalMetadata={additionalMetadata}
{...props}
/>
</ProjectProvider>
<SaveAppModal
artifact={artifact}
messageId={message?.id}
code={code ?? undefined}
onSaveSuccess={setArtifact}
isConfirmation
additionalMetadata={additionalMetadata}
{...props}
/>
));
return;
}
Expand Down Expand Up @@ -313,16 +308,11 @@ function AppBuilderContent() {
artifact
? () =>
openModal((props) => (
<ProjectProvider
project={project}
organization={organization}
>
<ShareAppModal
{...props}
artifact={artifact}
onSuccess={setArtifact}
/>
</ProjectProvider>
<ShareAppModal
{...props}
artifact={artifact}
onSuccess={setArtifact}
/>
))
: undefined
}
Expand All @@ -337,19 +327,14 @@ function AppBuilderContent() {
onClick={() => {
if (code) {
openModal((props) => (
<ProjectProvider
project={project}
organization={organization}
>
<SaveAppModal
artifact={artifact}
messageId={message?.id}
code={code}
onSaveSuccess={setArtifact}
additionalMetadata={additionalMetadata}
{...props}
/>
</ProjectProvider>
<SaveAppModal
artifact={artifact}
messageId={message?.id}
code={code}
onSaveSuccess={setArtifact}
additionalMetadata={additionalMetadata}
{...props}
/>
));
}
}}
Expand Down
5 changes: 1 addition & 4 deletions src/modules/apps/builder/AppBuilderNavbarActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import { useBreakpoint } from '@/hooks/useBreakpoint';
import { useAppContext } from '@/layout/providers/AppProvider';
import { useModal } from '@/layout/providers/ModalProvider';
import { ProjectProvider } from '@/layout/providers/ProjectProvider';
import { Button, OverflowMenu, OverflowMenuItem } from '@carbon/react';
import { useRouter } from 'next-nprogress-bar';
import { useDeleteArtifact } from '../hooks/useDeleteArtifact';
Expand Down Expand Up @@ -50,9 +49,7 @@ export function AppBuilderNavbarActions({ artifact, showShareButton }: Props) {
kind="tertiary"
onClick={() =>
openModal((props) => (
<ProjectProvider project={project} organization={organization}>
<ShareAppModal {...props} artifact={artifact} />
</ProjectProvider>
<ShareAppModal {...props} artifact={artifact} />
))
}
>
Expand Down
Loading

0 comments on commit b81bbbd

Please sign in to comment.