Skip to content

Commit

Permalink
Merge pull request #204 from metrico/dev
Browse files Browse the repository at this point in the history
fix: extra requests to labels, protected route
  • Loading branch information
jacovinus authored Dec 22, 2022
2 parents 1ed5885 + c2b0587 commit d076e97
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
9 changes: 5 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ interface storeProps {
apiUrl:string;
}
export default function App() {
const url = useSelector((store:storeProps) => store.apiUrl)
useEffect(()=>{
boscoRequest('https://cdn.jsdelivr.net/gh/metrico/boscaiolog@main/boscaiolog.js',{url})
},[])
// const url = useSelector((store:storeProps) => store.apiUrl)
// useEffect(()=>{
// console.log('making apiUrl request to',url)
// boscoRequest('https://cdn.jsdelivr.net/gh/metrico/boscaiolog@main/boscaiolog.js',{url})
// },[])


return <Main />;
Expand Down
49 changes: 10 additions & 39 deletions src/components/LabelBrowser/components/QueryBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ export const QueryBar = (props) => {
}

// search for auth params and send inside
const labels = sendLabels(
dataSourceId,
dataSourceType,
currentDataSource?.url, // which one should be?
start,
stop
);
// const labels = sendLabels(
// dataSourceId,
// dataSourceType,
// currentDataSource?.url, // which one should be?
// start,
// stop
// );
// if is view only mode (embedded) do an auto request on init
if (isEmbed)
dispatch(
Expand All @@ -221,38 +221,10 @@ export const QueryBar = (props) => {
)
);

if (onQueryValid(expr) && currentDataSource?.type !== "flux") {
return labels.then((data) => {
if (data) {
const prevLabels = [...props.data.labels];
const prevMap = prevLabels.map((m) => m.name) || [];
const newLabels = [...data];
setLabels(newLabels);
if (newLabels.length > 0) {
if (prevMap.length > 0) {
newLabels.forEach((l) => {
const labelFound = prevMap.includes(l.name);
if (labelFound) {
const pl = prevLabels.find(
(f) => f.name === l.name
);
l = { ...pl };
}
});
}
decodeQuery(
expr,
currentDataSource.url,
newLabels,
currentDataSource.id
);
}
}
});
} else {

// if there is nothing to request, show empty view
dispatch(setIsEmptyView(true));
}
// dispatch(setIsEmptyView(true));

}, []);

// force single view from small width
Expand Down Expand Up @@ -291,7 +263,6 @@ export const QueryBar = (props) => {
);
}
const labelsDecoded = decodeExpr(data.expr);

panelCP.forEach((query) => {
if (query.id === id) {
query.labels = [...labelsDecoded];
Expand Down
2 changes: 1 addition & 1 deletion src/components/LabelBrowser/components/ValuesList.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const ValuesList = (props) => {
const actQuery = panelQuery.find((f) => f.id === id);
isOpen = actQuery["browserOpen"];
setOpen(isOpen);
}, [panelQuery, id]);
}, [panelQuery,id]);

return (
open && (
Expand Down
6 changes: 2 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import store from "./store/store";
import DataSources from "./views/DataSources/DataSources";

import { CookiesProvider } from "react-cookie";
import ProtectedRoute from "./providers/ProtectedRoute";
errorInterceptor(axios);
const showDs = store.getState()["showDataSourceSetting"] || false;
ReactDOM.render(
<React.StrictMode>
<CookiesProvider>
Expand All @@ -20,12 +20,10 @@ ReactDOM.render(
<Routes>
<Route path="/" element={<App />} />
<Route path="/search" element={<App />} />
{showDs && (
<Route
path="/datasources/*"
element={showDs ? <DataSources /> : <App />}
element={<ProtectedRoute> <DataSources /></ProtectedRoute>}
/>
)}
</Routes>
</BrowserRouter>
</Provider>
Expand Down
29 changes: 29 additions & 0 deletions src/providers/ProtectedRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useMemo } from "react";
import { Navigate } from "react-router-dom";

export default function ProtectedRoute({ children }) {
const cookieMemo = useMemo(() => {
let cookie = false;
let url = "";
try {
const key = btoa("cookie-location");
const hasCookie = localStorage.getItem(key);
const parsed = JSON.parse(atob(hasCookie));

if (parsed && parsed?.cookiesAvailable) {
cookie = parsed?.cookiesAvailable;
url = "/" + parsed?.url;
} else {
cookie = false;
}
} catch (e) {
cookie = false;
}
return { cookie, url };
}, []);

if (cookieMemo.cookie) {
return <Navigate to={cookieMemo.url} />;
}
return children;
}
29 changes: 21 additions & 8 deletions src/views/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { useLocation } from "react-router-dom";
import setDataSources from "./DataSources/store/setDataSources";
import { setShowDataSourceSetting } from "./Main/setShowDataSourceSetting";


export const MainContainer = styled.div`
position: absolute;
display: flex;
Expand Down Expand Up @@ -284,9 +283,9 @@ export function updateDataSourcesWithUrl(
if (!haveUrl && basicAuth) {
apiUrl = window.location.protocol + "//" + window.location.host;
urlApi = true;
}
if(apiUrl === '') {
}

if (apiUrl === "") {
urlApi = true;
apiUrl = url;
}
Expand Down Expand Up @@ -317,11 +316,11 @@ export function updateDataSourcesWithUrl(
},
}));

if(cookies && cookieAuth) {

dispatch(setShowDataSourceSetting(false))
if (cookies && cookieAuth) {
dispatch(setShowDataSourceSetting(false));
}

localStorage.setItem("dataSources", JSON.stringify(newDs));
dispatch(setDataSources(newDs));
}

Expand Down Expand Up @@ -361,6 +360,17 @@ export default function Main() {
}
}, []);

useEffect(() => {
const urlSetting = {
url: window.location.hash,
cookiesAvailable,
};

localStorage.setItem(
btoa("cookie-location"),
btoa(JSON.stringify(urlSetting))
);
}, [cookiesAvailable]);
const isTabletOrMobile = useMediaQuery({ query: "(max-width: 914px)" });
const isAutoDark = useMediaQuery({ query: "(prefers-color-scheme: dark)" });
const dispatch = useDispatch();
Expand All @@ -375,7 +385,10 @@ export default function Main() {
if (autoTheme) {
const theme = isAutoDark ? "dark" : "light";
dispatch(setTheme(theme));
localStorage.setItem("theme", JSON.stringify({ theme: theme, auto: autoTheme }));
localStorage.setItem(
"theme",
JSON.stringify({ theme: theme, auto: autoTheme })
);
}
}, [isAutoDark, autoTheme, dispatch]);
if (!isTabletOrMobile) {
Expand Down

0 comments on commit d076e97

Please sign in to comment.