|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | + |
| 3 | +// Define the fields to display and their custom headers |
| 4 | +const DISPLAY_FIELDS = { |
| 5 | + alg: 'Algorithm Name', |
| 6 | + decodedMediaTypes: 'Media Types', |
| 7 | + 'entryMetadata.informationalUrl': 'URL', |
| 8 | + 'entryMetadata.contact': 'Contact', |
| 9 | +}; |
| 10 | + |
| 11 | +// Helper function to safely extract nested values and flatten arrays |
| 12 | +const getNestedValue = (obj, path) => { |
| 13 | + const value = path |
| 14 | + .split('.') |
| 15 | + .reduce( |
| 16 | + (acc, key) => (acc && acc[key] !== undefined ? acc[key] : 'N/A'), |
| 17 | + obj, |
| 18 | + ); |
| 19 | + return Array.isArray(value) ? value.join(', ') : value; // Flatten arrays into a comma-separated string |
| 20 | +}; |
| 21 | + |
| 22 | +// Function to extract and filter only the required fields |
| 23 | +const extractAndFilterObject = (obj) => { |
| 24 | + let result = {}; |
| 25 | + for (const key in DISPLAY_FIELDS) { |
| 26 | + result[key] = getNestedValue(obj, key); |
| 27 | + } |
| 28 | + return result; |
| 29 | +}; |
| 30 | + |
| 31 | +// Helper function to capitalize each word in a string |
| 32 | +const capitalizeWords = (str) => { |
| 33 | + return str |
| 34 | + .split(' ') |
| 35 | + .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) |
| 36 | + .join(' '); |
| 37 | +}; |
| 38 | + |
| 39 | +const JSONToTable = () => { |
| 40 | + const [jsonData, setJsonData] = useState(null); |
| 41 | + const [error, setError] = useState(null); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + fetch('/sb-alg-list.json') |
| 45 | + .then((response) => { |
| 46 | + if (!response.ok) { |
| 47 | + throw new Error('Failed to fetch the JSON file'); |
| 48 | + } |
| 49 | + return response.json(); |
| 50 | + }) |
| 51 | + .then((data) => { |
| 52 | + setJsonData(data); |
| 53 | + setError(null); |
| 54 | + }) |
| 55 | + .catch((err) => { |
| 56 | + setError(err.message); |
| 57 | + setJsonData(null); |
| 58 | + }); |
| 59 | + }, []); |
| 60 | + |
| 61 | + // Function to group data by the 'type' field |
| 62 | + const groupDataByType = (data) => { |
| 63 | + return data.reduce((acc, item) => { |
| 64 | + const type = getNestedValue(item, 'type'); |
| 65 | + if (!acc[type]) acc[type] = []; |
| 66 | + acc[type].push(item); |
| 67 | + return acc; |
| 68 | + }, {}); |
| 69 | + }; |
| 70 | + |
| 71 | + const generateTableHeaders = () => { |
| 72 | + return Object.keys(DISPLAY_FIELDS).map((key) => ( |
| 73 | + <th key={key}>{DISPLAY_FIELDS[key]}</th> |
| 74 | + )); |
| 75 | + }; |
| 76 | + |
| 77 | + const generateTableRows = (data) => { |
| 78 | + return data.map((item, rowIndex) => { |
| 79 | + const filteredData = extractAndFilterObject(item); |
| 80 | + const url = filteredData['entryMetadata.informationalUrl']; |
| 81 | + const description = getNestedValue(item, 'entryMetadata.description'); |
| 82 | + |
| 83 | + return ( |
| 84 | + <tr key={rowIndex}> |
| 85 | + {Object.keys(DISPLAY_FIELDS).map((key, index) => ( |
| 86 | + <td key={index}> |
| 87 | + {key === 'entryMetadata.informationalUrl' ? ( |
| 88 | + url !== 'N/A' && description !== 'N/A' ? ( |
| 89 | + <a href={url} target="_blank" rel="noopener noreferrer"> |
| 90 | + {description} |
| 91 | + </a> |
| 92 | + ) : ( |
| 93 | + 'N/A' |
| 94 | + ) |
| 95 | + ) : ( |
| 96 | + filteredData[key] |
| 97 | + )} |
| 98 | + </td> |
| 99 | + ))} |
| 100 | + </tr> |
| 101 | + ); |
| 102 | + }); |
| 103 | + }; |
| 104 | + |
| 105 | + if (error) return <p style={{ color: 'red' }}>{error}</p>; |
| 106 | + |
| 107 | + if (!jsonData || !Array.isArray(jsonData)) { |
| 108 | + return <p>No data found in the JSON. Expected an array of objects.</p>; |
| 109 | + } |
| 110 | + |
| 111 | + const groupedData = groupDataByType(jsonData); |
| 112 | + |
| 113 | + return ( |
| 114 | + <div> |
| 115 | + {Object.entries(groupedData).map(([type, data]) => ( |
| 116 | + <div key={type} style={{ marginBottom: '30px' }}> |
| 117 | + <h2>{capitalizeWords(type)} algorithms</h2> |
| 118 | + <table style={{ borderCollapse: 'collapse' }}> |
| 119 | + <thead> |
| 120 | + <tr>{generateTableHeaders()}</tr> |
| 121 | + </thead> |
| 122 | + <tbody>{generateTableRows(data)}</tbody> |
| 123 | + </table> |
| 124 | + </div> |
| 125 | + ))} |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default JSONToTable; |
0 commit comments