Skip to content

Implement NewsApi in Tasks #1281

@DasProffi

Description

@DasProffi

Describe the chore

The NewsApi was removed from hightide implement it in tasks

import { z } from 'zod'
import type { Language } from '../localization/util'
import { LanguageUtil } from '../localization/util'

export type News = {
  title: string,
  date: Date,
  description: (string | URL)[],
  externalResource?: URL,
  keys: string[],
}

export type LocalizedNews = Record<Language, News[]>

export const newsSchema = z.object({
  title: z.string(),
  description: z.string(),
  date: z.string(),
  image: z.string().url().optional(),
  externalResource: z.string().url().optional(),
  keys: z.array(z.string())
}).transform<News>((obj) => {
  let description: (string | URL)[] = [obj.description]
  if (obj.image) {
    description = [new URL(obj.image), ...description]
  }

  return {
    title: obj.title,
    date: new Date(obj.date),
    description,
    externalResource: obj.externalResource ? new URL(obj.externalResource) : undefined,
    keys: obj.keys
  }
})

export const newsListSchema = z.array(newsSchema)

export const localizedNewsSchema = z.record(z.enum(LanguageUtil.languages), newsListSchema)

export const filterNews = (localizedNews: News[], requiredKeys: string[]) => {
  return localizedNews.filter(news => requiredKeys.every(value => news.keys.includes(value)))
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions