Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions apps/studio/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { FrameValues } from "@openclapper/model";

export interface Me { user: { id: string; email: string; name: string | null }; workspaces: { id: string; name: string; role: WorkspaceRole; personal: boolean }[] }
export type WorkspaceRole = "owner" | "admin" | "editor" | "viewer";
export interface Project { id: string; name: string; workspace_id: string; created_at: string; updated_at: string | null; file_count: number; canvas_live: CanvasLive; /** The master timeline: the order of the project's timelines. */ master: string[]; /** The extensions switched on (docs/rewrite/08-extensions.md). */ extensions: string[] }
export interface Project { id: string; name: string; workspace_id: string; created_at: string; updated_at: string | null; file_count: number; canvas_live: CanvasLive; /** The master timeline: the order of the project's timelines. */ master: string[]; /** The extensions switched on (docs/rewrite/08-extensions.md). */ extensions: string[]; /** What it is about (#426). */ description: string; /** When its picture was set; null: none. */ picture_at: string | null }
/** How watchers of a canvas see it change while someone edits it: at once, or every few seconds. */
export type CanvasLive = "realtime" | "gradual";
export interface ServerBuild { version: string; commit: string; instance: string }
Expand Down Expand Up @@ -60,7 +60,16 @@ export const signIn = (email: string, password: string) => send<unknown>("POST",
export const signOut = () => fetch("/api/auth/logout", { method: "POST", credentials: "same-origin" }).then(() => undefined);

export const fetchProjects = () => get<{ projects: Project[] }>("/api/projects").then((r) => r.projects);
export const putProject = (id: string, body: { name: string; workspace_id?: string; canvas_live?: CanvasLive; master?: string[]; extensions?: string[] }) => send<{ project: Project }>("PUT", `/api/projects/${id}`, body).then((r) => r.project);
/** A project's picture (#426): its URL, versioned by when it was set; null without one. */
export const projectPictureUrl = (p: Project) => (p.picture_at ? `/api/projects/${p.id}/picture?v=${encodeURIComponent(p.picture_at)}` : null);
export async function putProjectPicture(id: string, body: Blob): Promise<Project> {
const res = await fetch(`/api/projects/${id}/picture`, { method: "PUT", credentials: "same-origin", headers: { "content-type": "application/octet-stream" }, body });
const b = (await res.json().catch(() => ({}))) as { project?: Project; error?: string };
if (!res.ok || !b.project) throw new ApiError(res.status, b.error ?? `Upload failed (${res.status})`);
return b.project;
}
export const deleteProjectPicture = (id: string) => send<{ project: Project }>("DELETE", `/api/projects/${id}/picture`).then((r) => r.project);
export const putProject = (id: string, body: { name: string; workspace_id?: string; canvas_live?: CanvasLive; master?: string[]; extensions?: string[]; description?: string }) => send<{ project: Project }>("PUT", `/api/projects/${id}`, body).then((r) => r.project);
export const deleteProject = (id: string) => send<{ deleted: string }>("DELETE", `/api/projects/${id}`);

/** A file as the API gives it (docs/api.md). `body` and `refs` come with single-file answers and with lists that ask for them. */
Expand Down
66 changes: 46 additions & 20 deletions apps/studio/src/apps/AccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,11 @@ function WorkspaceCard({ ws, meId, onChanged }: { ws: Workspace; meId: string; o
);
}

/** Settings › Account: who is signed in, the password, and the workspaces they are in with members and roles. */
/** Settings › Account: who is signed in and the password; nothing else (#426: workspaces have their own section). */
export function AccountPage() {
const user = useUser();
const [list, setList] = useState<Workspace[] | null>(null);
const [error, setError] = useState<string | null>(null);
const load = () => fetchWorkspaces().then((w) => { setList(w); setError(null); }, (e: Error) => setError(e.message));
useEffect(() => { void load(); }, []);
if (user.status !== "signed-in") return null;
const me = user.me.user;
const newWorkspace = async () => {
const name = await prompt({ title: "New workspace", label: "Name", confirmLabel: "Create" });
if (!name) return;
// The list of one's workspaces is part of who is signed in: read that again too.
try { await putWorkspace(crypto.randomUUID(), name); await load(); void boot(); } catch (e) { toastError(e); }
};
return (
<>
<SettingsGroup title="Signed in">
Expand All @@ -144,15 +134,51 @@ export function AccountPage() {
<Button icon="lock" onClick={() => openDialog((close) => <PasswordDialog email={me.email} close={close} />)}>Change password</Button>
</div>
</SettingsGroup>
<SettingsGroup title="Workspaces">
<div className="st-setrow">
<span className="st-note">Every project belongs to one workspace; its members see it with their role. Your personal workspace is yours; invite people into a shared one. Viewers look, editors change content, admins also manage editors and viewers, the owner also admins.</span>
<Button icon="plus" onClick={() => void newWorkspace()}>New workspace</Button>
</div>
{error && <p className="st-note">{error}</p>}
{!list && !error && <Spinner />}
{list?.map((w) => <WorkspaceCard key={w.id} ws={w} meId={me.id} onChanged={() => void load()} />)}
</SettingsGroup>
</>
);
}

/** The workspaces one belongs to, read once per Settings window and again after a change. */
export function useWorkspaceList(): { list: Workspace[] | null; error: string | null; reload: () => void } {
const [list, setList] = useState<Workspace[] | null>(null);
const [error, setError] = useState<string | null>(null);
const reload = () => { void fetchWorkspaces().then((w) => { setList(w); setError(null); }, (e: Error) => setError(e.message)); };
useEffect(reload, []);
return { list, error, reload };
}

/**
* Settings › Workspaces (#426): every workspace you belong to, with your role, its members and projects; one opens on
* its own page. A workspace is shared, so it is a page of Settings, not a file.
*/
export function WorkspacesPage({ list, error, reload, onOpen }: { list: Workspace[] | null; error: string | null; reload: () => void; onOpen: (id: string) => void }) {
const newWorkspace = async () => {
const name = await prompt({ title: "New workspace", label: "Name", confirmLabel: "Create" });
if (!name) return;
// The list of one's workspaces is part of who is signed in: read that again too.
try { const id = crypto.randomUUID(); await putWorkspace(id, name); reload(); void boot(); onOpen(id); } catch (e) { toastError(e); }
};
return (
<SettingsGroup title="Your workspaces">
<div className="st-setrow">
<span className="st-note">Every project belongs to one workspace; its members see it with their role. Your personal workspace is yours; invite people into a shared one. Viewers look, editors change content, admins also manage members.</span>
<Button icon="plus" onClick={() => void newWorkspace()}>New workspace</Button>
</div>
{error && <p className="st-note">{error}</p>}
{!list && !error && <Spinner />}
{list?.map((w) => (
<button key={w.id} type="button" className="st-setrow st-wsrow" onClick={() => onOpen(w.id)}>
<span><strong>{w.name}</strong> {w.personal && <Badge>Personal</Badge>}<br /><span className="st-note">Your role: {ROLE_LABEL[w.role]} · {w.member_count} member{w.member_count === 1 ? "" : "s"} · {w.project_count} project{w.project_count === 1 ? "" : "s"}</span></span>
<span className="st-note">Open ›</span>
</button>
))}
</SettingsGroup>
);
}

/** One workspace's page: rename, members and roles, a new project in it. */
export function WorkspacePage({ ws, onChanged }: { ws: Workspace; onChanged: () => void }) {
const user = useUser();
if (user.status !== "signed-in") return null;
return <WorkspaceCard ws={ws} meId={user.me.user.id} onChanged={onChanged} />;
}
85 changes: 68 additions & 17 deletions apps/studio/src/apps/ProjectApp.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from "preact/hooks";
import { Badge, Button, EmptyState, FormField, PropertyList, PropertyRow, PropertySection, Segmented, Select, SettingsGroup, Table, TextInput } from "@openclapper/design";
import { ApiError } from "../api";
import { AppIcon, Badge, Button, EmptyState, FormField, PropertyList, PropertyRow, PropertySection, Segmented, Select, SettingsGroup, TextArea, TextInput } from "@openclapper/design";
import { ApiError, projectPictureUrl, type Project } from "../api";
import { confirm, prompt, toast, toastError } from "../hosts";
import { switchProject } from "../project";
import { removeProject, saveProject, setCanvasLive, useProjects } from "../projects";
import { removeProject, saveProject, setCanvasLive, setDescription, setPicture, useProjects } from "../projects";
import { useUser } from "../user";
import { useCatalogue } from "../props";
import { NamingPage } from "./project/Naming";
Expand Down Expand Up @@ -95,6 +95,9 @@ export function ProjectPage({ tab, winId, projectId }: { tab: ProjectTab; winId:
</span>
</FormField>
</SettingsGroup>
<SettingsGroup title="Picture and description">
<ProjectLook p={current} />
</SettingsGroup>
<SettingsGroup title="About this project">
<PropertyList>
<PropertySection title="Facts" icon="info">
Expand All @@ -120,22 +123,70 @@ export function ProjectPage({ tab, winId, projectId }: { tab: ProjectTab; winId:
{editable.length > 1 && <Select size="sm" value={newWs || editable.find((w) => w.personal)?.id || editable[0].id} onChange={setNewWs} options={editable.map((w) => ({ value: w.id, label: w.name }))} aria-label="Workspace of the new project" />}
<Button variant="primary" icon="plus" disabled={editable.length === 0} onClick={() => void create()}>New project</Button>
</span>
{projects.length === 0 ? <EmptyState icon="slate" title="No project yet" /> : (
<Table className="st-projects" cursor={projectId} onOpen={(id) => void open(id)}
columns={[{ key: "name", label: "Name" }, { key: "ws", label: "Workspace" }, { key: "files", label: "Files", numeric: true }, { key: "created", label: "Created" }, { key: "act", label: "", width: 190 }]}
rows={projects.map((p) => ({
id: p.id,
name: <span className="st-inline"><strong>{p.name}</strong>{p.id === projectId && <Badge tone="accent">Open</Badge>}</span>,
ws: wsName(p.workspace_id), files: String(p.file_count), created: day(p.created_at),
act: <span className="st-inline st-inline--end">
{p.id !== projectId && <Button size="sm" onClick={() => void open(p.id)}>Open</Button>}
<Button size="sm" variant="quiet" icon="edit" label={`Rename ${p.name}`} onClick={() => void askRename(p.id, p.name)} />
<Button size="sm" variant="quiet" icon="trash" label={`Delete ${p.name}`} onClick={() => void remove(p.id)} />
</span>,
}))} />
)}
{projects.length === 0 ? <EmptyState icon="slate" title="No project yet" /> : workspaces.filter((w) => projects.some((p) => p.workspace_id === w.id)).map((w) => (
<section key={w.id} className="st-pgroup">
<h3 className="st-pgroup__head">{w.name}{w.personal && <Badge>Personal</Badge>}<span className="st-note">{projects.filter((p) => p.workspace_id === w.id).length} project{projects.filter((p) => p.workspace_id === w.id).length === 1 ? "" : "s"}</span></h3>
<div className="st-pcards">
{projects.filter((p) => p.workspace_id === w.id).map((p) => (
<ProjectCard key={p.id} p={p} open={p.id === projectId} onOpen={() => void open(p.id)} onRename={() => void askRename(p.id, p.name)} onDelete={() => void remove(p.id)} />
))}
</div>
</section>
))}
</>
)}
</>
);
}

/** A project as a card (#426): its picture, name, description and facts; open, rename, delete. Room for more facts later. */
function ProjectCard({ p, open, onOpen, onRename, onDelete }: { p: Project; open: boolean; onOpen: () => void; onRename: () => void; onDelete: () => void }) {
const pic = projectPictureUrl(p);
return (
<article className={`st-pcard${open ? " is-open" : ""}`} onDblClick={open ? undefined : onOpen}>
<div className="st-pcard__pic">{pic ? <img src={pic} alt="" loading="lazy" /> : <AppIcon glyph="slate" color={2} size={40} />}</div>
<div className="st-pcard__body">
<span className="st-inline"><strong className="st-pcard__name">{p.name}</strong>{open && <Badge tone="accent">Open</Badge>}</span>
{p.description ? <p className="st-pcard__desc">{p.description}</p> : <p className="st-note">No description</p>}
<span className="st-note">{p.file_count} file{p.file_count === 1 ? "" : "s"} · created {day(p.created_at)}</span>
</div>
<div className="st-pcard__act">
{!open && <Button size="sm" onClick={onOpen}>Open</Button>}
<Button size="sm" variant="quiet" icon="edit" label={`Rename ${p.name}`} onClick={onRename} />
<Button size="sm" variant="quiet" icon="trash" label={`Delete ${p.name}`} onClick={onDelete} />
</div>
</article>
);
}

/** The project's picture (uploaded from the computer, scaled to fit 2000 × 2000) and its description, saved on leaving the field. */
function ProjectLook({ p }: { p: Project }) {
const [text, setText] = useState(p.description);
const [busy, setBusy] = useState(false);
useEffect(() => { setText(p.description); }, [p.id, p.description]);
const pic = projectPictureUrl(p);
const choose = (e: Event) => {
const file = (e.currentTarget as HTMLInputElement).files?.[0];
(e.currentTarget as HTMLInputElement).value = "";
if (!file) return;
if (file.size > 40 * 1024 * 1024) { toast("That image is too large to use (at most 40 MB before it is scaled)", { tone: "warning" }); return; }
setBusy(true);
void setPicture(p, file).then(() => toast("Picture set"), toastError).finally(() => setBusy(false));
};
return (
<div className="st-plook">
<div className="st-pcard__pic st-plook__pic">{pic ? <img src={pic} alt="" /> : <AppIcon glyph="slate" color={2} size={48} />}</div>
<div className="st-plook__side">
<span className="st-inline">
<label className="oc-btn oc-btn--secondary oc-btn--sm st-plook__upload">{busy ? "Uploading…" : pic ? "Replace picture…" : "Upload a picture…"}<input type="file" accept="image/jpeg,image/png,image/webp" hidden disabled={busy} onChange={choose} /></label>
{pic && <Button size="sm" variant="quiet" icon="trash" disabled={busy} onClick={() => { setBusy(true); void setPicture(p, null).catch(toastError).finally(() => setBusy(false)); }}>Remove</Button>}
</span>
<span className="st-note">JPEG, PNG or WebP; a larger one is scaled to fit 2000 × 2000.</span>
<FormField label="Description" htmlFor={`pd-${p.id}`}>
<TextArea id={`pd-${p.id}`} rows={4} maxLength={2000} value={text} placeholder="What this project is about" onInput={(e) => setText(e.currentTarget.value)}
onBlur={() => { if (text.trim() !== p.description) void setDescription(p, text.trim()).catch(toastError); }} />
</FormField>
</div>
</div>
);
}
Loading
Loading