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
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@tiptap/starter-kit": "^3.0.0",
"class-variance-authority": "^0.7.1",
"cn": "^0.5.2",
"fflate": "^0.8.3",
"highlight.js": "^11.12.0",
"i18next": "^27.1.1",
"lowlight": "^3.3.0",
Expand Down
200 changes: 114 additions & 86 deletions web/src/components/CreateSpaceDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Upload } from "lucide-react"
import { useEffect, useState } from "react"
import { useTranslation } from "react-i18next"
import { useNavigate } from "react-router"
Expand All @@ -14,6 +15,7 @@ import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Textarea } from "@/components/ui/textarea"
import { ApiError, describeApiError, type Space } from "@/lib/api"
import { ImportSpaceDialog } from "@/components/ImportSpaceDialog"
import { isValidSpaceSlug, spaceSlugCandidate } from "@/lib/space"
import { useSpacesStore } from "@/stores/spaces"

Expand Down Expand Up @@ -45,6 +47,7 @@ export function CreateSpaceDialog({
const [busy, setBusy] = useState(false)
const [createError, setCreateError] = useState("")
const [slugError, setSlugError] = useState("")
const [importOpen, setImportOpen] = useState(false)

useEffect(() => {
if (open) {
Expand Down Expand Up @@ -123,97 +126,122 @@ export function CreateSpaceDialog({
}

return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{t("shell.spaces.new")}</DialogTitle>
<DialogDescription>{t("shell.spaces.create.hint")}</DialogDescription>
</DialogHeader>
<form
className="space-y-3"
onSubmit={(event) => {
event.preventDefault()
void submitCreate()
}}
>
<div className="space-y-1.5">
<Label htmlFor="new-space-name">
{t("shell.spaces.create.name")}
</Label>
<Input
id="new-space-name"
name="space-name"
autoFocus
maxLength={64}
value={name}
onChange={(event) => handleNameChange(event.target.value)}
placeholder={t("shell.spaces.create.namePlaceholder")}
/>
</div>
<div className="space-y-1.5">
<Label htmlFor="new-space-slug">
{t("space.settings.info.slug")}
</Label>
<div className="relative">
<span
aria-hidden="true"
className="pointer-events-none absolute top-1/2 left-2.5 -translate-y-1/2 font-mono text-sm text-muted-foreground select-none"
>
/s/
</span>
<>
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle>{t("shell.spaces.new")}</DialogTitle>
<DialogDescription>
{t("shell.spaces.create.hint")}
</DialogDescription>
</DialogHeader>
<form
className="space-y-3"
onSubmit={(event) => {
event.preventDefault()
void submitCreate()
}}
>
<div className="space-y-1.5">
<Label htmlFor="new-space-name">
{t("shell.spaces.create.name")}
</Label>
<Input
id="new-space-slug"
name="space-slug"
value={slug}
onChange={(event) => handleSlugChange(event.target.value)}
autoCapitalize="none"
autoComplete="off"
spellCheck={false}
className="pl-8 font-mono"
aria-invalid={Boolean(slugError)}
id="new-space-name"
name="space-name"
autoFocus
maxLength={64}
value={name}
onChange={(event) => handleNameChange(event.target.value)}
placeholder={t("shell.spaces.create.namePlaceholder")}
/>
</div>
{slugError ? (
<div className="space-y-1.5">
<Label htmlFor="new-space-slug">
{t("space.settings.info.slug")}
</Label>
<div className="relative">
<span
aria-hidden="true"
className="pointer-events-none absolute top-1/2 left-2.5 -translate-y-1/2 font-mono text-sm text-muted-foreground select-none"
>
/s/
</span>
<Input
id="new-space-slug"
name="space-slug"
value={slug}
onChange={(event) => handleSlugChange(event.target.value)}
autoCapitalize="none"
autoComplete="off"
spellCheck={false}
className="pl-8 font-mono"
aria-invalid={Boolean(slugError)}
/>
</div>
{slugError ? (
<p role="alert" className="text-xs text-destructive">
{slugError}
</p>
) : null}
</div>
<div className="space-y-1.5">
<Label htmlFor="new-space-description">
{t("shell.spaces.create.description")}
</Label>
<Textarea
id="new-space-description"
name="space-description"
maxLength={500}
rows={3}
value={description}
onChange={(event) => setDescription(event.target.value)}
placeholder={t("shell.spaces.create.descriptionPlaceholder")}
/>
</div>
{createError ? (
<p role="alert" className="text-xs text-destructive">
{slugError}
{createError}
</p>
) : null}
</div>
<div className="space-y-1.5">
<Label htmlFor="new-space-description">
{t("shell.spaces.create.description")}
</Label>
<Textarea
id="new-space-description"
name="space-description"
maxLength={500}
rows={3}
value={description}
onChange={(event) => setDescription(event.target.value)}
placeholder={t("shell.spaces.create.descriptionPlaceholder")}
/>
</div>
{createError ? (
<p role="alert" className="text-xs text-destructive">
{createError}
</p>
) : null}
<DialogFooter>
<Button
type="button"
variant="ghost"
onClick={() => onOpenChange(false)}
>
{t("common.cancel")}
</Button>
<Button type="submit" disabled={busy}>
{busy
? t("shell.spaces.create.creating")
: t("shell.spaces.create.submit")}
</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
<DialogFooter className="flex-row justify-between sm:justify-between items-center gap-2">
<Button
type="button"
variant="outline"
size="sm"
onClick={() => {
onOpenChange(false)
setImportOpen(true)
}}
>
<Upload className="size-3.5" aria-hidden="true" />
{t("shell.spaces.import")}
</Button>
<div className="flex items-center gap-2">
<Button
type="button"
variant="ghost"
onClick={() => onOpenChange(false)}
>
{t("common.cancel")}
</Button>
<Button type="submit" disabled={busy}>
{busy
? t("shell.spaces.create.creating")
: t("shell.spaces.create.submit")}
</Button>
</div>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
<ImportSpaceDialog
open={importOpen}
onOpenChange={setImportOpen}
onSuccess={(space) => {
if (space && onSuccess) onSuccess(space)
}}
/>
</>
)
}
102 changes: 102 additions & 0 deletions web/src/components/ExportSpaceDialog.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { beforeEach, describe, expect, it, vi } from "vitest"
import { ExportSpaceDialog } from "./ExportSpaceDialog"
import { pagesApi } from "@/lib/api"
import * as exporter from "@/lib/space-archive/export"

vi.mock("@/lib/api", async (importOriginal) => {
const actual = await importOriginal<typeof import("@/lib/api")>()
return {
...actual,
pagesApi: {
...actual.pagesApi,
tree: vi.fn()
}
}
})

vi.mock("@/lib/space-archive/export", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@/lib/space-archive/export")>()
return {
...actual,
exportSpaceArchive: vi.fn()
}
})

describe("ExportSpaceDialog", () => {
beforeEach(() => {
vi.clearAllMocks()
})

it("打开时展示页面数量与预估请求数", async () => {
vi.mocked(pagesApi.tree).mockResolvedValue([
{
id: 1,
parentId: null,
title: "首页",
sortOrder: 0,
updatedAt: "2026-11-28",
children: [
{
id: 2,
parentId: 1,
title: "子页",
sortOrder: 0,
updatedAt: "2026-11-28",
children: []
}
]
}
])

render(
<ExportSpaceDialog
spaceId={1}
spaceName="测试空间"
open={true}
onOpenChange={vi.fn()}
/>
)

expect(await screen.findByText("测试空间")).toBeInTheDocument()
expect(await screen.findByText("2")).toBeInTheDocument() // 页面数量
expect(screen.getByText("~4")).toBeInTheDocument() // 预估请求数 (2 * 2)
})

it("点击导出触发 exportSpaceArchive 并展示完成状态", async () => {
const user = userEvent.setup()
vi.mocked(pagesApi.tree).mockResolvedValue([
{
id: 1,
parentId: null,
title: "首页",
sortOrder: 0,
updatedAt: "2026-11-28",
children: []
}
])

vi.mocked(exporter.exportSpaceArchive).mockResolvedValue({
filename: "test-space-export.zip",
manifest: { warnings: ["测试警告"] } as any,
warningCount: 1
})

render(
<ExportSpaceDialog
spaceId={1}
spaceName="测试空间"
open={true}
onOpenChange={vi.fn()}
/>
)

const startBtn = await screen.findByRole("button", { name: /开始导出/i })
await user.click(startBtn)

expect(await screen.findByText("test-space-export.zip")).toBeInTheDocument()
expect(screen.getByText(/测试警告/)).toBeInTheDocument()
})
})
Loading