fleet/frontend/hooks/usePlatformCompatibility.tsx
jacobshandling 2118616f64
21855 – Paginate and filter Queries on the server, update platform filtering from compatible to targeted platforms (#24446)
## Addresses #21855 and all of its subtasks

**Frontend:**
- Update list queries API call to include pagination and filter-related
query params, including new `platform` param for filtering queries by
platforms they've been set to target
- Convert all filtering, sorting, and pagination functionality of the
Manage queries page from client-side to server-side
- Remove unneeded variable declarations / logic
- Various typing and naming improvements

**Server:**
- Add new `platform` `ListQueryOption`
- Update service and datastore level list queries logic to handle
filtering queries by targeted platform
- Update service and datastore level list queries logic to include
`meta` and `count` fields in addition to filtered/paginated queries


- [x] Changes file added for user-visible changes in `changes/`, `
- [x] Added/updated tests
  - [x] update DB, integration
  - [x] add integration (pagination)
  - [x] add integration (platform filter)
  - [x] add DB (pagination)
  - [x] add DB (platform filter)
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-12-11 10:50:28 -08:00

60 lines
1.7 KiB
TypeScript

import React, { useCallback, useState } from "react";
import { useDebouncedCallback } from "use-debounce";
import { QueryablePlatform, QUERYABLE_PLATFORMS } from "interfaces/platform";
import { checkPlatformCompatibility } from "utilities/sql_tools";
import PlatformCompatibility from "components/PlatformCompatibility";
export interface IPlatformCompatibility {
getCompatiblePlatforms: () => QueryablePlatform[];
setCompatiblePlatforms: (sqlString: string) => void;
render: () => JSX.Element;
}
const DEBOUNCE_DELAY = 300;
const usePlatformCompatibility = (): IPlatformCompatibility => {
const [compatiblePlatforms, setCompatiblePlatforms] = useState<
QueryablePlatform[] | null
>(null);
const [error, setError] = useState<Error | null>(null);
const checkCompatibility = (sqlStr: string) => {
const { platforms, error: compatibilityError } = checkPlatformCompatibility(
sqlStr
);
setCompatiblePlatforms(platforms || []);
setError(compatibilityError);
};
const debounceCompatiblePlatforms = useDebouncedCallback(
(queryString: string) => {
checkCompatibility(queryString);
},
DEBOUNCE_DELAY,
{ leading: true, trailing: true }
);
const getCompatiblePlatforms = useCallback(
() => QUERYABLE_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;