2022-09-23 23:42:19 +00:00
|
|
|
import { createSignal } from 'solid-js'
|
2022-10-25 16:25:42 +00:00
|
|
|
import { useRouter } from './router'
|
2022-11-13 19:35:57 +00:00
|
|
|
import type { AuthModalSearchParams, ConfirmEmailSearchParams } from '../components/Nav/AuthModal/types'
|
2023-02-17 09:21:02 +00:00
|
|
|
import type { RootSearchParams } from '../pages/types'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-11-27 05:49:48 +00:00
|
|
|
export type ModalType = 'auth' | 'subscribe' | 'feedback' | 'thank' | 'donate' | 'inviteToChat'
|
2022-09-09 11:53:35 +00:00
|
|
|
type WarnKind = 'error' | 'warn' | 'info'
|
|
|
|
|
|
|
|
export interface Warning {
|
|
|
|
body: string
|
|
|
|
kind: WarnKind
|
|
|
|
seen?: boolean
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
export const MODALS: Record<ModalType, ModalType> = {
|
|
|
|
auth: 'auth',
|
|
|
|
subscribe: 'subscribe',
|
|
|
|
feedback: 'feedback',
|
|
|
|
thank: 'thank',
|
2022-11-27 05:49:48 +00:00
|
|
|
donate: 'donate',
|
|
|
|
inviteToChat: 'inviteToChat'
|
2022-10-25 16:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const [modal, setModal] = createSignal<ModalType | null>(null)
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
const [warnings, setWarnings] = createSignal<Warning[]>([])
|
2022-09-09 11:53:35 +00:00
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
export const showModal = (modalType: ModalType) => setModal(modalType)
|
2022-11-13 19:35:57 +00:00
|
|
|
|
|
|
|
// TODO: find a better solution
|
2022-10-25 16:25:42 +00:00
|
|
|
export const hideModal = () => {
|
2022-11-13 19:35:57 +00:00
|
|
|
const { searchParams, changeSearchParam } = useRouter<
|
|
|
|
AuthModalSearchParams & ConfirmEmailSearchParams & RootSearchParams
|
|
|
|
>()
|
|
|
|
|
|
|
|
if (searchParams().modal === 'auth') {
|
|
|
|
if (searchParams().mode === 'confirm-email') {
|
|
|
|
changeSearchParam('token', null, true)
|
|
|
|
}
|
|
|
|
changeSearchParam('mode', null, true)
|
|
|
|
}
|
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
changeSearchParam('modal', null, true)
|
2022-11-13 19:35:57 +00:00
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
setModal(null)
|
|
|
|
}
|
2022-10-17 20:53:04 +00:00
|
|
|
|
2022-10-25 16:25:42 +00:00
|
|
|
export const clearWarns = () => setWarnings([])
|
|
|
|
export const warn = (warning: Warning) => setWarnings([...warnings(), warning])
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export const useWarningsStore = () => {
|
|
|
|
return {
|
2022-10-25 16:25:42 +00:00
|
|
|
warnings
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useModalStore = () => {
|
|
|
|
return {
|
2022-10-25 16:25:42 +00:00
|
|
|
modal
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
|
|
|
}
|