PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
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 / SnippetsTable / TableColumns.tsx

TableColumns.tsx in Code Snippets 3.10.0, at js/components/ManageMenu/SnippetsTable/TableColumns.tsx

202 lines 6.2 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 { RawHTML } from '@wordpress/element'
3 import { __ } from '@wordpress/i18n'
4 import React, { Fragment } from 'react'
5 import { useSnippetsAPI } from '../../../hooks/useSnippetsAPI'
6 import { useSnippetsList } from '../../../hooks/useSnippetsList'
7 import { handleUnknownError } from '../../../utils/errors'
8 import { isNetworkAdmin } from '../../../utils/screen'
9 import { getSnippetDisplayName, getSnippetEditUrl, getSnippetType } from '../../../utils/snippets/snippets'
10 import { buildUrl } from '../../../utils/urls'
11 import { Badge } from '../../common/Badge'
12 import { SnippetPriorityInput } from '../../common/snippets/SnippetPriorityInput'
13 import { Tooltip } from '../../common/Tooltip'
14 import { RowActions } from './RowActions'
15 import { useFilteredSnippets } from './WithFilteredSnippetsContext'
16 import { useSnippetsFilters } from './WithSnippetsTableFilters'
17 import type { Key } from 'react'
18 import type { Snippet } from '../../../types/Snippet'
19 import type { ListTableColumn } from '../../common/ListTable'
20
21 interface ColumnProps {
22 snippet: Snippet
23 }
24
25 const RunOnceButton: React.FC<ColumnProps> = ({ snippet }) =>
26 <a
27 className="snippet-execution-button"
28 title={__('Run Once', 'code-snippets')}
29 href={buildUrl(window.location.href, { action: 'run-once', snippet: snippet.id })}
30 >
31 <span className="screen-reader-text">{__('Run Once', 'code-snippets')}</span>
32 <span aria-hidden="true">&nbsp;</span>
33 </a>
34
35 const ActivationSwitch: React.FC<ColumnProps> = ({ snippet }) => {
36 const { activate, deactivate } = useSnippetsAPI()
37 const { refreshSnippetsList } = useSnippetsList()
38
39 const actionText = snippet.network && !snippet.shared_network
40 ? snippet.active ? __('Network Deactivate', 'code-snippets') : __('Network Activate', 'code-snippets')
41 : snippet.active ? __('Deactivate', 'code-snippets') : __('Activate', 'code-snippets')
42
43 return (
44 <input
45 id={`snippet-${snippet.id}-switch`}
46 type="checkbox"
47 role="switch"
48 checked={snippet.active}
49 aria-checked={snippet.active}
50 className="switch"
51 title={actionText}
52 aria-label={actionText}
53 onChange={() => {
54 (snippet.active ? deactivate(snippet) : activate(snippet))
55 .then(refreshSnippetsList)
56 .catch(handleUnknownError)
57 }}
58 />
59 )
60 }
61
62 export const ActivateColumn: React.FC<ColumnProps> = ({ snippet }) => {
63 const { activeByCondition } = useFilteredSnippets()
64
65 if (snippet.trashed) {
66 return ''
67 }
68
69 switch (snippet.scope) {
70 case 'single-use':
71 return <RunOnceButton snippet={snippet} />
72
73 case 'condition':
74 return (
75 <a className="snippet-condition-count" href={getSnippetEditUrl(snippet)}>
76 {activeByCondition.get(snippet.id)?.length ?? 0}
77 </a>
78 )
79
80 default:
81 return <ActivationSwitch snippet={snippet} />
82 }
83 }
84
85 export const SnippetExtraIcons: React.FC<ColumnProps> = ({ snippet }) =>
86 <div className="extra-icons">
87 {snippet.locked && (
88 <Tooltip
89 inline
90 end
91 label={__('About snippet lock', 'code-snippets')}
92 icon={<span className="dashicons dashicons-lock" aria-hidden="true"></span>}
93 >
94 {__('This snippet is locked and cannot be modified.', 'code-snippets')}
95 </Tooltip>)}
96 </div>
97
98 export const SnippetName: React.FC<ColumnProps> = ({ snippet }) =>
99 <>
100 {!snippet.trashed && (isNetworkAdmin() || !snippet.network || window.CODE_SNIPPETS_MANAGE?.hasNetworkCap)
101 ? <a
102 href={getSnippetEditUrl(snippet)}
103 className="snippet-name"
104 title={getSnippetDisplayName(snippet)}
105 >
106 {getSnippetDisplayName(snippet)}
107 </a>
108 : <span className="snippet-name" title={getSnippetDisplayName(snippet)}>
109 {getSnippetDisplayName(snippet)}
110 </span>}
111
112 {snippet.shared_network && <span className="badge">{__('Shared on Network', 'code-snippets')}</span>}
113 </>
114
115 const NameColumn: React.FC<ColumnProps> = ({ snippet }) =>
116 <>
117 <SnippetExtraIcons snippet={snippet} />
118 <SnippetName snippet={snippet} />
119 <RowActions snippet={snippet} />
120 </>
121
122 export const TypeColumn: React.FC<ColumnProps> = ({ snippet }) => {
123 const { setCurrentType } = useSnippetsFilters()
124 const type = getSnippetType(snippet)
125
126 return (
127 <a
128 href={buildUrl(window.location.href, { type })}
129 onClick={event => {
130 event.preventDefault()
131 setCurrentType(type)
132 }}
133 >
134 <Badge name={type} />
135 </a>
136 )
137 }
138
139 export const TagsColumn: React.FC<ColumnProps> = ({ snippet }) =>
140 snippet.tags.map((tag, index) =>
141 <Fragment key={tag}>
142 <a key={tag} href={buildUrl(window.location.href, { tag })}>
143 {tag}
144 </a>
145 {index < snippet.tags.length - 1 ? ', ' : ''}
146 </Fragment>)
147
148 export const DateColumn: React.FC<ColumnProps> = ({ snippet }) =>
149 snippet.modified
150 ? <span className="modified-column-content" title={snippet.modified}>
151 <time dateTime={snippet.modified}>
152 {humanTimeDiff(snippet.modified, undefined)}
153 </time>
154 </span>
155 : <>&#8212;</>
156
157 const baseTableColumns: ListTableColumn<Snippet>[] = [
158 {
159 id: 'activate',
160 title: <span className="screen-reader-text">{__('Activate', 'code-snippets')}</span>,
161 render: snippet => <ActivateColumn snippet={snippet} />
162 },
163 {
164 id: 'name',
165 title: __('Name', 'code-snippets'),
166 isPrimary: true,
167 sortedValue: snippet => getSnippetDisplayName(snippet).toLowerCase(),
168 render: snippet => <NameColumn snippet={snippet} />
169 },
170 {
171 id: 'type',
172 title: __('Type', 'code-snippets'),
173 sortedValue: snippet => getSnippetType(snippet),
174 render: snippet => <TypeColumn snippet={snippet} />
175 },
176 {
177 id: 'desc',
178 title: __('Description', 'code-snippets'),
179 render: snippet => <div className="snippet-description-content"><RawHTML>{snippet.desc}</RawHTML></div>
180 },
181 {
182 id: 'tags',
183 title: __('Tags', 'code-snippets'),
184 render: snippet => <TagsColumn snippet={snippet} />
185 },
186 {
187 id: 'date',
188 title: __('Modified', 'code-snippets'),
189 sortedValue: snippet => snippet.modified ? new Date(snippet.modified).toISOString() : '',
190 render: snippet => <DateColumn snippet={snippet} />
191 },
192 {
193 id: 'priority',
194 title: __('Priority', 'code-snippets'),
195 sortedValue: snippet => snippet.priority,
196 render: snippet => <SnippetPriorityInput snippet={snippet} />
197 }
198 ]
199
200 export const getTableColumns = (hiddenColumns: Set<Key>): ListTableColumn<Snippet>[] =>
201 baseTableColumns.map(column => ({ ...column, isHidden: hiddenColumns.has(column.id) }))
202