diff --git a/src/app/hooks/useCategorizedSpaces.js b/src/app/hooks/useCategorizedSpaces.js new file mode 100644 index 00000000..6d3c02af --- /dev/null +++ b/src/app/hooks/useCategorizedSpaces.js @@ -0,0 +1,25 @@ +/* eslint-disable import/prefer-default-export */ +import { useState, useEffect } from 'react'; + +import initMatrix from '../../client/initMatrix'; +import cons from '../../client/state/cons'; + +export function useCategorizedSpaces() { + const { accountData } = initMatrix; + const [categorizedSpaces, setCategorizedSpaces] = useState([...accountData.categorizedSpaces]); + + useEffect(() => { + const handleCategorizedSpaces = () => { + setCategorizedSpaces([...accountData.categorizedSpaces]); + }; + accountData.on(cons.events.accountData.CATEGORIZE_SPACE_UPDATED, handleCategorizedSpaces); + return () => { + accountData.removeListener( + cons.events.accountData.CATEGORIZE_SPACE_UPDATED, + handleCategorizedSpaces, + ); + }; + }, []); + + return [categorizedSpaces]; +}