import { useState } from 'react' import Markdown from 'react-markdown' // If these vars are missing, we suspect they are not desctructured in the draft method const knownVars = [ 'sa', 'Path', 'Point', 'Snippet', 'paths', 'points', 'snippets', 'absoluteOptions', 'complete', 'measurements', 'options', 'paperless', 'sa', 'scale', 'context', 'getId', 'hide', 'log', 'macro', 'setHidden', 'store', 'unhide', 'units', 'utils', 'Bezier', 'part', ] // Make it easy to suppress escapint in i18next const interpolation = { escapeValue: false } // Extend this method if you want to handle other things than errors export const analyzeDraftLogLine = ({ type, line, t }) => { if (type === 'error' && line.stack) return return null } // Helper component to toggle the stack trace const ShowStackButton = ({ setDetails, details, t, txt = 'clickHereForStackTrace' }) => ( ) // This explains how the error is likely do to restructuring const NotDestructured = ({ missing, setDetails, details, t }) => (
{t('notDestructured', { missing })}
{t('seeLinkOrClick', { link: `[${t('theDraftMethodDocs')}](https://freesewing.dev/reference/api/part/draft)`, click: '', interpolation, })}
) // This explains a var is undefined in the design const DesignsVarUndefined = ({ missing, err, t }) => (
{t('designVarUndefined', { missing, file: err.stack.split('\n')[0].split('/designs/').pop(), interpolation, })}
) // Some other var not being defined const OtherVarUndefined = ({ details, setDetails, missing, err, t }) => (
{t('otherVarUndefined', { missing, interpolation })}
{t('checkForDetailsOrClick', { file: err.stack[0], click: '', interpolation, })}
) // Component that displays an error log line const DraftError = ({ err, t }) => { const [details, setDetails] = useState(false) const data = [] const stack = err.stack.split('\n') // Leave this here, it's intentional. We log the error to you can inpect it. console.log(err) if (err.name === 'ReferenceError') { if (err.message.includes('is not defined')) { const missing = err.message.split(' ').shift() if (stack[0].includes('/designs/')) { data.push() if (knownVars.includes(missing)) data.push() } else data.push() } } return ( <> {data} {t('alsoLogged')} {details ? ( <>
{t('stackTrace')}
    {stack.map((line, i) => (
  1. {line}
  2. ))}
) : null} ) }