mirror of
https://github.com/spliit-app/spliit.git
synced 2026-02-15 03:56:13 +01:00
* feat: initialise a new totals tab with basic UI * fix: update group tabs and add stats page * fix: styling within the new elements * Prettier * Display active user expenses only if active user is set --------- Co-authored-by: Sebastien Castiel <sebastien@castiel.me>
65 lines
1.6 KiB
TypeScript
65 lines
1.6 KiB
TypeScript
import { useEffect, useState } from 'react'
|
|
|
|
export function useMediaQuery(query: string): boolean {
|
|
const getMatches = (query: string): boolean => {
|
|
// Prevents SSR issues
|
|
if (typeof window !== 'undefined') {
|
|
return window.matchMedia(query).matches
|
|
}
|
|
return false
|
|
}
|
|
|
|
const [matches, setMatches] = useState<boolean>(getMatches(query))
|
|
|
|
function handleChange() {
|
|
setMatches(getMatches(query))
|
|
}
|
|
|
|
useEffect(() => {
|
|
const matchMedia = window.matchMedia(query)
|
|
|
|
// Triggered at the first client-side load and if query changes
|
|
handleChange()
|
|
|
|
// Listen matchMedia
|
|
if (matchMedia.addListener) {
|
|
matchMedia.addListener(handleChange)
|
|
} else {
|
|
matchMedia.addEventListener('change', handleChange)
|
|
}
|
|
|
|
return () => {
|
|
if (matchMedia.removeListener) {
|
|
matchMedia.removeListener(handleChange)
|
|
} else {
|
|
matchMedia.removeEventListener('change', handleChange)
|
|
}
|
|
}
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [query])
|
|
|
|
return matches
|
|
}
|
|
|
|
export function useBaseUrl() {
|
|
const [baseUrl, setBaseUrl] = useState<string | null>(null)
|
|
useEffect(() => {
|
|
setBaseUrl(window.location.origin)
|
|
}, [])
|
|
return baseUrl
|
|
}
|
|
|
|
/**
|
|
* @returns The active user, or `null` until it is fetched from local storage
|
|
*/
|
|
export function useActiveUser(groupId: string) {
|
|
const [activeUser, setActiveUser] = useState<string | null>(null)
|
|
|
|
useEffect(() => {
|
|
const activeUser = localStorage.getItem(`${groupId}-activeUser`)
|
|
if (activeUser) setActiveUser(activeUser)
|
|
}, [groupId])
|
|
|
|
return activeUser
|
|
}
|