impr: use svelte/reactivity classes for URL, Map, Set
This commit is contained in:
8 files changed
+21
-38
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import type { Comment, CommentView } from 'lemmy-js-client'
|
||||
import { SvelteMap } from 'svelte/reactivity'
|
||||
|
||||
export interface CommentNodeI {
|
||||
comment_view: CommentView
|
||||
@@ -28,7 +29,7 @@ export function buildCommentsTree(
|
||||
baseDepth: number = 0,
|
||||
filter: (c: CommentView) => boolean = () => true,
|
||||
): CommentNodeI[] {
|
||||
const map = new Map<number, CommentNodeI>()
|
||||
const map = new SvelteMap<number, CommentNodeI>()
|
||||
|
||||
let min_depth = Number.MAX_VALUE
|
||||
for (const comment_view of comments) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
post: PostView
|
||||
}
|
||||
|
||||
let { post } = $props()
|
||||
let { post }: Props = $props()
|
||||
</script>
|
||||
|
||||
<Post {post} class="py-0!" />
|
||||
@@ -1,5 +1,6 @@
|
||||
import { settings } from '$lib/settings.svelte'
|
||||
import type { PostView } from 'lemmy-js-client'
|
||||
import { SvelteMap, SvelteSet } from 'svelte/reactivity'
|
||||
|
||||
export type PostViewWithCrossposts = PostView & {
|
||||
withCrossposts: true
|
||||
@@ -10,12 +11,12 @@ export type PostViewWithoutCrossposts = PostView & { withCrossposts?: false }
|
||||
export const combineCrossposts = (
|
||||
posts: PostView[],
|
||||
): (PostViewWithCrossposts | PostViewWithoutCrossposts)[] => {
|
||||
const urlMap = new Map<
|
||||
const urlMap = new SvelteMap<
|
||||
string,
|
||||
PostViewWithCrossposts | PostViewWithoutCrossposts
|
||||
>()
|
||||
const results: (PostViewWithCrossposts | PostViewWithoutCrossposts)[] = []
|
||||
const seenUrls = new Set<string>()
|
||||
const seenUrls = new SvelteSet<string>()
|
||||
|
||||
posts?.forEach(post => {
|
||||
if (
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
import { fly } from 'svelte/transition'
|
||||
import { page as pageData } from '$app/state'
|
||||
import { invalidate } from '$app/navigation'
|
||||
import { SvelteURLSearchParams } from 'svelte/reactivity'
|
||||
|
||||
interface Props {
|
||||
page?: number
|
||||
@@ -27,8 +28,8 @@
|
||||
|
||||
let customHref = (href?: string) => {
|
||||
if (href?.startsWith('?')) {
|
||||
const current = new URLSearchParams(pageData.url.searchParams)
|
||||
const newParams = new URLSearchParams(href)
|
||||
const current = new SvelteURLSearchParams(pageData.url.searchParams)
|
||||
const newParams = new SvelteURLSearchParams(href)
|
||||
|
||||
current.delete(Array.from(newParams.keys())[0])
|
||||
current.append(
|
||||
|
||||
@@ -45,6 +45,7 @@ import {
|
||||
ViewColumns,
|
||||
type IconSource,
|
||||
} from 'svelte-hero-icons'
|
||||
import { SvelteURL } from 'svelte/reactivity'
|
||||
|
||||
export interface Group {
|
||||
name: string
|
||||
@@ -411,7 +412,7 @@ export function getGroups(
|
||||
f.community.name,
|
||||
f.community.actor_id,
|
||||
)}`,
|
||||
detail: new URL(f.community.actor_id).hostname,
|
||||
detail: new SvelteURL(f.community.actor_id).hostname,
|
||||
})) ?? [],
|
||||
},
|
||||
]
|
||||
|
||||
+4
-27
@@ -7,6 +7,7 @@ import { toast } from 'mono-svelte'
|
||||
import { t } from './i18n/translations'
|
||||
import { site } from './lemmy.svelte'
|
||||
import { errorMessage } from './lemmy/error'
|
||||
import { SvelteURL } from 'svelte/reactivity'
|
||||
|
||||
// Despite the name, this will round up
|
||||
// Example: findClosestNumber([8, 16, 32, 64, 128], 76) will return 128
|
||||
@@ -29,7 +30,7 @@ export const searchParam = (
|
||||
}
|
||||
|
||||
export const fullCommunityName = (name: string, actorId: string) =>
|
||||
`${name}@${new URL(actorId).hostname}`
|
||||
`${name}@${new SvelteURL(actorId).hostname}`
|
||||
|
||||
export const placeholders = {
|
||||
url: ['https://example.com'],
|
||||
@@ -133,30 +134,6 @@ export async function uploadImage(
|
||||
|
||||
if (res.url) return res.url
|
||||
else throw new Error(`Failed to upload image. ${res.msg}`)
|
||||
} else {
|
||||
const response = await fetch(
|
||||
`${
|
||||
window.location.origin
|
||||
}/cors/${instance}/pictrs/image?${new URLSearchParams({
|
||||
auth: jwt,
|
||||
})}`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
},
|
||||
)
|
||||
|
||||
const json = await response.json()
|
||||
|
||||
if (json.msg == 'ok') {
|
||||
return `https://${instance}/pictrs/image/${json.files?.[0]?.file}`
|
||||
}
|
||||
throw new Error(
|
||||
`${
|
||||
(await response.text().catch(() => undefined)) ??
|
||||
'Failed to upload image'
|
||||
}: ${response.status}: ${response.statusText}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +144,7 @@ export const instanceToURL = (input: string) =>
|
||||
|
||||
export function canParseUrl(url: string): boolean {
|
||||
try {
|
||||
new URL(url)
|
||||
new SvelteURL(url)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
@@ -175,7 +152,7 @@ export function canParseUrl(url: string): boolean {
|
||||
}
|
||||
|
||||
export function instanceId(actorId: string) {
|
||||
return new URL(actorId).hostname
|
||||
return new SvelteURL(actorId).hostname
|
||||
}
|
||||
|
||||
export function escapeHtml(input: string): string {
|
||||
|
||||
Reference in new issue
Block a user