feat: Add search function
This commit is contained in:
1 parent
91134d05e4
commit
1dd179d636
22 files changed
+455
-70
No files matched your search
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "photon-lemmy",
|
||||
"version": "0.2.3",
|
||||
"version": "0.2.4",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
|
||||
@@ -66,15 +66,14 @@
|
||||
${buttonColor[color]}
|
||||
${buttonSize[size]}
|
||||
${buttonRoundness[rounded]}
|
||||
text-sm transition-all disabled:!opacity-70 disabled:!pointer-events-none
|
||||
text-sm transition-colors disabled:!opacity-70 disabled:!pointer-events-none
|
||||
disabled:!border disabled:!border-slate-300 disabled:!bg-slate-200 disabled:dark:!border-zinc-700 disabled:dark:!bg-zinc-800
|
||||
${$$props.class}
|
||||
`} {loading
|
||||
? color == 'primary'
|
||||
? '!bg-transparent !text-inherit'
|
||||
: ''
|
||||
: ''} {loading
|
||||
? ' !border !border-slate-300 !bg-slate-200 dark:!border-zinc-700 dark:!bg-zinc-800'
|
||||
: ''} {$$props.disabled ? 'opacity-70 pointer-events-none' : ''}"
|
||||
: ''}"
|
||||
>
|
||||
<div class="flex flex-row items-center justify-center gap-1.5 h-full">
|
||||
{#if loading}
|
||||
@@ -96,14 +95,13 @@
|
||||
${buttonColor[color]}
|
||||
${buttonSize[size]}
|
||||
${buttonRoundness[rounded]}
|
||||
text-sm transition-all disabled:!opacity-70 disabled:!pointer-events-none
|
||||
text-sm transition-colors disabled:!opacity-70 disabled:!pointer-events-none
|
||||
disabled:!border disabled:!border-slate-300 disabled:!bg-slate-200 disabled:dark:!border-zinc-700 disabled:dark:!bg-zinc-800
|
||||
${$$props.class}
|
||||
`} {loading
|
||||
? color == 'primary'
|
||||
? '!bg-transparent !text-inherit'
|
||||
: ''
|
||||
: ''} {loading
|
||||
? ' !border !border-slate-300 !bg-slate-200 dark:!border-zinc-700 dark:!bg-zinc-800'
|
||||
: ''}"
|
||||
type={submit ? 'submit' : 'button'}
|
||||
>
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="flex overflow-hidden flex-row items-center w-max bg-slate-100
|
||||
rounded-lg dark:bg-zinc-900 gap-1 p-1 border border-slate-200 dark:border-zinc-800"
|
||||
class="flex overflow-hidden flex-row items-center max-w-full w-max bg-slate-100
|
||||
rounded-lg dark:bg-zinc-900 gap-1 p-1 border border-slate-200 dark:border-zinc-800 flex-wrap"
|
||||
>
|
||||
{#each options as option, index}
|
||||
<button
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
|
||||
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
import { getInstance } from '$lib/lemmy.js'
|
||||
import type { CommentView } from 'lemmy-js-client'
|
||||
|
||||
export let comment: CommentView
|
||||
</script>
|
||||
|
||||
<Card class="flex flex-col bg-white rounded-md p-5 flex-1 gap-1">
|
||||
<div class="flex flex-row justify-between items-center">
|
||||
<PostMeta
|
||||
community={comment.community}
|
||||
deleted={false}
|
||||
featured={false}
|
||||
nsfw={comment.post.nsfw}
|
||||
published={new Date(comment.post.published)}
|
||||
saved={false}
|
||||
/>
|
||||
<Button
|
||||
color="secondary"
|
||||
href="/post/{comment.post.id}#{comment.comment.id}"
|
||||
size="sm"
|
||||
class="self-start"
|
||||
>
|
||||
Jump
|
||||
</Button>
|
||||
</div>
|
||||
<a href="/post/{getInstance()}/{comment.post.id}" class="font-bold">
|
||||
{comment.post.name}
|
||||
</a>
|
||||
<div class="list-none">
|
||||
<Comment
|
||||
postId={comment.post.id}
|
||||
node={{ children: [], comment_view: comment, depth: 1 }}
|
||||
replying={false}
|
||||
/>
|
||||
</div>
|
||||
</Card>
|
||||
File renamed without changes.
@@ -0,0 +1,74 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import type { CommunityView } from 'lemmy-js-client'
|
||||
import Subscribe from '../../../../routes/communities/Subscribe.svelte'
|
||||
import {
|
||||
ChatBubbleOvalLeftEllipsis,
|
||||
Icon,
|
||||
PencilSquare,
|
||||
UserGroup,
|
||||
} from 'svelte-hero-icons'
|
||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||
|
||||
export let community: CommunityView
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="py-4 border-b border-black/20 dark:border-white/20
|
||||
flex flex-col gap-1 text-sm max-w-full"
|
||||
>
|
||||
<div class="flex flex-row items-center">
|
||||
<span
|
||||
class="break-words max-w-full w-max text-base font-bold text-sky-400 hover:underline"
|
||||
>
|
||||
<CommunityLink avatar community={community.community} />
|
||||
</span>
|
||||
<div class="ml-auto">
|
||||
<Subscribe {community} let:subscribe let:subscribing>
|
||||
<Button
|
||||
disabled={subscribing}
|
||||
loading={subscribing}
|
||||
on:click={async () => {
|
||||
const res = await subscribe()
|
||||
|
||||
if (res) {
|
||||
community.subscribed =
|
||||
res.community_view.subscribed != 'NotSubscribed'
|
||||
? 'Subscribed'
|
||||
: 'NotSubscribed'
|
||||
}
|
||||
}}
|
||||
color={community.subscribed == 'Subscribed' ? 'primary' : 'ghost'}
|
||||
>
|
||||
{community.subscribed == 'Subscribed' ? 'Subscribed' : 'Subscribe'}
|
||||
</Button>
|
||||
</Subscribe>
|
||||
</div>
|
||||
</div>
|
||||
<span class="opacity-80 mb-2">
|
||||
{new URL(community.community.actor_id).hostname}
|
||||
</span>
|
||||
<div class="flex flex-row gap-3 items-center">
|
||||
<div class="flex flex-row gap-1 items-center">
|
||||
<Icon src={UserGroup} width={16} mini />
|
||||
<span>
|
||||
{Intl.NumberFormat('en', { notation: 'compact' }).format(
|
||||
community.counts.subscribers
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex flex-row gap-1 items-center">
|
||||
<Icon src={PencilSquare} mini width={16} />
|
||||
<span>
|
||||
{Intl.NumberFormat('en', { notation: 'compact' }).format(
|
||||
community.counts.posts
|
||||
)}
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex flex-row gap-1 items-center">
|
||||
<Icon src={ChatBubbleOvalLeftEllipsis} mini width={16} />
|
||||
<FormattedNumber number={community.counts.comments} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
File renamed without changes.
@@ -16,7 +16,7 @@
|
||||
|
||||
<Card class="bg-white flex flex-col w-full p-5 gap-2.5">
|
||||
<div class="flex flex-row w-full gap-2.5">
|
||||
<div class="flex flex-col gap-2.5 grow">
|
||||
<div class="flex flex-col gap-1 grow">
|
||||
<PostMeta
|
||||
community={post.community}
|
||||
user={post.creator}
|
||||
@@ -38,23 +38,21 @@
|
||||
</div>
|
||||
{#if $userSettings.showCompactPosts && (post.post.thumbnail_url || isImage(post.post.url))}
|
||||
<div class="flex-none w-24 h-24">
|
||||
<a
|
||||
href="/post/{getInstance()}/{post.post.id}"
|
||||
>
|
||||
<a href="/post/{getInstance()}/{post.post.id}">
|
||||
{#if post.post.thumbnail_url}
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.thumbnail_url}?thumbnail=256&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md h-24 w-24 border border-slate-200 dark:border-zinc-700"
|
||||
/>
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.thumbnail_url}?thumbnail=256&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md h-24 w-24 border border-slate-200 dark:border-zinc-700"
|
||||
/>
|
||||
{:else}
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.url}?thumbnail=256&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md h-24 w-24 border border-slate-200 dark:border-zinc-700"
|
||||
/>
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.url}?thumbnail=256&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md h-24 w-24 border border-slate-200 dark:border-zinc-700"
|
||||
/>
|
||||
{/if}
|
||||
</a>
|
||||
</div>
|
||||
@@ -71,30 +69,30 @@
|
||||
{/if}
|
||||
{#if isImage(post.post.url)}
|
||||
<div class="self-start" class:blur-3xl={post.post.nsfw}>
|
||||
<picture
|
||||
class="rounded-md overflow-hidden max-h-[min(50vh,500px)] w-full max-w-full"
|
||||
>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=256&format=webp"
|
||||
media="(max-width: 256px)"
|
||||
/>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=512&format=webp"
|
||||
media="(max-width: 512px)"
|
||||
/>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=720&format=webp"
|
||||
media="(max-width: 720px)"
|
||||
/>
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.url}?thumbnail=1024&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md max-h-[50vh] w-full"
|
||||
width={512}
|
||||
height={300}
|
||||
/>
|
||||
</picture>
|
||||
<picture
|
||||
class="rounded-md overflow-hidden max-h-[min(50vh,500px)] w-full max-w-full"
|
||||
>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=256&format=webp"
|
||||
media="(max-width: 256px)"
|
||||
/>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=512&format=webp"
|
||||
media="(max-width: 512px)"
|
||||
/>
|
||||
<source
|
||||
srcset="{post.post.url}?thumbnail=720&format=webp"
|
||||
media="(max-width: 720px)"
|
||||
/>
|
||||
<!-- svelte-ignore a11y-missing-attribute -->
|
||||
<img
|
||||
src="{post.post.url}?thumbnail=1024&format=webp"
|
||||
loading="lazy"
|
||||
class="object-cover bg-slate-100 rounded-md max-h-[50vh] w-full"
|
||||
width={512}
|
||||
height={300}
|
||||
/>
|
||||
</picture>
|
||||
</div>
|
||||
{:else if post.post.thumbnail_url && post.post.url}
|
||||
<PostLink
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import Avatar from '$lib/components/ui/Avatar.svelte'
|
||||
import Badge from '$lib/components/ui/Badge.svelte'
|
||||
import UserLink from '$lib/components/user/UserLink.svelte'
|
||||
@@ -8,12 +8,12 @@
|
||||
import { Bookmark, Icon, InformationCircle, Trash } from 'svelte-hero-icons'
|
||||
|
||||
export let community: Community
|
||||
export let user: Person
|
||||
export let user: Person | undefined = undefined
|
||||
export let published: Date
|
||||
|
||||
// Score
|
||||
export let upvotes: number
|
||||
export let downvotes: number
|
||||
export let upvotes: number | undefined = undefined
|
||||
export let downvotes: number | undefined = undefined
|
||||
|
||||
// Badges
|
||||
export let nsfw: boolean
|
||||
@@ -27,13 +27,17 @@
|
||||
<div class="flex flex-col text-xs">
|
||||
<CommunityLink {community} />
|
||||
<span class="text-slate-600 dark:text-zinc-400 flex flex-row gap-1">
|
||||
<UserLink {user} />
|
||||
<span>•</span>
|
||||
{#if user}
|
||||
<UserLink {user} />
|
||||
<span>•</span>
|
||||
{/if}
|
||||
<RelativeDate date={published} />
|
||||
<span>•</span>
|
||||
<span>
|
||||
{Math.floor((upvotes / (upvotes + downvotes || 1)) * 100)}%
|
||||
</span>
|
||||
{#if upvotes && downvotes}
|
||||
<span>•</span>
|
||||
<span>
|
||||
{Math.floor((upvotes / (upvotes + downvotes || 1)) * 100)}%
|
||||
</span>
|
||||
{/if}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<script lang="ts">
|
||||
import type { PersonView } from 'lemmy-js-client'
|
||||
import {
|
||||
ChatBubbleOvalLeftEllipsis,
|
||||
Icon,
|
||||
PencilSquare,
|
||||
} from 'svelte-hero-icons'
|
||||
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
|
||||
import UserLink from '$lib/components/user/UserLink.svelte'
|
||||
|
||||
export let user: PersonView
|
||||
</script>
|
||||
|
||||
<div
|
||||
class="py-2
|
||||
flex flex-col gap-3 text-sm max-w-full"
|
||||
>
|
||||
<div class="flex flex-row items-center">
|
||||
<span
|
||||
class="break-words max-w-full w-max text-base font-bold text-sky-400 hover:underline"
|
||||
>
|
||||
<UserLink avatar user={user.person} />
|
||||
</span>
|
||||
</div>
|
||||
<div class="flex flex-row gap-3 items-center">
|
||||
<div class="flex flex-row gap-1 items-center" title="Post count">
|
||||
<Icon src={PencilSquare} mini width={16} />
|
||||
<FormattedNumber number={user.counts.post_count} />
|
||||
</div>
|
||||
<div class="flex flex-row gap-1 items-center" title="Comment count">
|
||||
<Icon src={ChatBubbleOvalLeftEllipsis} mini width={16} />
|
||||
<FormattedNumber number={user.counts.comment_count} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -49,11 +49,20 @@
|
||||
</a>
|
||||
</div>
|
||||
<div class="flex flex-row gap-2 py-2 px-2">
|
||||
<Button
|
||||
href="/search"
|
||||
label="Search"
|
||||
class="max-sm:w-9 max-sm:h-8 max-sm:!p-0 items-center justify-center
|
||||
dark:text-zinc-300 text-slate-700 hover:text-inherit hover:bg-slate-200 hover:dark:bg-zinc-900"
|
||||
>
|
||||
<Icon src={MagnifyingGlass} width={16} slot="icon" />
|
||||
<span class="hidden sm:inline">Search</span>
|
||||
</Button>
|
||||
<Button
|
||||
href="/communities"
|
||||
label="Communities"
|
||||
class="max-sm:w-9 max-sm:h-8 max-sm:!p-0 items-center justify-center
|
||||
dark:text-zinc-300 text-slate-700 hover:text-inherit"
|
||||
dark:text-zinc-300 text-slate-700 hover:text-inherit hover:bg-slate-200 hover:dark:bg-zinc-900"
|
||||
>
|
||||
<Icon src={GlobeAlt} width={16} slot="icon" />
|
||||
<span class="hidden sm:inline">Explore</span>
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import { createEventDispatcher } from 'svelte'
|
||||
|
||||
export let page: number
|
||||
|
||||
const dispatcher = createEventDispatcher<{ change: number }>()
|
||||
</script>
|
||||
|
||||
<div class="flex flex-row w-full gap-4">
|
||||
<Button
|
||||
on:click={() => {
|
||||
dispatcher('change', --page)
|
||||
}}
|
||||
disabled={page <= 1}
|
||||
class="flex-1"
|
||||
>
|
||||
Back
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
on:click={() => {
|
||||
dispatcher('change', ++page)
|
||||
}}
|
||||
class="flex-1"
|
||||
>
|
||||
Next
|
||||
</Button>
|
||||
</div>
|
||||
@@ -5,7 +5,7 @@
|
||||
import Post from '../../../lib/components/lemmy/post/Post.svelte'
|
||||
import { goto } from '$app/navigation'
|
||||
import Card from '$lib/components/ui/StickyCard.svelte'
|
||||
import CommunityCard from '$lib/components/community/CommunityCard.svelte'
|
||||
import CommunityCard from '$lib/components/lemmy/community/CommunityCard.svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
import { Color } from '$lib/ui/colors'
|
||||
import { fly } from 'svelte/transition'
|
||||
|
||||
@@ -9,8 +9,7 @@
|
||||
QuestionMarkCircle,
|
||||
UserGroup,
|
||||
} from 'svelte-hero-icons'
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import { Color } from '$lib/ui/colors.js'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import Subscribe from './Subscribe.svelte'
|
||||
import Button from '$lib/components/input/Button.svelte'
|
||||
@@ -38,7 +37,8 @@
|
||||
}}
|
||||
/>
|
||||
<div class="flex flex-col sm:flex-row gap-2 sm:ml-auto items-center">
|
||||
<TextInput bind:value={search}
|
||||
<TextInput
|
||||
bind:value={search}
|
||||
on:change={() => {
|
||||
$page.url.searchParams.set('q', search)
|
||||
goto($page.url.toString(), {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<h1 class="font-bold text-4xl">I don't take donations.</h1>
|
||||
|
||||
<p>
|
||||
This is a project that I'm making because it's fun, not to gain money. If you
|
||||
want to support me, contribute! Report bugs, fix typos, etc.
|
||||
</p>
|
||||
@@ -33,12 +33,16 @@
|
||||
return false
|
||||
}
|
||||
|
||||
let markingAsRead = false
|
||||
|
||||
async function markAllAsRead() {
|
||||
if (!$user) {
|
||||
goto('/login')
|
||||
return
|
||||
}
|
||||
|
||||
markingAsRead = true
|
||||
|
||||
const response = await getClient().markAllAsRead({
|
||||
auth: $authData!.token,
|
||||
})
|
||||
@@ -47,6 +51,8 @@
|
||||
|
||||
goto($page.url, {
|
||||
invalidateAll: true,
|
||||
}).then(() => {
|
||||
markingAsRead = false
|
||||
})
|
||||
|
||||
return response.replies
|
||||
@@ -59,8 +65,13 @@
|
||||
|
||||
<div class="flex flex-row justify-between">
|
||||
<h1 class="font-bold text-3xl">Inbox</h1>
|
||||
<Button on:click={markAllAsRead}>
|
||||
<Icon src={Check} width={16} mini />
|
||||
<Button
|
||||
on:click={markAllAsRead}
|
||||
loading={markingAsRead}
|
||||
disabled={markingAsRead}
|
||||
size="md"
|
||||
>
|
||||
<Icon src={Check} width={16} mini slot="icon" />
|
||||
Mark all as read
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
buildCommentsTreeAsync,
|
||||
} from '$lib/components/lemmy/comment/comments.js'
|
||||
import Comments from '$lib/components/lemmy/comment/Comments.svelte'
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import { isImage } from '$lib/ui/image.js'
|
||||
import { authData, getClient, user } from '$lib/lemmy.js'
|
||||
import CommentForm from '$lib/components/lemmy/comment/CommentForm.svelte'
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import InboxItem from '../inbox/InboxItem.svelte'
|
||||
import Card from '$lib/components/ui/Card.svelte'
|
||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import Link from '$lib/components/input/Link.svelte'
|
||||
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
|
||||
import { fly } from 'svelte/transition'
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
<script>
|
||||
import { page } from '$app/stores'
|
||||
</script>
|
||||
|
||||
<div class="text-center">
|
||||
<h1 class="text-4xl font-black">{$page.status}</h1>
|
||||
<span>The search timed out. This is an issue on the server's side.</span>
|
||||
</div>
|
||||
@@ -0,0 +1,98 @@
|
||||
<script lang="ts">
|
||||
import { goto } from '$app/navigation'
|
||||
import { page } from '$app/stores'
|
||||
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
|
||||
import TextInput from '$lib/components/input/TextInput.svelte'
|
||||
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
|
||||
import CommentItem from '$lib/components/lemmy/comment/CommentItem.svelte'
|
||||
import CommunityItem from '$lib/components/lemmy/community/CommunityItem.svelte'
|
||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||
import UserItem from '$lib/components/lemmy/user/UserItem.svelte'
|
||||
import Pageination from '$lib/components/ui/Pageination.svelte'
|
||||
import type {
|
||||
CommentView,
|
||||
CommunityView,
|
||||
PersonView,
|
||||
PostView,
|
||||
} from 'lemmy-js-client'
|
||||
import { Icon, MagnifyingGlass } from 'svelte-hero-icons'
|
||||
|
||||
type Result = PostView | CommentView | PersonView | CommunityView
|
||||
|
||||
export let data
|
||||
|
||||
let query = ''
|
||||
|
||||
const isPost = (item: Result): item is PostView =>
|
||||
'post' in item && !('comment' in item)
|
||||
|
||||
const isComment = (item: Result): item is CommentView => 'comment' in item
|
||||
|
||||
const isCommunity = (item: Result): item is CommunityView =>
|
||||
'community' in item
|
||||
|
||||
const isUser = (item: Result): item is PersonView => 'person' in item
|
||||
|
||||
let pageNum = data.page
|
||||
</script>
|
||||
|
||||
<svelte:head>
|
||||
<title>Search</title>
|
||||
</svelte:head>
|
||||
|
||||
<div class="flex flex-row flex-wrap sm:justify-between items-center gap-4">
|
||||
<MultiSelect
|
||||
options={['All', 'Posts', 'Comments', 'Users', 'Communities']}
|
||||
selected={$page.url.searchParams.get('type') ?? 'All'}
|
||||
on:select={(e) => {
|
||||
const url = $page.url
|
||||
url.searchParams.set('type', e.detail)
|
||||
url.searchParams.delete('page')
|
||||
goto(url, {
|
||||
invalidateAll: true,
|
||||
})
|
||||
}}
|
||||
/>
|
||||
<TextInput
|
||||
bind:value={query}
|
||||
placeholder="Search for something..."
|
||||
on:change={() => {
|
||||
$page.url.searchParams.set('q', query)
|
||||
goto($page.url.toString(), {
|
||||
invalidateAll: true,
|
||||
})
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
{#if !data.results}
|
||||
<div
|
||||
class="text-slate-600 dark:text-zinc-400 flex flex-col justify-center items-center py-8"
|
||||
>
|
||||
<Icon src={MagnifyingGlass} size="48" solid />
|
||||
<h1 class="font-bold text-3xl">No results</h1>
|
||||
<p class="mt-2 text-center">Search for something!</p>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="flex flex-col gap-4 mt-4">
|
||||
{#each data.results as result}
|
||||
{#if isPost(result)}
|
||||
<Post post={result} />
|
||||
{:else if isComment(result)}
|
||||
<CommentItem comment={result} />
|
||||
{:else if isCommunity(result)}
|
||||
<CommunityItem community={result} />
|
||||
{:else if isUser(result)}
|
||||
<UserItem user={result} />
|
||||
{/if}
|
||||
{/each}
|
||||
</div>
|
||||
<Pageination
|
||||
bind:page={pageNum}
|
||||
on:change={(p) => {
|
||||
$page.url.searchParams.set('page', p.detail.toString())
|
||||
goto($page.url, {
|
||||
invalidateAll: true,
|
||||
})
|
||||
}}
|
||||
/>
|
||||
{/if}
|
||||
@@ -0,0 +1,74 @@
|
||||
import { authData, getClient } from '$lib/lemmy.js'
|
||||
import type {
|
||||
CommentView,
|
||||
CommunityView,
|
||||
PersonView,
|
||||
PostView,
|
||||
SearchResponse,
|
||||
SearchType,
|
||||
SortType,
|
||||
} from 'lemmy-js-client'
|
||||
import { get } from 'svelte/store'
|
||||
|
||||
function getSearchItemPublished(
|
||||
item: PostView | CommentView | PersonView | CommunityView
|
||||
) {
|
||||
if ('comment' in item) {
|
||||
return item.comment.published
|
||||
} else if ('post' in item) {
|
||||
return item.post.published
|
||||
}
|
||||
|
||||
if ('person' in item) {
|
||||
return item.person.published
|
||||
}
|
||||
|
||||
if ('community' in item) {
|
||||
return item.community.published
|
||||
}
|
||||
|
||||
return ''
|
||||
}
|
||||
|
||||
export async function load({ url }) {
|
||||
const query = url.searchParams.get('q')
|
||||
const page = Number(url.searchParams.get('page')) || 1
|
||||
const community = url.searchParams.get('community_name')
|
||||
const sort = url.searchParams.get('sort')
|
||||
const type = url.searchParams.get('type')
|
||||
|
||||
if (query) {
|
||||
const results = await getClient().search({
|
||||
q: query,
|
||||
auth: get(authData)?.token,
|
||||
community_name: community ?? undefined,
|
||||
limit: 40,
|
||||
page: page,
|
||||
sort: (sort as SortType) || 'New',
|
||||
listing_type: 'All',
|
||||
type_: (type as SearchType) ?? 'All',
|
||||
})
|
||||
|
||||
const [posts, comments, users, communities] = [
|
||||
results.posts,
|
||||
results.comments,
|
||||
results.users,
|
||||
results.communities,
|
||||
]
|
||||
|
||||
const everything = [...posts, ...comments, ...users, ...communities].sort(
|
||||
(a, b) =>
|
||||
Date.parse(getSearchItemPublished(b)) -
|
||||
Date.parse(getSearchItemPublished(a))
|
||||
)
|
||||
|
||||
return {
|
||||
page: page,
|
||||
results: everything,
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
page: 1,
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
|
||||
import CommunityLink from '$lib/components/lemmy/community/CommunityLink.svelte'
|
||||
import Link from '$lib/components/input/Link.svelte'
|
||||
import Post from '$lib/components/lemmy/post/Post.svelte'
|
||||
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
|
||||
|
||||
Reference in new issue
Block a user