poi-plugin-quest-info-2 0.8.3 → 0.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import React, { createContext, useContext } from 'react'
2
+ import type { ReactNode } from 'react'
3
+ import { GameQuest } from '../poi/types'
4
+ import { useGameQuest } from '../poi/hooks'
5
+
6
+ export const GameQuestContext = createContext<GameQuest[]>([])
7
+
8
+ export const GameQuestProvider = ({ children }: { children?: ReactNode }) => {
9
+ const gameQuest = useGameQuest()
10
+ return (
11
+ <GameQuestContext.Provider value={gameQuest}>
12
+ {children}
13
+ </GameQuestContext.Provider>
14
+ )
15
+ }
16
+
17
+ export const useGlobalGameQuest = () => {
18
+ const gameQuest = useContext(GameQuestContext)
19
+ return gameQuest
20
+ }
@@ -1,5 +1,5 @@
1
1
  import { useCallback } from 'react'
2
- import { useGameQuest, usePluginTranslation } from '../poi/hooks'
2
+ import { usePluginTranslation } from '../poi/hooks'
3
3
  import {
4
4
  DocQuest,
5
5
  getCategory,
@@ -11,6 +11,7 @@ import {
11
11
  QUEST_STATUS,
12
12
  UnionQuest,
13
13
  } from '../questHelper'
14
+ import { useGlobalGameQuest } from './gameQuest'
14
15
  import { checkIsKcwikiSupportedLanguages, useKcwikiData } from './kcwiki'
15
16
  import { useStore, useSyncWithGame } from './store'
16
17
 
@@ -51,7 +52,7 @@ const useQuestMap = (): Record<string, DocQuest> => {
51
52
 
52
53
  export const useQuest = (): UnionQuest[] => {
53
54
  const docQuestMap = useQuestMap()
54
- const gameQuest = useGameQuest()
55
+ const gameQuest = useGlobalGameQuest()
55
56
  const { syncWithGame } = useSyncWithGame()
56
57
 
57
58
  if (syncWithGame && gameQuest.length) {
@@ -101,7 +102,7 @@ export const useQuestByCode = (code: string) => {
101
102
  }
102
103
 
103
104
  export const useQuestStatus = (gameId: number | null) => {
104
- const gameQuest = useGameQuest()
105
+ const gameQuest = useGlobalGameQuest()
105
106
 
106
107
  if (!gameId) {
107
108
  return QUEST_STATUS.UNKNOWN
@@ -4,10 +4,12 @@ import React, {
4
4
  SetStateAction,
5
5
  useCallback,
6
6
  useContext,
7
+ useState,
7
8
  } from 'react'
8
- import { ALL_TYPE_TAG, ALL_CATEGORY_TAG } from '../tags'
9
+ import { useMount, useUpdateEffect } from 'react-use'
9
10
  import { name as PACKAGE_NAME } from '../../package.json'
10
- import { createGlobalState, useMount, useUpdateEffect } from 'react-use'
11
+ import { ALL_CATEGORY_TAG, ALL_TYPE_TAG } from '../tags'
12
+ import { GameQuestProvider } from './gameQuest'
11
13
 
12
14
  export const initialState = {
13
15
  searchInput: '',
@@ -27,28 +29,29 @@ export type State = typeof initialState
27
29
  // Persist state
28
30
  const STORAGE_KEY = PACKAGE_NAME
29
31
 
30
- const useStorage = (
31
- store: State,
32
- setState: (state: State) => void,
33
- merge = true
34
- ) => {
32
+ const useStorage = <T,>(initialValue: T) => {
33
+ const [state, setState] = useState<T>(initialValue)
35
34
  // Load storage at mount
36
35
  useMount(() => {
37
- const stringStore = localStorage.getItem(STORAGE_KEY)
38
- if (stringStore == null) {
39
- return
36
+ try {
37
+ const stringStore = localStorage.getItem(STORAGE_KEY)
38
+ if (stringStore == null) {
39
+ return
40
+ }
41
+ const parsedStorage: T = JSON.parse(stringStore)
42
+ setState(parsedStorage)
43
+ } catch (error) {
44
+ console.error('Failed to load storage', error)
40
45
  }
41
- const parsedStorage: State = JSON.parse(stringStore)
42
- // TODO use deep merge
43
- const storageStore = merge ? { ...store, ...parsedStorage } : parsedStorage
44
- setState(storageStore)
45
46
  })
46
47
 
47
48
  // Save storage when store change
48
49
  useUpdateEffect(() => {
49
- const serializedStore = JSON.stringify(store)
50
+ const serializedStore = JSON.stringify(state)
50
51
  localStorage.setItem(STORAGE_KEY, serializedStore)
51
- }, [store])
52
+ }, [state])
53
+
54
+ return [state, setState] as const
52
55
  }
53
56
 
54
57
  export const getStorage = () => {
@@ -62,15 +65,14 @@ export const getStorage = () => {
62
65
  const StateContext = createContext<State>(initialState)
63
66
  const SetStateContext = createContext<Dispatch<SetStateAction<State>>>(() => {})
64
67
 
65
- const useGlobalState = createGlobalState<State>(initialState)
66
-
67
68
  export const StoreProvider = ({ children }: { children?: React.ReactNode }) => {
68
- const [state, setState] = useGlobalState()
69
- useStorage(state, setState)
69
+ const [state, setState] = useStorage<State>(initialState)
70
70
  return (
71
- <SetStateContext.Provider value={setState}>
72
- <StateContext.Provider value={state}>{children}</StateContext.Provider>
73
- </SetStateContext.Provider>
71
+ <GameQuestProvider>
72
+ <SetStateContext.Provider value={setState}>
73
+ <StateContext.Provider value={state}>{children}</StateContext.Provider>
74
+ </SetStateContext.Provider>
75
+ </GameQuestProvider>
74
76
  )
75
77
  }
76
78
 
package/src/tags.tsx CHANGED
@@ -2,7 +2,7 @@ import { Tag } from '@blueprintjs/core'
2
2
  import React from 'react'
3
3
  import styled from 'styled-components'
4
4
  import { IN_POI } from './poi/env'
5
- import { useGameQuest, useGameTab, usePluginTranslation } from './poi/hooks'
5
+ import { useGameTab, usePluginTranslation } from './poi/hooks'
6
6
  import { GameQuest, QuestTab } from './poi/types'
7
7
  import type { UnionQuest } from './questHelper'
8
8
  import {
@@ -27,8 +27,8 @@ import {
27
27
  } from './questHelper'
28
28
  import { useSyncWithGame } from './store'
29
29
  import { useFilterTags } from './store/filterTags'
30
-
31
- const yes = () => true as const
30
+ import { useGlobalGameQuest } from './store/gameQuest'
31
+ import { yes } from './utils'
32
32
 
33
33
  export const ALL_CATEGORY_TAG = {
34
34
  name: 'All',
@@ -119,7 +119,7 @@ export const TypeTags = () => {
119
119
  const { t } = usePluginTranslation()
120
120
  const gameTab = useGameTab()
121
121
  const { syncWithGame } = useSyncWithGame()
122
- const gameQuests = useGameQuest()
122
+ const gameQuests = useGlobalGameQuest()
123
123
  const inProgressQuest = gameQuests.filter((gameQuest) =>
124
124
  isInProgressQuest(gameQuest)
125
125
  )
package/src/utils.ts ADDED
@@ -0,0 +1,15 @@
1
+ export const noop = () => {}
2
+
3
+ export const id = <T>(x: T) => x
4
+
5
+ export const yes = () => true as const
6
+
7
+ export const And =
8
+ <T extends (...args: any[]) => boolean>(...fnArray: T[]) =>
9
+ (...args: Parameters<T>) =>
10
+ fnArray.every((fn) => fn(...args))
11
+
12
+ export const Or =
13
+ <T extends (...args: any[]) => boolean>(...fnArray: T[]) =>
14
+ (...args: Parameters<T>) =>
15
+ fnArray.some((fn) => fn(...args))