2025-06-12 22:18:52 +07:00
|
|
|
import { Design } from '@revenge-mod/discord/design'
|
2025-10-23 17:50:32 +07:00
|
|
|
import { createContext, useContext, useRef, useState } from 'react'
|
2025-07-25 21:19:59 +07:00
|
|
|
import { useClickOutside } from 'react-native-click-outside'
|
2025-07-13 15:59:21 +07:00
|
|
|
import type { RefObject } from 'react'
|
2025-06-12 22:18:52 +07:00
|
|
|
import type { View } from 'react-native'
|
|
|
|
|
|
|
|
|
|
const { useTooltip } = Design
|
|
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
interface TooltipContextValue {
|
|
|
|
|
targetRef: RefObject<View | null>
|
|
|
|
|
setVisible: (visible: boolean) => void
|
2025-06-12 22:18:52 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
const EnablePluginTooltipContext = createContext<TooltipContextValue | null>(
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
const EssentialPluginTooltipContext = createContext<TooltipContextValue | null>(
|
|
|
|
|
null,
|
|
|
|
|
)
|
|
|
|
|
|
2025-06-12 22:18:52 +07:00
|
|
|
export function EnablePluginTooltipProvider({
|
|
|
|
|
children,
|
|
|
|
|
}: {
|
|
|
|
|
children: React.ReactNode
|
|
|
|
|
}) {
|
|
|
|
|
const [visible, setVisible] = useState(false)
|
2025-10-23 17:50:32 +07:00
|
|
|
const targetRef = useRef<View | null>(null)
|
2025-06-12 22:18:52 +07:00
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
useTooltip(targetRef, {
|
2025-06-12 22:18:52 +07:00
|
|
|
label: 'Plugin must be enabled first',
|
|
|
|
|
position: 'top',
|
|
|
|
|
visible,
|
|
|
|
|
})
|
|
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
return (
|
|
|
|
|
<EnablePluginTooltipContext.Provider value={{ targetRef, setVisible }}>
|
|
|
|
|
{children}
|
|
|
|
|
</EnablePluginTooltipContext.Provider>
|
|
|
|
|
)
|
2025-06-12 22:18:52 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function EssentialPluginTooltipProvider({
|
|
|
|
|
children,
|
|
|
|
|
}: {
|
|
|
|
|
children: React.ReactNode
|
|
|
|
|
}) {
|
|
|
|
|
const [visible, setVisible] = useState(false)
|
2025-10-23 17:50:32 +07:00
|
|
|
const targetRef = useRef<View | null>(null)
|
2025-06-12 22:18:52 +07:00
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
useTooltip(targetRef, {
|
2025-06-12 22:18:52 +07:00
|
|
|
label: 'Plugin needed for Revenge to function properly',
|
|
|
|
|
position: 'top',
|
|
|
|
|
visible,
|
|
|
|
|
})
|
|
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
return (
|
|
|
|
|
<EssentialPluginTooltipContext.Provider
|
|
|
|
|
value={{ targetRef, setVisible }}
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</EssentialPluginTooltipContext.Provider>
|
|
|
|
|
)
|
2025-06-12 22:18:52 +07:00
|
|
|
}
|
|
|
|
|
|
2025-10-23 17:50:32 +07:00
|
|
|
export function useEnablePluginTooltip() {
|
|
|
|
|
const context = useContext(EnablePluginTooltipContext)
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'useEnablePluginTooltip must be used within EnablePluginTooltipProvider',
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useEssentialPluginTooltip() {
|
|
|
|
|
const context = useContext(EssentialPluginTooltipContext)
|
|
|
|
|
if (!context) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'useEssentialPluginTooltip must be used within EssentialPluginTooltipProvider',
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return context
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function useResetTooltips() {
|
|
|
|
|
const enableTooltip = useContext(EnablePluginTooltipContext)
|
|
|
|
|
const essentialTooltip = useContext(EssentialPluginTooltipContext)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
enableTooltip?.setVisible(false)
|
|
|
|
|
essentialTooltip?.setVisible(false)
|
|
|
|
|
}
|
2025-06-12 22:18:52 +07:00
|
|
|
}
|
2025-07-25 21:19:59 +07:00
|
|
|
|
|
|
|
|
export function useClickOutsideTooltip(
|
2025-10-23 17:50:32 +07:00
|
|
|
contextHook: () => TooltipContextValue,
|
2025-07-25 21:19:59 +07:00
|
|
|
onClickOutside: () => void,
|
|
|
|
|
) {
|
2025-10-23 17:50:32 +07:00
|
|
|
const { targetRef, setVisible } = contextHook()
|
|
|
|
|
|
2025-07-25 21:19:59 +07:00
|
|
|
const ref = useClickOutside<View>(() => {
|
2025-10-23 17:50:32 +07:00
|
|
|
setVisible(false)
|
|
|
|
|
if (targetRef.current === ref.current) {
|
|
|
|
|
onClickOutside()
|
|
|
|
|
}
|
2025-07-25 21:19:59 +07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ref
|
|
|
|
|
}
|