Spaces:
Running
Running
File size: 5,565 Bytes
61e6dfe 9379f91 61e6dfe b41f5a5 61e6dfe b41f5a5 61e6dfe 9379f91 61e6dfe b41f5a5 2d01256 b41f5a5 9379f91 b41f5a5 318e485 61e6dfe b41f5a5 61e6dfe b41f5a5 61e6dfe b41f5a5 9379f91 b41f5a5 61e6dfe b41f5a5 61e6dfe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 | "use client";
import { useState, useMemo } from "react";
import dynamic from "next/dynamic";
import { useBeforeUnload, useLocalStorage } from "react-use";
import { ArrowRight } from "lucide-react";
import { useTheme } from "next-themes";
import { amethyst } from "@codesandbox/sandpack-themes";
import { NextStep } from "nextstepjs";
import confetti from "canvas-confetti";
import { AppEditorHeader } from "./header";
import { AppViewer } from "@/components/viewer";
import { ActivityType, DeviceType, File, MobileTabType } from "@/lib/type";
import { useProject } from "@/components/projects/useProject";
import { AskAI } from "@/components/ask-ai/ask-ai";
import { AppEditorChat } from "@/components/chat";
import { cn, defaultHTML } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { AppEditorCode } from "@/components/code";
import { ProjectWithCommits } from "@/actions/projects";
import { HistoryView } from "./history-view";
import { TourCustomCard } from "@/components/tour/card";
import { steps } from "@/lib/onboarding";
const SandpackProvider = dynamic(
() =>
import("@codesandbox/sandpack-react").then((mod) => mod.SandpackProvider),
{ ssr: false }
);
export function AppEditor({
project: initialProject,
files: initialFiles,
initialPrompt,
isNew = false,
isHistoryView = false,
}: {
project?: ProjectWithCommits | null;
files?: File[];
initialPrompt?: string;
isNew?: boolean;
isHistoryView?: boolean;
}) {
const [currentActivity, setCurrentActivity] = useState<ActivityType>("chat");
const [device, setDevice] = useState<DeviceType>("desktop");
const [mobileTab, setMobileTab] = useState<MobileTabType>("left-sidebar");
const { project, files } = useProject(initialProject, initialFiles, isNew);
const { resolvedTheme } = useTheme();
const projectName = isNew ? "new" : project?.name ?? "new";
const toggleDevice = () => {
setDevice((prev) => (prev === "desktop" ? "mobile" : "desktop"));
};
const [tourHasBeenShown, setTourHasBeenShown] = useLocalStorage<boolean>(
"tour-has-been-shown",
false
);
useBeforeUnload(
!project && (files?.length ?? 0) > 0,
"You have unsaved changes, are you sure?"
);
const sandpackFiles = useMemo(() => {
if (files && files.length > 0) {
return files.reduce(
(acc, file) => ({
...acc,
[file.path]: { code: file.content ?? "" },
}),
{}
);
} else {
return {
"/index.html": {
code: defaultHTML,
},
};
}
}, [files]);
return (
<NextStep
cardComponent={TourCustomCard}
clickThroughOverlay={false}
shadowRgb="0, 0, 0"
shadowOpacity="0.8"
steps={steps}
onComplete={() => {
setTourHasBeenShown(true);
confetti({
particleCount: 100,
spread: 70,
origin: { y: 1, x: 0.1 },
});
}}
>
<SandpackProvider
template="static"
options={{
initMode: "immediate",
autoReload: false,
recompileDelay: 3000,
recompileMode: "immediate",
}}
files={sandpackFiles}
theme={resolvedTheme === "dark" ? amethyst : undefined}
className="h-screen! w-full! flex! flex-col! justify-between! lg:overflow-hidden!"
>
<AppEditorHeader
currentActivity={currentActivity}
onToggleActivity={setCurrentActivity}
onToggleDevice={toggleDevice}
device={device}
mobileTab={mobileTab}
onToggleMobileTab={setMobileTab}
isNew={isNew}
isHistoryView={isHistoryView}
/>
<main className="flex-1! flex! items-center! pb-3! gap-3! px-3!">
<div
className={cn(
"w-1/3 flex flex-col justify-between h-full relative",
mobileTab !== "left-sidebar" ? "max-lg:hidden" : "max-lg:w-full!"
)}
>
{currentActivity === "chat" ? (
<div className="justify-between flex flex-col h-[calc(100vh-70px)] grow">
<AppEditorChat
isNew={isNew}
projectName={projectName}
onSelectFile={() => {
setCurrentActivity("code");
}}
/>
<div className="">
<div className="lg:hidden flex items-center justify-end pb-2">
<Button
variant="bordered"
size="xs"
onClick={() => setMobileTab("right-sidebar")}
>
Go to Preview <ArrowRight className="size-3" />
</Button>
</div>
<AskAI
isHistoryView={isHistoryView}
isNew={isNew}
projectName={projectName}
initialPrompt={initialPrompt}
tourHasBeenShown={tourHasBeenShown}
files={files}
medias={project?.medias ?? []}
onToggleMobileTab={setMobileTab}
/>
</div>
</div>
) : (
<AppEditorCode />
)}
{isHistoryView && <HistoryView />}
</div>
<AppViewer
mobileTab={mobileTab}
onToggleMobileTab={setMobileTab}
device={device}
/>
</main>
</SandpackProvider>
</NextStep>
);
}
|