-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web)!: preserve state when search
- Loading branch information
Showing
14 changed files
with
235 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { useEffect, useRef, EffectCallback, DependencyList } from 'react' | ||
|
||
export const useDidMountEffect = (func: EffectCallback, deps: DependencyList) => { | ||
const didMount = useRef(false) | ||
|
||
useEffect(() => { | ||
if (didMount.current) func() | ||
else didMount.current = true | ||
}, deps) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import React from 'react' | ||
|
||
export * from './onServiceWorkerInstalled' | ||
export * from './onServiceWorkerUpdateFound' | ||
export * from './onServiceWorkerUpdateReady' | ||
// export * from './replaceHydrateFunction' | ||
export * from './wrapPageElement' | ||
export * from './wrapRootElement' | ||
|
||
export const onClientEntry = () => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
const whyDidYouRender = require('@welldone-software/why-did-you-render') | ||
whyDidYouRender(React, { | ||
trackAllPureComponents: true | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { SearchStore, SearchCache } from './SearchStore' | ||
|
||
export interface SearchEvent { | ||
'search/update': { | ||
target: 'collection' | 'search' | ||
value: Partial<SearchCache> | ||
} | ||
'search/override': SearchStore | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ListingHentai } from '../../core/@types/ListingHentai' | ||
|
||
export interface SearchCache { | ||
// State for observing raw input | ||
input: string | ||
// Lock search query when page changes and text input change as well | ||
query: string | ||
// Set search dialog to be show on first time or not | ||
first: boolean | ||
// Put all searh results in here (for listing) | ||
res: ListingHentai[] | ||
// Set mode between search from list or nhentai (if modeLock is present, then hide the selector and lock search into that mode) | ||
mode: 'list' | 'nh' | ||
// Stuff to be rendered | ||
page: number | ||
maxPage: number | ||
renderedRaw: ListingHentai[] | ||
} | ||
|
||
export interface SearchStore { | ||
search: { | ||
search: SearchCache | ||
collection: SearchCache | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { StoreonModule } from 'storeon' | ||
|
||
import { SearchStore, SearchCache } from '../@types/SearchStore' | ||
import { SearchEvent } from '../@types/SearchEvent' | ||
|
||
export const search: StoreonModule<SearchStore, SearchEvent> = store => { | ||
store.on('@init', () => { | ||
const defaultState: SearchCache = { | ||
input: '', | ||
query: '', | ||
first: true, | ||
res: [], | ||
page: 1, | ||
maxPage: 5, | ||
renderedRaw: [], | ||
mode: 'list', | ||
} | ||
|
||
return { | ||
search: { | ||
search: defaultState, | ||
collection: defaultState, | ||
} | ||
} | ||
}) | ||
|
||
store.on('search/update', (state, event) => { | ||
// eslint-disable-next-line prefer-const | ||
let payload = { | ||
...state | ||
} | ||
|
||
payload.search[event.target] = { | ||
...payload.search[event.target], | ||
...event.value, | ||
} | ||
|
||
return payload | ||
}) | ||
|
||
// store.on('search/toggle', (state, event) => { | ||
// switch (event) { | ||
// case 'safemode': | ||
// return { | ||
// ...state, | ||
// settings: { | ||
// ...state.settings, | ||
// safemode: !state.settings.safemode, | ||
// }, | ||
// } | ||
// default: | ||
// return state | ||
// } | ||
// }) | ||
|
||
store.on('search/override', (state, event) => event) | ||
} |
Oops, something went wrong.