columns.tsx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. import { cx } from '@emotion/css';
  2. import { isNumber } from 'lodash';
  3. import React from 'react';
  4. import SVG from 'react-inlinesvg';
  5. import { Field, getFieldDisplayName } from '@grafana/data';
  6. import { config, getDataSourceSrv } from '@grafana/runtime';
  7. import { Checkbox, Icon, IconButton, IconName, TagList } from '@grafana/ui';
  8. import { PluginIconName } from 'app/features/plugins/admin/types';
  9. import { QueryResponse, SearchResultMeta } from '../../service';
  10. import { SelectionChecker, SelectionToggle } from '../selection';
  11. import { TableColumn } from './SearchResultsTable';
  12. const TYPE_COLUMN_WIDTH = 250;
  13. const DATASOURCE_COLUMN_WIDTH = 200;
  14. const SORT_FIELD_WIDTH = 175;
  15. export const generateColumns = (
  16. response: QueryResponse,
  17. availableWidth: number,
  18. selection: SelectionChecker | undefined,
  19. selectionToggle: SelectionToggle | undefined,
  20. clearSelection: () => void,
  21. styles: { [key: string]: string },
  22. onTagSelected: (tag: string) => void,
  23. onDatasourceChange?: (datasource?: string) => void
  24. ): TableColumn[] => {
  25. const columns: TableColumn[] = [];
  26. const access = response.view.fields;
  27. const uidField = access.uid;
  28. const kindField = access.kind;
  29. const sortField = (access as any)[response.view.dataFrame.meta?.custom?.sortBy] as Field;
  30. if (sortField) {
  31. availableWidth -= SORT_FIELD_WIDTH; // pre-allocate the space for the last column
  32. }
  33. let width = 50;
  34. if (selection && selectionToggle) {
  35. width = 30;
  36. columns.push({
  37. id: `column-checkbox`,
  38. width,
  39. Header: () => {
  40. if (selection('*', '*')) {
  41. return (
  42. <div className={styles.checkboxHeader}>
  43. <IconButton name={'check-square' as any} onClick={clearSelection} />
  44. </div>
  45. );
  46. }
  47. return (
  48. <div className={styles.checkboxHeader}>
  49. <Checkbox
  50. checked={false}
  51. onChange={(e) => {
  52. e.stopPropagation();
  53. e.preventDefault();
  54. const { view } = response;
  55. const count = Math.min(view.length, 50);
  56. for (let i = 0; i < count; i++) {
  57. const item = view.get(i);
  58. if (item.uid && item.kind) {
  59. if (!selection(item.kind, item.uid)) {
  60. selectionToggle(item.kind, item.uid);
  61. }
  62. }
  63. }
  64. }}
  65. />
  66. </div>
  67. );
  68. },
  69. Cell: (p) => {
  70. const uid = uidField.values.get(p.row.index);
  71. const kind = kindField ? kindField.values.get(p.row.index) : 'dashboard'; // HACK for now
  72. const selected = selection(kind, uid);
  73. const hasUID = uid != null; // Panels don't have UID! Likely should not be shown on pages with manage options
  74. return (
  75. <div {...p.cellProps}>
  76. <div className={styles.checkbox}>
  77. <Checkbox
  78. disabled={!hasUID}
  79. value={selected && hasUID}
  80. onChange={(e) => {
  81. selectionToggle(kind, uid);
  82. }}
  83. />
  84. </div>
  85. </div>
  86. );
  87. },
  88. field: uidField,
  89. });
  90. availableWidth -= width;
  91. }
  92. // Name column
  93. width = Math.max(availableWidth * 0.2, 300);
  94. columns.push({
  95. Cell: (p) => {
  96. let classNames = cx(styles.nameCellStyle);
  97. let name = access.name.values.get(p.row.index);
  98. if (!name?.length) {
  99. name = 'Missing title'; // normal for panels
  100. classNames += ' ' + styles.missingTitleText;
  101. }
  102. return (
  103. <a {...p.cellProps} href={p.userProps.href} className={classNames} title={name}>
  104. {name}
  105. </a>
  106. );
  107. },
  108. id: `column-name`,
  109. field: access.name!,
  110. Header: () => {
  111. return <div className={styles.headerNameStyle}>Name</div>;
  112. },
  113. width,
  114. });
  115. availableWidth -= width;
  116. width = TYPE_COLUMN_WIDTH;
  117. columns.push(makeTypeColumn(access.kind, access.panel_type, width, styles));
  118. availableWidth -= width;
  119. const meta = response.view.dataFrame.meta?.custom as SearchResultMeta;
  120. if (meta?.locationInfo && availableWidth > 0) {
  121. width = Math.max(availableWidth / 1.75, 300);
  122. availableWidth -= width;
  123. columns.push({
  124. Cell: (p) => {
  125. const parts = (access.location?.values.get(p.row.index) ?? '').split('/');
  126. return (
  127. <div {...p.cellProps} className={cx(styles.locationCellStyle)}>
  128. {parts.map((p) => {
  129. const info = meta.locationInfo[p];
  130. return info ? (
  131. <a key={p} href={info.url} className={styles.locationItem}>
  132. <Icon name={getIconForKind(info.kind)} /> {info.name}
  133. </a>
  134. ) : (
  135. <span key={p}>{p}</span>
  136. );
  137. })}
  138. </div>
  139. );
  140. },
  141. id: `column-location`,
  142. field: access.location ?? access.url,
  143. Header: 'Location',
  144. width,
  145. });
  146. }
  147. // Show datasources if we have any
  148. if (access.ds_uid && onDatasourceChange) {
  149. width = DATASOURCE_COLUMN_WIDTH;
  150. columns.push(
  151. makeDataSourceColumn(
  152. access.ds_uid,
  153. width,
  154. styles.typeIcon,
  155. styles.datasourceItem,
  156. styles.invalidDatasourceItem,
  157. onDatasourceChange
  158. )
  159. );
  160. availableWidth -= width;
  161. }
  162. if (availableWidth > 0) {
  163. columns.push(makeTagsColumn(access.tags, availableWidth, styles.tagList, onTagSelected));
  164. }
  165. if (sortField) {
  166. columns.push({
  167. Header: () => <div className={styles.sortedHeader}>{getFieldDisplayName(sortField)}</div>,
  168. Cell: (p) => {
  169. let value = sortField.values.get(p.row.index);
  170. try {
  171. if (isNumber(value)) {
  172. value = Number(value).toLocaleString();
  173. }
  174. } catch {}
  175. return (
  176. <div {...p.cellProps} className={styles.sortedItems}>
  177. {`${value}`}
  178. </div>
  179. );
  180. },
  181. id: `column-sort-field`,
  182. field: sortField,
  183. width: SORT_FIELD_WIDTH,
  184. });
  185. }
  186. return columns;
  187. };
  188. function getIconForKind(v: string): IconName {
  189. if (v === 'dashboard') {
  190. return 'apps';
  191. }
  192. if (v === 'folder') {
  193. return 'folder';
  194. }
  195. return 'question-circle';
  196. }
  197. function makeDataSourceColumn(
  198. field: Field<string[]>,
  199. width: number,
  200. iconClass: string,
  201. datasourceItemClass: string,
  202. invalidDatasourceItemClass: string,
  203. onDatasourceChange: (datasource?: string) => void
  204. ): TableColumn {
  205. const srv = getDataSourceSrv();
  206. return {
  207. id: `column-datasource`,
  208. field,
  209. Header: 'Data source',
  210. Cell: (p) => {
  211. const dslist = field.values.get(p.row.index);
  212. if (!dslist?.length) {
  213. return null;
  214. }
  215. return (
  216. <div {...p.cellProps} className={cx(datasourceItemClass)}>
  217. {dslist.map((v, i) => {
  218. const settings = srv.getInstanceSettings(v);
  219. const icon = settings?.meta?.info?.logos?.small;
  220. if (icon) {
  221. return (
  222. <span
  223. key={i}
  224. onClick={(e) => {
  225. e.stopPropagation();
  226. e.preventDefault();
  227. onDatasourceChange(settings.uid);
  228. }}
  229. >
  230. <img src={icon} width={14} height={14} title={settings.type} className={iconClass} />
  231. {settings.name}
  232. </span>
  233. );
  234. }
  235. return (
  236. <span className={invalidDatasourceItemClass} key={i}>
  237. {v}
  238. </span>
  239. );
  240. })}
  241. </div>
  242. );
  243. },
  244. width,
  245. };
  246. }
  247. function makeTypeColumn(
  248. kindField: Field<string>,
  249. typeField: Field<string>,
  250. width: number,
  251. styles: Record<string, string>
  252. ): TableColumn {
  253. return {
  254. id: `column-type`,
  255. field: kindField ?? typeField,
  256. Header: 'Type',
  257. Cell: (p) => {
  258. const i = p.row.index;
  259. const kind = kindField?.values.get(i) ?? 'dashboard';
  260. let icon = 'public/img/icons/unicons/apps.svg';
  261. let txt = 'Dashboard';
  262. if (kind) {
  263. txt = kind;
  264. switch (txt) {
  265. case 'dashboard':
  266. txt = 'Dashboard';
  267. break;
  268. case 'folder':
  269. icon = 'public/img/icons/unicons/folder.svg';
  270. txt = 'Folder';
  271. break;
  272. case 'panel':
  273. icon = `public/img/icons/unicons/${PluginIconName.panel}.svg`;
  274. const type = typeField.values.get(i);
  275. if (type) {
  276. txt = type;
  277. const info = config.panels[txt];
  278. if (info?.name) {
  279. txt = info.name;
  280. } else {
  281. switch (type) {
  282. case 'row':
  283. txt = 'Row';
  284. icon = `public/img/icons/unicons/bars.svg`;
  285. break;
  286. case 'singlestat': // auto-migration
  287. txt = 'Singlestat';
  288. break;
  289. default:
  290. icon = `public/img/icons/unicons/question.svg`; // plugin not found
  291. }
  292. }
  293. }
  294. break;
  295. }
  296. }
  297. return (
  298. <div {...p.cellProps} className={styles.typeText}>
  299. <SVG src={icon} width={14} height={14} title={txt} className={styles.typeIcon} />
  300. {txt}
  301. </div>
  302. );
  303. },
  304. width,
  305. };
  306. }
  307. function makeTagsColumn(
  308. field: Field<string[]>,
  309. width: number,
  310. tagListClass: string,
  311. onTagSelected: (tag: string) => void
  312. ): TableColumn {
  313. return {
  314. Cell: (p) => {
  315. const tags = field.values.get(p.row.index);
  316. return tags ? (
  317. <div {...p.cellProps}>
  318. <TagList className={tagListClass} tags={tags} onClick={onTagSelected} />
  319. </div>
  320. ) : null;
  321. },
  322. id: `column-tags`,
  323. field: field,
  324. Header: 'Tags',
  325. width,
  326. };
  327. }