Skip to content

Commit 88e0e26

Browse files
authored
Display existing notebooks on the new page (#156)
* Show notebooks in New page * Fix ci
1 parent bcca03d commit 88e0e26

File tree

1 file changed

+96
-36
lines changed

1 file changed

+96
-36
lines changed

app/EditorPage.jsx

Lines changed: 96 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import {useState, useEffect, useRef, useCallback, useSyncExternalStore} from "re
33
import {notFound} from "next/navigation";
44
import {Pencil} from "lucide-react";
55
import {Editor} from "./Editor.jsx";
6-
import {getNotebookById, createNotebook, addNotebook, saveNotebook} from "./api.js";
6+
import {getNotebookById, createNotebook, addNotebook, saveNotebook, getNotebooks} from "./api.js";
77
import {isDirtyStore, countStore} from "./store.js";
88
import {cn} from "./cn.js";
9+
import {SafeLink} from "./SafeLink.jsx";
910

1011
const UNSET = Symbol("UNSET");
1112

1213
export function EditorPage({id: initialId}) {
1314
const [notebook, setNotebook] = useState(UNSET);
15+
const [notebookList, setNotebookList] = useState([]);
1416
const [showInput, setShowInput] = useState(false);
1517
const [autoRun, setAutoRun] = useState(false);
1618
const [id, setId] = useState(initialId);
@@ -59,6 +61,11 @@ export function EditorPage({id: initialId}) {
5961
}, 100);
6062
}, [notebook, isAdded]);
6163

64+
useEffect(() => {
65+
const notebooks = getNotebooks();
66+
setNotebookList(notebooks.slice(0, 4));
67+
}, [isAdded]);
68+
6269
useEffect(() => {
6370
const onBeforeUnload = (e) => {
6471
if (isDirty) e.preventDefault();
@@ -137,44 +144,97 @@ export function EditorPage({id: initialId}) {
137144
}
138145

139146
return (
140-
<div className={cn("max-w-screen-lg lg:mx-auto mx-4 lg:my-10 my-4 editor-page")}>
141-
<Editor
142-
initialCode={initialCode}
143-
key={notebook.id}
144-
onUserInput={onUserInput}
145-
onBeforeEachRun={onBeforeEachRun}
146-
autoRun={autoRun}
147-
toolBarStart={
148-
<div className={cn("flex items-center gap-2")}>
149-
{!isAdded && (
150-
<button
151-
onClick={onSave}
152-
className={cn("bg-green-700 text-white rounded-md px-3 py-1 text-sm hover:bg-green-800")}
153-
>
154-
Create
155-
</button>
147+
<div>
148+
{!isAdded && notebookList.length > 0 && (
149+
<div className={cn("flex h-[72px] bg-gray-100 p-2 w-full border-b border-gray-200")}>
150+
<div
151+
className={cn(
152+
"flex items-center justify-between gap-2 h-full max-w-screen-lg lg:mx-auto mx-4 w-full hidden md:flex",
156153
)}
157-
{!showInput && isAdded && (
158-
<button onClick={onRename}>
159-
<Pencil className="w-4 h-4" />
160-
</button>
161-
)}
162-
{showInput || !isAdded ? (
163-
<input
164-
type="text"
165-
value={title}
166-
onChange={onTitleChange}
167-
onBlur={onTitleBlur}
168-
onKeyDown={onTitleKeyDown}
169-
ref={titleRef}
170-
className={cn("border border-gray-200 rounded-md px-3 py-1 text-sm bg-white")}
171-
/>
172-
) : (
173-
<span className={cn("text-sm py-1 border border-gray-100 rounded-md")}>{notebook.title}</span>
154+
>
155+
{notebookList.map((notebook) => (
156+
<div key={notebook.id} className={cn("flex items-start flex-col gap-1")}>
157+
<SafeLink
158+
href={`/notebooks/${notebook.id}`}
159+
className={cn(
160+
"font-semibold hover:underline text-blue-500 whitespace-nowrap line-clamp-1 max-w-[150px] text-ellipsis",
161+
)}
162+
>
163+
{notebook.title}
164+
</SafeLink>
165+
<span
166+
className={cn("text-xs text-gray-500 line-clamp-1 whitespace-nowrap max-w-[150px] text-ellipsis")}
167+
>
168+
Created {new Date(notebook.created).toLocaleDateString()}
169+
</span>
170+
</div>
171+
))}
172+
<SafeLink href="/notebooks" className={cn("font-semibold text-blue-500 hover:underline")}>
173+
View your notebooks
174+
</SafeLink>
175+
</div>
176+
<div
177+
className={cn(
178+
"flex items-center justify-between gap-2 h-full max-w-screen-lg lg:mx-auto mx-4 w-full md:hidden",
174179
)}
180+
>
181+
<SafeLink
182+
href="/notebooks"
183+
className={cn(
184+
"font-medium w-full border border-gray-300 rounded-md px-3 py-2 text-sm text-center hover:bg-gray-200",
185+
)}
186+
>
187+
View your notebooks
188+
</SafeLink>
175189
</div>
176-
}
177-
/>
190+
</div>
191+
)}
192+
{!isAdded && notebookList.length === 0 && (
193+
<div className={cn("flex items-center justify-center h-[72px] mt-6 mb-10 lg:mb-0")}>
194+
<p className={cn("text-3xl text-gray-800 font-light text-center mx-10")}>
195+
Explore code and art with instant feedback.
196+
</p>
197+
</div>
198+
)}
199+
<div className={cn("max-w-screen-lg lg:mx-auto mx-4 lg:my-10 my-4 editor-page")}>
200+
<Editor
201+
initialCode={initialCode}
202+
key={notebook.id}
203+
onUserInput={onUserInput}
204+
onBeforeEachRun={onBeforeEachRun}
205+
autoRun={autoRun}
206+
toolBarStart={
207+
<div className={cn("flex items-center gap-2")}>
208+
{!isAdded && (
209+
<button
210+
onClick={onSave}
211+
className={cn("bg-green-700 text-white rounded-md px-3 py-1 text-sm hover:bg-green-800")}
212+
>
213+
Create
214+
</button>
215+
)}
216+
{!showInput && isAdded && (
217+
<button onClick={onRename}>
218+
<Pencil className="w-4 h-4" />
219+
</button>
220+
)}
221+
{showInput || !isAdded ? (
222+
<input
223+
type="text"
224+
value={title}
225+
onChange={onTitleChange}
226+
onBlur={onTitleBlur}
227+
onKeyDown={onTitleKeyDown}
228+
ref={titleRef}
229+
className={cn("border border-gray-200 rounded-md px-3 py-1 text-sm bg-white")}
230+
/>
231+
) : (
232+
<span className={cn("text-sm py-1 border border-gray-100 rounded-md")}>{notebook.title}</span>
233+
)}
234+
</div>
235+
}
236+
/>
237+
</div>
178238
</div>
179239
);
180240
}

0 commit comments

Comments
 (0)