|
1 |
| -/* |
2 | 1 | import type {SanityDocument} from '@sanity/client'
|
3 |
| -import {Box, Card, Text} from '@sanity/ui' |
4 |
| -import {IntentLink} from 'part:sanity/router' |
5 |
| -import Preview from 'part:sanity/preview' |
6 |
| -import schema from 'part:sanity/schema' |
7 |
| -import {WithReferringDocuments} from 'part:sanity/with-referring-documents' |
| 2 | +import {Box, Button, Card, Stack, Text} from '@sanity/ui' |
8 | 3 | import React from 'react'
|
9 |
| -import styled from 'styled-components' |
| 4 | +import {useIntentLink, useSchema} from 'sanity' |
| 5 | +import {SanityPreview, useDocumentStore, WithReferringDocuments} from 'sanity/_unstable' |
10 | 6 |
|
11 | 7 | type Props = {
|
12 | 8 | assetId: string
|
13 | 9 | }
|
14 | 10 |
|
15 |
| -// Brute force styles on all of Sanity's preview components. |
16 |
| -// TODO: Consider using a custom preview component that is able to resolve custom titles |
17 |
| -// (and potentially subtitles) defined at the document schema level. Or anything to ensure |
18 |
| -// that future upstream changes to Sanity's preview components don't break anything here. |
19 |
| -const Container = styled(Box)` |
20 |
| - * { |
21 |
| - color: ${props => props.theme.sanity.color.base.fg}; |
22 |
| - } |
23 |
| -
|
24 |
| - a { |
25 |
| - text-decoration: none; |
26 |
| - } |
27 |
| -
|
28 |
| - h2 { |
29 |
| - font-size: ${props => props.theme.sanity.fonts.text.sizes[1]}; |
30 |
| - } |
31 |
| -` |
32 |
| -
|
33 | 11 | const DocumentList = (props: Props) => {
|
34 | 12 | const {assetId} = props
|
35 | 13 |
|
36 |
| - const renderChild = (renderProps: {isLoading: boolean; referringDocuments: SanityDocument}) => { |
37 |
| - const {isLoading, referringDocuments} = renderProps |
| 14 | + const documentStore = useDocumentStore() |
38 | 15 |
|
39 |
| - const draftIds = referringDocuments.reduce( |
40 |
| - (acc: string[], doc: SanityDocument) => |
41 |
| - doc._id.startsWith('drafts.') ? acc.concat(doc._id.slice(7)) : acc, |
42 |
| - [] |
43 |
| - ) |
| 16 | + return ( |
| 17 | + <WithReferringDocuments documentStore={documentStore} id={assetId}> |
| 18 | + {({isLoading, referringDocuments}) => ( |
| 19 | + <ReferringDocuments isLoading={isLoading} referringDocuments={referringDocuments} /> |
| 20 | + )} |
| 21 | + </WithReferringDocuments> |
| 22 | + ) |
| 23 | +} |
44 | 24 |
|
45 |
| - const filteredDocuments: SanityDocument[] = referringDocuments.filter( |
46 |
| - (doc: SanityDocument) => !draftIds.includes(doc._id) |
47 |
| - ) |
| 25 | +const ReferringDocuments = (props: {isLoading: boolean; referringDocuments: SanityDocument[]}) => { |
| 26 | + const {isLoading, referringDocuments} = props |
48 | 27 |
|
49 |
| - if (isLoading) { |
50 |
| - return <Text size={1}>Loading...</Text> |
51 |
| - } |
| 28 | + const schema = useSchema() |
52 | 29 |
|
53 |
| - if (filteredDocuments.length === 0) { |
54 |
| - return <Text size={1}>No documents are referencing this asset</Text> |
55 |
| - } |
| 30 | + const draftIds = referringDocuments.reduce( |
| 31 | + (acc: string[], doc: SanityDocument) => |
| 32 | + doc._id.startsWith('drafts.') ? acc.concat(doc._id.slice(7)) : acc, |
| 33 | + [] |
| 34 | + ) |
56 | 35 |
|
57 |
| - return filteredDocuments?.map(doc => { |
58 |
| - const schemaType = schema.get(doc._type) |
| 36 | + const filteredDocuments: SanityDocument[] = referringDocuments.filter( |
| 37 | + (doc: SanityDocument) => !draftIds.includes(doc._id) |
| 38 | + ) |
59 | 39 |
|
60 |
| - return ( |
61 |
| - <Card |
62 |
| - key={doc._id} |
63 |
| - marginBottom={2} |
64 |
| - padding={2} |
65 |
| - radius={2} |
66 |
| - shadow={1} |
67 |
| - style={{overflow: 'hidden'}} |
68 |
| - > |
69 |
| - <Box> |
70 |
| - {schemaType ? ( |
71 |
| - <IntentLink intent="edit" params={{id: doc._id}} key={doc._id}> |
72 |
| - <Preview layout="default" value={doc} type={schemaType} /> |
73 |
| - </IntentLink> |
74 |
| - ) : ( |
75 |
| - <Box padding={2}> |
76 |
| - <Text size={1}> |
77 |
| - A document of the unknown type <em>{doc._type}</em> |
78 |
| - </Text> |
79 |
| - </Box> |
80 |
| - )} |
81 |
| - </Box> |
82 |
| - </Card> |
83 |
| - ) |
84 |
| - }) |
| 40 | + if (isLoading) { |
| 41 | + return <Text size={1}>Loading...</Text> |
| 42 | + } |
| 43 | + |
| 44 | + if (filteredDocuments.length === 0) { |
| 45 | + return <Text size={1}>No documents are referencing this asset</Text> |
85 | 46 | }
|
86 | 47 |
|
87 | 48 | return (
|
88 |
| - <Container> |
89 |
| - <WithReferringDocuments id={assetId}>{renderChild}</WithReferringDocuments> |
90 |
| - </Container> |
| 49 | + <Card flex={1} marginBottom={2} padding={2} radius={2} shadow={1}> |
| 50 | + <Stack space={2}> |
| 51 | + {filteredDocuments?.map(doc => { |
| 52 | + const schemaType = schema.get(doc._type) |
| 53 | + |
| 54 | + const {onClick} = useIntentLink({ |
| 55 | + intent: 'edit', |
| 56 | + params: {id: doc._id} |
| 57 | + }) |
| 58 | + |
| 59 | + return schemaType ? ( |
| 60 | + <Button |
| 61 | + key={doc._id} |
| 62 | + mode="bleed" |
| 63 | + onClick={onClick} |
| 64 | + padding={2} |
| 65 | + style={{width: '100%'}} |
| 66 | + > |
| 67 | + <SanityPreview layout="default" schemaType={schemaType} value={doc} /> |
| 68 | + </Button> |
| 69 | + ) : ( |
| 70 | + <Box padding={2}> |
| 71 | + <Text size={1}> |
| 72 | + A document of the unknown type <em>{doc._type}</em> |
| 73 | + </Text> |
| 74 | + </Box> |
| 75 | + ) |
| 76 | + })} |
| 77 | + </Stack> |
| 78 | + </Card> |
91 | 79 | )
|
92 | 80 | }
|
93 |
| -*/ |
94 |
| - |
95 |
| -import React from 'react' |
96 |
| -import {Text} from '@sanity/ui' |
97 |
| - |
98 |
| -type Props = { |
99 |
| - assetId: string |
100 |
| -} |
101 |
| - |
102 |
| -const DocumentList = (props: Props) => { |
103 |
| - const {assetId} = props |
104 |
| - return <Text>Document list for Asset with ID {assetId} should go here</Text> |
105 |
| -} |
106 | 81 |
|
107 | 82 | export default DocumentList
|
0 commit comments