Skip to content

Commit

Permalink
Lazy load auth and ai tabs only when ext is enabled + lazy load schem…
Browse files Browse the repository at this point in the history
…a graph (#398)
  • Loading branch information
jaclarke authored Jan 7, 2025
1 parent b17f774 commit 4aeb27a
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 431 deletions.
2 changes: 1 addition & 1 deletion shared/schemaGraph/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from "./state/query";
export * from "./state/interfaces";

export {Schema} from "./state";
export {Schema, type SchemaState} from "./state";

export {schemaContext} from "./state/provider";

Expand Down
83 changes: 83 additions & 0 deletions shared/studio/components/lazyTabs/lazyTabs.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
@import "@edgedb/common/mixins.scss";

.tabWrapper {
position: relative;
flex-grow: 1;
background-color: var(--app_panel_background);
border-radius: 12px;
box-shadow: 0 0 8px rgba(0, 0, 0, 0.04), 0 0 4px rgba(0, 0, 0, 0.06);
display: flex;
min-width: 0;
min-height: 0;
font-family: "Roboto Flex Variable", sans-serif;

@include isMobile {
flex-direction: column;
border-radius: 0;
}

@include darkTheme {
box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.2),
0px 0px 4px 0px rgba(0, 0, 0, 0.3);
}
}

.loadingSchema {
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
font-style: italic;
}

.fallbackSpinner {
margin: auto;
}

.extDisabled {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;
margin: auto;
font-family: "Roboto Flex Variable", sans-serif;
color: #4d4d4d;

h2 {
color: inherit;
font-size: 18px;
font-style: normal;
font-weight: 500;
line-height: 26px;
margin: 0;
}

p {
margin: 8px 0;
}

pre {
background: #eee;
margin: 12px;
padding: 12px 16px;
border-radius: 6px;
user-select: text;

@include darkTheme {
background: #383838;
}
}

a {
color: #b044c2;

@include darkTheme {
color: #ac86f6;
}
}

@include darkTheme {
color: #ccc;
}
}
14 changes: 14 additions & 0 deletions shared/studio/state/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
_async,
_await,
frozen,
modelAction,
} from "mobx-keystone";

import {
Expand Down Expand Up @@ -82,6 +83,19 @@ export class DatabaseState extends Model({
@observable
currentRole: string | null = null;

_getTabState(modelType: string, stateClass: ModelClass<AnyModel>) {
if (!this.tabStates.has(modelType)) {
this._initTabState(stateClass);
}
return this.tabStates.get(modelType)!;
}

@modelAction
private _initTabState(stateClass: ModelClass<AnyModel>) {
const state = new stateClass({});
this.tabStates.set(state.$modelType, state);
}

@observable
loadingTabs = new Map<string, boolean>();

Expand Down
2 changes: 1 addition & 1 deletion shared/studio/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ export {useDatabaseState};

export function useTabState<T extends AnyModel>(stateClass: ModelClass<T>): T {
const modelType = (getTypeInfo(stateClass) as ModelTypeInfo).modelType;
return useDatabaseState().tabStates.get(modelType) as T;
return useDatabaseState()._getTabState(modelType, stateClass) as T;
}
96 changes: 96 additions & 0 deletions shared/studio/tabs/ai/ai.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import {useEffect, useLayoutEffect} from "react";
import {Observer, observer} from "mobx-react-lite";

import cn from "@edgedb/common/utils/classNames";

import {useTabState} from "../../state";
import {useDBRouter} from "../../hooks/dbRoute";

import {AIAdminState} from "./state";
import {ProvidersTab} from "./providers";
import {PlaygroundTab} from "./playground";
import {PromptsTab} from "./prompts";

import styles from "./aiAdmin.module.scss";
import {WarningIcon} from "@edgedb/common/newui";

const aiAdminTabs = [
{
path: "",
label: "Playground",
element: <PlaygroundTab />,
},
{
path: "prompts",
label: "Prompts",
element: <PromptsTab />,
},
{
path: "providers",
label: "Providers",
element: <ProvidersTab />,
warning: (
<Observer
render={() => {
const state = useTabState(AIAdminState);
return state.indexesWithoutProviders?.length ? (
<WarningIcon />
) : null;
}}
/>
),
},
];

const AIAdminPage = observer(function AIAdminPage() {
const state = useTabState(AIAdminState);

useEffect(() => {
state.refreshConfig();
}, []);

const {navigate, currentPath} = useDBRouter();

const activePath = currentPath.slice(2).join("/");

useEffect(() => {
state.setLastSelectedTab(currentPath.slice(2).join("/"));
}, [currentPath]);

useLayoutEffect(() => {
if (currentPath.length == 2 && state.lastSelectedTab) {
navigate(`${currentPath.join("/")}/${state.lastSelectedTab}`, true);
}
}, []);

return (
<div className={styles.mainLayout}>
<div className={styles.tabs}>
{aiAdminTabs.map((tab) => (
<div
key={tab.path}
className={cn(styles.tab, {
[styles.active]: activePath === tab.path,
})}
onClick={() => {
navigate(
[...currentPath.slice(0, 2), tab.path]
.join("/")
.replace(/\/$/, "")
);
}}
>
{tab.label}
{tab.warning}
</div>
))}
</div>

<div className={styles.tabContent}>
{aiAdminTabs.find((tab) => tab.path === activePath)?.element}
</div>
</div>
);
});

export default AIAdminPage;
89 changes: 12 additions & 77 deletions shared/studio/tabs/ai/aiAdmin.module.scss
Original file line number Diff line number Diff line change
@@ -1,81 +1,5 @@
@import "@edgedb/common/mixins.scss";

.aiAdmin {
position: relative;
flex-grow: 1;
border-radius: 12px;
background: #fbfbfb;
color: #4d4d4d;
box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.04),
0px 0px 4px 0px rgba(0, 0, 0, 0.06);
display: flex;
justify-content: center;
gap: 32px;
min-width: 0;
min-height: 0;
font-family: "Roboto Flex Variable", sans-serif;

h2 {
color: inherit;
font-size: 18px;
font-style: normal;
font-weight: 500;
line-height: 26px;
margin: 0;
}

@include darkTheme {
color: #ccc;
background: var(--Grey14, #242424);
box-shadow: 0px 0px 8px 0px rgba(0, 0, 0, 0.2),
0px 0px 4px 0px rgba(0, 0, 0, 0.3);
}

@include isMobile {
border-radius: 0;
}
}

.loadingSchema {
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
font-style: italic;
}

.extDisabled {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
text-align: center;

p {
margin: 8px 0;
}

pre {
background: #eee;
margin: 12px;
padding: 12px 16px;
border-radius: 6px;
user-select: text;

@include darkTheme {
background: #383838;
}
}

a {
color: #b044c2;

@include darkTheme {
color: #ac86f6;
}
}
}

.mainLayout {
flex-grow: 1;
display: grid;
Expand All @@ -94,6 +18,15 @@
min-height: 0;
min-width: 0;

h2 {
color: inherit;
font-size: 18px;
font-style: normal;
font-weight: 500;
line-height: 26px;
margin: 0;
}

@include isMobile {
grid-column: 1;
}
Expand Down Expand Up @@ -168,8 +101,10 @@
justify-self: stretch;
flex-direction: row;
justify-content: center;
background: var(--header_background);
padding: 4px 8px;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.04), 0 2px 3px rgba(0, 0, 0, 0.03);
z-index: 2;

.tab {
display: flex;
Expand Down Expand Up @@ -1079,7 +1014,7 @@
&:before {
content: "";
position: absolute;
background: #fbfbfb;
background: var(--app_panel_background);
top: -16px;
left: -1px;
right: -1px;
Expand Down
Loading

0 comments on commit 4aeb27a

Please sign in to comment.