From e63f8ab8b8b00c70285ec909292cc808d3a07036 Mon Sep 17 00:00:00 2001 From: Ajay Bura Date: Wed, 2 Mar 2022 21:24:30 +0530 Subject: [PATCH] Add categorized spaces hook Signed-off-by: Ajay Bura --- src/app/hooks/useCategorizedSpaces.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/app/hooks/useCategorizedSpaces.js diff --git a/src/app/hooks/useCategorizedSpaces.js b/src/app/hooks/useCategorizedSpaces.js new file mode 100644 index 0000000..6d3c02a --- /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]; +}