fleet/frontend/hooks/usePlatformCompatibility.tsx
Martavis Parker cfb1474eb8
Auth Redux Removal (#4924)
* all login methods no longer use redux

* removed redux from registration

* redirect user from registration

* removed redux from sso invite

* removed redundant component

* refactored user settings page

* removed redux from logout

* cleaned up unused redux calls

* lint fixes

* removed test

* removed old config interface

* fixed registration bug

* team permission fix

* removed remaining redux references from pages - #4436

* better way to set config
2022-04-07 09:08:00 -07:00

64 lines
1.8 KiB
TypeScript

import React, { useCallback, useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { IOsqueryPlatform, SUPPORTED_PLATFORMS } from "interfaces/platform";
import checkPlatformCompatibility from "utilities/sql_tools";
import PlatformCompatibility from "components/PlatformCompatibility";
export interface IPlatformCompatibility {
getCompatiblePlatforms: () => ("darwin" | "windows" | "linux")[];
setCompatiblePlatforms: (sqlString: string) => void;
render: () => JSX.Element;
}
const DEBOUNCE_DELAY = 300;
const usePlatformCompatibility = (): IPlatformCompatibility => {
const [compatiblePlatforms, setCompatiblePlatforms] = useState<
IOsqueryPlatform[] | null
>(null);
const [error, setError] = useState<Error | null>(null);
const tryCheckCompatibility = (sqlStr: string) => {
try {
const platforms = checkPlatformCompatibility(sqlStr);
setCompatiblePlatforms(platforms);
setError(null);
return;
} catch (err: unknown) {
setError(new Error(`Invalid usage: ${err}`));
}
};
const debounceCompatiblePlatforms = useDebouncedCallback(
(queryString: string) => {
tryCheckCompatibility(queryString);
},
DEBOUNCE_DELAY,
{ leading: true, trailing: true }
);
const getCompatiblePlatforms = useCallback(
() => SUPPORTED_PLATFORMS.filter((p) => compatiblePlatforms?.includes(p)),
[compatiblePlatforms]
);
const render = useCallback(() => {
return (
<PlatformCompatibility
compatiblePlatforms={compatiblePlatforms}
error={error}
/>
);
}, [compatiblePlatforms, error]);
return {
getCompatiblePlatforms,
setCompatiblePlatforms: debounceCompatiblePlatforms,
render,
};
};
export default usePlatformCompatibility;