webapp/src/stores/ui.ts

89 lines
1.9 KiB
TypeScript
Raw Normal View History

import type {
AuthModalSearchParams,
AuthModalSource,
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
import { createSignal } from 'solid-js'
import { useRouter } from './router'
export type ModalType =
| 'auth'
| 'subscribe'
| 'feedback'
| 'thank'
| 'confirm'
| 'donate'
| 'inviteToChat'
| 'uploadImage'
| 'simplifiedEditorUploadImage'
2023-05-11 11:43:14 +00:00
| 'uploadCoverImage'
| 'editorInsertLink'
| 'followers'
| 'following'
2023-11-02 19:21:51 +00:00
| 'search'
| 'inviteCoAuthors'
| 'share'
2023-05-11 11:43:14 +00:00
2022-10-25 16:25:42 +00:00
export const MODALS: Record<ModalType, ModalType> = {
auth: 'auth',
subscribe: 'subscribe',
feedback: 'feedback',
thank: 'thank',
confirm: 'confirm',
2022-11-27 05:49:48 +00:00
donate: 'donate',
inviteToChat: 'inviteToChat',
2023-05-11 11:43:14 +00:00
uploadImage: 'uploadImage',
simplifiedEditorUploadImage: 'simplifiedEditorUploadImage',
uploadCoverImage: 'uploadCoverImage',
editorInsertLink: 'editorInsertLink',
followers: 'followers',
2023-11-02 19:21:51 +00:00
following: 'following',
2024-01-21 13:56:36 +00:00
search: 'search',
inviteCoAuthors: 'inviteCoAuthors',
share: 'share',
2022-10-25 16:25:42 +00:00
}
const [modal, setModal] = createSignal<ModalType>(null)
2022-09-09 11:53:35 +00:00
const { searchParams, changeSearchParams } = useRouter<
AuthModalSearchParams & ConfirmEmailSearchParams & RootSearchParams
>()
export const showModal = (modalType: ModalType, modalSource?: AuthModalSource) => {
if (modalSource) {
changeSearchParams({
source: modalSource,
})
}
setModal(modalType)
}
// TODO: find a better solution
2022-10-25 16:25:42 +00:00
export const hideModal = () => {
const newSearchParams: Partial<AuthModalSearchParams & ConfirmEmailSearchParams & RootSearchParams> = {
modal: null,
source: null,
}
if (searchParams().modal === 'auth') {
if (searchParams().mode === 'confirm-email') {
newSearchParams.token = null
}
newSearchParams.mode = null
}
changeSearchParams(newSearchParams, true)
2022-10-25 16:25:42 +00:00
setModal(null)
}
2022-10-17 20:53:04 +00:00
2022-09-09 11:53:35 +00:00
export const useModalStore = () => {
return {
modal,
2022-09-09 11:53:35 +00:00
}
}