-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache articles and positions in page
- Loading branch information
Showing
13 changed files
with
365 additions
and
266 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Fix: | ||
Retain the old search term when a suggestion is selected. This is the behaviour of GoldenDict. | ||
New Feature: | ||
Caching the search results in memory. |
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 |
---|---|---|
@@ -1,164 +1,19 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { loadDataFromJsonResponse } from '../utils'; | ||
import { QueryProvider } from './QueryScreen/QueryContext'; | ||
import QueryBar from './QueryScreen/QueryBar'; | ||
import QueryContent from './QueryScreen/QueryContent'; | ||
import { useAppContext } from '../AppContext'; | ||
import { localisedStrings } from '../translations/l10n'; | ||
|
||
export default function QueryScreen({ navigation }) { | ||
const { serverAddress, dictionaries, groups, groupings, history, setHistory, sizeSuggestion } = useAppContext(); | ||
const apiPrefix = `${serverAddress}/api`; | ||
const [query, setQuery] = useState(''); | ||
const textInputRef = useRef(null); | ||
|
||
const [localHistory, setLocalHistory] = useState([]); | ||
const [locationInLocalHistory, setLocationInLocalHistory] = useState(0); | ||
|
||
const [latestSuggestionsTimestamp, setLatestSuggestionsTimestamp] = useState(0); | ||
const [suggestions, setSuggestions] = useState([]); | ||
|
||
const [nameActiveGroup, setNameActiveGroup] = useState('Default Group'); | ||
|
||
const [article, setArticle] = useState(''); | ||
const [namesActiveDictionaries, setNamesActiveDictionaries] = useState([]); | ||
const [nameDictionaryToJumpTo, setNameDictionaryToJumpTo] = useState(''); | ||
|
||
useEffect(function() { | ||
setNameActiveGroup('Default Group'); | ||
}, [groups.length]); | ||
|
||
useEffect(function () { | ||
if (query.length === 0) { | ||
setLatestSuggestionsTimestamp(Date.now()); | ||
setSuggestions(['']); | ||
resetNamesActiveDictionaries(); | ||
} else { | ||
fetch(`${apiPrefix}/suggestions/${nameActiveGroup}/${encodeURIComponent(query)}?timestamp=${Date.now()}`) | ||
.then(loadDataFromJsonResponse) | ||
.then((data) => { | ||
if (data['timestamp'] > latestSuggestionsTimestamp) { | ||
setLatestSuggestionsTimestamp(data['timestamp']); | ||
// Filter out empty suggestions | ||
const newSuggestions = data['suggestions'].filter((suggestion) => suggestion.length > 0); | ||
if (newSuggestions.length > 0) { | ||
setSuggestions(newSuggestions); | ||
} else { | ||
setSuggestions(['']); | ||
} | ||
} | ||
}) | ||
.catch((error) => { | ||
alert(localisedStrings['query-screen-failure-fetch-suggestions']); | ||
}); | ||
} | ||
}, [dictionaries, groupings, nameActiveGroup, query, sizeSuggestion]); | ||
|
||
useEffect(function() { | ||
search(query); | ||
}, [nameActiveGroup]); | ||
|
||
function search(newQuery) { | ||
if (newQuery.length === 0) { | ||
return; | ||
} | ||
|
||
try { | ||
newQuery = decodeURIComponent(newQuery); | ||
// setQuery(newQuery); | ||
newQuery = encodeURIComponent(newQuery); | ||
} | ||
catch (error) { | ||
// setQuery(newQuery); | ||
newQuery = encodeURIComponent(newQuery); | ||
} | ||
|
||
fetch(`${apiPrefix}/query/${nameActiveGroup}/${newQuery}?dicts=True`) | ||
.then(loadDataFromJsonResponse) | ||
.then((data) => { | ||
setArticle(data['articles']); | ||
setNamesActiveDictionaries(data['dictionaries']); | ||
|
||
fetch(`${apiPrefix}/management/history`) | ||
.then(loadDataFromJsonResponse) | ||
.then((data) => { | ||
setHistory(data); | ||
}); | ||
}) | ||
.catch((error) => { | ||
resetNamesActiveDictionaries(); | ||
alert(localisedStrings['query-screen-failure-fetch-articles']); | ||
}); | ||
} | ||
|
||
function setQueryAndFocusOnInput(newQuery) { | ||
setQuery(newQuery); | ||
textInputRef.current.focus(); | ||
} | ||
|
||
function resetNamesActiveDictionaries() { | ||
if (groupings[nameActiveGroup]) { | ||
const dictionariesInGroup = []; | ||
for (let dictionary of dictionaries) { | ||
if (groupings[nameActiveGroup].includes(dictionary.name)) { | ||
dictionariesInGroup.push(dictionary.name); | ||
} | ||
} | ||
setNamesActiveDictionaries(dictionariesInGroup); | ||
} | ||
} | ||
|
||
function searchBranching(newQuery) { | ||
search(newQuery); | ||
// If the current location is not the end of the local history, pop to the current location | ||
setLocationInLocalHistory(localHistory.length); | ||
setLocalHistory([...localHistory.slice(0, locationInLocalHistory + 1), newQuery]); | ||
} | ||
|
||
function searchInLocalHistory(direction) { | ||
search(localHistory[locationInLocalHistory + direction]); | ||
setLocationInLocalHistory(locationInLocalHistory + direction); | ||
} | ||
|
||
function handleInputSubmit(e) { | ||
const firstSuggetion = suggestions[0]; | ||
if (firstSuggetion && firstSuggetion.length > 0) { | ||
searchBranching(firstSuggetion); | ||
} else { | ||
searchBranching(query); | ||
} | ||
} | ||
|
||
return ( | ||
<View style={{ flex: 1 }}> | ||
<QueryBar | ||
openDrawer={navigation.openDrawer} | ||
query={query} | ||
setQuery={setQuery} | ||
textInputRef={textInputRef} | ||
handleInputSubmit={handleInputSubmit} | ||
setArticle={setArticle} | ||
dictionaries={dictionaries} | ||
groups={groups} | ||
groupings={groupings} | ||
nameActiveGroup={nameActiveGroup} | ||
setNameActiveGroup={setNameActiveGroup} | ||
namesActiveDictionaries={namesActiveDictionaries} | ||
setNameDictionaryToJumpTo={setNameDictionaryToJumpTo} | ||
/> | ||
<QueryContent | ||
serverAddress={serverAddress} | ||
queryIsEmpty={query.length === 0} | ||
history={history} | ||
suggestions={suggestions} | ||
search={searchBranching} | ||
article={article} | ||
nameDictionaryToJumpTo={nameDictionaryToJumpTo} | ||
setQuery={setQueryAndFocusOnInput} | ||
searchInLocalHistory={searchInLocalHistory} | ||
ableToGoBack={locationInLocalHistory > 0} | ||
ableToGoForward={locationInLocalHistory < localHistory.length - 1} | ||
/> | ||
</View> | ||
<QueryProvider> | ||
<View style={{ flex: 1 }}> | ||
<QueryBar | ||
openDrawer={navigation.openDrawer} | ||
/> | ||
<QueryContent /> | ||
</View> | ||
</QueryProvider> | ||
); | ||
} | ||
} |
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
Oops, something went wrong.