Skip to content

Commit 0f5a988

Browse files
committed
refactor: optimize types
1 parent ff2856e commit 0f5a988

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

app/components/OgImage/ShareCard.vue

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,13 @@ const props = withDefaults(
66
defineProps<{
77
name: string
88
theme?: 'light' | 'dark'
9-
color?: string
9+
color?: AccentColorId
1010
}>(),
1111
{ theme: 'dark' },
1212
)
1313
1414
const theme = computed(() => SHARE_CARD_THEMES[props.theme])
15-
const primaryColor = computed(() => {
16-
const id = props.color as AccentColorId | undefined
17-
if (id && id in ACCENT_COLOR_TOKENS) {
18-
return ACCENT_COLOR_TOKENS[id][props.theme].hex
19-
}
20-
return ACCENT_COLOR_TOKENS.sky[props.theme].hex
21-
})
15+
const primaryColor = computed(() => ACCENT_COLOR_TOKENS[props.color ?? 'sky'][props.theme].hex)
2216
2317
const compactFormatter = useCompactNumberFormatter()
2418
const bytesFormatter = useBytesFormatter()

app/components/Package/ShareModal.vue

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ const colorMode = useColorMode()
1111
const theme = computed(() => (colorMode.value === 'dark' ? 'dark' : 'light'))
1212
const { selectedAccentColor } = useAccentColor()
1313
14-
const colorParam = computed(() => {
15-
const id = selectedAccentColor.value as keyof typeof ACCENT_COLOR_TOKENS
16-
if (!id || !(id in ACCENT_COLOR_TOKENS)) return ''
17-
return `&color=${encodeURIComponent(id)}`
18-
})
14+
const colorParam = computed(() =>
15+
selectedAccentColor.value ? `&color=${encodeURIComponent(selectedAccentColor.value)}` : '',
16+
)
1917
2018
const cardUrl = computed(
2119
() => `/api/card/${props.packageName}.png?theme=${theme.value}${colorParam.value}`,

app/pages/share-card/[[org]]/[name].vue

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
<script setup lang="ts">
2-
import { ACCENT_COLOR_IDS, ACCENT_COLOR_TOKENS, type AccentColorId } from '#shared/utils/constants'
2+
import { type AccentColorId, ACCENT_COLOR_TOKENS } from '#shared/utils/constants'
33
44
// This page exists only as a rendering target for nuxt-og-image.
55
// Visiting it directly redirects to the package page.
66
7-
const route = useRoute()
8-
const org = (route.params as any).org as string | undefined
9-
const name = (route.params as any).name as string
7+
const route = useRoute('share-card-org-name')
8+
const { org, name } = route.params
109
const packageName = org ? `${org}/${name}` : name
1110
const theme = route.query.theme === 'light' ? 'light' : 'dark'
12-
const colorParam = route.query.color as string | undefined
13-
const color: AccentColorId = ACCENT_COLOR_IDS.includes(colorParam as AccentColorId)
14-
? (colorParam as AccentColorId)
15-
: 'sky'
16-
17-
const primaryColor = ACCENT_COLOR_TOKENS[color][theme].hex
11+
const colorParam = route.query.color
12+
const color: AccentColorId =
13+
typeof colorParam === 'string' && colorParam in ACCENT_COLOR_TOKENS
14+
? (colorParam as AccentColorId)
15+
: 'sky'
1816
1917
defineOgImageComponent(
2018
'ShareCard',
21-
{ name: packageName, theme, primaryColor },
19+
{ name: packageName, theme, color },
2220
{ width: 1280, height: 520 },
2321
)
2422

0 commit comments

Comments
 (0)