1
1
import { Button , Modal , useMantineTheme } from '@mantine/core' ;
2
2
import { useState } from 'preact/hooks' ;
3
3
import { route } from 'preact-router' ;
4
+ import { isSlugValid , normalizeSlug } from '../../../shared/slug.js' ;
5
+ import { useCollectionSchema } from '../../hooks/useCollectionSchema.js' ;
4
6
import { cmsCreateDoc } from '../../utils/doc.js' ;
5
7
import { getDefaultFieldValue } from '../../utils/fields.js' ;
6
8
import { SlugInput } from '../SlugInput/SlugInput.js' ;
7
9
import './NewDocModal.css' ;
8
- import { logAction } from '../../utils/actions.js' ;
9
- import { useCollectionSchema } from '../../hooks/useCollectionSchema.js' ;
10
10
11
11
interface NewDocModalProps {
12
12
collection : string ;
13
13
opened ?: boolean ;
14
14
onClose ?: ( ) => void ;
15
15
}
16
16
17
- function isSlugValid ( slug : string ) : boolean {
18
- return Boolean ( slug && slug . match ( / ^ [ a - z 0 - 9 ] + (?: - - ? [ a - z 0 - 9 ] + ) * $ / ) ) ;
19
- }
20
-
21
- /**
22
- * Normalizes a user-entered slug value into one appropriate for the CMS.
23
- *
24
- * In order to keep the slugs "flat" within firestore, nested paths use a double
25
- * dash separator. For example, a URL like "/about/foo" should have a slug like
26
- * "about--foo".
27
- *
28
- * Transformations include:
29
- * Remove leading and trailing space
30
- * Remove leading and trailing slash
31
- * Lower case
32
- * Replace '/' with '--', e.g. 'foo/bar' -> 'foo--bar'
33
- */
34
- function normalizeSlug ( slug : string ) : string {
35
- return slug
36
- . replace ( / ^ [ \s / ] * / g, '' )
37
- . replace ( / [ \s / ] * $ / g, '' )
38
- . replace ( / ^ \/ + | \/ + $ / g, '' )
39
- . toLowerCase ( )
40
- . replaceAll ( '/' , '--' ) ;
41
- }
42
-
43
17
export function NewDocModal ( props : NewDocModalProps ) {
44
18
const [ slug , setSlug ] = useState ( '' ) ;
45
19
const [ rpcLoading , setRpcLoading ] = useState ( false ) ;
@@ -64,7 +38,8 @@ export function NewDocModal(props: NewDocModalProps) {
64
38
setSlugError ( '' ) ;
65
39
66
40
const cleanSlug = normalizeSlug ( slug ) ;
67
- if ( ! isSlugValid ( cleanSlug ) ) {
41
+ const slugRegex = rootCollection . slugRegex ;
42
+ if ( ! isSlugValid ( cleanSlug , slugRegex ) ) {
68
43
setSlugError ( 'Please enter a valid slug (e.g. "foo-bar-123").' ) ;
69
44
setRpcLoading ( false ) ;
70
45
return ;
0 commit comments