Skip to content

Commit

Permalink
feat(site): latest news in home page
Browse files Browse the repository at this point in the history
  • Loading branch information
ourai committed Dec 24, 2024
1 parent f4e4c3e commit 438dd32
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 5 deletions.
22 changes: 20 additions & 2 deletions .knosys/sites/default/src/content/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import { defineCollection } from 'astro:content';
import { defineCollection, z } from 'astro:content';
import { docsSchema } from '@astrojs/starlight/schema';

const docs = defineCollection({ schema: docsSchema() });

const postSchema = z.object({
title: z.string(),
description: z.string().optional(),
url: z.string(),
}).optional();

const posts = defineCollection({
type: 'data',
schema: z.object({
date: z.coerce.date(),
en: postSchema,
zh: postSchema,
}),
});

export const collections = {
docs: defineCollection({ schema: docsSchema() }),
docs,
posts,
};
5 changes: 5 additions & 0 deletions .knosys/sites/default/src/content/posts/20241217211112.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
date: 2024-12-17 21:11:12 +0800
zh:
title: 期待已久的开源 OpenBuild 官网前端代码库来啦!
description: 加入 OpenBuild,共创 Web3 开源!
url: https://openbuildxyz.github.io/eco/zh/posts/the-openbuild-official-website-frontend-codebase-is-open-now/
1 change: 1 addition & 0 deletions .knosys/sites/default/src/domain/post/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as LatestListView } from './views/latest-list';
20 changes: 20 additions & 0 deletions .knosys/sites/default/src/domain/post/repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { SupportedLocale, LocaleValue } from '@/types';
import { getCollection, unwrapLocalValue } from '@/utils';

import type { InternalPost, Post } from './typing';

const posts: InternalPost[] = await getCollection('posts');

function getList(locale: SupportedLocale): Post[] {
return posts
.map(({ date, ...others }) => {
const unwrapped = unwrapLocalValue<Post>(others as LocaleValue<Post>, locale);

return unwrapped ? { ...unwrapped, date } : undefined;
})
.filter(post => !!post)
.slice()
.sort((a, b) => b.date.valueOf() - a.date.valueOf());
}

export { getList };
15 changes: 15 additions & 0 deletions .knosys/sites/default/src/domain/post/typing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { SupportedLocale, PickCollectionData } from '@/types';

type PostLocale = {
browseMore: string;
};

type InternalPost = PickCollectionData<'posts'>;

type Post = Omit<InternalPost, SupportedLocale> & {
title: string;
description: string;
url: string;
};

export type { PostLocale, InternalPost, Post };
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
import type { SupportedLocale } from '@/types';
import { getList } from '../../repository';
import PostCardWidget from '../../widgets/post-card';
const locale = Astro.currentLocale as SupportedLocale;
const posts = getList(locale).slice(0, 3);
---

{posts.length > 0 && (
<div class="grid gap-4 lg:gap-9 grid-cols-1 sm:grid-cols-3">
{posts.map(post => (
<PostCardWidget
dataSource={post}
headerClassName="p-6 pb-1.5"
bodyClassName="p-6 pt-1.5"
footerClassName="px-6"
client:visible
/>
))}
</div>
)}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './LatestList.astro';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import clsx from 'clsx';

import { Card, CardHeader, CardBody, CardFooter } from '@/controls';

import type { Post } from '../../typing';

type PostCardWidgetProps = {
dataSource: Post;
className?: string;
headerClassName?: string;
bodyClassName?: string;
footerClassName?: string;
};

function PostCardWidget({ dataSource, className, headerClassName, bodyClassName, footerClassName }: PostCardWidgetProps) {
return (
<Card className={className}>
<CardHeader className={clsx('flex-col items-start leading-none', headerClassName)}>
<span className="text-lg/tight font-semibold">{dataSource.title}</span>
</CardHeader>
<CardBody className={clsx('py-0 text-sm font-light break-all', bodyClassName)}>{dataSource.description || 'No description'}</CardBody>
<CardFooter className={footerClassName}>{dataSource.date.toLocaleDateString()}</CardFooter>
<a
className="absolute inset-0 z-50"
href={dataSource.url}
target="_blank"
rel="external nofollow"
/>
</Card>
);
}

export default PostCardWidget;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './PostCard';
3 changes: 3 additions & 0 deletions .knosys/sites/default/src/entry/layouts/home/HomeLayout.astro
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
import type { SupportedLocale } from '@/types';
import { LatestListView } from '~/post';
import DefaultLayout from '../default';
import { resolveLocale } from './helper';
Expand All @@ -16,5 +18,6 @@ const locale = resolveLocale((Astro.currentLocale || 'en') as SupportedLocale);
<p class="m-0 text-xl sm:text-2xl text-neutral-500 text-center">{locale.slogan}</p>
<ActionBar className="mt-12" locale={locale} client:visible />
</div>
<LatestListView />
</article>
</DefaultLayout>
7 changes: 7 additions & 0 deletions .knosys/sites/default/src/shared/types/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { CollectionEntry } from 'astro:content';

type DataCollectionKey = 'posts';

type PickCollectionData<K extends DataCollectionKey> = CollectionEntry<K>['data'];

export type { DataCollectionKey, PickCollectionData };
1 change: 1 addition & 0 deletions .knosys/sites/default/src/shared/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './content';
export * from './locale';
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<p><a href="https://openbuild.xyz"><img src="public/images/svg/logo-black.svg" alt="OpenBuild logo" width="300" height="64"></a></p>
<p>An open-source community bridging Web2 to Web3, connecting builders and businesses, and empowering them to succeed!</p>
<p>
<a href="https://openbuild.xyz">Official Website</a>
<a href="https://openbuild.xyz" target="_blank" rel="external">Official Website</a>
·
<a href="https://openbuildxyz.github.io/openbuild-frontend/">Dev Docs</a>
<a href="https://openbuildxyz.github.io/openbuild-frontend/" target="_blank" rel="external">Dev Docs</a>
</p>
</div>

Expand All @@ -14,7 +14,7 @@ This project is a part of [OpenBuild Ecosystem](https://openbuildxyz.github.io/e

## Contributing

Contributions are welcome! But before starting to build, please read the [contributing guide](https://openbuildxyz.github.io/openbuild-frontend/guides/).
Contributions are welcome! But before starting to build, please read the [contributing guide](.github/contributing.md).

## License

Expand Down

0 comments on commit 438dd32

Please sign in to comment.