Skip to content

Commit 276b2ef

Browse files
committed
refactor(devtools): unify SEO severity handling across components
This commit refactors the SEO tab components to standardize the handling of severity levels for issues. The `Severity` type has been replaced with `SeoSeverity`, and the `severityColor` function has been removed in favor of a centralized `seoSeverityColor` function. This change improves code consistency and maintainability across the `canonical-url-preview`, `heading-structure-preview`, `json-ld-preview`, and `links-preview` components, ensuring a unified approach to displaying issue severity in the SEO analysis features.
1 parent ffb1f35 commit 276b2ef

File tree

5 files changed

+29
-87
lines changed

5 files changed

+29
-87
lines changed

packages/devtools/src/tabs/seo-tab/canonical-url-preview.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { For } from 'solid-js'
22
import { Section, SectionDescription } from '@tanstack/devtools-ui'
33
import { useStyles } from '../../styles/use-styles'
4-
5-
type Severity = 'error' | 'warning' | 'info'
4+
import { seoSeverityColor, type SeoSeverity } from './seo-severity'
65

76
type Issue = {
8-
severity: Severity
7+
severity: SeoSeverity
98
message: string
109
}
1110

@@ -19,12 +18,6 @@ type CanonicalData = {
1918
issues: Array<Issue>
2019
}
2120

22-
function severityColor(severity: Severity): string {
23-
if (severity === 'error') return '#dc2626'
24-
if (severity === 'warning') return '#d97706'
25-
return '#2563eb'
26-
}
27-
2821
function getCanonicalData(): CanonicalData {
2922
const currentUrl = window.location.href
3023
const current = new URL(currentUrl)
@@ -173,7 +166,7 @@ export function CanonicalUrlPreviewSection() {
173166
<ul class={styles().serpErrorList}>
174167
<For each={data.issues}>
175168
{(issue) => (
176-
<li style={{ color: severityColor(issue.severity), 'margin-top': '4px' }}>
169+
<li style={{ color: seoSeverityColor(issue.severity), 'margin-top': '4px' }}>
177170
[{issue.severity}] {issue.message}
178171
</li>
179172
)}

packages/devtools/src/tabs/seo-tab/heading-structure-preview.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { For, Show } from 'solid-js'
22
import { Section, SectionDescription } from '@tanstack/devtools-ui'
33
import { useStyles } from '../../styles/use-styles'
4-
5-
type Severity = 'error' | 'warning' | 'info'
4+
import { seoSeverityColor, type SeoSeverity } from './seo-severity'
65

76
type HeadingItem = {
87
id: string
@@ -12,16 +11,10 @@ type HeadingItem = {
1211
}
1312

1413
type HeadingIssue = {
15-
severity: Severity
14+
severity: SeoSeverity
1615
message: string
1716
}
1817

19-
function severityColor(severity: Severity): string {
20-
if (severity === 'error') return '#dc2626'
21-
if (severity === 'warning') return '#d97706'
22-
return '#2563eb'
23-
}
24-
2518
function extractHeadings(): Array<HeadingItem> {
2619
const nodes = Array.from(
2720
document.body.querySelectorAll<HTMLHeadingElement>('h1,h2,h3,h4,h5,h6'),
@@ -141,7 +134,7 @@ export function HeadingStructurePreviewSection() {
141134
<ul class={styles().serpErrorList}>
142135
<For each={issues}>
143136
{(issue) => (
144-
<li style={{ color: severityColor(issue.severity), 'margin-top': '4px' }}>
137+
<li style={{ color: seoSeverityColor(issue.severity), 'margin-top': '4px' }}>
145138
[{issue.severity}] {issue.message}
146139
</li>
147140
)}

packages/devtools/src/tabs/seo-tab/json-ld-preview.tsx

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { For, Show } from 'solid-js'
22
import { Section, SectionDescription } from '@tanstack/devtools-ui'
33
import { useStyles } from '../../styles/use-styles'
4+
import { seoSeverityColor, type SeoSeverity } from './seo-severity'
45

56
type JsonLdValue = Record<string, unknown>
67

7-
type IssueSeverity = 'error' | 'warning' | 'info'
8-
98
type ValidationIssue = {
10-
severity: IssueSeverity
9+
severity: SeoSeverity
1110
message: string
1211
}
1312

1413
type SchemaRule = {
1514
required: Array<string>
1615
recommended: Array<string>
1716
optional: Array<string>
18-
allowed: Array<string>
1917
}
2018

2119
type JsonLdEntry = {
@@ -31,85 +29,41 @@ const SUPPORTED_RULES: Record<string, SchemaRule> = {
3129
required: ['@context', '@type', 'name', 'url'],
3230
recommended: ['potentialAction'],
3331
optional: ['description', 'inLanguage'],
34-
allowed: ['name', 'url', 'description', 'inLanguage', 'potentialAction'],
3532
},
3633
Organization: {
3734
required: ['@context', '@type', 'name', 'url'],
3835
recommended: ['logo', 'sameAs'],
3936
optional: ['description', 'email', 'telephone'],
40-
allowed: [
41-
'name',
42-
'url',
43-
'logo',
44-
'sameAs',
45-
'description',
46-
'email',
47-
'telephone',
48-
],
4937
},
5038
Person: {
5139
required: ['@context', '@type', 'name'],
5240
recommended: ['url', 'sameAs'],
53-
optional: ['image', 'jobTitle'],
54-
allowed: ['name', 'url', 'sameAs', 'image', 'jobTitle', 'description'],
41+
optional: ['image', 'jobTitle', 'description'],
5542
},
5643
Article: {
5744
required: ['@context', '@type', 'headline', 'datePublished', 'author'],
5845
recommended: ['dateModified', 'image', 'mainEntityOfPage'],
5946
optional: ['description', 'publisher'],
60-
allowed: [
61-
'headline',
62-
'datePublished',
63-
'author',
64-
'dateModified',
65-
'image',
66-
'mainEntityOfPage',
67-
'description',
68-
'publisher',
69-
],
7047
},
7148
Product: {
7249
required: ['@context', '@type', 'name'],
7350
recommended: ['image', 'description', 'offers'],
7451
optional: ['brand', 'sku', 'aggregateRating', 'review'],
75-
allowed: [
76-
'name',
77-
'image',
78-
'description',
79-
'offers',
80-
'brand',
81-
'sku',
82-
'aggregateRating',
83-
'review',
84-
],
8552
},
8653
BreadcrumbList: {
8754
required: ['@context', '@type', 'itemListElement'],
8855
recommended: [],
8956
optional: ['name'],
90-
allowed: ['itemListElement', 'name'],
9157
},
9258
FAQPage: {
9359
required: ['@context', '@type', 'mainEntity'],
9460
recommended: [],
9561
optional: [],
96-
allowed: ['mainEntity'],
9762
},
9863
LocalBusiness: {
9964
required: ['@context', '@type', 'name', 'address'],
10065
recommended: ['telephone', 'openingHours'],
101-
optional: ['geo', 'priceRange', 'url', 'sameAs'],
102-
allowed: [
103-
'name',
104-
'address',
105-
'telephone',
106-
'openingHours',
107-
'geo',
108-
'priceRange',
109-
'url',
110-
'sameAs',
111-
'image',
112-
],
66+
optional: ['geo', 'priceRange', 'url', 'sameAs', 'image'],
11367
},
11468
}
11569

@@ -222,7 +176,12 @@ function validateEntityByType(entity: JsonLdValue, typeName: string): Array<Vali
222176
})
223177
}
224178

225-
const allowedSet = new Set([...rules.allowed, ...Array.from(RESERVED_KEYS)])
179+
const allowedSet = new Set([
180+
...rules.required,
181+
...rules.recommended,
182+
...rules.optional,
183+
...RESERVED_KEYS,
184+
])
226185
const unknownKeys = Object.keys(entity).filter((key) => !allowedSet.has(key))
227186
if (unknownKeys.length > 0) {
228187
issues.push({
@@ -317,12 +276,6 @@ function analyzeJsonLdScripts(): Array<JsonLdEntry> {
317276
})
318277
}
319278

320-
function severityColor(severity: IssueSeverity): string {
321-
if (severity === 'error') return '#dc2626'
322-
if (severity === 'warning') return '#d97706'
323-
return '#2563eb'
324-
}
325-
326279
function getJsonLdScore(entries: Array<JsonLdEntry>): number {
327280
let errors = 0
328281
let warnings = 0
@@ -409,7 +362,7 @@ function JsonLdBlock(props: { entry: JsonLdEntry; index: number }) {
409362
<ul class={styles().serpErrorList}>
410363
<For each={props.entry.issues}>
411364
{(issue) => (
412-
<li style={{ color: severityColor(issue.severity), 'margin-top': '4px' }}>
365+
<li style={{ color: seoSeverityColor(issue.severity), 'margin-top': '4px' }}>
413366
[{issue.severity}] {issue.message}
414367
</li>
415368
)}

packages/devtools/src/tabs/seo-tab/links-preview.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { For, Show } from 'solid-js'
22
import { Section, SectionDescription } from '@tanstack/devtools-ui'
33
import { useStyles } from '../../styles/use-styles'
4+
import { seoSeverityColor, type SeoSeverity } from './seo-severity'
45

5-
type Severity = 'error' | 'warning' | 'info'
66
type LinkKind = 'internal' | 'external' | 'non-web' | 'invalid'
77

88
type LinkIssue = {
9-
severity: Severity
9+
severity: SeoSeverity
1010
message: string
1111
}
1212

@@ -18,12 +18,6 @@ type LinkRow = {
1818
issues: Array<LinkIssue>
1919
}
2020

21-
function severityColor(severity: Severity): string {
22-
if (severity === 'error') return '#dc2626'
23-
if (severity === 'warning') return '#d97706'
24-
return '#2563eb'
25-
}
26-
2721
function classifyLink(anchor: HTMLAnchorElement): LinkRow {
2822
const href = anchor.getAttribute('href')?.trim() || ''
2923
const text =
@@ -162,7 +156,7 @@ export function LinksPreviewSection() {
162156
<ul class={styles().serpErrorList}>
163157
<For each={row.issues}>
164158
{(issue) => (
165-
<li style={{ color: severityColor(issue.severity) }}>
159+
<li style={{ color: seoSeverityColor(issue.severity) }}>
166160
[{issue.severity}] {issue.message}
167161
</li>
168162
)}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export type SeoSeverity = 'error' | 'warning' | 'info'
2+
3+
export function seoSeverityColor(severity: SeoSeverity): string {
4+
return severity === 'error'
5+
? '#dc2626'
6+
: severity === 'warning'
7+
? '#d97706'
8+
: '#2563eb'
9+
}

0 commit comments

Comments
 (0)