mirror of
https://github.com/MetaCubeX/metacubexd.git
synced 2024-12-28 20:04:14 +08:00
refactor: move utils to global helper
This commit is contained in:
parent
c4a7163786
commit
41fb51e270
@ -1,15 +1,9 @@
|
|||||||
import { JSX, Show, createMemo, createSignal } from 'solid-js'
|
import { JSX, Show, createMemo } from 'solid-js'
|
||||||
|
import { useWindowWidth } from '~/helpers'
|
||||||
import { renderInTwoColumn } from '~/signals'
|
import { renderInTwoColumn } from '~/signals'
|
||||||
|
|
||||||
const [windowWidth, setWindowWidth] = createSignal(0)
|
|
||||||
|
|
||||||
setWindowWidth(document?.body?.clientWidth)
|
|
||||||
|
|
||||||
window.addEventListener('resize', () => {
|
|
||||||
setWindowWidth(document.body.clientWidth)
|
|
||||||
})
|
|
||||||
|
|
||||||
export const ForTwoColumns = (props: { subChild: JSX.Element[] }) => {
|
export const ForTwoColumns = (props: { subChild: JSX.Element[] }) => {
|
||||||
|
const { windowWidth } = useWindowWidth()
|
||||||
const isShowTwoColumns = createMemo(
|
const isShowTwoColumns = createMemo(
|
||||||
() => windowWidth() >= 640 && renderInTwoColumn(),
|
() => windowWidth() >= 640 && renderInTwoColumn(),
|
||||||
) // 640 is sm size in daisyui
|
) // 640 is sm size in daisyui
|
||||||
|
46
src/helpers/global.ts
Normal file
46
src/helpers/global.ts
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
import { createSignal, onCleanup } from 'solid-js'
|
||||||
|
|
||||||
|
export const useWindowWidth = () => {
|
||||||
|
const [windowWidth, setWindowWidth] = createSignal(0)
|
||||||
|
const set = () => {
|
||||||
|
setWindowWidth(document.body?.clientWidth ?? 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
set()
|
||||||
|
window.addEventListener('resize', set, {})
|
||||||
|
|
||||||
|
onCleanup(() => {
|
||||||
|
window.removeEventListener('resize', set)
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
windowWidth,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useStringBooleanMap = () => {
|
||||||
|
const [map, setMap] = createSignal<Record<string, boolean>>({})
|
||||||
|
const set = (name: string, value: boolean) => {
|
||||||
|
setMap({
|
||||||
|
...map(),
|
||||||
|
[name]: value,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const setWithCallback = async (
|
||||||
|
name: string,
|
||||||
|
callback: () => Promise<void>,
|
||||||
|
) => {
|
||||||
|
set(name, true)
|
||||||
|
try {
|
||||||
|
await callback()
|
||||||
|
} catch {}
|
||||||
|
set(name, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
map,
|
||||||
|
set,
|
||||||
|
setWithCallback,
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
|
export * from './global'
|
||||||
export * from './proxies'
|
export * from './proxies'
|
||||||
|
@ -1,36 +1,9 @@
|
|||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import { createSignal } from 'solid-js'
|
|
||||||
import { PROXIES_ORDERING_TYPE } from '~/constants'
|
import { PROXIES_ORDERING_TYPE } from '~/constants'
|
||||||
|
|
||||||
export const formatTimeFromNow = (time: number | string) => {
|
export const formatTimeFromNow = (time: number | string) => {
|
||||||
return dayjs(time).fromNow()
|
return dayjs(time).fromNow()
|
||||||
}
|
}
|
||||||
export const useStringBooleanMap = () => {
|
|
||||||
const [map, setMap] = createSignal<Record<string, boolean>>({})
|
|
||||||
const set = (name: string, value: boolean) => {
|
|
||||||
setMap({
|
|
||||||
...map(),
|
|
||||||
[name]: value,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const setWithCallback = async (
|
|
||||||
name: string,
|
|
||||||
callback: () => Promise<void>,
|
|
||||||
) => {
|
|
||||||
set(name, true)
|
|
||||||
try {
|
|
||||||
await callback()
|
|
||||||
} catch {}
|
|
||||||
set(name, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
map,
|
|
||||||
set,
|
|
||||||
setWithCallback,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const formatProxyType = (type = '') => {
|
export const formatProxyType = (type = '') => {
|
||||||
const t = type.toLowerCase()
|
const t = type.toLowerCase()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user