feat: switch to a string instead of enum

This commit is contained in:
Xyphyn committed 2023-08-02 14:22:27 -07:00
1 parent aeb727ae3b
commit 87612d6507
23 files changed
+92 -128

No files matched your search

+2 -2
View File
@@ -1,5 +1,5 @@
import { amModOfAny } from '$lib/components/lemmy/moderation/moderation.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { DEFAULT_INSTANCE_URL, getClient, instance } from '$lib/lemmy.js'
import type { MyUserInfo } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
@@ -87,7 +87,7 @@ export async function setUser(jwt: string, inst: string, username: string) {
if (!user) {
toast({
content: 'Failed to fetch your user. Is your instance down?',
type: ToastType.error,
type: 'error',
})
}
@@ -16,7 +16,7 @@
import CommentActions from '$lib/components/lemmy/comment/CommentActions.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
export let node: CommentNodeI
@@ -52,13 +52,13 @@
toast({
content:
'Successfully edited comment. You may need to refresh to see changes.',
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
// @ts-ignore i hate this
content: err,
type: ToastType.error,
type: 'error',
})
}
}}
@@ -18,7 +18,7 @@
Trash,
} from 'svelte-hero-icons'
import { getClient, getInstance } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import Menu from '$lib/components/ui/menu/Menu.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import Button from '$lib/components/input/Button.svelte'
@@ -50,7 +50,7 @@
toast({
content: error as any,
type: ToastType.error,
type: 'error',
})
}
}
@@ -95,7 +95,9 @@
<MenuButton
on:click={() => {
navigator.clipboard.writeText(
`${$page.url.origin}/post/${getInstance()}/${comment.post.id}?thread=${comment.comment.path}#${comment.comment.id}`
`${$page.url.origin}/post/${getInstance()}/${
comment.post.id
}?thread=${comment.comment.path}#${comment.comment.id}`
)
}}
>
@@ -126,12 +128,12 @@
})
toast({
content: 'The comment was deleted.',
type: ToastType.success,
type: 'success',
})
} catch (error) {
toast({
content: 'Failed to delete comment',
type: ToastType.error,
type: 'error',
})
}
}}
@@ -4,11 +4,11 @@
import type { CommentResponse } from 'lemmy-js-client'
import { getClient } from '$lib/lemmy.js'
import { createEventDispatcher } from 'svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import TextArea from '$lib/components/input/TextArea.svelte'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import { profile } from '$lib/auth.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
export let postId: number
export let parentId: number | undefined = undefined
@@ -40,18 +40,16 @@
dispatch('comment', response)
value = ''
addToast(
'Success',
'Your comment was added. You may need to refresh to see it.',
ToastType.success
)
toast({
content: 'Your comment was added. You may need to refresh to see it.',
type: 'success',
})
} catch (err) {
console.error(err)
addToast(
'Error',
'Failed to comment. (Sometimes this error appears even when successful.)',
ToastType.error
)
toast({
content: err as any,
type: 'error',
})
}
loading = false
@@ -8,7 +8,7 @@
import { getClient } from '$lib/lemmy.js'
import type { CommentView, Post } from 'lemmy-js-client'
import { fly } from 'svelte/transition'
import { toast, ToastType } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
export let nodes: CommentNodeI[]
@@ -53,7 +53,7 @@
loadingChildren = false
toast({
content: 'The API returned no comments.',
type: ToastType.error,
type: 'error',
})
return
}
@@ -77,7 +77,7 @@
console.error(error)
toast({
content: `Failed to fetch comments. ${error as any}`,
type: ToastType.error,
type: 'error',
})
}
}
@@ -5,7 +5,7 @@
import Avatar from '$lib/components/ui/Avatar.svelte'
import Card from '$lib/components/ui/Card.svelte'
import StickyCard from '$lib/components/ui/StickyCard.svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
import { getClient } from '$lib/lemmy.js'
@@ -39,7 +39,7 @@
follow: !subscribed,
})
} catch (error) {
addToast('Error', error as any, ToastType.error)
toast({ content: error as any, type: 'error' })
}
community_view.subscribed = subscribed ? 'NotSubscribed' : 'Subscribed'
@@ -58,7 +58,7 @@
block: !blocked,
})
} catch (error) {
addToast('Error', error as any, ToastType.error)
toast({ content: error as any, type: 'error' })
}
community_view.blocked = !blocked
@@ -6,7 +6,7 @@
import Modal from '$lib/components/ui/modal/Modal.svelte'
import type { Community, Person, PersonView } from 'lemmy-js-client'
import { getClient } from '$lib/lemmy.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import TextInput from '$lib/components/input/TextInput.svelte'
import { profile } from '$lib/auth.js'
@@ -40,7 +40,7 @@
if (date.toLocaleString('en') == 'Invalid Date') {
toast({
content: 'Invalid date. It must be an absolute date.',
type: ToastType.error,
type: 'error',
})
loading = false
@@ -51,7 +51,7 @@
if (date.getTime() < Date.now()) {
toast({
content: 'Invalid date. It is before the current time.',
type: ToastType.error,
type: 'error',
})
loading = false
@@ -75,14 +75,14 @@
content: `Successfully ${
banned ? 'unbanned' : 'banned'
} that user. You may need to refresh to see changes.`,
type: ToastType.success,
type: 'success',
})
item.banned = !banned
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
@@ -15,7 +15,7 @@
} from 'svelte-hero-icons'
import { Color } from '$lib/ui/colors.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
export let item: PostView | CommentView
@@ -42,12 +42,12 @@
content: `Successfully ${
lock ? 'locked' : 'unlocked'
} that post. You must refresh to see changes.`,
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
@@ -73,12 +73,12 @@
content: `Successfully ${
pinned ? 'pinned' : 'unpinned'
} that post. You must refresh to see changes.`,
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
@@ -4,7 +4,7 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Post from '$lib/components/lemmy/post/Post.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import { isComment, isPost } from '$lib/lemmy/item.js'
import type { CommentView, PostView } from 'lemmy-js-client'
@@ -54,12 +54,12 @@
content: `Successfully ${
removed ? 'restored' : 'removed'
} that submission.`,
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
@@ -4,7 +4,7 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Post from '$lib/components/lemmy/post/Post.svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import type { CommentView, PostView } from 'lemmy-js-client'
import { profile } from '$lib/auth.js'
@@ -42,10 +42,10 @@
open = false
toast({
content: 'That submission has been reported.',
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({ content: err as any, type: ToastType.error })
toast({ content: err as any, type: 'error' })
}
loading = false
+2 -2
View File
@@ -7,7 +7,7 @@
import { userSettings } from '$lib/settings.js'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import Markdown from '$lib/components/markdown/Markdown.svelte'
export let post: PostView
@@ -125,7 +125,7 @@
post.post.url = e.detail.post.url
toast({
content: 'The post was edited successfully.',
type: ToastType.success,
type: 'success',
})
}}
/>
@@ -20,7 +20,7 @@
import Button from '$lib/components/input/Button.svelte'
import MenuButton from '$lib/components/ui/menu/MenuButton.svelte'
import { page } from '$app/stores'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { createEventDispatcher } from 'svelte'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import PostForm from '$lib/components/lemmy/post/PostForm.svelte'
@@ -44,12 +44,12 @@
post.post.deleted = !deleted
toast({
content: `That post was ${deleted ? 'restored' : 'deleted'}.`,
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
}
@@ -72,7 +72,7 @@
toast({
content: error as any,
type: ToastType.error,
type: 'error',
})
}
}
@@ -6,7 +6,7 @@
import TextArea from '$lib/components/input/TextArea.svelte'
import FileInput from '$lib/components/input/FileInput.svelte'
import Button from '$lib/components/input/Button.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import SearchInput from '$lib/components/input/SearchInput.svelte'
import { Check, Icon } from 'svelte-hero-icons'
import { profile } from '$lib/auth.js'
@@ -77,7 +77,7 @@
async function submit() {
if (!data.community && !edit) {
toast({
type: ToastType.warning,
type: 'warning',
content: 'You need to set a community.',
})
return
@@ -123,7 +123,7 @@
dispatcher('submit', post.post_view)
}
} catch (err) {
toast({ content: err as any, type: ToastType.error })
toast({ content: err as any, type: 'error' })
}
}
+5 -9
View File
@@ -1,9 +1,5 @@
<script lang="ts">
import {
ToastType,
type Toast,
toasts,
} from '$lib/components/ui/toasts/toasts'
import { type Toast, toasts } from '$lib/components/ui/toasts/toasts'
import {
InformationCircle,
ExclamationTriangle,
@@ -29,13 +25,13 @@
size="20"
solid
class="relative m-2 ml-4 flex-shrink-0"
src={toast.type == ToastType.info
src={toast.type == 'info'
? InformationCircle
: toast.type == ToastType.success
: toast.type == 'success'
? CheckCircle
: toast.type == ToastType.warning
: toast.type == 'warning'
? ExclamationTriangle
: toast.type == ToastType.error
: toast.type == 'error'
? ExclamationCircle
: ExclamationCircle}
/>
+8 -35
View File
@@ -1,10 +1,12 @@
import { writable } from 'svelte/store'
export enum ToastType {
'error' = 'bg-red-500 text-white border border-red-600',
'warning' = 'bg-yellow-400 text-black border border-yellow-500',
'success' = 'bg-green-400 text-black border border-green-500',
'info' = 'dark:bg-zinc-900 bg-white border border-slate-200 dark:border-zinc-800 shadow-lg',
type ToastType = keyof typeof toastColors
const toastColors = {
error: 'bg-red-500 text-white border border-red-600',
warning: 'bg-yellow-400 text-black border border-yellow-500',
success: 'bg-green-400 text-black border border-green-500',
info: 'dark:bg-zinc-900 bg-white border border-slate-200 dark:border-zinc-800 shadow-lg',
}
export interface Toast {
@@ -19,7 +21,7 @@ export const toasts = writable<Toast[]>([])
export function toast({
title,
content,
type = ToastType.info,
type = 'info',
duration = 5000,
}: {
title?: string
@@ -47,32 +49,3 @@ export function toast({
toasts.update((toasts) => toasts.filter((toast) => toast.id != id))
}, duration)
}
/**
* @deprecated use @function toast
*/
export function addToast(
title: string | undefined,
content: string,
type: ToastType
) {
let id = 0
toasts.update((toasts) => {
id = Math.floor(Math.random() * 10000)
return [
{
id: id,
content: content,
title: title,
type: type,
},
...toasts,
]
})
setTimeout(() => {
toasts.update((toasts) => toasts.filter((toast) => toast.id != id))
}, 5000)
}
+3 -3
View File
@@ -9,7 +9,7 @@
import Button from '$lib/components/input/Button.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import EditableList from '$lib/components/ui/list/EditableList.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import {
LINKED_INSTANCE_URL,
instance,
@@ -32,12 +32,12 @@
toast({
content: 'Changed guest instance.',
type: ToastType.success,
type: 'success',
})
} catch (err) {
toast({
content: 'Failed to contact that instance URL. Is it down?',
type: ToastType.error,
type: 'error',
})
loading = false
+2 -2
View File
@@ -1,8 +1,8 @@
<script lang="ts">
import { getClient } from '$lib/lemmy.js'
import type { CommunityView } from 'lemmy-js-client'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import { profile } from '$lib/auth.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
export let community: CommunityView
@@ -23,7 +23,7 @@
subscribing = false
return res
} catch (error) {
addToast('Error', 'Failed to subscribe to community', ToastType.error)
toast({ content: 'Failed to subscribe to community', type: 'error' })
}
subscribing = false
+6 -7
View File
@@ -5,7 +5,6 @@
import Comment from '$lib/components/lemmy/comment/Comment.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
import Card from '$lib/components/ui/Card.svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import { getClient } from '$lib/lemmy.js'
import { profile } from '$lib/auth.js'
@@ -16,6 +15,7 @@
} from 'lemmy-js-client'
import { ChatBubbleOvalLeft, Icon } from 'svelte-hero-icons'
import { page } from '$app/stores'
import { toast } from '$lib/components/ui/toasts/toasts.js'
export let item: CommentReplyView | PersonMentionView | PrivateMessageView
export let read: boolean
@@ -44,11 +44,10 @@
recipient_id: message.creator.id,
})
addToast(
'Success',
'Successfully replied to that message.',
ToastType.success
)
toast({
content: 'Successfully replied to that message.',
type: 'success',
})
replying = false
@@ -57,7 +56,7 @@
replaceState: true,
})
} catch (error) {
addToast('Error', error as any, ToastType.error)
toast({ content: error as any, type: 'error' })
}
loading = false
+4 -8
View File
@@ -1,14 +1,10 @@
<script lang="ts">
import { goto } from '$app/navigation'
import { profileData, setUser } from '$lib/auth.js'
import { setUser } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import Card from '$lib/components/ui/Card.svelte'
import {
ToastType,
addToast,
toast,
} from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import {
DEFAULT_INSTANCE_URL,
LINKED_INSTANCE_URL,
@@ -42,7 +38,7 @@
if (response?.jwt) {
await setUser(response.jwt, data.instance, data.username)
toast({ content: 'Successfully logged in.', type: ToastType.success })
toast({ content: 'Successfully logged in.', type: 'success' })
goto('/')
} else {
throw new Error('Invalid credentials')
@@ -50,7 +46,7 @@
} catch (error) {
toast({
content: error as any,
type: ToastType.error,
type: 'error',
})
}
data.loading = false
+4 -4
View File
@@ -2,7 +2,7 @@
import Button from '$lib/components/input/Button.svelte'
import CommentItem from '$lib/components/lemmy/comment/CommentItem.svelte'
import Post from '$lib/components/lemmy/post/Post.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import { getClient } from '$lib/lemmy.js'
import { isCommentReport, isPostReport } from '$lib/lemmy/item.js'
import type {
@@ -38,7 +38,7 @@
toast({
content: `${resolved ? 'Resolved' : 'Unresolved'} that report.`,
type: ToastType.success,
type: 'success',
})
} else if (isPostReport(item)) {
await getClient().resolvePostReport({
@@ -50,7 +50,7 @@
resolved = !resolved
toast({
content: `${resolved ? 'Resolved' : 'Unresolved'} that report.`,
type: ToastType.success,
type: 'success',
})
}
@@ -65,7 +65,7 @@
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
+2 -2
View File
@@ -18,7 +18,7 @@
import Card from '$lib/components/ui/Card.svelte'
import PostLink from '$lib/components/lemmy/post/PostLink.svelte'
import PostMeta from '$lib/components/lemmy/post/PostMeta.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import type { CommentSortType } from 'lemmy-js-client'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import { profile } from '$lib/auth.js'
@@ -145,7 +145,7 @@
toast({
content: 'The post was edited successfully.',
type: ToastType.success,
type: 'success',
})
}}
/>
+1 -1
View File
@@ -10,7 +10,7 @@
} from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
import Button from '$lib/components/input/Button.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import Setting from './Setting.svelte'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
+5 -5
View File
@@ -22,7 +22,7 @@
import { getClient } from '$lib/lemmy.js'
import { isBlocked } from '$lib/lemmy/user.js'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import { ToastType, toast } from '$lib/components/ui/toasts/toasts.js'
import { toast } from '$lib/components/ui/toasts/toasts.js'
import Modal from '$lib/components/ui/modal/Modal.svelte'
import UserLink from '$lib/components/lemmy/user/UserLink.svelte'
import TextArea from '$lib/components/input/TextArea.svelte'
@@ -54,7 +54,7 @@
toast({
content: `Successfully ${blocked ? 'unblocked' : 'blocked'} that user.`,
type: ToastType.success,
type: 'success',
})
goto($page.url, {
@@ -63,7 +63,7 @@
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}
blocking = false
@@ -87,14 +87,14 @@
toast({
content: 'Successfully sent that person a message.',
type: ToastType.success,
type: 'success',
})
messaging = false
} catch (err) {
toast({
content: err as any,
type: ToastType.error,
type: 'error',
})
}