refactor: modal
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import Sidebar from '@/components/Sidebar'
|
import Sidebar from '@/components/Sidebar'
|
||||||
import { Separator } from '@/components/ui/separator'
|
import { Separator } from '@/components/ui/separator'
|
||||||
import { cn, isAndroid } from '@/lib/utils'
|
import { cn } from '@/lib/utils'
|
||||||
import NoteListPage from '@/pages/primary/NoteListPage'
|
import NoteListPage from '@/pages/primary/NoteListPage'
|
||||||
import HomePage from '@/pages/secondary/HomePage'
|
import HomePage from '@/pages/secondary/HomePage'
|
||||||
import { TPageRef } from '@/types'
|
import { TPageRef } from '@/types'
|
||||||
@@ -92,6 +92,7 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
|
|||||||
const { isSmallScreen } = useScreenSize()
|
const { isSmallScreen } = useScreenSize()
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
window.history.pushState(null, '', window.location.href)
|
||||||
if (window.location.pathname !== '/') {
|
if (window.location.pathname !== '/') {
|
||||||
if (
|
if (
|
||||||
['/users', '/notes', '/relays'].some((path) => window.location.pathname.startsWith(path)) &&
|
['/users', '/notes', '/relays'].some((path) => window.location.pathname.startsWith(path)) &&
|
||||||
@@ -118,7 +119,10 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
|
|||||||
|
|
||||||
const onPopState = (e: PopStateEvent) => {
|
const onPopState = (e: PopStateEvent) => {
|
||||||
const closeModal = modalManager.pop()
|
const closeModal = modalManager.pop()
|
||||||
if (closeModal) return
|
if (closeModal) {
|
||||||
|
window.history.forward()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let state = e.state as { index: number; url: string } | null
|
let state = e.state as { index: number; url: string } | null
|
||||||
setSecondaryStack((pre) => {
|
setSecondaryStack((pre) => {
|
||||||
@@ -173,25 +177,10 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
const onLeave = (event: BeforeUnloadEvent) => {
|
|
||||||
// Cancel the event as stated by the standard.
|
|
||||||
event.preventDefault()
|
|
||||||
// Chrome requires returnValue to be set.
|
|
||||||
event.returnValue = ''
|
|
||||||
}
|
|
||||||
|
|
||||||
window.addEventListener('popstate', onPopState)
|
window.addEventListener('popstate', onPopState)
|
||||||
|
|
||||||
if (isAndroid()) {
|
|
||||||
window.addEventListener('beforeunload', onLeave)
|
|
||||||
}
|
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener('popstate', onPopState)
|
window.removeEventListener('popstate', onPopState)
|
||||||
|
|
||||||
if (isAndroid()) {
|
|
||||||
window.removeEventListener('beforeunload', onLeave)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
@@ -211,9 +200,6 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const pushSecondaryPage = (url: string, index?: number) => {
|
const pushSecondaryPage = (url: string, index?: number) => {
|
||||||
// FIXME: Temporary solution to prevent the back action after closing
|
|
||||||
// the modal when navigating.
|
|
||||||
setTimeout(() => {
|
|
||||||
setSecondaryStack((prevStack) => {
|
setSecondaryStack((prevStack) => {
|
||||||
if (isCurrentPage(prevStack, url)) {
|
if (isCurrentPage(prevStack, url)) {
|
||||||
const currentItem = prevStack[prevStack.length - 1]
|
const currentItem = prevStack[prevStack.length - 1]
|
||||||
@@ -229,7 +215,6 @@ export function PageManager({ maxStackSize = 5 }: { maxStackSize?: number }) {
|
|||||||
}
|
}
|
||||||
return newStack
|
return newStack
|
||||||
})
|
})
|
||||||
}, 10)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const popSecondaryPage = () => {
|
const popSecondaryPage = () => {
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ export default function NoteOptions({ event, className }: { event: Event; classN
|
|||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
setIsDrawerOpen(false)
|
||||||
setIsRawEventDialogOpen(true)
|
setIsRawEventDialogOpen(true)
|
||||||
}}
|
}}
|
||||||
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
className="w-full p-6 justify-start text-lg gap-4 [&_svg]:size-5"
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
class ModalManagerService {
|
class ModalManagerService {
|
||||||
static instance: ModalManagerService
|
static instance: ModalManagerService
|
||||||
|
|
||||||
private modal?: { id: string; cb: () => void }
|
private modals: { id: string; cb: () => void }[] = []
|
||||||
private closeByUnregister = false
|
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
if (!ModalManagerService.instance) {
|
if (!ModalManagerService.instance) {
|
||||||
@@ -12,30 +11,22 @@ class ModalManagerService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
register(id: string, cb: () => void) {
|
register(id: string, cb: () => void) {
|
||||||
if (this.modal) {
|
this.modals.push({ id, cb })
|
||||||
this.modal.cb()
|
|
||||||
}
|
|
||||||
this.modal = { id, cb }
|
|
||||||
window.history.pushState(window.history.state, '', window.location.href)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unregister(id: string) {
|
unregister(id: string) {
|
||||||
if (!this.modal || this.modal.id !== id) return
|
const modal = this.modals.find((m) => m.id === id)
|
||||||
|
if (!modal) return
|
||||||
|
|
||||||
this.modal.cb()
|
modal.cb()
|
||||||
this.modal = undefined
|
this.modals = this.modals.filter((m) => m.id !== id)
|
||||||
this.closeByUnregister = true
|
|
||||||
window.history.back()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pop() {
|
pop() {
|
||||||
if (this.closeByUnregister) {
|
const modal = this.modals.pop()
|
||||||
this.closeByUnregister = false
|
if (!modal) return false
|
||||||
return true
|
|
||||||
}
|
modal.cb()
|
||||||
if (!this.modal) return false
|
|
||||||
this.modal.cb()
|
|
||||||
this.modal = undefined
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user