Files
nebula.js/commands/serve/web/hooks/useAppList.js
renovate[bot] 5cde83c607 chore(deps): update dependency react-router-dom to v7 (#1644)
* chore(deps): update dependency react-router-dom to v7

* chore: additions

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: caele <tsm@qlik.com>
2024-12-27 14:54:55 +01:00

46 lines
1.5 KiB
JavaScript

import { useState, useEffect } from 'react';
import { useNavigate } from 'react-router';
import { useRootContext } from '../contexts/RootContext';
import { getConnectionInfo } from '../connect';
import { checkIfAuthorized, getAppList } from '../utils';
export const useAppList = ({ glob, info }) => {
const navigate = useNavigate();
const { setInfo, setActiveStep } = useRootContext();
const [appList, setAppList] = useState();
const [loading, setLoading] = useState(false);
useEffect(() => {
setActiveStep(1);
getConnectionInfo().then((i) => {
setInfo(i);
});
}, []);
useEffect(() => {
if (appList?.length) return;
setLoading(true);
const searchParam = new URLSearchParams(window.location.search);
// if is already authorized and does not have "shouldFetchAppList" -> append it to the url
if (searchParam.get('qlik-client-id') && !searchParam.get('shouldFetchAppList')) {
checkIfAuthorized().then(({ isAuthorized }) => {
if (isAuthorized) {
const url = new URL(window.location.href);
if (!url.search.includes('shouldFetchAppList')) {
url.searchParams.append('shouldFetchAppList', true);
}
navigate(decodeURIComponent(`${url.pathname}${url.search}`));
}
});
}
(searchParam.get('shouldFetchAppList') ? getAppList() : glob?.getDocList())?.then((apps) => {
setAppList(apps);
if (apps) setLoading(false);
});
}, [window.location.search, setLoading, info, glob]);
return { appList, loading };
};