teqo-backup/src/utilities/documents.ts
Francisco Ignácio Fontoura Solla 28647e4760 Add Post and Tag collections for news and publications
- Add Post + Tag collections (notícias/publicações) with tag-based
  "Esconder" visibility control for electoral-law compliance
- Add static SEO routes /[type]/[category]/[slug] plus /[type] and
  /[type]/[category] listings, with shared header/breadcrumb and PostCard
- Recreate the homepage and refresh the site color palette (styles.css)
- Add posts/slug/seo utilities and revalidation wiring (documents.ts)
- Add live-fetch seed script (scripts/seed-posts.mjs + seed-loader.mjs,
  db:seed:posts)
- Upgrade Payload 3.79.0 -> 3.82.0 (fixes admin relationship-filter crash)
- Remove obsolete Header component; add shadcn card/badge/breadcrumb;
  update AGENTS.md and components rule docs
2026-07-15 19:12:11 -03:00

43 lines
1.5 KiB
TypeScript

import type { Config } from '@/payload-types'
import configPromise from '@payload-config'
import { revalidateTag, unstable_cache } from 'next/cache'
import { getPayload } from 'payload'
type Collection = keyof Config['collections']
export const getDocuments = <Slug extends Collection>(collection: Slug, depth?: number) =>
getPayload({ config: configPromise }).then((payload) => payload.find({ collection, depth }))
export const getDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
depth?: number,
) =>
getPayload({ config: configPromise }).then((payload) =>
payload.findByID({ collection, id, depth }),
)
const getTag = <Slug extends Collection>(collection: Slug, id: string | number) =>
`document_${collection}:${id}`
export const getCachedDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
depth?: number,
) =>
unstable_cache(() => getDocumentById(collection, id, depth), [getTag(collection, id)], {
tags: [getTag(collection, id)],
})
export const revalidateDocumentById = <Slug extends Collection>(
collection: Slug,
id: string | number,
) => revalidateTag(getTag(collection, id))
const getListingTag = <Slug extends Collection>(collection: Slug) => `${collection}s`
export const revalidateCollectionListing = <Slug extends Collection>(collection: Slug) =>
revalidateTag(getListingTag(collection))
export const revalidatePostsListing = () => revalidateCollectionListing('post')