refactor: move utils to global helper

This commit is contained in:
Zephyruso 2023-09-05 16:03:01 +08:00
parent c4a7163786
commit 41fb51e270
4 changed files with 50 additions and 36 deletions

View File

@ -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'
const [windowWidth, setWindowWidth] = createSignal(0)
setWindowWidth(document?.body?.clientWidth)
window.addEventListener('resize', () => {
setWindowWidth(document.body.clientWidth)
})
export const ForTwoColumns = (props: { subChild: JSX.Element[] }) => {
const { windowWidth } = useWindowWidth()
const isShowTwoColumns = createMemo(
() => windowWidth() >= 640 && renderInTwoColumn(),
) // 640 is sm size in daisyui

46
src/helpers/global.ts Normal file
View 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,
}
}

View File

@ -1 +1,2 @@
export * from './global'
export * from './proxies'

View File

@ -1,36 +1,9 @@
import dayjs from 'dayjs'
import { createSignal } from 'solid-js'
import { PROXIES_ORDERING_TYPE } from '~/constants'
export const formatTimeFromNow = (time: number | string) => {
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 = '') => {
const t = type.toLowerCase()