Skip to content

Commit

Permalink
馃悰 Fix homepage reloading
Browse files Browse the repository at this point in the history
  • Loading branch information
awtkns committed Nov 1, 2023
1 parent b0523b9 commit 44f043e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 28 deletions.
8 changes: 7 additions & 1 deletion next/src/components/drawer/LeftSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import LinkIconItem from "../sidebar/LinkIconItem";
import LinkItem from "../sidebar/LinkItem";
import { PAGE_LINKS, SOCIAL_LINKS } from "../sidebar/links";

const LeftSidebar = ({ show, setShow }: DisplayProps) => {
const LeftSidebar = ({ show, setShow, onReload }: DisplayProps & { onReload?: () => void }) => {
const router = useRouter();
const { session, signIn, signOut, status } = useAuth();
const [t] = useTranslation("drawer");
Expand Down Expand Up @@ -95,6 +95,12 @@ const LeftSidebar = ({ show, setShow }: DisplayProps) => {
href={link.href}
badge={link.badge}
onClick={() => {
console.log(router.pathname, link.href, link.forceRefresh);
if (router.pathname === link.href && link.forceRefresh) {
onReload?.();
return;
}

void router.push(link.href);
}}
>
Expand Down
6 changes: 3 additions & 3 deletions next/src/components/index/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Input from "../Input";
type LandingProps = {
messages: Message[];
disableStartAgent: boolean;
handlePlay: (goal: string) => void;
handlePlay: () => void;
handleKeyPress: (e: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
goalInputRef: RefObject<HTMLInputElement>;
goalInput: string;
Expand Down Expand Up @@ -69,7 +69,7 @@ const Landing = (props: LandingProps) => {
disabled={agent != null}
value={props.goalInput}
onChange={(e) => props.setGoalInput(e.target.value)}
onKeyDown={(e) => props.handleKeyPress(e)}
onKeyDown={props.handleKeyPress}
placeholder={`${t("PLACEHOLDER_AGENT_GOAL")}`}
type="textarea"
/>
Expand All @@ -83,7 +83,7 @@ const Landing = (props: LandingProps) => {
<FaCog />
</Button>
<Button
onClick={() => props.handlePlay(props.goalInput)}
onClick={props.handlePlay}
className="border-0 bg-gradient-to-t from-[#02FCF1] to-[#A02BFE] subpixel-antialiased saturate-[75%] hover:saturate-100"
>
<FaPlay />
Expand Down
1 change: 1 addition & 0 deletions next/src/components/sidebar/LinkItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Badge from "../Badge";
const LinkItem = (props: {
title: string;
children: ReactNode;
forceRefresh?: boolean;
href?: string;
badge?: { text: string; className?: string };
onClick: () => void;
Expand Down
2 changes: 2 additions & 0 deletions next/src/components/sidebar/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { FaXTwitter } from "react-icons/fa6";
type LinkMetadata = {
name: string;
href: string;
forceRefresh?: boolean;
icon: IconType;
badge?: {
text: string;
Expand All @@ -29,6 +30,7 @@ export const PAGE_LINKS: LinkMetadata[] = [
href: "/",
icon: FaHome,
enabled: true,
forceRefresh: true,
},
{
name: "Help",
Expand Down
8 changes: 7 additions & 1 deletion next/src/layout/dashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type SidebarSettings = {
type DashboardLayoutProps = {
children: ReactNode;
rightSidebar?: ReactNode;
onReload?: () => void;
};

const defaultState: SidebarSettings = {
Expand Down Expand Up @@ -45,7 +46,11 @@ const DashboardLayout = (props: DashboardLayoutProps) => {
<AppHead />
{/* Left sidebar */}
{/* Mobile */}
<LeftSidebar show={leftSettings.mobile} setShow={setMobile(leftSettings, setLeftSettings)} />
<LeftSidebar
show={leftSettings.mobile}
setShow={setMobile(leftSettings, setLeftSettings)}
onReload={props.onReload}
/>
<div className={leftSettings.mobile ? "hidden" : "lg:hidden"}>
<SidebarControlButton
side="left"
Expand All @@ -58,6 +63,7 @@ const DashboardLayout = (props: DashboardLayoutProps) => {
<LeftSidebar
show={leftSettings.desktop}
setShow={setDesktop(leftSettings, setLeftSettings)}
onReload={props.onReload}
/>
</div>
<div className={leftSettings.desktop ? "hidden" : "hidden lg:block"}>
Expand Down
22 changes: 13 additions & 9 deletions next/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ const Home: NextPage = () => {

const handlePlay = (goal: string) => {
if (agentLifecycle === "stopped") handleRestart();
else handleNewAgent("", goal.trim());
else handleNewAgent(goal.trim());
};

const handleNewAgent = (name: string, goal: string) => {
const handleNewAgent = (goal: string) => {
if (session === null) {
storeAgentDataInLocalStorage(name, goal);
storeAgentDataInLocalStorage("", goal);
setShowSignInDialog(true);
return;
}
Expand All @@ -82,13 +82,12 @@ const Home: NextPage = () => {
return;
}

const model = new DefaultAgentRunModel(name.trim(), goal.trim());
const model = new DefaultAgentRunModel(goal.trim());
const messageService = new MessageService(addMessage);
const agentApi = new AgentApi({
model_settings: toApiModelSettings(settings, session),
name: name,
goal: goal,
session,
session: session,
agentUtils: agentUtils,
});
const newAgent = new AutonomousAgent(
Expand Down Expand Up @@ -122,7 +121,7 @@ const Home: NextPage = () => {
localStorage.removeItem("agentData");
}
}
}, [session]);
}, [session, setGoalInput, setNameInput]);

const handleRestart = () => {
resetAllMessageSlices();
Expand All @@ -138,7 +137,12 @@ const Home: NextPage = () => {
};

return (
<DashboardLayout>
<DashboardLayout
onReload={() => {
agent?.stopAgent();
handleRestart();
}}
>
<HelpDialog />

<SignInDialog show={showSignInDialog} setOpen={setShowSignInDialog} />
Expand All @@ -163,7 +167,7 @@ const Home: NextPage = () => {
<Landing
messages={messages}
disableStartAgent={disableStartAgent}
handlePlay={handlePlay}
handlePlay={() => handlePlay(goalInput)}
handleKeyPress={handleKeyPress}
goalInputRef={goalInputRef}
goalInput={goalInput}
Expand Down
1 change: 0 additions & 1 deletion next/src/services/agent/agent-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { RequestBody } from "../../utils/interfaces";
import * as apiUtils from "../api-utils";

type ApiProps = Pick<RequestBody, "model_settings" | "goal"> & {
name: string;
session?: Session;
agentUtils: AgentUtils;
};
Expand Down
18 changes: 5 additions & 13 deletions next/src/services/agent/agent-run-model.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import type { Task, TaskStatus } from "../../types/task";
export interface AgentRunModel {
getId(): string;

getName(): string;

getGoal(): string;

getLifecycle(): AgentLifecycle;
Expand All @@ -30,26 +28,20 @@ export interface AgentRunModel {

addTask(taskValue: string): void;
}

export type AgentLifecycle = "offline" | "running" | "pausing" | "paused" | "stopped";

export class DefaultAgentRunModel implements AgentRunModel {
id: string;
name: string;
goal: string;
tasks: string[];
completedTasks: string[];
private readonly id: string;
private readonly goal: string;

constructor(name: string, goal: string) {
constructor(goal: string) {
this.id = v4().toString();
this.name = name;
this.goal = goal;
this.tasks = [];
this.completedTasks = [];
}

getId = () => this.id;
getName = () => this.name;

getGoal = () => this.goal;
getLifecycle = (): AgentLifecycle => useAgentStore.getState().lifecycle;
setLifecycle = (lifecycle: AgentLifecycle) => useAgentStore.getState().setLifecycle(lifecycle);
Expand Down

0 comments on commit 44f043e

Please sign in to comment.