Skip to content

Commit 72ffb86

Browse files
authored
normalize pkg name while updating (#2061)
1 parent d595bde commit 72ffb86

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/components/dialogs/edit-package-details-dialog.tsx

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useState } from "react"
1+
import { useState, useMemo } from "react"
22
import {
33
Select,
44
SelectContent,
@@ -28,6 +28,7 @@ import { ChevronDown } from "lucide-react"
2828
import { useLocation } from "wouter"
2929
import { useDeletePackage } from "@/hooks/use-delete-package"
3030
import { GitHubRepositorySelector } from "./GitHubRepositorySelector"
31+
import { normalizeName } from "@/lib/utils/normalizeName"
3132

3233
interface EditPackageDetailsDialogProps {
3334
open: boolean
@@ -95,6 +96,11 @@ export const EditPackageDetailsDialog = ({
9596
const [dangerOpen, setDangerOpen] = useState(false)
9697
const [, setLocation] = useLocation()
9798

99+
const normalizedPackageName = useMemo(() => {
100+
if (!formData.unscopedPackageName.trim()) return ""
101+
return normalizeName(formData.unscopedPackageName)
102+
}, [formData.unscopedPackageName])
103+
98104
const deletePackageMutation = useDeletePackage({
99105
onSuccess: async () => {
100106
await qc.invalidateQueries(["packages"]) // Invalidate the packages query
@@ -126,16 +132,16 @@ export const EditPackageDetailsDialog = ({
126132
formData.githubRepoFullName === "unlink//repo"
127133
? null
128134
: formData.githubRepoFullName,
129-
...(formData.unscopedPackageName !== unscopedPackageName && {
130-
name: formData.unscopedPackageName.trim(),
135+
...(normalizedPackageName !== unscopedPackageName && {
136+
name: normalizedPackageName,
131137
}),
132138
})
133139
if (response.status !== 200)
134140
throw new Error("Failed to update package details")
135141

136142
const filesRes = await axios.get("/package_files/list", {
137143
params: {
138-
package_name_with_version: `${packageAuthor}/${formData.unscopedPackageName}`,
144+
package_name_with_version: `${packageAuthor}/${normalizedPackageName}`,
139145
},
140146
})
141147
const packageFiles: string[] =
@@ -150,25 +156,24 @@ export const EditPackageDetailsDialog = ({
150156
if (hasLicenseChanged) {
151157
if (packageFiles.includes("LICENSE") && !licenseContent) {
152158
await axios.post("/package_files/delete", {
153-
package_name_with_version: `${packageAuthor}/${formData.unscopedPackageName}`,
159+
package_name_with_version: `${packageAuthor}/${normalizedPackageName}`,
154160
file_path: "LICENSE",
155161
})
156162
}
157163
if (licenseContent) {
158164
await axios.post("/package_files/create_or_update", {
159-
package_name_with_version: `${packageAuthor}/${formData.unscopedPackageName}`,
165+
package_name_with_version: `${packageAuthor}/${normalizedPackageName}`,
160166
file_path: "LICENSE",
161167
content_text: licenseContent,
162168
})
163169
}
164170
}
165171

166-
if (formData.unscopedPackageName !== unscopedPackageName) {
167-
// Use router for client-side navigation
172+
if (normalizedPackageName !== unscopedPackageName) {
168173
window.history.replaceState(
169174
{},
170175
"",
171-
`/${packageAuthor}/${formData.unscopedPackageName}`,
176+
`/${packageAuthor}/${normalizedPackageName}`,
172177
)
173178
}
174179

@@ -267,14 +272,25 @@ export const EditPackageDetailsDialog = ({
267272
onChange={(e) =>
268273
setFormData((prev) => ({
269274
...prev,
270-
unscopedPackageName: e.target.value.replace(/\s+/g, ""),
275+
unscopedPackageName: e.target.value,
271276
}))
272277
}
273-
placeholder="Enter package name"
278+
placeholder="my-awesome-package"
274279
disabled={updatePackageDetailsMutation.isLoading}
275280
className="w-full"
276281
autoComplete="off"
277282
/>
283+
{formData.unscopedPackageName.trim() &&
284+
normalizedPackageName !==
285+
formData.unscopedPackageName.trim() &&
286+
normalizedPackageName && (
287+
<div className="flex items-center gap-2 flex-wrap text-xs text-slate-500">
288+
<span>Will be saved as:</span>
289+
<code className="px-1.5 py-0.5 bg-slate-100 dark:bg-slate-800 rounded text-slate-700 dark:text-slate-300 font-mono break-all">
290+
{`${packageAuthor}/${normalizedPackageName}`}
291+
</code>
292+
</div>
293+
)}
278294
</div>
279295
<div className="space-y-1">
280296
<Label htmlFor="website">Website</Label>
@@ -453,7 +469,8 @@ export const EditPackageDetailsDialog = ({
453469
disabled={
454470
updatePackageDetailsMutation.isLoading ||
455471
!hasChanges ||
456-
!isFormValid
472+
!isFormValid ||
473+
!normalizedPackageName
457474
}
458475
className="sm:w-auto lg:w-[115px]"
459476
>

0 commit comments

Comments
 (0)