import { HydrationBoundary, dehydrate } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { searchParamsCache } from '@/lib/searchparams';
import { userPermissionAssignmentsQueryOptions } from '../api/queries';
import { UserPermissionTable } from './user-permission-table';

export default async function UserPermissionListing() {
  const page = searchParamsCache.get('page');
  const perPage = searchParamsCache.get('perPage');
  const search = searchParamsCache.get('search');
  const sort = searchParamsCache.get('sort');

  const filters = {
    page,
    limit: perPage,
    ...(search && { search }),
    ...(sort && { sort })
  };

  const queryClient = getQueryClient();
  await queryClient.fetchQuery(userPermissionAssignmentsQueryOptions(filters));

  return (
    <HydrationBoundary state={dehydrate(queryClient)}>
      <UserPermissionTable />
    </HydrationBoundary>
  );
}
