@@ -2,14 +2,26 @@ import crypto from 'node:crypto'
22import yaml from 'yaml'
33import defu from 'defu'
44import { ZodError , z } from 'zod'
5- import type { CompleteConfig , Service } from '~/types'
5+ import type { CompleteConfig , Service , Tag } from '~/types'
66
77type DraftService = Omit < Service , 'id' >
88
9- function determineServiceId ( items : DraftService [ ] ) : Service [ ] {
9+ type TagMap = Map < Tag [ 'name' ] , Tag >
10+
11+ function determineService ( items : DraftService [ ] , tags : TagMap ) : Service [ ] {
1012 return items . map ( ( item ) => ( {
11- id : crypto . randomUUID ( ) ,
1213 ...item ,
14+ id : crypto . randomUUID ( ) ,
15+ tags : ( item . tags || [ ] ) . map ( ( tag ) : Tag => {
16+ if ( typeof tag === 'string' ) {
17+ return tags . get ( tag ) || {
18+ name : tag ,
19+ color : 'blue' ,
20+ }
21+ }
22+
23+ return tag
24+ } ) ,
1325 } ) )
1426}
1527
@@ -22,6 +34,7 @@ export function getDefaultConfig(): CompleteConfig {
2234 behaviour : {
2335 target : '_blank' ,
2436 } ,
37+ tags : [ ] ,
2538 services : [ ] ,
2639 }
2740}
@@ -40,6 +53,11 @@ export function validateConfigSchema(config: any) {
4053 color : z . string ( ) . optional ( ) ,
4154 } )
4255
56+ const tag = z . object ( {
57+ name : z . string ( ) ,
58+ color : z . string ( ) ,
59+ } )
60+
4361 const service = z . object ( {
4462 title : z . string ( ) . nullish ( ) . optional ( ) ,
4563 description : z . string ( ) . nullish ( ) . optional ( ) ,
@@ -57,6 +75,7 @@ export function validateConfigSchema(config: any) {
5775 lang : z . string ( ) . optional ( ) ,
5876 theme : z . string ( ) . optional ( ) ,
5977 checkUpdates : z . boolean ( ) . optional ( ) ,
78+ tags : z . array ( tag ) . optional ( ) ,
6079 services : z . union ( [
6180 z . array ( service ) ,
6281 z . record ( z . array ( service ) ) ,
@@ -66,6 +85,14 @@ export function validateConfigSchema(config: any) {
6685 return schema . parse ( config )
6786}
6887
88+ function createTagMap ( tags : Tag [ ] ) : TagMap {
89+ return tags . reduce ( ( acc , tag ) => {
90+ acc . set ( tag . name , tag )
91+
92+ return acc
93+ } , new Map ( ) )
94+ }
95+
6996export async function loadLocalConfig ( ) : Promise < CompleteConfig > {
7097 const defaultConfig = getDefaultConfig ( )
7198 const storage = useStorage ( 'data' )
@@ -79,20 +106,21 @@ export async function loadLocalConfig(): Promise<CompleteConfig> {
79106 const raw = await storage . getItem < string > ( file )
80107 const config = yaml . parse ( raw || '' ) || { }
81108 const services : CompleteConfig [ 'services' ] = [ ]
109+ const tags : TagMap = createTagMap ( config . tags || [ ] )
82110
83111 validateConfigSchema ( config )
84112
85113 if ( Array . isArray ( config . services ) ) {
86114 services . push ( {
87- items : determineServiceId ( config . services ) ,
115+ items : determineService ( config . services , tags ) ,
88116 } )
89117 } else {
90118 const entries = Object . entries < DraftService [ ] > ( config . services || [ ] )
91119
92120 for ( const [ title , items ] of entries ) {
93121 services . push ( {
94122 title,
95- items : determineServiceId ( items ) ,
123+ items : determineService ( items , tags ) ,
96124 } )
97125 }
98126 }
0 commit comments