[Assitants] Community | User tabs (#773)
Browse files* [Assistants] Add pagination
* [Assitants] Community | User tabs
* format
* minimize diff
* Update src/routes/assistants/+page.svelte
Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
* use data.user
* rm unused import
* Use a href & simplify overall
* lint
* it will never be a button
* statements need to be reactive
* format
* set filter differntly
* Revert "set filter differntly"
This reverts commit fc8a2fc67463fb5ad39be3c3ae16ce74caa1f05d.
* use inline
* avoid ugly image change
* fix query
* ui update
* update link to profiles
* link to HF profile
* dark mode
* Update +page.svelte
* refactor getHref to be more descriptive
* Preserve `user` searchQuery when chaning model ids
* `getHref` utility
* dont show single "Community" btn for non-logged in users
* fix iterator
---------
Co-authored-by: Victor Muštar <victor.mustar@gmail.com>
Files changed (7)
- src/lib/components/Pagination.svelte+4-9
- src/lib/components/chat/AssistantIntroduction.svelte+1-2
- src/lib/utils/getHref.tsAdded+41
- src/routes/assistant/[assistantId]/+page.svelte+1-2
- src/routes/assistants/+page.server.ts+10-1
- src/routes/assistants/+page.svelte+66-9
- src/routes/settings/assistants/[assistantId]/+page.svelte+1-1
123456 unmodified lines12131415161718192021222342 unmodified lines666768697071728 unmodified lines818283848586871 unmodified line89909192939495<script lang="ts">import { page } from "$app/stores";import PaginationArrow from "./PaginationArrow.svelte";export let classNames = "";6 unmodified lines$: pageIndex = parseInt($page.url.searchParams.get("p") ?? "0");$: pageIndexes = getPageIndexes(pageIndex, numTotalPages);function getHref(url: URL | string, pageIdx: number) {const newUrl = new URL(url);newUrl.searchParams.set("p", pageIdx.toString());return newUrl.toString();}function getPageIndexes(pageIdx: number, nTotalPages: number) {let pageIdxs: number[] = [];42 unmodified lines><li><PaginationArrowhref={getHref($page.url, pageIndex - 1)}direction="previous"isDisabled={pageIndex - 1 < 0}/>8 unmodified lines: ''}"class:pointer-events-none={pageIdx === ELLIPSIS_IDX || pageIndex === pageIdx}href={getHref($page.url, pageIdx)}>{pageIdx === ELLIPSIS_IDX ? "..." : pageIdx + 1}</a>1 unmodified line{/each}<li><PaginationArrowhref={getHref($page.url, pageIndex + 1)}direction="next"isDisabled={pageIndex + 1 >= numTotalPages}/>1234566 unmodified lines13141516171842 unmodified lines616263646566678 unmodified lines767778798081821 unmodified line84858687888990<script lang="ts">import { page } from "$app/stores";import { getHref } from "$lib/utils/getHref";import PaginationArrow from "./PaginationArrow.svelte";export let classNames = "";6 unmodified lines$: pageIndex = parseInt($page.url.searchParams.get("p") ?? "0");$: pageIndexes = getPageIndexes(pageIndex, numTotalPages);function getPageIndexes(pageIdx: number, nTotalPages: number) {let pageIdxs: number[] = [];42 unmodified lines><li><PaginationArrowhref={getHref($page.url, { newKeys: { p: (pageIndex - 1).toString() } })}direction="previous"isDisabled={pageIndex - 1 < 0}/>8 unmodified lines: ''}"class:pointer-events-none={pageIdx === ELLIPSIS_IDX || pageIndex === pageIdx}href={getHref($page.url, { newKeys: { p: pageIdx.toString() } })}>{pageIdx === ELLIPSIS_IDX ? "..." : pageIdx + 1}</a>1 unmodified line{/each}<li><PaginationArrowhref={getHref($page.url, { newKeys: { p: (pageIndex + 1).toString() } })}direction="next"isDisabled={pageIndex + 1 >= numTotalPages}/>
46 unmodified lines474849505152535446 unmodified lines<p class="pt-2 text-sm text-gray-400 dark:text-gray-500">Created by <aclass="hover:underline"href="https://hf.co/{assistant.createdByName}"target="_blank">{assistant.createdByName}</a>46 unmodified lines4748495051525346 unmodified lines<p class="pt-2 text-sm text-gray-400 dark:text-gray-500">Created by <aclass="hover:underline"href="{base}/assistants?user={assistant.createdByName}">{assistant.createdByName}</a>
1234567891011121314151617181920212223242526272829303132333435363738394041export function getHref( url: URL | string, modifications: { newKeys?: Record<string, string | undefined | null>; existingKeys?: { behaviour: "delete_except" | "delete"; keys: string[] }; }) { const newUrl = new URL(url); const { newKeys, existingKeys } = modifications;
// exsiting keys logic if (existingKeys) { const { behaviour, keys } = existingKeys; if (behaviour === "delete") { for (const key of keys) { newUrl.searchParams.delete(key); } } else { // delete_except const keysToPreserve = keys; for (const key of [...newUrl.searchParams.keys()]) { if (!keysToPreserve.includes(key)) { newUrl.searchParams.delete(key); } } } }
// new keys logic if (newKeys) { for (const [key, val] of Object.entries(newKeys)) { if (val) { newUrl.searchParams.set(key, val); } else { newUrl.searchParams.delete(key); } } }
return newUrl.toString();}72 unmodified lines737475767778798072 unmodified lines<p class="mt-2 text-sm text-gray-500">Created by <aclass="hover:underline"href="https://hf.co/{data.assistant.createdByName}"target="_blank">{data.assistant.createdByName}</a>72 unmodified lines7374757677787972 unmodified lines<p class="mt-2 text-sm text-gray-500">Created by <aclass="hover:underline"href="{base}/assistants?user={data.assistant.createdByName}">{data.assistant.createdByName}</a>
1 unmodified line23456785 unmodified lines1415161718192021222324251 unmodified lineimport { ENABLE_ASSISTANTS } from "$env/static/private";import { collections } from "$lib/server/database.js";import type { Assistant } from "$lib/types/Assistant";import { redirect } from "@sveltejs/kit";import type { Filter } from "mongodb";const NUM_PER_PAGE = 24;5 unmodified linesconst modelId = url.searchParams.get("modelId");const pageIndex = parseInt(url.searchParams.get("p") ?? "0");// fetch the top assistants sorted by user count from biggest to smallest, filter out all assistants with only 1 users. filter by model too if modelId is providedconst filter: Filter<Assistant> = {userCount: { $gt: 1 },modelId: modelId ?? { $exists: true },featured: true,};const assistants = await collections.assistants.find(filter)1 unmodified line23456785 unmodified lines1415161718192021222324252627282930313233341 unmodified lineimport { ENABLE_ASSISTANTS } from "$env/static/private";import { collections } from "$lib/server/database.js";import type { Assistant } from "$lib/types/Assistant";import { error, redirect } from "@sveltejs/kit";import type { Filter } from "mongodb";const NUM_PER_PAGE = 24;5 unmodified linesconst modelId = url.searchParams.get("modelId");const pageIndex = parseInt(url.searchParams.get("p") ?? "0");const createdByName = url.searchParams.get("user");if (createdByName) {const existingUser = await collections.users.findOne({ username: createdByName });if (!existingUser) {throw error(404, `User "${createdByName}" doesn't exist`);}}// fetch the top assistants sorted by user count from biggest to smallest, filter out all assistants with only 1 users. filter by model too if modelId is providedconst filter: Filter<Assistant> = {userCount: { $gt: 1 },modelId: modelId ?? { $exists: true },featured: true,...(createdByName && { createdByName }),};const assistants = await collections.assistants.find(filter)
9 unmodified lines1011121314151617181920212223242553 unmodified lines798081828384858623 unmodified lines1101111121131141151161179 unmodified linesimport CarbonAdd from "~icons/carbon/add";import CarbonHelpFilled from "~icons/carbon/help-filled";import Pagination from "$lib/components/Pagination.svelte";export let data: PageData;const onModelChange = (e: Event) => {const newUrl = new URL($page.url);newUrl.search = ""; // clear searchParams (such as "p" for pagination)if ((e.target as HTMLSelectElement).value) {newUrl.searchParams.set("modelId", (e.target as HTMLSelectElement).value);}goto(newUrl);};</script>53 unmodified lines<CarbonAdd />Create New assistant</a></div><div class="mt-10 grid grid-cols-2 gap-3 sm:gap-5 md:grid-cols-3 lg:grid-cols-4">{#each data.assistants as assistant}<ahref="{base}/assistant/{assistant._id}"class="flex flex-col items-center justify-center overflow-hidden text-balance rounded-xl border bg-gray-50/50 px-4 py-6 text-center shadow hover:bg-gray-50 hover:shadow-inner max-sm:px-4 sm:h-64 sm:pb-4 dark:border-gray-800/70 dark:bg-gray-950/20 dark:hover:bg-gray-950/40"23 unmodified lines<p class="mt-auto pt-2 text-xs text-gray-400 dark:text-gray-500">Created by <aclass="hover:underline"href="https://hf.co/{assistant.createdByName}"target="_blank">{assistant.createdByName}</a>9 unmodified lines1011121314151617181920212223242526272829303153 unmodified lines85868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314423 unmodified lines1681691701711721731749 unmodified linesimport CarbonAdd from "~icons/carbon/add";import CarbonHelpFilled from "~icons/carbon/help-filled";import CarbonClose from "~icons/carbon/close";import CarbonArrowUpRight from "~icons/carbon/arrow-up-right";import CarbonEarthAmerica from "~icons/carbon/earth-americas-filled";import Pagination from "$lib/components/Pagination.svelte";import { getHref } from "$lib/utils/getHref";export let data: PageData;$: assistantsCreator = $page.url.searchParams.get("user");$: createdByMe = data.user?.username && data.user.username === assistantsCreator;const onModelChange = (e: Event) => {const newUrl = getHref($page.url, {newKeys: { modelId: (e.target as HTMLSelectElement).value },existingKeys: { behaviour: "delete_except", keys: ["user"] },});goto(newUrl);};</script>53 unmodified lines<CarbonAdd />Create New assistant</a></div><div class="mt-7 flex items-center gap-x-2 text-sm">{#if assistantsCreator && !createdByMe}<divclass="flex items-center gap-1.5 rounded-full border border-gray-300 bg-gray-50 px-3 py-1 dark:border-gray-600 dark:bg-gray-700 dark:text-white">{assistantsCreator}'s Assistants<ahref={getHref($page.url, {existingKeys: { behaviour: "delete", keys: ["user", "modelId", "p"] },})}class="group"><CarbonCloseclass="text-xs group-hover:text-gray-800 dark:group-hover:text-gray-300"/></a></div>{#if isHuggingChat}<ahref="https://hf.co/{assistantsCreator}"target="_blank"class="ml-auto flex items-center text-xs text-gray-500 underline hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-300"><CarbonArrowUpRight class="mr-1 flex-none text-[0.58rem]" target="_blank" />View {assistantsCreator}on HF</a>{/if}{:else if data.user?.username}<ahref={getHref($page.url, {existingKeys: { behaviour: "delete", keys: ["user", "modelId", "p"] },})}class="flex items-center gap-1.5 rounded-full border px-3 py-1 {!assistantsCreator? 'border-gray-300 bg-gray-50 dark:border-gray-600 dark:bg-gray-700 dark:text-white': 'border-transparent text-gray-400 hover:text-gray-800 dark:hover:text-gray-300'}"><CarbonEarthAmerica class="text-xs" />Community</a><ahref={getHref($page.url, {newKeys: { user: data.user.username },existingKeys: { behaviour: "delete", keys: ["modelId", "p"] },})}class="flex items-center gap-1.5 rounded-full border px-3 py-1 {assistantsCreator &&createdByMe? 'border-gray-300 bg-gray-50 dark:border-gray-600 dark:bg-gray-700 dark:text-white': 'border-transparent text-gray-400 hover:text-gray-800 dark:hover:text-gray-300'}">{data.user.username}</a>{/if}</div><div class="mt-8 grid grid-cols-2 gap-3 sm:gap-5 md:grid-cols-3 lg:grid-cols-4">{#each data.assistants as assistant (assistant._id)}<ahref="{base}/assistant/{assistant._id}"class="flex flex-col items-center justify-center overflow-hidden text-balance rounded-xl border bg-gray-50/50 px-4 py-6 text-center shadow hover:bg-gray-50 hover:shadow-inner max-sm:px-4 sm:h-64 sm:pb-4 dark:border-gray-800/70 dark:bg-gray-950/20 dark:hover:bg-gray-950/40"23 unmodified lines<p class="mt-auto pt-2 text-xs text-gray-400 dark:text-gray-500">Created by <aclass="hover:underline"href="{base}/assistants?user={assistant.createdByName}">{assistant.createdByName}</a>
61 unmodified lines6263646566676861 unmodified lines<p class="text-sm text-gray-500">Model: <span class="font-semibold"> {assistant?.modelId} </span><span class="text-gray-300">•</span> Created by<a class="underline" target="_blank" href={"https://hf.co/" + assistant?.createdByName}>{assistant?.createdByName}</a></p>61 unmodified lines6263646566676861 unmodified lines<p class="text-sm text-gray-500">Model: <span class="font-semibold"> {assistant?.modelId} </span><span class="text-gray-300">•</span> Created by<a class="underline" href="{base}/assistants?user={assistant?.createdByName}">{assistant?.createdByName}</a></p>