2022-03-21 16:51:00 +00:00
|
|
|
import React, { useCallback, useState } from "react";
|
2022-04-07 16:08:00 +00:00
|
|
|
import { useDebouncedCallback } from "use-debounce";
|
2022-03-21 16:51:00 +00:00
|
|
|
|
2023-07-17 21:09:12 +00:00
|
|
|
import { OsqueryPlatform, SUPPORTED_PLATFORMS } from "interfaces/platform";
|
2023-08-03 16:42:51 +00:00
|
|
|
import { checkPlatformCompatibility } from "utilities/sql_tools";
|
2022-03-21 16:51:00 +00:00
|
|
|
|
|
|
|
import PlatformCompatibility from "components/PlatformCompatibility";
|
|
|
|
|
|
|
|
export interface IPlatformCompatibility {
|
2023-06-06 12:58:32 +00:00
|
|
|
getCompatiblePlatforms: () => ("darwin" | "windows" | "linux" | "chrome")[];
|
2022-03-21 16:51:00 +00:00
|
|
|
setCompatiblePlatforms: (sqlString: string) => void;
|
|
|
|
render: () => JSX.Element;
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEBOUNCE_DELAY = 300;
|
|
|
|
|
|
|
|
const usePlatformCompatibility = (): IPlatformCompatibility => {
|
|
|
|
const [compatiblePlatforms, setCompatiblePlatforms] = useState<
|
2023-07-17 21:09:12 +00:00
|
|
|
OsqueryPlatform[] | null
|
2022-03-21 16:51:00 +00:00
|
|
|
>(null);
|
|
|
|
const [error, setError] = useState<Error | null>(null);
|
|
|
|
|
2022-04-11 20:18:31 +00:00
|
|
|
const checkCompatibility = (sqlStr: string) => {
|
|
|
|
const { platforms, error: compatibilityError } = checkPlatformCompatibility(
|
|
|
|
sqlStr
|
|
|
|
);
|
|
|
|
setCompatiblePlatforms(platforms || []);
|
|
|
|
setError(compatibilityError);
|
2022-03-21 16:51:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const debounceCompatiblePlatforms = useDebouncedCallback(
|
|
|
|
(queryString: string) => {
|
2022-04-11 20:18:31 +00:00
|
|
|
checkCompatibility(queryString);
|
2022-03-21 16:51:00 +00:00
|
|
|
},
|
|
|
|
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;
|