|
| 1 | +/* global __VERSIONS__, __GIT_SHORT_SHA__ */ |
| 2 | + |
| 3 | +import { useDatasetStore } from '@/src/store/datasets'; |
| 4 | +import { useDICOMStore } from '@/src/store/datasets-dicom'; |
| 5 | +import { useImageCacheStore } from '@/src/store/image-cache'; |
| 6 | +import { useSegmentGroupStore } from '@/src/store/segmentGroups'; |
| 7 | + |
| 8 | +const MAX_ERROR_LENGTH = 4000; |
| 9 | + |
| 10 | +const COMPOUND_EXTENSIONS = ['nii.gz', 'iwi.cbor', 'seg.nrrd']; |
| 11 | + |
| 12 | +const parseBrowserInfo = (): string => { |
| 13 | + if (typeof navigator === 'undefined') return 'unknown'; |
| 14 | + |
| 15 | + const { userAgent: ua } = navigator; |
| 16 | + |
| 17 | + const patterns: [string, RegExp][] = [ |
| 18 | + ['Firefox', /Firefox\/([\d.]+)/], |
| 19 | + ['Edge', /Edg\/([\d.]+)/], |
| 20 | + ['Chrome', /Chrome\/([\d.]+)/], |
| 21 | + ['Safari', /Version\/([\d.]+).*Safari/], |
| 22 | + ]; |
| 23 | + |
| 24 | + const match = patterns |
| 25 | + .map(([name, re]) => { |
| 26 | + const m = ua.match(re); |
| 27 | + return m ? `${name} ${m[1]}` : null; |
| 28 | + }) |
| 29 | + .find(Boolean); |
| 30 | + |
| 31 | + const browser = match ?? 'Unknown'; |
| 32 | + |
| 33 | + const os = ['Windows', 'Mac', 'Linux', 'Android', 'iPhone', 'iPad'].find( |
| 34 | + (name) => ua.includes(name) |
| 35 | + ); |
| 36 | + |
| 37 | + const osLabel = |
| 38 | + os === 'Mac' |
| 39 | + ? 'macos' |
| 40 | + : os === 'iPhone' || os === 'iPad' |
| 41 | + ? 'ios' |
| 42 | + : (os?.toLowerCase() ?? 'unknown'); |
| 43 | + |
| 44 | + return `${browser} (${osLabel})`; |
| 45 | +}; |
| 46 | + |
| 47 | +const getSourceFormat = (name: string, isDicom: boolean): string => { |
| 48 | + if (isDicom) return 'DICOM'; |
| 49 | + const lower = name.toLowerCase(); |
| 50 | + const compound = COMPOUND_EXTENSIONS.find((ext) => lower.endsWith(`.${ext}`)); |
| 51 | + if (compound) return compound; |
| 52 | + const lastDot = name.lastIndexOf('.'); |
| 53 | + return lastDot >= 0 ? name.slice(lastDot + 1).toLowerCase() : 'unknown'; |
| 54 | +}; |
| 55 | + |
| 56 | +const formatScalarType = (constructorName: string): string => |
| 57 | + constructorName.replace('Array', ''); |
| 58 | + |
| 59 | +const formatError = (error: unknown): string => { |
| 60 | + if (!error) return 'No error details available'; |
| 61 | + const text = |
| 62 | + error instanceof Error ? (error.stack ?? String(error)) : String(error); |
| 63 | + return text.length > MAX_ERROR_LENGTH |
| 64 | + ? `${text.slice(0, MAX_ERROR_LENGTH)}\n... (truncated)` |
| 65 | + : text; |
| 66 | +}; |
| 67 | + |
| 68 | +const collectDatasetInfo = (): string[] => { |
| 69 | + const datasetStore = useDatasetStore(); |
| 70 | + const imageCacheStore = useImageCacheStore(); |
| 71 | + const dicomStore = useDICOMStore(); |
| 72 | + const segmentGroupStore = useSegmentGroupStore(); |
| 73 | + |
| 74 | + return datasetStore.idsAsSelections.map((id, i) => { |
| 75 | + const metadata = imageCacheStore.getImageMetadata(id); |
| 76 | + const imageData = imageCacheStore.getVtkImageData(id); |
| 77 | + |
| 78 | + const dims = metadata |
| 79 | + ? Array.from(metadata.dimensions).join('\u00d7') |
| 80 | + : 'unknown'; |
| 81 | + |
| 82 | + const scalars = imageData?.getPointData().getScalars()?.getData(); |
| 83 | + const dataType = scalars |
| 84 | + ? formatScalarType(scalars.constructor.name) |
| 85 | + : 'unknown'; |
| 86 | + |
| 87 | + const isDicom = id in dicomStore.volumeInfo; |
| 88 | + const sourceFormat = metadata |
| 89 | + ? getSourceFormat(metadata.name, isDicom) |
| 90 | + : isDicom |
| 91 | + ? 'DICOM' |
| 92 | + : 'unknown'; |
| 93 | + |
| 94 | + const segCount = segmentGroupStore.orderByParent[id]?.length ?? 0; |
| 95 | + const segPart = segCount > 0 ? ` (segment groups: ${segCount})` : ''; |
| 96 | + |
| 97 | + return ` [${i}] ${dims} ${dataType} from ${sourceFormat}${segPart}`; |
| 98 | + }); |
| 99 | +}; |
| 100 | + |
| 101 | +export const generateBugReport = (error: unknown): string => { |
| 102 | + const versions = __VERSIONS__; |
| 103 | + const sha = __GIT_SHORT_SHA__; |
| 104 | + |
| 105 | + const lines = [ |
| 106 | + '--- VolView Bug Report ---', |
| 107 | + `Build: volview ${versions.volview} (${sha}) | vtk.js: ${versions['vtk.js']}, itk-wasm: ${versions['itk-wasm']}`, |
| 108 | + `Browser: ${parseBrowserInfo()}`, |
| 109 | + '', |
| 110 | + 'Error:', |
| 111 | + formatError(error), |
| 112 | + ]; |
| 113 | + |
| 114 | + try { |
| 115 | + const datasets = collectDatasetInfo(); |
| 116 | + const segmentGroupStore = useSegmentGroupStore(); |
| 117 | + |
| 118 | + lines.push('', `Datasets: ${datasets.length}`); |
| 119 | + lines.push(...datasets); |
| 120 | + lines.push(`Save format: ${segmentGroupStore.saveFormat}`); |
| 121 | + } catch { |
| 122 | + lines.push('', 'Datasets: unavailable'); |
| 123 | + } |
| 124 | + |
| 125 | + lines.push('--- End Report ---'); |
| 126 | + |
| 127 | + return lines.join('\n'); |
| 128 | +}; |
0 commit comments