-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* show url below slug input * reuse getsanitydocumentid logic * add readme * cleanup * improve typing * Update src/components/SlugField.tsx Co-authored-by: Djovanni Tehubijuluw <[email protected]> * use renderdefault * fix typo --------- Co-authored-by: Djovanni Tehubijuluw <[email protected]>
- Loading branch information
1 parent
5207418
commit 6322508
Showing
10 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Stack, Text } from '@sanity/ui'; | ||
import { Reference, SlugInputProps, SlugValue, useEditState, useFormValue } from 'sanity'; | ||
import { PageTreeConfig } from '../types'; | ||
import { usePageTreeItem } from '../hooks/usePageTreeItem'; | ||
import { getSanityDocumentId } from '../utils/sanity'; | ||
|
||
export type SlugFieldProps = { | ||
config: PageTreeConfig; | ||
} & SlugInputProps; | ||
|
||
export const SlugField = (props: SlugFieldProps) => { | ||
const id = useFormValue(['_id']); | ||
const type = useFormValue(['_type']); | ||
// eslint-disable-next-line no-warning-comments | ||
// TODO ideally this would be more type safe. | ||
const parentRef = useFormValue(['parent']) as Reference | undefined; | ||
|
||
const { config, value, renderDefault } = props; | ||
return ( | ||
<Stack space={3}> | ||
{renderDefault(props)} | ||
{typeof id == 'string' && typeof type == 'string' && !!parentRef?._ref && ( | ||
<UrlExplanation id={id} type={type} parentId={parentRef?._ref} config={config} value={value} /> | ||
)} | ||
</Stack> | ||
); | ||
}; | ||
|
||
type UrlExplanationProps = { | ||
id: string; | ||
type: string; | ||
parentId: string; | ||
value: SlugValue | undefined; | ||
config: PageTreeConfig; | ||
}; | ||
|
||
const UrlExplanation = ({ id, type, parentId, value, config }: UrlExplanationProps) => { | ||
const state = useEditState(getSanityDocumentId(id), type ?? ''); | ||
const isPublished = !!state.published; | ||
|
||
// we use published perspective so we don't get a draft version of the slug that has been changed of a parent page. | ||
const { page, isLoading } = usePageTreeItem(parentId, config, 'published'); | ||
if (isLoading) return null; | ||
|
||
const path = page?.path == '/' ? `${page?.path}${value?.current}` : `${page?.path}/${value?.current}`; | ||
|
||
const url = config.baseUrl ? `${config.baseUrl}${path}` : null; | ||
const linkToPage = url && ( | ||
<a href={url} target="blank"> | ||
{url} | ||
</a> | ||
); | ||
|
||
const content = isPublished ? <>Link to page: {linkToPage}</> : <>Page url once published: {url ?? path}</>; | ||
|
||
return ( | ||
<Text muted size={1}> | ||
{content} | ||
</Text> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { getRawPageMetadataQuery } from '../queries'; | ||
import { RawPageMetadata, PageTreeConfig } from '../types'; | ||
import { getAllPageMetadata } from '../helpers/page-tree'; | ||
import { useListeningQuery } from 'sanity-plugin-utils'; | ||
import { ClientPerspective } from 'next-sanity'; | ||
|
||
export const usePageTreeItem = (documentId: string, config: PageTreeConfig, perspective?: ClientPerspective) => { | ||
const { data, loading } = useListeningQuery<RawPageMetadata[]>(getRawPageMetadataQuery(config), { | ||
options: { apiVersion: config.apiVersion, perspective }, | ||
}); | ||
|
||
const pageTree = useMemo(() => (data ? getAllPageMetadata(config, data) : undefined), [config, data]); | ||
|
||
return { | ||
isLoading: loading, | ||
page: pageTree?.find(page => page._id === documentId), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const DRAFTS_PREFIX = 'drafts.'; | ||
|
||
/** | ||
* Strips draft id prefix from Sanity document id when draft id is provided. | ||
*/ | ||
export const getSanityDocumentId = (val: string) => val.replace(DRAFTS_PREFIX, ''); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters