impr: restructure src/lib
This commit is contained in:
376 files changed
+1000
-1033
No files matched your search
@@ -0,0 +1,117 @@
|
||||
import { profile } from '$lib/app/auth.svelte'
|
||||
import type {
|
||||
Comment,
|
||||
CommentReportView,
|
||||
CommentView,
|
||||
Community,
|
||||
CommunityView,
|
||||
PersonView,
|
||||
Post,
|
||||
PostReportView,
|
||||
PostView,
|
||||
PrivateMessage,
|
||||
PrivateMessageReportView,
|
||||
PrivateMessageView,
|
||||
} from '$lib/client/types'
|
||||
import { writable } from 'svelte/store'
|
||||
|
||||
export type Result =
|
||||
| PostView
|
||||
| Post
|
||||
| CommentView
|
||||
| Comment
|
||||
| PersonView
|
||||
| CommunityView
|
||||
| PrivateMessageView
|
||||
| PostReportView
|
||||
| CommentReportView
|
||||
| PrivateMessageReportView
|
||||
| PrivateMessage
|
||||
|
||||
export function getItemPublished(item: Result) {
|
||||
// reports... why
|
||||
if ('post_report' in item) return item.post_report.published
|
||||
if ('comment_report' in item) return item.comment_report.published
|
||||
if ('private_message_report' in item)
|
||||
return item.private_message_report.published
|
||||
|
||||
// others
|
||||
if ('private_message' in item) return item.private_message.published
|
||||
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 const isPostView = (item: Result): item is PostView =>
|
||||
'post' in item && !('comment' in item)
|
||||
|
||||
export const isPost = (item: Result): item is Post =>
|
||||
'name' in item && !('content' in item)
|
||||
|
||||
export const isCommentView = (item: Result): item is CommentView =>
|
||||
'comment' in item
|
||||
|
||||
export const isComment = (item: Result): item is Comment => 'content' in item
|
||||
|
||||
export const isCommunityView = (item: Result): item is CommunityView =>
|
||||
'community' in item
|
||||
|
||||
export const isUser = (item: Result): item is PersonView => 'person' in item
|
||||
|
||||
export const isPostReport = (item: Result): item is PostReportView =>
|
||||
'post_report' in item
|
||||
|
||||
export const isCommentReport = (item: Result): item is CommentReportView =>
|
||||
'comment_report' in item
|
||||
|
||||
export const isPrivateMessageReport = (
|
||||
item: Result,
|
||||
): item is PrivateMessageReportView => 'private_message_report' in item
|
||||
|
||||
export interface ResumableItem {
|
||||
url: string
|
||||
id?: number
|
||||
name: string
|
||||
avatar?: string
|
||||
type: 'community' | 'post'
|
||||
subdivision?: {
|
||||
avatar?: string
|
||||
name: string
|
||||
}
|
||||
}
|
||||
|
||||
function resumableStore(limit: number = 10) {
|
||||
const { subscribe, set, update } = writable<ResumableItem[]>([])
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
set,
|
||||
update,
|
||||
add: (item: ResumableItem) => {
|
||||
update((resumables) => {
|
||||
if (resumables.find((i) => JSON.stringify(i) == JSON.stringify(item)))
|
||||
return resumables
|
||||
resumables.unshift(item)
|
||||
if (resumables.length > limit) resumables.pop()
|
||||
return resumables
|
||||
})
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export const resumables = resumableStore()
|
||||
|
||||
export function addFavorite(item: Community, add: boolean = true) {
|
||||
const favs = profile.current.favorites ?? []
|
||||
if (favs.map((fav) => fav.id).includes(item.id)) if (add) return
|
||||
if (!add) {
|
||||
favs.splice(favs.map((c) => c.id).indexOf(item.id), 1)
|
||||
} else {
|
||||
favs.unshift(item)
|
||||
}
|
||||
profile.current.favorites = favs
|
||||
}
|
||||
Reference in new issue
Block a user