Skip to content

Commit

Permalink
fix: more client ts checks
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Feb 8, 2024
1 parent 494639a commit 5028cb2
Show file tree
Hide file tree
Showing 20 changed files with 156 additions and 78 deletions.
1 change: 1 addition & 0 deletions src/assets/mui/global.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import * as React from 'react'
import GlobalStyles from '@mui/material/GlobalStyles'
import { darken, lighten } from '@mui/material/styles'
Expand Down
14 changes: 8 additions & 6 deletions src/components/ClearStorage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ import { useMemory } from '@hooks/useMemory'
import { useStorage } from '@hooks/useStorage'

export default function ClearStorage() {
localStorage.clear()
sessionStorage.clear()

React.useEffect(() => {
localStorage.clear()
sessionStorage.clear()

const { filters, menus } = useMemory.getState()
useStorage.setState({
filters: {},
menus: {},
filters: structuredClone(filters),
menus: structuredClone(menus),
location: [
CONFIG.map.general.startLat || 0,
CONFIG.map.general.startLon || 0,
],
zoom: CONFIG.map.general.startZoom,
})
useMemory.setState({ Icons: null })
useMemory.setState({ Icons: null, Audio: null })
}, [])

return <Navigate to="/" />
}
1 change: 1 addition & 0 deletions src/components/layout/dialogs/filters/FilterMenu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default function FilterMenu() {
setTempFilters(filters?.filter)
}, [category, filters?.filter])

if (!category || !type) return null
return (
<DialogWrapper
open={open && type === 'filters' && !!tempFilters}
Expand Down
111 changes: 57 additions & 54 deletions src/components/layout/general/Menu.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react'
import * as React from 'react'
import Box from '@mui/material/Box'
import DialogContent from '@mui/material/DialogContent'
import Typography from '@mui/material/Typography'
Expand All @@ -22,11 +22,12 @@ import { GenericSearch } from '../drawer/ItemSearch'
import { applyToAllWebhooks, useWebhookStore } from '../dialogs/webhooks/store'

/**
* @template {import('@rm/types').AdvCategories} T
* @param {{
* category: string
* category: T
* webhookCategory?: string
* tempFilters: import('@rm/types').Filters
* children: (item: import('@rm/types').MenuItem, key: string) => React.ReactNode
* tempFilters: import('@rm/types').AllFilters[T]
* children: (index: number, key: string) => React.ReactNode
* categories?: import('@rm/types').Available[]
* title: string
* titleAction: () => void
Expand All @@ -51,7 +52,7 @@ export default function Menu({
const { t } = useTranslation()
const menus = useStorage((state) => state.menus)

const [filterDrawer, setFilterDrawer] = useState(false)
const [filterDrawer, setFilterDrawer] = React.useState(false)

const { filteredArr, count } = useFilter(
tempFilters,
Expand All @@ -74,55 +75,57 @@ export default function Menu({
)

const footerButtons = React.useMemo(
() => [
{
name: 'help',
action: () =>
useLayoutStore.setState({ help: { open: true, category } }),
icon: 'HelpOutline',
},
{
name: '',
disabled: true,
},
{
name: 'apply_to_all',
action: () =>
(webhookCategory ? useWebhookStore : useLayoutStore).setState({
[webhookCategory ? 'advanced' : 'advancedFilter']: {
open: true,
id: 'global',
category: webhookCategory || category,
selectedIds: filteredArr,
},
}),
icon: category === 'pokemon' || webhookCategory ? 'Tune' : 'FormatSize',
},
{
name: 'disable_all',
action: () =>
webhookCategory
? applyToAllWebhooks(false, filteredArr)
: applyToAll({ enabled: false }, category, filteredArr),
icon: 'Clear',
color: 'error',
},
{
name: 'enable_all',
action: () =>
webhookCategory
? applyToAllWebhooks(true, filteredArr)
: applyToAll(
{ enabled: true },
category,
filteredArr,
!webhookCategory,
),
icon: 'Check',
color: 'success',
},
...(extraButtons ?? []),
],
() =>
/** @type {import('@components/layout/general/Footer').FooterButton[]} */ ([
{
name: 'help',
action: () =>
useLayoutStore.setState({ help: { open: true, category } }),
icon: 'HelpOutline',
},
{
name: '',
disabled: true,
},
{
name: 'apply_to_all',
action: () =>
(webhookCategory ? useWebhookStore : useLayoutStore).setState({
[webhookCategory ? 'advanced' : 'advancedFilter']: {
open: true,
id: 'global',
category: webhookCategory || category,
selectedIds: filteredArr,
},
}),
icon:
category === 'pokemon' || webhookCategory ? 'Tune' : 'FormatSize',
},
{
name: 'disable_all',
action: () =>
webhookCategory
? applyToAllWebhooks(false, filteredArr)
: applyToAll({ enabled: false }, category, filteredArr),
icon: 'Clear',
color: 'error',
},
{
name: 'enable_all',
action: () =>
webhookCategory
? applyToAllWebhooks(true, filteredArr)
: applyToAll(
{ enabled: true },
category,
filteredArr,
!webhookCategory,
),
icon: 'Check',
color: 'success',
},
...(extraButtons ?? []),
]),
[category, webhookCategory, extraButtons, filteredArr, tempFilters],
)

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useLayoutStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { useStorage } from './useStorage'
* },
* dialog: {
* open: boolean,
* category: string,
* category: import('@rm/types').AdvCategories | '',
* type: string,
* },
* gymBadge: {
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'leaflet.locatecontrol'
*/
export default function useLocation() {
const map = useMap()
const [color, setColor] = useState('inherit')
const [color, setColor] = useState(
/** @type {import('@mui/material').ButtonProps['color']} */ ('inherit'),
)
const { t } = useTranslation()

const lc = useMemo(() => {
Expand Down
12 changes: 10 additions & 2 deletions src/services/filtering/genPokemon.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { t } from 'i18next'

/* eslint-disable no-nested-ternary */
/**
*
* @param {import('@rm/masterfile').Masterfile} pokemon
* @param {string[]} categories
* @returns
*/
export default function genPokemon(pokemon, categories) {
const tempObj = Object.fromEntries(
categories.map((x) => [
Expand All @@ -25,7 +30,10 @@ export default function genPokemon(pokemon, categories) {
(x) => `poke_type_${x}`,
)
const name =
form.name && form.name !== 'Normal' && j != 0 && j != pkmn.defaultFormId
form.name &&
form.name !== 'Normal' &&
j !== '0' &&
j != pkmn.defaultFormId
? formName
: pokeName
tempObj.pokemon[id] = {
Expand Down
12 changes: 9 additions & 3 deletions src/services/filtering/genPokestops.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { t } from 'i18next'

/**
*
* @param {import('@rm/masterfile').Masterfile} pokemon
* @param {*} pokestops
* @param {*} categories
* @returns
*/
export default function genPokestops(pokemon, pokestops, categories) {
const tempObj = Object.fromEntries(categories.map((x) => [x, {}]))
if (!pokestops?.filter) return {}
Expand Down Expand Up @@ -149,9 +156,8 @@ export default function genPokestops(pokemon, pokestops, categories) {
tempObj.quest_reward_9 &&
pokemon[monId]
) {
const category = [
id.charAt(0) === 'c' ? 'quest_reward_4' : 'quest_reward_9',
]
const category =
id.charAt(0) === 'c' ? 'quest_reward_4' : 'quest_reward_9'
tempObj[category][id] = {
name: `${t(`poke_${monId}`)} ${
id.charAt(0) === 'c' ? t('candy') : t('xl')
Expand Down
10 changes: 9 additions & 1 deletion src/services/functions/checkAdvFilter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @ts-check
/* eslint-disable no-plusplus */
/* eslint-disable no-cond-assign */
/* eslint-disable default-case */
export default function checkIVFilterValid(filter, dnf = true) {

/**
* @param {string} filter
* @param {boolean} [dnf]
*/
function checkIVFilterValid(filter, dnf = true) {
const input = filter.toUpperCase()
const tokenizer =
/\s*([()|&!,]|([ADSLXG]?|CP|LC|[GU]L)\s*([0-9]+(?:\.[0-9]*)?)(?:\s*-\s*([0-9]+(?:\.[0-9]*)?))?)/g
Expand Down Expand Up @@ -58,3 +64,5 @@ export default function checkIVFilterValid(filter, dnf = true) {
}
return true
}

export default checkIVFilterValid
10 changes: 9 additions & 1 deletion src/services/functions/dayCheck.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export default function dayCheck(currentStamp, desiredStamp) {
// @ts-check

/**
* @param {number} currentStamp
* @param {number} desiredStamp
*/
function dayCheck(currentStamp, desiredStamp) {
const locale = localStorage.getItem('i18nextLng') || 'en'
if (currentStamp - desiredStamp < 86400) {
return new Date(desiredStamp * 1000).toLocaleTimeString(locale)
}
return new Date(desiredStamp * 1000).toLocaleString(locale)
}

export default dayCheck
7 changes: 7 additions & 0 deletions src/services/functions/deepMerge.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// @ts-check

/** @param {any} item */
function isObject(item) {
return (
item && typeof item === 'object' && !Array.isArray(item) && item !== null
)
}

/**
* @param {Record<string, any>} target
* @param {...Record<string, any>} sources
*/
export function deepMerge(target, ...sources) {
if (!sources.length) return target
const source = sources.shift()
Expand Down
7 changes: 6 additions & 1 deletion src/services/functions/formatInterval.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function formatInterval(intervalMs) {
// @ts-check

/** @param {number} intervalMs */
function formatInterval(intervalMs) {
const diff = Math.floor(intervalMs / 1000)
const d = Math.floor(diff / 86400)
const h = Math.floor(diff / 3600)
Expand All @@ -16,3 +19,5 @@ export default function formatInterval(intervalMs) {

return { str: str.join(' '), diff }
}

export default formatInterval
2 changes: 2 additions & 0 deletions src/services/functions/fromSearchCategory.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
*
* @param {string} searchCategory
Expand Down
6 changes: 5 additions & 1 deletion src/services/functions/fromSnakeCase.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable import/prefer-default-export */
// @ts-check
import { capitalize } from '@mui/material'

/**
* @param {string} str
* @param {string} [separator]
*/
export function fromSnakeCase(str, separator = ' ') {
return capitalize(str)
.replace(/_/g, separator)
Expand Down
2 changes: 2 additions & 0 deletions src/services/functions/getBadge.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
* Get rank badge, commonly used for pvp and contests
* @param {number} rank
Expand Down
8 changes: 7 additions & 1 deletion src/services/functions/getProperName.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export default function getProperName(word) {
// @ts-check
/**
* @param {string} word
*/
function getProperName(word) {
const capital = `${word.charAt(0).toUpperCase()}${word.slice(1)}`
return capital.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
}

export default getProperName
2 changes: 2 additions & 0 deletions src/services/functions/isLocalStorageEnabled.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

export function isLocalStorageEnabled() {
const test = 'test'

Expand Down
13 changes: 8 additions & 5 deletions src/services/functions/offset.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/* eslint-disable no-bitwise */

/**
Expand Down Expand Up @@ -33,9 +34,11 @@ export const cyrb53 = (str, seed = 0) => {
*/
export const getOffset = (coords, amount, id, seed = 0) => {
const rand = cyrb53(id, seed)
return [0, 1].map((i) => {
let offset = rand[i] * (0.0002 / 4294967296) - 0.0001
offset += offset >= 0 ? -amount : amount
return coords[i] + offset
})
return /** @type {[number, number]} */ (
[0, 1].map((i) => {
let offset = rand[i] * (0.0002 / 4294967296) - 0.0001
offset += offset >= 0 ? -amount : amount
return coords[i] + offset
})
)
}
7 changes: 6 additions & 1 deletion src/services/functions/parseConditions.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function parseQuestConditions(conditions) {
// @ts-check

/** @param {string} conditions */
function parseQuestConditions(conditions) {
const [type1, type2] = JSON.parse(conditions)
const conditionsToReturn = []
const parseMadRewards = (specifics) => {
Expand Down Expand Up @@ -60,3 +63,5 @@ export default function parseQuestConditions(conditions) {
}
return conditionsToReturn
}

export default parseQuestConditions
Loading

0 comments on commit 5028cb2

Please sign in to comment.