Skip to content

Commit

Permalink
refactor(webui): avoid importing expo-router in API routes (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
byCedric authored May 26, 2024
1 parent 423bcc0 commit caf009f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
3 changes: 2 additions & 1 deletion webui/src/app/(atlas)/[bundle].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import {
NoDataState,
NoDataWithFiltersState,
} from '~/components/StateInfo';
import { useModuleFilters } from '~/hooks/useModuleFilters';
import { BundleDeltaToast, useBundle } from '~/providers/bundle';
import { Layout, LayoutHeader, LayoutNavigation, LayoutTitle } from '~/ui/Layout';
import { Tag } from '~/ui/Tag';
import { fetchApi, handleApiError } from '~/utils/api';
import { type ModuleFilters, moduleFiltersToParams, useModuleFilters } from '~/utils/filters';
import { type ModuleFilters, moduleFiltersToParams } from '~/utils/filters';
import { formatFileSize } from '~/utils/formatString';

export default function BundlePage() {
Expand Down
3 changes: 2 additions & 1 deletion webui/src/app/(atlas)/[bundle]/folders/[path].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
NoDataState,
NoDataWithFiltersState,
} from '~/components/StateInfo';
import { useModuleFilters } from '~/hooks/useModuleFilters';
import { BundleDeltaToast, useBundle } from '~/providers/bundle';
import { Layout, LayoutHeader, LayoutNavigation, LayoutTitle } from '~/ui/Layout';
import { Tag } from '~/ui/Tag';
import { fetchApi, handleApiError } from '~/utils/api';
import { type ModuleFilters, useModuleFilters, moduleFiltersToParams } from '~/utils/filters';
import { type ModuleFilters, moduleFiltersToParams } from '~/utils/filters';
import { formatFileSize } from '~/utils/formatString';

export default function FolderPage() {
Expand Down
2 changes: 1 addition & 1 deletion webui/src/components/ModuleFilterForm.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useRouter } from 'expo-router';
import { type FormEvent, type KeyboardEvent, useState, useCallback } from 'react';

import { useModuleFilters } from '~/hooks/useModuleFilters';
import { Button } from '~/ui/Button';
import { Checkbox } from '~/ui/Checkbox';
import { Input } from '~/ui/Input';
Expand All @@ -14,7 +15,6 @@ import {
SheetTrigger,
} from '~/ui/Sheet';
import { debounce } from '~/utils/debounce';
import { useModuleFilters } from '~/utils/filters';

type ModuleFiltersFormProps = {
disableNodeModules?: boolean;
Expand Down
17 changes: 17 additions & 0 deletions webui/src/hooks/useModuleFilters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useGlobalSearchParams, useRouter } from 'expo-router';

import { DEFAULT_FILTERS, type ModuleFilters } from '~/utils/filters';

/**
* Get the current module filters from URL search params, using Expo Router.
* This returns the filters, with default values, and if any of the filters has been defined.
*/
export function useModuleFilters() {
const router = useRouter();
const filters = useGlobalSearchParams<ModuleFilters>();
return {
filters,
filtersEnabled: !!filters.scope || !!filters.include || !!filters.exclude,
resetFilters: () => router.setParams(DEFAULT_FILTERS),
};
}
15 changes: 0 additions & 15 deletions webui/src/utils/filters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useGlobalSearchParams, useRouter } from 'expo-router';
import picomatch from 'picomatch';

import { type AtlasModule } from '~core/data/types';
Expand Down Expand Up @@ -48,20 +47,6 @@ export function moduleFiltersToParams(filters: ModuleFilters) {
return params;
}

/**
* Get the current module filters from URL search params, using Expo Router.
* This returns the filters, with default values, and if any of the filters has been defined.
*/
export function useModuleFilters() {
const router = useRouter();
const filters = useGlobalSearchParams<ModuleFilters>();
return {
filters,
filtersEnabled: !!filters.scope || !!filters.include || !!filters.exclude,
resetFilters: () => router.setParams(DEFAULT_FILTERS),
};
}

/** Filter the modules based on the filters, and an optional (root) path. */
export function filterModules(
modules: AtlasModule[],
Expand Down

0 comments on commit caf009f

Please sign in to comment.