Skip to content

Commit 11dfa5f

Browse files
committed
feat: remove checkbox and add passthrough select
1 parent ccfeffb commit 11dfa5f

File tree

4 files changed

+28
-126
lines changed

4 files changed

+28
-126
lines changed

packages/msw-dev-tool/src/lib/handlerStore.ts

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@ import { FlattenHandler, Handler, HttpHandlerBehavior } from "./type";
44
import {
55
getHandlerResponseByBehavior,
66
getRowId,
7-
getTotalEnableHandlers,
87
initMSWDevToolStore,
98
isHttpHandler,
10-
updateEnableHandlers,
9+
mergeStorageData,
1110
} from "./util";
12-
import { OnChangeFn, RowSelectionState } from "@tanstack/react-table";
13-
import isFunction from "lodash/isFunction";
1411
import { setupWorker as _setupWorker } from "../utils/mswBrowser";
1512
import { createJSONStorage, persist } from "zustand/middleware";
1613
import { STORAGE_KEY } from "./const";
@@ -27,10 +24,8 @@ export interface HandlerStoreState {
2724
*/
2825
restHandlers: Handler[];
2926
flattenHandlers: FlattenHandler[];
30-
handlerRowSelection: RowSelectionState;
3127
setupDevToolWorker: (...handlers: Handler[]) => Promise<SetupWorker>;
3228
resetMSWDevTool: () => void;
33-
handleHandlerRowSelectionChange: OnChangeFn<RowSelectionState>;
3429
getWorker: () => SetupWorker;
3530
getFlattenHandlerById: (id: string) => FlattenHandler | undefined;
3631
getHandlerBehavior: (id: string) => HttpHandlerBehavior | undefined;
@@ -73,71 +68,34 @@ export const useHandlerStore = create<HandlerStoreState>()(
7368
}
7469
const worker = setupWorker(..._handlers);
7570

76-
const { flattenHandlers, handlerRowSelection, unsupportedHandlers } =
71+
const { flattenHandlers, unsupportedHandlers } =
7772
initMSWDevToolStore(worker);
73+
74+
const { flattenHandlers: mergedHandlers } = mergeStorageData({
75+
flattenHandlers,
76+
});
77+
7878
set({
7979
worker,
80-
flattenHandlers,
81-
handlerRowSelection,
80+
flattenHandlers: mergedHandlers,
8281
restHandlers: unsupportedHandlers,
8382
});
8483

85-
const totalEnableHandlers = getTotalEnableHandlers(
86-
flattenHandlers,
87-
unsupportedHandlers
88-
);
89-
updateEnableHandlers(worker, totalEnableHandlers);
90-
9184
return worker;
9285
},
9386
resetMSWDevTool: () => {
9487
const _worker = get().getWorker();
9588
_worker.resetHandlers();
9689

97-
const {
98-
worker,
99-
flattenHandlers,
100-
handlerRowSelection,
101-
unsupportedHandlers,
102-
} = initMSWDevToolStore(_worker);
90+
const { worker, flattenHandlers, unsupportedHandlers } =
91+
initMSWDevToolStore(_worker);
10392

10493
set({
10594
worker,
10695
flattenHandlers,
107-
handlerRowSelection,
10896
restHandlers: unsupportedHandlers,
10997
});
11098
},
111-
handleHandlerRowSelectionChange: (updater) => {
112-
const worker = get().getWorker();
113-
114-
if (isFunction(updater)) {
115-
set(({ handlerRowSelection }) => {
116-
const next = updater(handlerRowSelection);
117-
const current = get().flattenHandlers.map((handler) =>
118-
next[handler.id]
119-
? { ...handler, enabled: true }
120-
: { ...handler, enabled: false }
121-
);
122-
return { handlerRowSelection: next, flattenHandlers: current };
123-
});
124-
} else {
125-
const current = get().flattenHandlers.map((handler) =>
126-
updater[handler.id]
127-
? { ...handler, enabled: true }
128-
: { ...handler, enabled: false }
129-
);
130-
set({ handlerRowSelection: updater, flattenHandlers: current });
131-
}
132-
133-
const flattenHandlers = get().flattenHandlers;
134-
const restHandlers = get().restHandlers;
135-
const totalEnableHandlers = getTotalEnableHandlers(
136-
flattenHandlers,
137-
restHandlers
138-
);
139-
updateEnableHandlers(worker, totalEnableHandlers);
140-
},
14199
getWorker: () => {
142100
const worker = get().worker;
143101
if (!worker) throw new Error("Worker is not initialized");

packages/msw-dev-tool/src/lib/type.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,41 @@ export type HttpRequestResolverExtras<Params extends PathParams> = {
1212
};
1313

1414
export type ValueUnion<T> = T[keyof T];
15+
16+
/**
17+
* Comment out rarely used status codes until the user requests them.
18+
*/
1519
export const HttpStatusCode = {
1620
// 4xx Client Error
1721
BAD_REQUEST: 400,
1822
UNAUTHORIZED: 401,
19-
PAYMENT_REQUIRED: 402,
23+
//PAYMENT_REQUIRED: 402,
2024
FORBIDDEN: 403,
2125
NOT_FOUND: 404,
2226
METHOD_NOT_ALLOWED: 405,
2327
NOT_ACCEPTABLE: 406,
2428
CONFLICT: 409,
25-
GONE: 410,
29+
//GONE: 410,
2630
PRECONDITION_FAILED: 412,
2731
PAYLOAD_TOO_LARGE: 413,
2832
UNSUPPORTED_MEDIA_TYPE: 415,
29-
RANGE_NOT_SATISFIABLE: 416,
33+
//RANGE_NOT_SATISFIABLE: 416,
3034
TOO_MANY_REQUESTS: 429,
3135

3236
// 5xx Server Error
3337
INTERNAL_SERVER_ERROR: 500,
34-
NOT_IMPLEMENTED: 501,
38+
//NOT_IMPLEMENTED: 501,
3539
BAD_GATEWAY: 502,
3640
SERVICE_UNAVAILABLE: 503,
3741
GATEWAY_TIMEOUT: 504,
38-
HTTP_VERSION_NOT_SUPPORTED: 505,
42+
//HTTP_VERSION_NOT_SUPPORTED: 505,
3943
} as const;
4044

4145
export type HttpStatusCode = ValueUnion<typeof HttpStatusCode>;
4246

4347
export const CustomBehavior = {
4448
DEFAULT: "default",
49+
DISABLE: "disable mock",
4550
DELAY: "delay",
4651
RETURN_NULL: "return null",
4752
NETWORK_ERROR: "network error",
@@ -65,7 +70,6 @@ export type FlattenHandler = {
6570
id: string;
6671
path: string;
6772
method: string;
68-
enabled: boolean;
6973
handler: HttpHandler;
7074
behavior: HttpHandlerBehavior;
7175
};

packages/msw-dev-tool/src/lib/util.ts

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { dummyHandler } from "../const/handler";
1111
import { SetupWorker } from "msw/lib/browser";
1212
import { RowSelectionState } from "@tanstack/react-table";
13-
import { AsyncResponseResolverReturnType, delay, HttpResponse } from "msw";
13+
import { AsyncResponseResolverReturnType, delay, HttpResponse, passthrough } from "msw";
1414
import { STORAGE_KEY } from "./const";
1515

1616
export const getRowId = ({ path, method }: { path: string; method: string }) =>
@@ -38,7 +38,6 @@ export const convertHandlers = (handlers: Handler[]) => {
3838
id: getRowId({ path, method }),
3939
path,
4040
method,
41-
enabled: true,
4241
handler,
4342
behavior: HttpHandlerBehavior.DEFAULT,
4443
});
@@ -48,46 +47,12 @@ export const convertHandlers = (handlers: Handler[]) => {
4847
return { flattenHandlers, unsupportedHandlers };
4948
};
5049

51-
export const getTotalEnableHandlers = (
52-
flattenHandlers: FlattenHandler[],
53-
restHandlers: Handler[]
54-
) => {
55-
const checkedHttpHandlers = flattenHandlers
56-
.filter((h) => h.enabled)
57-
.map((h) => h.handler);
58-
return [...checkedHttpHandlers, ...restHandlers];
59-
};
60-
61-
/**
62-
* This has to do with `msw` internal workings.
63-
* If I spread an empty array in `resetHandlers`, it will be replaced by `initialHandler`.
64-
* Therefore, I proposed the `clear` method, but unfortunately it was not accepted!
65-
*/
66-
export const updateEnableHandlers = (
67-
worker: SetupWorker,
68-
totalEnableHandlers: Handler[]
69-
) => {
70-
if (totalEnableHandlers.length === 0) {
71-
worker.resetHandlers(dummyHandler);
72-
return;
73-
}
74-
75-
worker.resetHandlers(...totalEnableHandlers);
76-
};
77-
7850
export const initMSWDevToolStore = (worker: SetupWorker) => {
7951
const handlers = worker.listHandlers() as Handler[];
80-
const { flattenHandlers: newFlattenHandlers, unsupportedHandlers } =
52+
const { flattenHandlers, unsupportedHandlers } =
8153
convertHandlers(handlers);
82-
const { flattenHandlers } = mergeStorageData({
83-
flattenHandlers: newFlattenHandlers,
84-
});
85-
const handlerRowSelection = flattenHandlers.reduce((acc, handler) => {
86-
acc[handler.id] = handler.enabled;
87-
return acc;
88-
}, {} as RowSelectionState);
8954

90-
return { worker, flattenHandlers, unsupportedHandlers, handlerRowSelection };
55+
return { worker, flattenHandlers, unsupportedHandlers };
9156
};
9257

9358
export const isHttpHandler = (handler: Handler): handler is HttpHandler => {
@@ -104,6 +69,10 @@ export const getHandlerResponseByBehavior = async (
10469
return originalResolverCallback();
10570
}
10671

72+
if (behavior === CustomBehavior.DISABLE) {
73+
return passthrough();
74+
}
75+
10776
if (behavior === CustomBehavior.DELAY) {
10877
await delay("infinite");
10978
return new Response();
@@ -148,7 +117,6 @@ export const mergeStorageData = ({
148117
if (savedHandler) {
149118
return {
150119
...newHandler,
151-
enabled: savedHandler.enabled,
152120
behavior: savedHandler.behavior,
153121
};
154122
}

packages/msw-dev-tool/src/ui/DevToolContent/hook/useFlattenHandlersTable.tsx

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,11 @@ import { BehaviorSelect } from "../HandlerTable/BehaviorSelect";
1212
export const useFlattenHandlersTable = () => {
1313
const {
1414
flattenHandlers,
15-
handlerRowSelection,
16-
handleHandlerRowSelectionChange,
1715
} = useHandlerStore();
1816

1917
const columnHelper = createColumnHelper<FlattenHandler>();
2018
const columns: ColumnDef<FlattenHandler, any>[] = useMemo(() => {
2119
return [
22-
columnHelper.accessor("enabled", {
23-
header: ({ table }) => (
24-
<input
25-
type="checkbox"
26-
checked={table.getIsAllRowsSelected()}
27-
onChange={(e) => {
28-
e.stopPropagation();
29-
table.toggleAllRowsSelected(e.target.checked);
30-
}}
31-
/>
32-
),
33-
cell: ({ row }) => (
34-
<input
35-
type="checkbox"
36-
checked={row.getIsSelected()}
37-
onChange={(e) => {
38-
e.stopPropagation();
39-
row.toggleSelected(e.target.checked);
40-
}}
41-
/>
42-
),
43-
}),
4420
columnHelper.accessor("path", {
4521
header: "Protocol",
4622
cell: ({ row }) => {
@@ -76,16 +52,12 @@ export const useFlattenHandlersTable = () => {
7652
},
7753
}),
7854
];
79-
}, [flattenHandlers, handlerRowSelection, handleHandlerRowSelectionChange]);
55+
}, [flattenHandlers]);
8056

8157
const table = useReactTable({
8258
columns,
8359
data: flattenHandlers,
8460
getCoreRowModel: getCoreRowModel(),
85-
state: {
86-
rowSelection: handlerRowSelection,
87-
},
88-
onRowSelectionChange: handleHandlerRowSelectionChange,
8961
getRowId: (row) => row.id,
9062
enableRowSelection: true,
9163
});

0 commit comments

Comments
 (0)