import React, {useContext} from 'react'; import {Link} from 'react-router-dom'; import {Message, Placeholder, PlaceholderHeader, PlaceholderLine, Table, TableCell, TableRow} from 'semantic-ui-react'; import _ from 'lodash'; import {SortableTable} from '../SortableTable'; import {humanFileSize} from '../Common'; import {ThemeContext, Media} from '../../contexts/contexts'; import {TagsContext} from '../../Tags'; import {allFrequencyOptions} from '../Vars'; /** * Format a frequency value (in seconds) to a human-readable string using allFrequencyOptions. * @param {number|null} frequency - Frequency in seconds * @returns {string} Human-readable frequency text or '-' if null/undefined */ function formatFrequency(frequency) { if (frequency === null || frequency === undefined) { return '-'; } const option = allFrequencyOptions[frequency]; return option ? option.text : `${frequency}s`; } /** * Generate a search link for a collection based on metadata routing configuration. * Supports both query parameter-based (e.g., ?domain=...) and route-based (e.g., /channel/:id/video) linking. * * @param {Object} collection - The collection object * @param {Object} metadata - Collection metadata with routes configuration * @param {string} primaryKey - The primary column key to use for the value * @returns {string|null} The generated link, or null if no link can be generated */ function getCollectionSearchLink(collection, metadata, primaryKey) { const searchRoute = metadata.routes?.search; if (!searchRoute) { return null; } // Check if metadata specifies a query parameter to use if (metadata.routes.searchParam) { // Query parameter-based linking (e.g., /archive?domain=example.com) return `${searchRoute}?${metadata.routes.searchParam}=${collection[primaryKey]}`; } else if (searchRoute.includes(':id')) { // Route parameter-based linking (e.g., /videos/channel/123/video) // Use id_field if specified (e.g., channel_id for channels), otherwise use id const idField = metadata.routes.id_field || 'id'; return searchRoute.replace(':id', collection[idField]); } // No linking strategy available return null; } /** * Mobile row component for collections */ function MobileCollectionRow({collection, metadata}) { const {SingleTag} = useContext(TagsContext); const primaryColumn = metadata.columns[0]; // Use id_field if specified (e.g., channel_id for channels), otherwise use id const idField = metadata.routes?.id_field || 'id'; const editRoute = metadata.routes?.edit?.replace(':id', collection[idField]); const searchLink = getCollectionSearchLink(collection, metadata, primaryColumn.key); return {searchLink ? (

{collection[primaryColumn.key]}

{collection.tag_name && } ) : ( <>

{collection[primaryColumn.key]}

{collection.tag_name && } )} {metadata.columns .filter(col => col.type !== 'actions' && col.key !== primaryColumn.key && col.key !== 'tag_name') .map(col => { let value = collection[col.key]; if (col.format === 'bytes') { value = humanFileSize(value); } else if (col.format === 'frequency') { value = formatFrequency(value); } return (

{col.label}: {value || '-'}

); }) }

{editRoute && Edit}

; } /** * Reusable table component for displaying collections (Domains, Channels, etc). * * @param {Array} collections - Array of collection objects * @param {Object} metadata - Backend-provided metadata containing columns, routes, etc. * @param {String} searchStr - Search filter string (managed by parent) * @param {Function} onRowClick - Optional callback when a row is clicked * @param {String} emptyMessage - Message to display when there are no collections */ export function CollectionTable({collections, metadata, searchStr = '', onRowClick, emptyMessage = 'No items yet'}) { const {inverted} = useContext(ThemeContext); const {SingleTag} = useContext(TagsContext); // Loading state if (collections === null) { return ; } // Error state if (collections === undefined) { return Could not fetch collections ; } // Empty state if (collections && collections.length === 0) { return {emptyMessage} ; } // No metadata available (backward compatibility) if (!metadata) { return No metadata available ; } // Filter collections by search string let filteredCollections = collections; if (searchStr) { const re = new RegExp(_.escapeRegExp(searchStr), 'i'); filteredCollections = collections.filter(collection => { // Search across all string fields return Object.values(collection).some(value => { if (typeof value === 'string') { return re.test(value); } return false; }); }); } // Build row renderer based on metadata const renderRow = (collection) => { const cells = metadata.columns.map(col => { let value = collection[col.key]; // Handle actions column - render action buttons if (col.type === 'actions') { // Use id_field if specified (e.g., channel_id for channels), otherwise use id const idField = metadata.routes?.id_field || 'id'; const editRoute = metadata.routes?.edit?.replace(':id', collection[idField]); const buttonClass = `ui button secondary ${inverted}`; return {editRoute && Edit} ; } // Format the value based on column configuration if (col.format === 'bytes') { value = humanFileSize(value); } else if (col.format === 'frequency') { value = formatFrequency(value); } // Special handling for tag_name column - render SingleTag component if (col.key === 'tag_name' && value) { value = ; } // Special handling for the primary column (usually domain/name) if (col.key === metadata.columns[0].key) { const searchLink = getCollectionSearchLink(collection, metadata, col.key); if (searchLink) { value = {value}; } } return {value || '-'} ; }); return onRowClick && onRowClick(collection)} style={onRowClick ? {cursor: 'pointer'} : {}} > {cells} ; }; // Build table headers from metadata (desktop) const headers = metadata.columns.map((col, index) => { // Determine width based on column position and type // Widths use Semantic UI's 16-column grid system and should sum to 16 let width = null; if (index === 0) { // First column (name/domain) - gets most space to expand // For 6 columns: 16 - (2+2+2+2+1) = 7 width = 7; } else if (col.type === 'actions') { // Actions column - minimum width to shrink to fit button width = 1; } else { // Other columns (tags, counts, frequency, sizes) width = 2; } return { key: col.key, text: col.label, sortBy: col.sortable ? col.key : null, width: width, }; }); // Build mobile headers - simplified columns const primaryColumn = metadata.columns[0]; const mobileHeaders = [ { key: primaryColumn.key, text: primaryColumn.label, sortBy: primaryColumn.sortable ? primaryColumn.key : null }, { key: 'manage', text: 'Manage' } ]; // Get default sort column from metadata (first column key) const defaultSortColumn = metadata.columns[0]?.key || 'id'; return <> } rowKey='id' tableHeaders={mobileHeaders} defaultSortColumn={defaultSortColumn} /> renderRow(collection)} rowKey='id' tableHeaders={headers} defaultSortColumn={defaultSortColumn} /> ; }