Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(workspace): auto-retry slug conflicts and show editable URL field (#892)
Jiayuan Zhangcd92aad9e146
diff --git a/packages/views/onboarding/step-workspace.tsx b/packages/views/onboarding/step-workspace.tsxindex 13038b87a1..1044832232 100644--- a/packages/views/onboarding/step-workspace.tsx+++ b/packages/views/onboarding/step-workspace.tsx@@ -1,6 +1,6 @@"use client";-import { useState } from "react";+import { useState, useRef } from "react";import { toast } from "sonner";import { Input } from "@multica/ui/components/ui/input";import { Label } from "@multica/ui/components/ui/label";@@ -8,6 +8,8 @@ import { Button } from "@multica/ui/components/ui/button";import { Card, CardContent } from "@multica/ui/components/ui/card";import { useCreateWorkspace } from "@multica/core/workspace/mutations";+const SLUG_REGEX = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;+function nameToSlug(name: string): string {return (name@@ -20,13 +22,34 @@ function nameToSlug(name: string): string {export function StepWorkspace({ onNext }: { onNext: () => void }) {const createWorkspace = useCreateWorkspace();const [name, setName] = useState("");+ const [slug, setSlug] = useState("");+ // Track whether the user has manually edited the slug field.+ const slugTouched = useRef(false);++ const slugError =+ slug.length > 0 && !SLUG_REGEX.test(slug)+ ? "Only lowercase letters, numbers, and hyphens"+ : null;++ const canSubmit =+ name.trim().length > 0 && slug.trim().length > 0 && !slugError;++ const handleNameChange = (value: string) => {+ setName(value);+ if (!slugTouched.current) {+ setSlug(nameToSlug(value));+ }+ };- const canSubmit = name.trim().length > 0;+ const handleSlugChange = (value: string) => {+ slugTouched.current = true;+ setSlug(value);+ };const handleCreate = () => {if (!canSubmit) return;createWorkspace.mutate(- { name: name.trim(), slug: nameToSlug(name.trim()) },+ { name: name.trim(), slug: slug.trim() },{onSuccess: () => onNext(),onError: () => toast.error("Failed to create workspace"),@@ -53,11 +76,30 @@ export function StepWorkspace({ onNext }: { onNext: () => void }) {autoFocustype="text"value={name}- onChange={(e) => setName(e.target.value)}+ onChange={(e) => handleNameChange(e.target.value)}placeholder="My Team"onKeyDown={(e) => e.key === "Enter" && handleCreate()}/></div>+ <div className="space-y-1.5">+ <Label>Workspace URL</Label>+ <div className="flex items-center gap-0 rounded-md border bg-background focus-within:ring-2 focus-within:ring-ring">+ <span className="pl-3 text-sm text-muted-foreground select-none">+ multica.ai/+ </span>+ <Input+ type="text"+ value={slug}+ onChange={(e) => handleSlugChange(e.target.value)}+ placeholder="my-team"+ className="border-0 shadow-none focus-visible:ring-0"+ onKeyDown={(e) => e.key === "Enter" && handleCreate()}+ />+ </div>+ {slugError && (+ <p className="text-xs text-destructive">{slugError}</p>+ )}+ </div></CardContent></Card>diff --git a/server/internal/handler/workspace.go b/server/internal/handler/workspace.goindex 5e4b429871..fdd1156583 100644--- a/server/internal/handler/workspace.go+++ b/server/internal/handler/workspace.go@@ -2,6 +2,7 @@ package handlerimport ("encoding/json"+ "fmt""log/slog""net/http""regexp"@@ -161,19 +162,31 @@ func (h *Handler) CreateWorkspace(w http.ResponseWriter, r *http.Request) {}qtx := h.Queries.WithTx(tx)- ws, err := qtx.CreateWorkspace(r.Context(), db.CreateWorkspaceParams{- Name: req.Name,- Slug: req.Slug,- Description: ptrToText(req.Description),- Context: ptrToText(req.Context),- IssuePrefix: issuePrefix,- })- if err != nil {- if isUniqueViolation(err) {- writeError(w, http.StatusConflict, "workspace slug already exists")++ // Try the requested slug first, then append -2, -3, … on conflict.+ const maxSlugAttempts = 10+ var ws db.Workspace+ slug := req.Slug+ for attempt := 1; attempt <= maxSlugAttempts; attempt++ {+ ws, err = qtx.CreateWorkspace(r.Context(), db.CreateWorkspaceParams{+ Name: req.Name,+ Slug: slug,+ Description: ptrToText(req.Description),+ Context: ptrToText(req.Context),+ IssuePrefix: issuePrefix,+ })+ if err == nil {+ break+ }+ if !isUniqueViolation(err) {+ writeError(w, http.StatusInternalServerError, "failed to create workspace: "+err.Error())return}- writeError(w, http.StatusInternalServerError, "failed to create workspace: "+err.Error())+ // Slug taken — try next suffix.+ slug = fmt.Sprintf("%s-%d", req.Slug, attempt+1)+ }+ if err != nil {+ writeError(w, http.StatusConflict, "workspace slug already exists")return}