| 1 |
import { humanTimeDiff } from '@wordpress/date' |
| 2 |
import { __, sprintf } from '@wordpress/i18n' |
| 3 |
import React, { useState } from 'react' |
| 4 |
import { getSnippetType } from '../../../utils/snippets/snippets' |
| 5 |
import { stripTags, truncateChars } from '../../../utils/text' |
| 6 |
import { Badge } from '../Badge' |
| 7 |
import { Button } from '../Button' |
| 8 |
import { SnippetCard } from '../snippets/SnippetCard' |
| 9 |
import { CloudSnippetPreviewModal } from '../snippets/SnippetPreviewModal' |
| 10 |
import { CloudStatusIndicator } from './CloudStatusBadge' |
| 11 |
import { CloudSnippetDownloadButton } from './CloudSnippetDownloadButton' |
| 12 |
import type { ReactNode } from 'react' |
| 13 |
import type { CloudSnippetSchema } from '../../../types/schema/CloudSnippetSchema' |
| 14 |
|
| 15 |
export interface CloudSnippetAuthorProps { |
| 16 |
codevaultSlug: string |
| 17 |
} |
| 18 |
|
| 19 |
export const CloudSnippetAuthor: React.FC<CloudSnippetAuthorProps> = ({ codevaultSlug }) => |
| 20 |
<p className="cloud-snippet-author"> |
| 21 |
<a |
| 22 |
href={`${window.CODE_SNIPPETS?.urls.cloud}/codevault/${codevaultSlug}`} |
| 23 |
target="_blank" |
| 24 |
rel="noopener noreferrer" |
| 25 |
> |
| 26 |
{// translators: %s: cloud library author name. |
| 27 |
sprintf(__('By %s', 'code-snippets'), codevaultSlug)} |
| 28 |
</a> |
| 29 |
</p> |
| 30 |
|
| 31 |
interface CloudSnippetDetailsProps { |
| 32 |
snippet: CloudSnippetSchema |
| 33 |
author?: ReactNode |
| 34 |
setIsPreviewOpen: (isOpen: boolean) => void |
| 35 |
} |
| 36 |
|
| 37 |
const CloudSnippetDetails: React.FC<CloudSnippetDetailsProps> = ({ |
| 38 |
snippet, |
| 39 |
author, |
| 40 |
setIsPreviewOpen |
| 41 |
}) => |
| 42 |
<div className="cloud-snippet card-inner"> |
| 43 |
<div className="snippet-card-header"> |
| 44 |
<Badge name={getSnippetType(snippet)} /> |
| 45 |
|
| 46 |
<h3> |
| 47 |
<button |
| 48 |
type="button" |
| 49 |
className="cloud-snippet-title-button" |
| 50 |
title={__('Preview this snippet', 'code-snippets')} |
| 51 |
onClick={() => { |
| 52 |
setIsPreviewOpen(true) |
| 53 |
}} |
| 54 |
> |
| 55 |
{snippet.name} |
| 56 |
</button> |
| 57 |
</h3> |
| 58 |
</div> |
| 59 |
|
| 60 |
{0 < snippet.tags.length || snippet.updated |
| 61 |
? <div className="snippet-card-meta"> |
| 62 |
{0 < snippet.tags.length |
| 63 |
? <span className="cloud-snippet-tags"> |
| 64 |
<span className="snippet-card-tags-label"> |
| 65 |
{__('Tags:', 'code-snippets')} |
| 66 |
</span> {snippet.tags.join(', ')} |
| 67 |
</span> |
| 68 |
: null} |
| 69 |
|
| 70 |
{snippet.updated |
| 71 |
? <time className="snippet-card-modified" dateTime={snippet.updated} title={snippet.updated}> |
| 72 |
{sprintf( |
| 73 |
/* translators: %s: human-readable time difference. */ |
| 74 |
__('Modified %s', 'code-snippets'), |
| 75 |
humanTimeDiff(snippet.updated, undefined) |
| 76 |
)} |
| 77 |
</time> |
| 78 |
: null} |
| 79 |
</div> |
| 80 |
: null} |
| 81 |
|
| 82 |
{snippet.description |
| 83 |
? <p className="snippet-description-content"> |
| 84 |
{truncateChars(stripTags(snippet.description))} |
| 85 |
</p> |
| 86 |
: null} |
| 87 |
|
| 88 |
{author} |
| 89 |
</div> |
| 90 |
|
| 91 |
export interface CloudSnippetCardProps { |
| 92 |
snippet: CloudSnippetSchema |
| 93 |
author?: ReactNode |
| 94 |
isSelected?: boolean |
| 95 |
onSelectedChange?: (isSelected: boolean) => void |
| 96 |
onSnippetDownloaded?: () => void |
| 97 |
} |
| 98 |
|
| 99 |
export const CloudSnippetCard: React.FC<CloudSnippetCardProps> = ({ |
| 100 |
snippet, |
| 101 |
author, |
| 102 |
isSelected = false, |
| 103 |
onSelectedChange, |
| 104 |
onSnippetDownloaded |
| 105 |
}) => { |
| 106 |
const [isPreviewOpen, setIsPreviewOpen] = useState(false) |
| 107 |
|
| 108 |
return ( |
| 109 |
<SnippetCard |
| 110 |
className="cloud-search-result" |
| 111 |
isSelected={isSelected} |
| 112 |
onSelectedChange={onSelectedChange} |
| 113 |
selectionLabel={sprintf( |
| 114 |
/* translators: %s: name of the snippet. */ |
| 115 |
__('Select %s', 'code-snippets'), |
| 116 |
snippet.name |
| 117 |
)} |
| 118 |
footerStatus={<CloudStatusIndicator status={snippet.status} />} |
| 119 |
footer={<> |
| 120 |
<Button secondary onClick={() => setIsPreviewOpen(true)}> |
| 121 |
{__('Preview', 'code-snippets')} |
| 122 |
</Button> |
| 123 |
|
| 124 |
<CloudSnippetDownloadButton snippet={snippet} onDownloaded={onSnippetDownloaded} /> |
| 125 |
</>} |
| 126 |
> |
| 127 |
<CloudSnippetDetails snippet={snippet} author={author} setIsPreviewOpen={setIsPreviewOpen} /> |
| 128 |
|
| 129 |
{isPreviewOpen && ( |
| 130 |
<CloudSnippetPreviewModal snippet={snippet} setIsOpen={setIsPreviewOpen} onDownloaded={onSnippetDownloaded} />)} |
| 131 |
</SnippetCard> |
| 132 |
) |
| 133 |
} |
| 134 |
|