From 1dd179d636364af20fa28528038bc09f5b87b720 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Wed, 26 Jul 2023 11:39:54 -0700 Subject: [PATCH] feat: Add search function --- package.json | 2 +- src/lib/components/input/Button.svelte | 12 +-- src/lib/components/input/MultiSelect.svelte | 4 +- .../lemmy/comment/CommentItem.svelte | 41 ++++++++ .../community/CommunityCard.svelte | 0 .../lemmy/community/CommunityItem.svelte | 74 ++++++++++++++ .../community/CommunityLink.svelte | 0 src/lib/components/lemmy/post/Post.svelte | 78 +++++++-------- src/lib/components/lemmy/post/PostMeta.svelte | 24 +++-- src/lib/components/lemmy/user/UserItem.svelte | 35 +++++++ src/lib/components/ui/Navbar.svelte | 11 ++- src/lib/components/ui/Pageination.svelte | 29 ++++++ src/routes/c/[name]/+page.svelte | 2 +- src/routes/communities/+page.svelte | 6 +- src/routes/donate/+page.svelte | 6 ++ src/routes/inbox/+page.svelte | 15 ++- src/routes/post/[instance]/[id]/+page.svelte | 2 +- src/routes/saved/+page.svelte | 2 +- src/routes/search/+error.svelte | 8 ++ src/routes/search/+page.svelte | 98 +++++++++++++++++++ src/routes/search/+page.ts | 74 ++++++++++++++ src/routes/u/[name]/+page.svelte | 2 +- 22 files changed, 455 insertions(+), 70 deletions(-) create mode 100644 src/lib/components/lemmy/comment/CommentItem.svelte rename src/lib/components/{ => lemmy}/community/CommunityCard.svelte (100%) create mode 100644 src/lib/components/lemmy/community/CommunityItem.svelte rename src/lib/components/{ => lemmy}/community/CommunityLink.svelte (100%) create mode 100644 src/lib/components/lemmy/user/UserItem.svelte create mode 100644 src/lib/components/ui/Pageination.svelte create mode 100644 src/routes/donate/+page.svelte create mode 100644 src/routes/search/+error.svelte create mode 100644 src/routes/search/+page.ts diff --git a/package.json b/package.json index 1b2370b2..b4bdcae5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "photon-lemmy", - "version": "0.2.3", + "version": "0.2.4", "private": true, "scripts": { "dev": "vite dev", diff --git a/src/lib/components/input/Button.svelte b/src/lib/components/input/Button.svelte index cbb003c9..1482ef2e 100644 --- a/src/lib/components/input/Button.svelte +++ b/src/lib/components/input/Button.svelte @@ -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' : ''}" + : ''}" >
{#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'} > diff --git a/src/lib/components/input/MultiSelect.svelte b/src/lib/components/input/MultiSelect.svelte index 1421c4d8..1f47217a 100644 --- a/src/lib/components/input/MultiSelect.svelte +++ b/src/lib/components/input/MultiSelect.svelte @@ -14,8 +14,8 @@
{#each options as option, index} +
+ + {comment.post.name} + +
+ +
+ diff --git a/src/lib/components/community/CommunityCard.svelte b/src/lib/components/lemmy/community/CommunityCard.svelte similarity index 100% rename from src/lib/components/community/CommunityCard.svelte rename to src/lib/components/lemmy/community/CommunityCard.svelte diff --git a/src/lib/components/lemmy/community/CommunityItem.svelte b/src/lib/components/lemmy/community/CommunityItem.svelte new file mode 100644 index 00000000..f676ae73 --- /dev/null +++ b/src/lib/components/lemmy/community/CommunityItem.svelte @@ -0,0 +1,74 @@ + + +
+
+ + + +
+ + + +
+
+ + {new URL(community.community.actor_id).hostname} + +
+
+ + + {Intl.NumberFormat('en', { notation: 'compact' }).format( + community.counts.subscribers + )} + +
+
+ + + {Intl.NumberFormat('en', { notation: 'compact' }).format( + community.counts.posts + )} + +
+
+ + +
+
+
diff --git a/src/lib/components/community/CommunityLink.svelte b/src/lib/components/lemmy/community/CommunityLink.svelte similarity index 100% rename from src/lib/components/community/CommunityLink.svelte rename to src/lib/components/lemmy/community/CommunityLink.svelte diff --git a/src/lib/components/lemmy/post/Post.svelte b/src/lib/components/lemmy/post/Post.svelte index e2e0927b..9cf83812 100644 --- a/src/lib/components/lemmy/post/Post.svelte +++ b/src/lib/components/lemmy/post/Post.svelte @@ -16,7 +16,7 @@
-
+
{#if $userSettings.showCompactPosts && (post.post.thumbnail_url || isImage(post.post.url))} @@ -71,30 +69,30 @@ {/if} {#if isImage(post.post.url)}
- - - - - - - + + + + + + +
{:else if post.post.thumbnail_url && post.post.url} - 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 @@
- - • + {#if user} + + • + {/if} - • - - {Math.floor((upvotes / (upvotes + downvotes || 1)) * 100)}% - + {#if upvotes && downvotes} + • + + {Math.floor((upvotes / (upvotes + downvotes || 1)) * 100)}% + + {/if}
diff --git a/src/lib/components/lemmy/user/UserItem.svelte b/src/lib/components/lemmy/user/UserItem.svelte new file mode 100644 index 00000000..113ed8ed --- /dev/null +++ b/src/lib/components/lemmy/user/UserItem.svelte @@ -0,0 +1,35 @@ + + +
+
+ + + +
+
+
+ + +
+
+ + +
+
+
diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index 874b36fa..c9c29aa8 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -49,11 +49,20 @@
+ + + +
diff --git a/src/routes/c/[name]/+page.svelte b/src/routes/c/[name]/+page.svelte index 9bb8dec1..357e345f 100644 --- a/src/routes/c/[name]/+page.svelte +++ b/src/routes/c/[name]/+page.svelte @@ -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' diff --git a/src/routes/communities/+page.svelte b/src/routes/communities/+page.svelte index 3a4cd7af..3d849c7a 100644 --- a/src/routes/communities/+page.svelte +++ b/src/routes/communities/+page.svelte @@ -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 @@ }} />
- { $page.url.searchParams.set('q', search) goto($page.url.toString(), { diff --git a/src/routes/donate/+page.svelte b/src/routes/donate/+page.svelte new file mode 100644 index 00000000..3fa705b6 --- /dev/null +++ b/src/routes/donate/+page.svelte @@ -0,0 +1,6 @@ +

I don't take donations.

+ +

+ 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. +

diff --git a/src/routes/inbox/+page.svelte b/src/routes/inbox/+page.svelte index 93f454a1..fbc4c0f3 100644 --- a/src/routes/inbox/+page.svelte +++ b/src/routes/inbox/+page.svelte @@ -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 @@

Inbox

-
diff --git a/src/routes/post/[instance]/[id]/+page.svelte b/src/routes/post/[instance]/[id]/+page.svelte index c91c4c8a..d87416ec 100644 --- a/src/routes/post/[instance]/[id]/+page.svelte +++ b/src/routes/post/[instance]/[id]/+page.svelte @@ -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' diff --git a/src/routes/saved/+page.svelte b/src/routes/saved/+page.svelte index 7d1ec71a..9eaf392b 100644 --- a/src/routes/saved/+page.svelte +++ b/src/routes/saved/+page.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' diff --git a/src/routes/search/+error.svelte b/src/routes/search/+error.svelte new file mode 100644 index 00000000..ea837df0 --- /dev/null +++ b/src/routes/search/+error.svelte @@ -0,0 +1,8 @@ + + +
+

{$page.status}

+ The search timed out. This is an issue on the server's side. +
diff --git a/src/routes/search/+page.svelte b/src/routes/search/+page.svelte index e69de29b..2aac929b 100644 --- a/src/routes/search/+page.svelte +++ b/src/routes/search/+page.svelte @@ -0,0 +1,98 @@ + + + + Search + + +
+ { + const url = $page.url + url.searchParams.set('type', e.detail) + url.searchParams.delete('page') + goto(url, { + invalidateAll: true, + }) + }} + /> + { + $page.url.searchParams.set('q', query) + goto($page.url.toString(), { + invalidateAll: true, + }) + }} + /> +
+{#if !data.results} +
+ +

No results

+

Search for something!

+
+{:else} +
+ {#each data.results as result} + {#if isPost(result)} + + {:else if isComment(result)} + + {:else if isCommunity(result)} + + {:else if isUser(result)} + + {/if} + {/each} +
+ { + $page.url.searchParams.set('page', p.detail.toString()) + goto($page.url, { + invalidateAll: true, + }) + }} + /> +{/if} diff --git a/src/routes/search/+page.ts b/src/routes/search/+page.ts new file mode 100644 index 00000000..00ccd44b --- /dev/null +++ b/src/routes/search/+page.ts @@ -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, + } +} diff --git a/src/routes/u/[name]/+page.svelte b/src/routes/u/[name]/+page.svelte index 22e3a624..44ead77a 100644 --- a/src/routes/u/[name]/+page.svelte +++ b/src/routes/u/[name]/+page.svelte @@ -1,5 +1,5 @@