Skip to content

Commit 24691ae

Browse files
committed
Fix lint and test errors
1 parent c489588 commit 24691ae

7 files changed

Lines changed: 187 additions & 580 deletions

File tree

‎src/actions/webmentions/__tests__/action.spec.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
type MockedAction<Input, Output> = {
4-
handler: (input: Input) => Promise<Output>
4+
handler: (_input: Input) => Promise<Output>
55
}
66

77
const throwActionErrorMock = vi.fn()

‎src/actions/webmentions/action.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { fetchWebmentions } from '@components/WebMentions/server'
77
import type { WebmentionDisplayItem, WebmentionsListResult } from './@types'
88

99
const listInputSchema = z.object({
10-
url: z.string().trim().url(),
10+
url: z.string().trim().pipe(z.url()),
1111
})
1212

1313
const displayProperties = new Set(['mention-of', 'in-reply-to'])

‎src/components/Content/Switcher/client/index.ts‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { LitElement, html } from 'lit'
22
import { defineCustomElement } from '@components/scripts/utils'
33
import type { WebComponentModule } from '@components/scripts/@types/webComponentModule'
4+
import { queryPrefetchLink } from './selectors'
45

56
export type ContentVariant = 'overview' | 'deep-dive'
67

@@ -13,12 +14,23 @@ const queueIdlePrefetch = (work: () => void): void => {
1314
return
1415
}
1516

17+
let hasRun = false
18+
const runOnce = () => {
19+
if (hasRun) {
20+
return
21+
}
22+
23+
hasRun = true
24+
work()
25+
}
26+
1627
if (typeof window.requestIdleCallback === 'function') {
17-
window.requestIdleCallback(() => work())
28+
window.requestIdleCallback(() => runOnce())
29+
window.setTimeout(runOnce, 150)
1830
return
1931
}
2032

21-
window.setTimeout(work, 150)
33+
window.setTimeout(runOnce, 150)
2234
}
2335

2436
const prefetchDocument = (href: string): void => {
@@ -32,7 +44,7 @@ const prefetchDocument = (href: string): void => {
3244

3345
prefetchedHrefs.add(href)
3446

35-
const existingLink = document.head.querySelector(`link[rel="prefetch"][href="${href}"]`)
47+
const existingLink = queryPrefetchLink(document.head, href)
3648
if (!existingLink) {
3749
const link = document.createElement('link')
3850
link.rel = 'prefetch'
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { isType1Element } from '@components/scripts/assertions/elements'
2+
3+
export const SELECTORS = {
4+
prefetchLink: 'link[rel="prefetch"]',
5+
} as const
6+
7+
export const queryPrefetchLink = (root: ParentNode, href: string): HTMLLinkElement | null => {
8+
if (!href) {
9+
return null
10+
}
11+
12+
const candidate = root.querySelector(`${SELECTORS.prefetchLink}[href="${href}"]`)
13+
return isType1Element(candidate) && candidate.tagName === 'LINK'
14+
? (candidate as HTMLLinkElement)
15+
: null
16+
}

‎src/components/WebMentions/client/index.ts‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { defineCustomElement } from '@components/scripts/utils'
55
import { handleScriptError } from '@components/scripts/errors/handler'
66
import type { WebComponentModule } from '@components/scripts/@types/webComponentModule'
77
import type { WebmentionDisplayItem, WebmentionsListResult } from '@actions/webmentions/@types'
8+
import { queryWebMentionsIconMarkup } from './selectors'
89

910
type LoadState = 'idle' | 'loading' | 'ready' | 'error'
1011

@@ -22,17 +23,6 @@ const formatDate = (dateString: string): string => {
2223
}).format(date)
2324
}
2425

25-
const queryIconMarkup = (iconBankId: string, iconName: string): string | null => {
26-
if (typeof document === 'undefined') {
27-
return null
28-
}
29-
30-
const iconBank = document.getElementById(iconBankId)
31-
const iconHost = iconBank?.querySelector(`[data-webmentions-icon="${iconName}"]`)
32-
const markup = iconHost?.innerHTML.trim()
33-
return markup ? markup : null
34-
}
35-
3626
export class WebMentionsElement extends LitElement {
3727
static registeredName = 'web-mentions'
3828

@@ -149,8 +139,14 @@ export class WebMentionsElement extends LitElement {
149139

150140
const newestMentions = [...this.mentions].reverse()
151141
const facepileMentions = newestMentions.slice(0, this.facepileLimit).reverse()
152-
const heartIconMarkup = queryIconMarkup(this.iconBankId, 'heart-filled')
153-
const repostIconMarkup = queryIconMarkup(this.iconBankId, 'background-broken')
142+
const heartIconMarkup = queryWebMentionsIconMarkup({
143+
iconBankId: this.iconBankId,
144+
iconName: 'heart-filled',
145+
})
146+
const repostIconMarkup = queryWebMentionsIconMarkup({
147+
iconBankId: this.iconBankId,
148+
iconName: 'background-broken',
149+
})
154150

155151
return html`
156152
<section class="mt-12 pt-8 border-t border-trim" id="webmentions" aria-labelledby="webmentions-heading">
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const queryWebMentionsIconMarkup = (params: {
2+
iconBankId: string
3+
iconName: 'background-broken' | 'heart-filled'
4+
root?: Document
5+
}): string | null => {
6+
const { iconBankId, iconName, root = document } = params
7+
8+
if (!iconBankId || !iconName) {
9+
return null
10+
}
11+
12+
const iconBank = root.getElementById(iconBankId)
13+
if (!(iconBank instanceof HTMLDivElement)) {
14+
return null
15+
}
16+
17+
const iconHost = iconBank.querySelector(`[data-webmentions-icon="${iconName}"]`)
18+
const markup = iconHost?.innerHTML.trim()
19+
return markup ? markup : null
20+
}

0 commit comments

Comments
 (0)