PluginProbe
Code Snippets / 3.10.0-beta.1
Code Snippets v3.10.0-beta.1
4.0.0-beta.2 3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 All 65 releases
code-snippets / js / components / ManageMenu / CommunityCloud / SearchResult.tsx

SearchResult.tsx in Code Snippets 3.10.0-beta.1, at js/components/ManageMenu/CommunityCloud/SearchResult.tsx

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