Skip to content

Commit

Permalink
replace conceptSchemeId
Browse files Browse the repository at this point in the history
conceptSchemeId was sometimes still having the old value.
This led to unneccessary double rendering.
Removal seems to improve performance and reduce errors with fetching the wrong index
(cause the old conceptSchemeId was still hanging around).
  • Loading branch information
sroertgen committed Sep 1, 2023
1 parent 533ac48 commit 6bc9c72
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
21 changes: 5 additions & 16 deletions src/templates/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const App = ({ pageContext, children }) => {
const { data } = useSkoHubContext()
const { config } = getConfigAndConceptSchemes()
const style = conceptStyle(config.colors)
const [conceptSchemeId, setConceptSchemeId] = useState(
data?.currentScheme?.id
)
const [index, setIndex] = useState({})
const [query, setQuery] = useState(null)
const [tree, setTree] = useState(
Expand Down Expand Up @@ -47,28 +44,20 @@ const App = ({ pageContext, children }) => {
}
}

// get concept scheme id from context
useEffect(() => {
if (data?.currentScheme?.id) {
setConceptSchemeId(data.currentScheme.id)
}
}, [data])

// Fetch and load the serialized index
useEffect(() => {
conceptSchemeId &&
importIndex(conceptSchemeId, labels, setIndex, pageContext.language)
}, [conceptSchemeId, pageContext.language, labels])
importIndex(data?.currentScheme?.id, labels, pageContext.language, setIndex)
}, [data, pageContext.language, labels])

// Fetch and load the tree
useEffect(() => {
conceptSchemeId &&
data?.currentScheme?.id &&
// if node.type would be concept scheme the tree would already have been set
pageContext.node.type !== "ConceptScheme" &&
fetch(withPrefix(getFilePath(conceptSchemeId, "json")))
fetch(withPrefix(getFilePath(data?.currentScheme?.id, "json")))
.then((response) => response.json())
.then((tree) => setTree(tree))
}, [conceptSchemeId, pageContext.node.type])
}, [data, pageContext.node.type])

// Scroll current item into view
useEffect(() => {
Expand Down
7 changes: 4 additions & 3 deletions src/templates/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ export const handleKeypresses = (labels, setLabels) => {
export const importIndex = async (
conceptSchemeId,
labels,
setIndex,
language
language,
setIndex
) => {
if (!conceptSchemeId) return
const idx = new Document({
tokenize: "full",
charset: "latin",
Expand Down Expand Up @@ -106,7 +107,7 @@ export const importIndex = async (
const jsonData = await data.json()
idx.import(key, jsonData ?? null)
} catch (e) {
// console.log(e) // eslint-disable-line no-console
// console.log(e)
}
}
setIndex(idx)
Expand Down

0 comments on commit 6bc9c72

Please sign in to comment.