8000 feat: added feature where space name is already in use it sugests a n… by Prasad48432 · Pull Request #2383 · dubinc/dub · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: added feature where space name is already in use it sugests a n… #2383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 32 additions & 10 deletions apps/web/ui/workspaces/create-workspace-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ export function CreateWorkspaceForm({

const { isMobile } = useMediaQuery();

async function suggestAvailableSlug(baseSlug: string): Promise<string> {
let counter = 1;
let newSlug = baseSlug;

while (true) {
const res = await fetch(`/api/workspaces/${newSlug}/exists`);
if (res.status === 200 && (await res.json()) === 0) {
return newSlug;
}
newSlug = `${baseSlug}_${counter}`;
counter++;
}
}

return (
<form
(data) => {
Expand Down Expand Up @@ -139,17 +153,20 @@ export function CreateWorkspaceForm({
maxLength: 48,
pattern: /^[a-zA-Z0-9\-]+$/,
})}
=> {
fetch(`/api/workspaces/${slug}/exists`).then(async (res) => {
if (res.status === 200) {
const exists = await res.json();
if (exists === 1)
setError("slug", {
message: `The slug "${slug}" is already in use.`,
});
else clearErrors("slug");
() => {
const res = await fetch(`/api/workspaces/${slug}/exists`);
if (res.status === 200) {
const exists = await res.json();
if (exists === 1) {
const newSlug = await suggestAvailableSlug(slug);
setValue("slug", newSlug, { shouldValidate: true });
toast.info(
`The slug "${slug}" is already in use. Suggested: "${newSlug}"`,
);
} else {
clearErrors("slug");
}
});
}
}}
aria-invalid="true"
/>
Expand All @@ -161,6 +178,11 @@ export function CreateWorkspaceForm({
/>
</div>
)}
{slug !== watch("slug") && (
<p className="mt-1 text-sm text-neutral-600">
Suggested slug: <strong>{watch("slug")}</strong>
</p>
)}
</div>
{errors.slug && (
<p className="mt-2 text-sm text-red-600" id="slug-error">
Expand Down
Loading
0