Skip to content
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
15 changes: 8 additions & 7 deletions packages/keystatic/src/app/create-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getCollectionFormat,
getCollectionItemPath,
getSlugFromState,
getSlugForNewItem,
isGitHubConfig,
useShowRestoredDraftMessage,
} from './utils';
Expand Down Expand Up @@ -275,7 +276,7 @@ function CreateItemLocal(props: {

useShowRestoredDraftMessage(props.draft, state, undefined);

const slug = getSlugFromState(collectionConfig, state);
const slug = getSlugForNewItem(collectionConfig, state);

const formatInfo = getCollectionFormat(props.config, props.collection);

Expand Down Expand Up @@ -368,7 +369,7 @@ function CreateItemCollab(props: {
const state = useYJsValue(schema, props.map) as Record<string, unknown>;
const previewProps = usePreviewPropsFromY(schema, props.map, state);

const slug = getSlugFromState(collectionConfig, state);
const slug = getSlugForNewItem(collectionConfig, state);

const formatInfo = getCollectionFormat(props.config, props.collection);

Expand Down Expand Up @@ -455,7 +456,7 @@ function CreateItemInner(props: {
return;
}
if (await props.createItem()) {
const slug = getSlugFromState(collectionConfig, props.state);
const slug = getSlugForNewItem(collectionConfig, props.state);
router.push(`${collectionPath}/item/${encodeURIComponent(slug)}`);
toastQueue.positive('Entry created', { timeout: 5000 }); // TODO: l10n
}
Expand All @@ -464,14 +465,14 @@ function CreateItemInner(props: {
const onCopy = () => {
copyEntryToClipboard(props.state, formatInfo, collectionConfig.schema, {
field: collectionConfig.slugField,
value: getSlugFromState(collectionConfig, props.state),
value: getSlugForNewItem(collectionConfig, props.state),
});
};

const onPaste = async () => {
const entry = await getPastedEntry(formatInfo, collectionConfig.schema, {
field: collectionConfig.slugField,
slug: getSlugFromState(collectionConfig, props.state),
slug: getSlugForNewItem(collectionConfig, props.state),
});
if (entry) {
setValueToPreviewProps(entry, props.previewProps);
Expand Down Expand Up @@ -603,7 +604,7 @@ function CreateItemInner(props: {
if (
await props.createItem({ branch: newBranch, sha: baseCommit })
) {
const slug = getSlugFromState(collectionConfig, props.state);
const slug = getSlugForNewItem(collectionConfig, props.state);

router.push(
`/keystatic/branch/${encodeURIComponent(
Expand All @@ -630,7 +631,7 @@ function CreateItemInner(props: {
<ForkRepoDialog
onCreate={async () => {
if (await props.createItem()) {
const slug = getSlugFromState(collectionConfig, props.state);
const slug = getSlugForNewItem(collectionConfig, props.state);
router.push(
`${collectionPath}/item/${encodeURIComponent(slug)}`
);
Expand Down
13 changes: 13 additions & 0 deletions packages/keystatic/src/app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,16 @@ export function useShowRestoredDraftMessage(
}
}, [draft, show]);
}

export function getSlugForNewItem(
collectionConfig: Collection<Record<string, ComponentSchema>, string> & {
computeSlug?: (args: { slug: string; value: Record<string, unknown> }) => string;
},
state: Record<string, unknown>
): string {
const rawSlug = getSlugFromState(collectionConfig, state);
if (typeof collectionConfig.computeSlug === 'function') {
return collectionConfig.computeSlug({ slug: rawSlug, value: state });
}
return rawSlug;
}