feat [1/2]: add topics & feeds listing

This commit is contained in:
James Xylight authored and xylight committed 2025-09-17 00:27:11 +00:00
1 parent 1a948281a8
commit 5ed8346027
22 files changed
+426 -124

No files matched your search

+2 -1
View File
@@ -223,8 +223,9 @@ export abstract class BaseClient {
form: types.UploadImage,
): Promise<types.UploadImageResponse>
abstract deleteImage(form: types.DeleteImage): Promise<boolean>
abstract setFlair?(form: types.SetPersonFlair): Promise<types.PersonView>
abstract getFeeds?(form: types.GetFeeds): Promise<types.GetFeedsResponse>
abstract getTopics?(form: types.GetTopics): Promise<types.GetTopicsResponse>
}
export type NullableFnArg<T, Fallback = never> = T extends (
+33
View File
@@ -20,6 +20,7 @@ import {
toCommentView,
toCommunity,
toCommunityView,
toFeedView,
toLocalSite,
toMyUser,
toPerson,
@@ -29,6 +30,7 @@ import {
toPostView,
toPrivateMessageView,
toSortType,
toTopicView,
} from './rewrite'
import type { paths } from './schema'
import type { SubscribedType } from '../types'
@@ -166,6 +168,7 @@ export class PiefedClient implements BaseClient {
).data!
return {
...response,
posts: response.posts.map(toPostView),
next_page: (Number(params.page_cursor ?? 1) + 1).toString(),
}
@@ -969,4 +972,34 @@ export class PiefedClient implements BaseClient {
async deleteImage(): ReturnType<BaseClient['deleteImage']> {
throw new Error('unsupported')
}
async getFeeds(
params: NullableFnArg<BaseClient['getFeeds']>[0],
): NullableFnReturn<BaseClient['getFeeds']> {
const response = (
await this.#client.GET('/api/alpha/feed/list', {
params: { query: params },
})
).data!
return {
...response,
feeds: response.feeds.map(toFeedView),
}
}
async getTopics(
params: NullableFnArg<BaseClient['getTopics']>[0],
): NullableFnReturn<BaseClient['getTopics']> {
const response = (
await this.#client.GET('/api/alpha/topic/list', {
params: { query: params },
})
).data!
return {
...response,
topics: response.topics.map(toTopicView),
}
}
}
+22
View File
@@ -7,6 +7,7 @@ import type {
CommunityView,
CreateComment,
CreatePost,
FeedView,
GetComments,
GetPost,
GetPosts,
@@ -23,6 +24,7 @@ import type {
Search,
SortType,
SubscribedType,
TopicView,
} from '../types'
import type { components } from './schema'
@@ -207,6 +209,26 @@ export function toPersonMentionView(
}
}
export function toTopicView(
topicView: components['schemas']['TopicView'],
): TopicView {
return {
...topicView,
communities: topicView.communities?.map(toCommunity),
children: topicView.children.map(toTopicView),
}
}
export function toFeedView(
feedView: components['schemas']['FeedView'],
): FeedView {
return {
...feedView,
communities: feedView.communities?.map(toCommunity),
children: feedView.children.map(toFeedView),
}
}
// Query calls
export function fromGetPosts(
getPosts: GetPosts,
+2
View File
@@ -16,4 +16,6 @@ export interface GetPosts {
show_read?: boolean
show_nsfw?: boolean
page_cursor?: PaginationCursor
feed_id?: string
topic_id?: string
}
+56
View File
@@ -2001,3 +2001,59 @@ export interface CommunityFlair {
blur_images: boolean
ap_id: string | null
}
export interface GetFeeds {
include_communities?: boolean
mine_only?: boolean
}
export interface FeedView {
actor_id: string
ap_domain: string
children: FeedView[]
communities?: Community[]
communities_count: number
id: number
is_instance_feed: boolean
local: boolean
name: string
nsfl: boolean
nsfw: boolean
owner: boolean
public: boolean
published: string
show_posts_from_children: boolean
subscribed: boolean
subscriptions_count: number
title: string
updated: string
user_id: number
banner?: string
description?: string
description_html?: string
icon?: string
parent_feed_id?: number
}
export interface GetFeedsResponse {
feeds: FeedView[]
}
export interface GetTopics {
include_communities: boolean
}
export interface TopicView {
children: TopicView[]
communities?: Community[]
communities_count: number
id: number
name: string
show_posts_from_children: boolean
title: string
parent_topic_id?: number
}
export interface GetTopicsResponse {
topics: TopicView[]
}
+1 -1
View File
@@ -18,7 +18,7 @@
<div class="flex flex-row gap-2 items-center">
{#if customIcon}{@render customIcon()}{:else}
<Avatar url={icon} width={32} />
<Avatar url={icon} width={32} alt={name} circle={false} />
{/if}
<div class="flex flex-col">
<span class="font-semibold text-base">{name}</span>
+1 -1
View File
@@ -3,7 +3,7 @@
interface Props {
label: string
content: string
content: string | number
formatted?: boolean
labelClass?: string
contentClass?: string
@@ -0,0 +1,29 @@
<script lang="ts">
import type { Snippet } from 'svelte'
import Avatar from '../Avatar.svelte'
interface Props {
href?: string
icon?: string
title: string
detail?: string
children?: Snippet
}
let { href, icon, title, detail, children }: Props = $props()
</script>
<div class="flex flex-row gap-2 items-center">
<Avatar url={icon} alt={title} width={detail ? 32 : 24} circle={false} />
<a class="flex-1 flex flex-col group" {href}>
<h3
class="font-medium text-base overflow-hidden text-ellipsis leading-5 group-hover:underline"
>
{title}
</h3>
<p class="text-sm text-slate-600 dark:text-zinc-400">
{detail}
</p>
</a>
{@render children?.()}
</div>
+10 -12
View File
@@ -70,23 +70,21 @@
&:hover {
background-color: var(--color-slate-25);
}
}
&:not(.xs) {
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 3.5);
}
ul > :global(li:not(.xs) > *) {
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 3.5);
@media screen and (min-width: 40rem) {
&:not(.xs) {
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 4);
}
padding-block: calc(var(--spacing) * 3);
padding-inline: calc(var(--spacing) * 4);
}
}
.xs {
padding-block: calc(var(--spacing) * 0.5);
padding-block: calc(var(--spacing) * 1);
}
ul > :global(li.xs > *) {
padding-block: calc(var(--spacing) * 0.5);
padding-block: calc(var(--spacing) * 1);
}
ul > :global(li).selected {
@@ -1,5 +1,5 @@
<script lang="ts">
import { navigating } from '$app/state'
import { navigating, page } from '$app/state'
import { t } from '$lib/i18n/translations'
import { Button, TextInput } from 'mono-svelte'
import { MagnifyingGlass } from 'svelte-hero-icons'
@@ -29,7 +29,7 @@
class="shrink-0 h-full aspect-square shadow-md"
title="Search"
rounding="pill"
loading={navigating.to?.route.id == '/search'}
loading={navigating.to?.route.id == page.url.pathname}
icon={MagnifyingGlass}
></Button>
</div>
+5
View File
@@ -264,6 +264,11 @@
},
"translators": "Translators",
"accounts": "Accounts",
"explore": {
"title": "Explore",
"feeds": "Feeds",
"topics": "Topics"
},
"communities": {
"title": "Communities",
"trending": "Trending",
+3 -44
View File
@@ -1,46 +1,5 @@
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte.js'
import { client } from '$lib/client/lemmy.svelte'
import { ReactiveState } from '$lib/util.svelte'
import { type ListingType, type SortType } from '$lib/client/types'
import { redirect } from '@sveltejs/kit'
export async function load({ url, fetch }) {
const typeParam = url.searchParams.get('type')
const typeInstance = typeParam?.split('instance-')[1]
const type = typeInstance
? 'Local'
: (typeParam as ListingType) || (LINKED_INSTANCE_URL ? 'Local' : 'All')
const sort = (url.searchParams.get('sort') as SortType) || 'TopDay'
const page = Number(url.searchParams.get('page')) || 1
const query = url.searchParams.get('q') || ''
const communities =
query != ''
? await client({ func: fetch, instanceURL: typeInstance }).search({
limit: 40,
page: page,
sort: sort,
type_: 'Communities',
listing_type: type,
q: query,
})
: await client({
func: fetch,
instanceURL: typeInstance,
}).listCommunities({
limit: 40,
page: page,
sort: sort,
type_: type,
show_nsfw: true,
})
return {
communities: new ReactiveState(communities.communities),
type: typeParam ?? type,
sort: sort,
query: query,
page: page,
}
export function load() {
redirect(302, '/explore/communities')
}
+88
View File
@@ -0,0 +1,88 @@
<script lang="ts">
import { page } from '$app/state'
import { profile } from '$lib/auth.svelte'
import { client } from '$lib/client/lemmy.svelte.js'
import Location from '$lib/components/lemmy/dropdowns/Location.svelte'
import Sort from '$lib/components/lemmy/dropdowns/Sort.svelte'
import { Header, SearchBar, Tabs } from '$lib/components/ui/layout'
import { t } from '$lib/i18n/translations'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import { Option } from 'mono-svelte'
import { ServerStack } from 'svelte-hero-icons/dist'
let { data, children } = $props()
let search = $state(page.data.query || '')
let form = $state<HTMLFormElement>()
</script>
{#if client().getTopics && client().getFeeds}
<Tabs
routes={[
{
href: '/explore/communities',
name: $t('routes.communities.title'),
},
{
href: '/explore/feeds',
name: $t('routes.explore.feeds'),
},
{
href: '/explore/topics',
name: $t('routes.explore.topics'),
},
]}
></Tabs>
{/if}
<svelte:head>
<title>{$t('routes.explore.title')}</title>
</svelte:head>
<Header pageHeader>
{$t('routes.explore.title')}
{#snippet extended()}
{#if page.route.id == '/explore/communities'}
<form
method="get"
action={page.url.pathname}
class="contents"
bind:this={form}
>
<SearchBar bind:query={search} />
<div class="flex flex-row flex-wrap gap-4 items-center">
<Location
name="type"
selected={data.type}
onchange={() => form?.requestSubmit()}
>
{#if !LINKED_INSTANCE_URL}
{@const instanceSet = new Set(
profile.meta.profiles.map((i) => i.instance),
)}
{#if instanceSet.size > 1}
<Option disabled data-label="true">—</Option>
{#each instanceSet as instance}
<Option
icon={ServerStack}
value={encodeURIComponent(`instance-${instance}`)}
>
{instance}
</Option>
{/each}
{/if}
{/if}
</Location>
<Sort
name="sort"
selected={data.sort}
onchange={() => form?.requestSubmit()}
/>
</div>
</form>
{/if}
{/snippet}
</Header>
{@render children?.()}
+25
View File
@@ -0,0 +1,25 @@
import type { ListingType, SortType } from '$lib/client/types'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
export function load({ url }) {
const sort = (url.searchParams.get('sort') as SortType) || 'TopDay'
const page = Number(url.searchParams.get('page')) || 1
const query = url.searchParams.get('q') || ''
const typeParam = url.searchParams.get('type')
const typeInstance = typeParam?.split('instance-')[1]
const type = (
typeInstance
? 'Local'
: typeParam || (LINKED_INSTANCE_URL ? 'Local' : 'All')
) as ListingType
return {
sort: sort,
page: page,
query: query,
type: type,
typeInstance: typeInstance,
}
}
@@ -1,30 +1,20 @@
<script lang="ts">
import { navigating, page } from '$app/state'
import { profile } from '$lib/auth.svelte.js'
import Location from '$lib/components/lemmy/dropdowns/Location.svelte'
import Sort from '$lib/components/lemmy/dropdowns/Sort.svelte'
import { CommunityItem } from '$lib/components/lemmy/community'
import EndPlaceholder from '$lib/components/ui/EndPlaceholder.svelte'
import Fixate from '$lib/components/ui/generic/Fixate.svelte'
import Skeleton from '$lib/components/ui/generic/Skeleton.svelte'
import CommonList from '$lib/components/ui/layout/CommonList.svelte'
import { Header } from '$lib/components/ui/layout'
import Pageination from '$lib/components/ui/layout/Pageination.svelte'
import Placeholder from '$lib/components/ui/Placeholder.svelte'
import { t } from '$lib/i18n/translations.js'
import { LINKED_INSTANCE_URL } from '$lib/instance.svelte'
import { Material, Option } from 'mono-svelte'
import { QuestionMarkCircle, ServerStack } from 'svelte-hero-icons'
import { Material } from 'mono-svelte'
import { QuestionMarkCircle } from 'svelte-hero-icons'
import { expoOut } from 'svelte/easing'
import { fly } from 'svelte/transition'
import SearchBar from '../../lib/components/ui/layout/SearchBar.svelte'
import Fixate from '$lib/components/ui/generic/Fixate.svelte'
import { CommunityItem } from '$lib/components/lemmy/community'
let { data } = $props()
let search = $state(data.query || '')
let searchElement = $state<HTMLInputElement>()
let form = $state<HTMLFormElement>()
let showTop = $derived(
(data.query ?? '' != '') &&
data.communities.value.length > 0 &&
@@ -32,55 +22,6 @@
)
</script>
<svelte:window
onkeydown={(e) => {
if (e.target == document.body) searchElement?.focus()
}}
/>
<svelte:head>
<title>{$t('routes.communities.title')}</title>
</svelte:head>
<Header pageHeader>
{$t('routes.communities.title')}
{#snippet extended()}
<form method="get" action="/communities" class="contents" bind:this={form}>
<SearchBar bind:query={search} />
<div class="flex flex-row flex-wrap gap-4 items-center">
<Location
name="type"
selected={data.type}
onchange={() => form?.requestSubmit()}
>
{#if !LINKED_INSTANCE_URL}
{@const instanceSet = new Set(
profile.meta.profiles.map((i) => i.instance),
)}
{#if instanceSet.size > 1}
<Option disabled data-label="true">—</Option>
{#each instanceSet as instance}
<Option
icon={ServerStack}
value={encodeURIComponent(`instance-${instance}`)}
>
{instance}
</Option>
{/each}
{/if}
{/if}
</Location>
<Sort
name="sort"
selected={data.sort}
onchange={() => form?.requestSubmit()}
/>
</div>
</form>
{/snippet}
</Header>
{#if navigating.to?.route.id == '/communities'}
<div class="flex flex-col gap-3">
{#each new Array(5) as _, index}
+35
View File
@@ -0,0 +1,35 @@
import { client } from '$lib/client/lemmy.svelte'
import { ReactiveState } from '$lib/util.svelte'
export async function load({ fetch, parent }) {
const { page, query, sort, type, typeInstance } = await parent()
const communities =
query != ''
? await client({ func: fetch, instanceURL: typeInstance }).search({
limit: 40,
page: page,
sort: sort,
type_: 'Communities',
listing_type: type,
q: query,
})
: await client({
func: fetch,
instanceURL: typeInstance,
}).listCommunities({
limit: 40,
page: page,
sort: sort,
type_: type,
show_nsfw: true,
})
return {
communities: new ReactiveState(communities.communities),
type: type,
sort: sort,
query: query,
page: page,
}
}
+12
View File
@@ -0,0 +1,12 @@
<script lang="ts">
import { CommonList } from '$lib/components/ui/layout'
import FeedItem from './FeedItem.svelte'
let { data } = $props()
</script>
<CommonList items={data.feeds.value}>
{#snippet item(feed)}
<FeedItem {feed} />
{/snippet}
</CommonList>
+15
View File
@@ -0,0 +1,15 @@
import { client } from '$lib/client/lemmy.svelte.js'
import { ReactiveState } from '$lib/util.svelte'
import { error } from '@sveltejs/kit'
export async function load({ fetch }) {
const piefed = client({ func: fetch })
if (!piefed.getFeeds) error(404, 'unsupported')
const feeds = await piefed.getFeeds({ include_communities: false })
return {
feeds: new ReactiveState(feeds.feeds),
}
}
+42
View File
@@ -0,0 +1,42 @@
<script lang="ts">
import type { FeedView } from '$lib/client/types'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Entity from '$lib/components/ui/Entity.svelte'
import LabelStat from '$lib/components/ui/LabelStat.svelte'
import CommonItem from '$lib/components/ui/layout/CommonItem.svelte'
import { t } from '$lib/i18n/translations'
import { Button, modal } from 'mono-svelte'
import { InformationCircle } from 'svelte-hero-icons'
interface Props {
feed: FeedView
}
let { feed }: Props = $props()
</script>
{#snippet feedModal()}
<Entity icon={feed.icon} name={feed.title} label={feed.name}></Entity>
<Markdown source={feed.description} />
<LabelStat
label={$t('cards.community.members')}
content={feed.subscriptions_count}
/>
{/snippet}
<CommonItem
icon={feed.icon}
title={feed.title}
detail="{feed.name} • {feed.subscriptions_count}"
href="/f/{feed.id}"
>
<Button
onclick={() => modal({ title: $t('common.info'), snippet: feedModal })}
color="ghost"
size="custom"
rounding="xl"
class="h-8.5 aspect-square"
icon={InformationCircle}
aria-label={$t('common.info')}
/>
</CommonItem>
+12
View File
@@ -0,0 +1,12 @@
<script lang="ts">
import { CommonList } from '$lib/components/ui/layout'
import TopicItem from './TopicItem.svelte'
let { data } = $props()
</script>
<CommonList items={data.topics.value}>
{#snippet item(topic)}
<TopicItem {topic} />
{/snippet}
</CommonList>
+15
View File
@@ -0,0 +1,15 @@
import { client } from '$lib/client/lemmy.svelte.js'
import { ReactiveState } from '$lib/util.svelte'
import { error } from '@sveltejs/kit'
export async function load({ fetch }) {
const piefed = client({ func: fetch })
if (!piefed.getTopics) error(404, 'unsupported')
const topics = await piefed.getTopics({ include_communities: false })
return {
topics: new ReactiveState(topics.topics),
}
}
@@ -0,0 +1,12 @@
<script lang="ts">
import type { TopicView } from '$lib/client/types'
import CommonItem from '$lib/components/ui/layout/CommonItem.svelte'
interface Props {
topic: TopicView
}
let { topic }: Props = $props()
</script>
<CommonItem title={topic.title ?? topic.name} href="/t/{topic.id}" />