mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-01 13:34:33 +00:00
refactor(monorepo): decouple api and webui into workspaces packages
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import * as React from 'react'
|
||||
|
||||
const MOBILE_BREAKPOINT = 768
|
||||
|
||||
export function useIsMobile() {
|
||||
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
|
||||
|
||||
React.useEffect(() => {
|
||||
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
|
||||
const onChange = () => {
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
}
|
||||
mql.addEventListener('change', onChange)
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
return () => mql.removeEventListener('change', onChange)
|
||||
}, [])
|
||||
|
||||
return !!isMobile
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import { client } from '@/ui/api/client'
|
||||
import { useQuery } from '@tanstack/react-query'
|
||||
import { useRouterState } from '@tanstack/react-router'
|
||||
import { useEffect, useState } from 'react'
|
||||
|
||||
function getIdsFromPathname(pathname: string): {
|
||||
teamId?: string
|
||||
projectId?: string
|
||||
} {
|
||||
const teamMatch = pathname.match(/teams\/([^/]+)/)
|
||||
if (teamMatch) {
|
||||
return { teamId: teamMatch[1] }
|
||||
}
|
||||
|
||||
const projectMatch = pathname.match(/projects\/([^/]+)/)
|
||||
if (projectMatch) {
|
||||
return { projectId: projectMatch[1] }
|
||||
}
|
||||
|
||||
return {}
|
||||
}
|
||||
|
||||
export function useTeamId() {
|
||||
const [teamId, setTeamId] = useState<string | undefined>()
|
||||
const pathname = useRouterState({ select: (s) => s.location.pathname })
|
||||
|
||||
const { teamId: paramsTeamId, projectId: paramsProjectId } = getIdsFromPathname(pathname)
|
||||
|
||||
const { data: projectTeam } = useQuery({
|
||||
queryKey: ['projects', paramsProjectId, 'team'],
|
||||
queryFn: async () => {
|
||||
const res = await client.api.projects[':projectId'].team.$get({
|
||||
param: { projectId: paramsProjectId! },
|
||||
})
|
||||
if (!res.ok) throw new Error('failed to fetch project team')
|
||||
return await res.json()
|
||||
},
|
||||
enabled: !!paramsProjectId,
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (paramsTeamId) {
|
||||
setTeamId(paramsTeamId)
|
||||
} else if (projectTeam?.teamId) {
|
||||
setTeamId(projectTeam.teamId)
|
||||
}
|
||||
}, [paramsTeamId, projectTeam])
|
||||
|
||||
return teamId
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import { useLayoutEffect, useState } from 'react'
|
||||
|
||||
type ScreenSize = {
|
||||
width: number
|
||||
height: number
|
||||
}
|
||||
|
||||
export function useScreenSize(): ScreenSize {
|
||||
const [size, setSize] = useState<ScreenSize>({
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
})
|
||||
|
||||
useLayoutEffect(() => {
|
||||
function handleResize() {
|
||||
setSize({
|
||||
width: window.innerWidth,
|
||||
height: window.innerHeight,
|
||||
})
|
||||
}
|
||||
|
||||
window.addEventListener('resize', handleResize)
|
||||
return () => window.removeEventListener('resize', handleResize)
|
||||
}, [])
|
||||
|
||||
return size
|
||||
}
|
||||
Reference in New Issue
Block a user