|
1 | | -import { ComponentView } from 'sanity/desk'; |
| 1 | +import { DeskToolContextValue } from 'sanity/desk'; |
| 2 | +import { FaRegCopy } from 'react-icons/fa'; |
| 3 | +import { IoReload } from 'react-icons/io5'; |
| 4 | +import { RxOpenInNewWindow } from 'react-icons/rx'; |
2 | 5 |
|
3 | 6 | /** |
4 | 7 | * |
5 | 8 | * @param {Object} context |
6 | 9 | * @return {ComponentView} |
7 | 10 | */ |
8 | | -export const Preview = (context: ComponentView) => { |
9 | | - const { document, documentId, schemaType } = context; |
| 11 | +export const Preview = (context: DeskToolContextValue) => { |
| 12 | + const { document: d, schemaType } = context; |
| 13 | + const types = [ |
| 14 | + 'documentation', |
| 15 | + 'post', |
| 16 | + 'story', |
| 17 | + 'tutorial', |
| 18 | + // 'landing', |
| 19 | + // 'release', |
| 20 | + ]; |
| 21 | + |
| 22 | + if (!types.includes(schemaType.name)) { |
| 23 | + return <h1>Preview not supported for this content type</h1>; |
| 24 | + } |
| 25 | + |
| 26 | + const base = |
| 27 | + process.env.MODE === 'development' |
| 28 | + ? 'http://localhost:4321' |
| 29 | + : 'https://chromeos.dev'; |
10 | 30 | // Site preview URL? |
11 | | - const previewURL = `https://staging.chromeos.dev/en/posts/${document.displayed.slug.current}?id=${document.displayed._id}&rev=${document.displayed._rev}&preview=true`; |
12 | | - console.log(document); |
13 | | - console.log(documentId); |
14 | | - console.log(schemaType); |
15 | | - console.log(previewURL); |
| 31 | + const previewURL = `${base}/preview?id=${d.displayed._id}&rev=${ |
| 32 | + d.displayed._rev |
| 33 | + }&key=${encodeURIComponent(process.env.SANITY_STUDIO_PREVIEW_KEY)}`; |
16 | 34 |
|
17 | | - const style = { |
18 | | - width: '100%', |
19 | | - height: '50vh', |
20 | | - }; |
| 35 | + const id = `preview-${Math.round(Math.random() * 1000)}`; |
21 | 36 |
|
22 | 37 | return ( |
23 | 38 | <div> |
24 | | - <h1>Document Preview</h1> |
25 | | - <p>Preview URL: {previewURL}</p> |
| 39 | + <h1>Preview</h1> |
| 40 | + <div |
| 41 | + className="actions" |
| 42 | + style={{ |
| 43 | + display: 'flex', |
| 44 | + justifyContent: 'space-between', |
| 45 | + paddingInline: '1rem', |
| 46 | + paddingBlock: '.5rem', |
| 47 | + backgroundColor: '#111', |
| 48 | + gap: '1rem', |
| 49 | + }} |
| 50 | + > |
| 51 | + <button |
| 52 | + style={{ |
| 53 | + appearance: 'none', |
| 54 | + border: 'none', |
| 55 | + backgroundColor: '#111', |
| 56 | + display: 'flex', |
| 57 | + alignItems: 'center', |
| 58 | + gap: '.5rem', |
| 59 | + cursor: 'pointer', |
| 60 | + }} |
| 61 | + onClick={async () => { |
| 62 | + await navigator.clipboard.writeText(previewURL); |
| 63 | + }} |
| 64 | + > |
| 65 | + <FaRegCopy></FaRegCopy> Copy URL |
| 66 | + </button> |
| 67 | + <span |
| 68 | + style={{ |
| 69 | + display: 'flex', |
| 70 | + justifyContent: 'space-between', |
| 71 | + gap: '1rem', |
| 72 | + }} |
| 73 | + > |
| 74 | + <button |
| 75 | + style={{ |
| 76 | + appearance: 'none', |
| 77 | + border: 'none', |
| 78 | + backgroundColor: '#111', |
| 79 | + cursor: 'pointer', |
| 80 | + }} |
| 81 | + onClick={() => { |
| 82 | + const iframe = document.querySelector(`#${id}`); |
| 83 | + if (iframe) iframe.src = previewURL; |
| 84 | + }} |
| 85 | + > |
| 86 | + <IoReload></IoReload> |
| 87 | + </button> |
| 88 | + <a |
| 89 | + href={previewURL} |
| 90 | + target="_blank" |
| 91 | + rel="noopener noreferrer" |
| 92 | + style={{ |
| 93 | + color: 'white', |
| 94 | + }} |
| 95 | + > |
| 96 | + <RxOpenInNewWindow /> |
| 97 | + </a> |
| 98 | + </span> |
| 99 | + </div> |
26 | 100 | <iframe |
27 | 101 | title="preview" |
28 | | - src="https://chromeos.dev" |
29 | | - frameBorder="0" |
30 | | - style={style} |
| 102 | + src={previewURL} |
| 103 | + style={{ |
| 104 | + width: '100%', |
| 105 | + height: '50vh', |
| 106 | + }} |
| 107 | + id={id} |
31 | 108 | ></iframe> |
32 | 109 | </div> |
33 | 110 | ); |
|
0 commit comments