Skip to content

Commit 7522e36

Browse files
committed
fix: simplify document name validation and auto-sanitize slug for DeckPanel
1 parent c58a8f7 commit 7522e36

1 file changed

Lines changed: 9 additions & 26 deletions

File tree

packages/webapp/components/pages/panels/DeckPanel.tsx

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,10 @@ const DeckPanel = ({ hostname }: DeckPanelProps) => {
1818
const { isAuthServiceAvailable } = useStore((state) => state.settings)
1919

2020
const [loadingDoc, setLoadingDoc] = useState<boolean>(false)
21-
const [error, setError] = useState<string | null>(null)
2221
const [documentName, setDocumentName] = useState<string>('')
2322
// @ts-ignore - TabsContext has a setActiveTab property
2423
const { setActiveTab } = useContext(TabsContext)
2524

26-
const validateDocName = (docSlug: string): [string | null, string] => {
27-
const slug = slugify(docSlug, { lower: true, strict: true })
28-
let errorMessage = null
29-
30-
if (docSlug.length < 3 || docSlug.length > 30) {
31-
errorMessage = 'Document name must be 3 to 30 characters'
32-
} else if (docSlug !== slug) {
33-
errorMessage = 'Only lowercase letters, numbers and dashes are allowed'
34-
}
35-
36-
return [errorMessage, slug]
37-
}
38-
3925
const enterToPad = (target: TargetType) => {
4026
setLoadingDoc(true)
4127
let docSlug = documentName
@@ -44,14 +30,17 @@ const DeckPanel = ({ hostname }: DeckPanelProps) => {
4430
docSlug = (Math.random() + 1).toString(36).substring(2)
4531
}
4632

47-
const [error, slug] = validateDocName(docSlug)
48-
if (error) {
49-
setError(error)
50-
setLoadingDoc(false)
51-
return
33+
// Auto-sanitize the slug behind the scenes
34+
let sanitizedSlug = slugify(docSlug, { lower: true, strict: true })
35+
36+
// Handle length constraints automatically
37+
if (sanitizedSlug.length < 3) {
38+
sanitizedSlug = sanitizedSlug.padEnd(3, 'x')
39+
} else if (sanitizedSlug.length > 30) {
40+
sanitizedSlug = sanitizedSlug.substring(0, 30)
5241
}
5342

54-
window.location.href = `/${slug}`
43+
window.location.href = `/${sanitizedSlug}`
5544
}
5645

5746
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
@@ -62,7 +51,6 @@ const DeckPanel = ({ hostname }: DeckPanelProps) => {
6251

6352
const handleInputChange = (e: ChangeEvent<HTMLInputElement>) => {
6453
setDocumentName(e.target.value)
65-
setError(null)
6654
}
6755

6856
return (
@@ -154,11 +142,6 @@ const DeckPanel = ({ hostname }: DeckPanelProps) => {
154142
onChange={handleInputChange}
155143
onKeyDown={handleKeyDown}
156144
/>
157-
{error && (
158-
<span className="validator-hint text-error md: !m-0 text-xs font-bold">
159-
Must be 3 to 30 characters containing only lowercase letters, numbers or dash
160-
</span>
161-
)}
162145

163146
<button
164147
className="btn btn-primary btn-block"

0 commit comments

Comments
 (0)