mirror of
https://github.com/stefanpejcic/openpanel
synced 2025-06-26 18:28:26 +00:00
no packages and conf copied
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
import type { StackFrame } from "error-stack-parser";
|
||||
|
||||
const unrelatedFunctionName = "renderWithHooks";
|
||||
|
||||
export const cleanStack = (stack: StackFrame[]) => {
|
||||
const firstUnrelatedIndex = stack.findIndex(
|
||||
(frame) => frame.functionName === unrelatedFunctionName,
|
||||
);
|
||||
|
||||
if (firstUnrelatedIndex !== -1) {
|
||||
return stack.slice(0, firstUnrelatedIndex);
|
||||
}
|
||||
|
||||
return stack;
|
||||
};
|
||||
@@ -1,4 +0,0 @@
|
||||
export const REFINE_PACKAGE_FILEPATH_REGEXP =
|
||||
__DEV_CONDITION__ !== "development"
|
||||
? /node_modules\/refinedev\/(?<name>.*?)\//
|
||||
: /\/refine\/packages\/(?<name>.*?)\//;
|
||||
@@ -1,17 +0,0 @@
|
||||
import { TraceType } from "@refinedev/devtools-shared";
|
||||
import { MutationKey, QueryKey } from "@tanstack/react-query";
|
||||
|
||||
export const createIdentifier = (
|
||||
key?: QueryKey | MutationKey,
|
||||
trace?: TraceType[],
|
||||
) => {
|
||||
const simpleTrace = trace?.map(
|
||||
(t) =>
|
||||
`${t.file}:${t.line}:${t.column}#${t.function}-${t.packageName}-${
|
||||
t.isRefine ? 1 : 0
|
||||
}`,
|
||||
);
|
||||
const str = JSON.stringify([...(key ?? []), ...(simpleTrace ?? [])]);
|
||||
|
||||
return str;
|
||||
};
|
||||
1
packages/devtools-internal/src/define.d.ts
vendored
1
packages/devtools-internal/src/define.d.ts
vendored
@@ -1 +0,0 @@
|
||||
declare const __DEV_CONDITION__: string;
|
||||
@@ -1,13 +0,0 @@
|
||||
import { REFINE_PACKAGE_FILEPATH_REGEXP } from "./constants";
|
||||
|
||||
export const getPackageNameFromFilename = (filename?: string) => {
|
||||
if (!filename) return;
|
||||
|
||||
const match = filename.match(REFINE_PACKAGE_FILEPATH_REGEXP);
|
||||
|
||||
const name = match?.groups?.name;
|
||||
|
||||
if (!name) return;
|
||||
|
||||
return `@refinedev/${name}`;
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
import {
|
||||
DevtoolsEvent,
|
||||
DevtoolsEventPayloads,
|
||||
RefineHook,
|
||||
scopes,
|
||||
} from "@refinedev/devtools-shared";
|
||||
|
||||
export type Activity =
|
||||
DevtoolsEventPayloads[DevtoolsEvent.DEVTOOLS_ACTIVITY_UPDATE]["updatedActivities"][number];
|
||||
|
||||
export const getResourcePath = (
|
||||
hookName: RefineHook,
|
||||
legacyKey: boolean,
|
||||
): string | null => {
|
||||
if (scopes[hookName] === "auth") {
|
||||
return null;
|
||||
}
|
||||
if (hookName === "useCan") {
|
||||
if (legacyKey) {
|
||||
return "key[1].resource";
|
||||
} else {
|
||||
return "key[1]";
|
||||
}
|
||||
}
|
||||
if (scopes[hookName] === "audit-log") {
|
||||
if (hookName === "useLog") {
|
||||
return "variables.resource";
|
||||
}
|
||||
return "key[1]";
|
||||
}
|
||||
if (scopes[hookName] === "data") {
|
||||
if (hookName === "useCustom" || hookName === "useCustomMutation") {
|
||||
return null;
|
||||
}
|
||||
switch (hookName) {
|
||||
case "useList":
|
||||
case "useInfiniteList":
|
||||
case "useOne":
|
||||
case "useMany":
|
||||
if (legacyKey) {
|
||||
return "key[1]";
|
||||
} else {
|
||||
return "key[2]";
|
||||
}
|
||||
case "useCreate":
|
||||
case "useCreateMany":
|
||||
case "useDelete":
|
||||
case "useDeleteMany":
|
||||
case "useUpdate":
|
||||
case "useUpdateMany":
|
||||
return "variables.resource";
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
@@ -1,35 +0,0 @@
|
||||
import ErrorStackParser from "error-stack-parser";
|
||||
import { cleanStack } from "./clean-stack";
|
||||
import { isRefineStack } from "./is-refine-stack";
|
||||
import { getPackageNameFromFilename } from "./get-package-name-from-filename";
|
||||
import { TraceType } from "@refinedev/devtools-shared";
|
||||
|
||||
export function getTrace() {
|
||||
if (__DEV_CONDITION__ !== "development") {
|
||||
return [];
|
||||
} else {
|
||||
try {
|
||||
const error = new Error();
|
||||
const stack = ErrorStackParser.parse(error);
|
||||
const clean = cleanStack(stack);
|
||||
const traces = clean
|
||||
.map(
|
||||
(frame) =>
|
||||
({
|
||||
file: frame.fileName,
|
||||
line: frame.lineNumber,
|
||||
column: frame.columnNumber,
|
||||
function: frame.functionName,
|
||||
isRefine: isRefineStack(frame.fileName),
|
||||
packageName: getPackageNameFromFilename(
|
||||
frame.fileName,
|
||||
),
|
||||
} as TraceType),
|
||||
)
|
||||
.filter((trace) => trace.function);
|
||||
return traces.slice(1);
|
||||
} catch (error) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
import { RefineHook, TraceType } from "@refinedev/devtools-shared";
|
||||
import { getTrace } from "./get-trace";
|
||||
import { getResourcePath } from "./get-resource-path";
|
||||
|
||||
export type XRayResponse = {
|
||||
hookName: string;
|
||||
trace: TraceType[];
|
||||
resourcePath: string | null;
|
||||
legacyKey: boolean;
|
||||
};
|
||||
|
||||
export function getXRay(hookName: string, legacyKey: boolean): XRayResponse {
|
||||
if (__DEV_CONDITION__ !== "development") {
|
||||
return {
|
||||
hookName: "",
|
||||
trace: [],
|
||||
resourcePath: null,
|
||||
legacyKey: false,
|
||||
};
|
||||
} else {
|
||||
const trace = getTrace().slice(1);
|
||||
|
||||
const resourcePath = getResourcePath(hookName as RefineHook, legacyKey);
|
||||
|
||||
return {
|
||||
hookName,
|
||||
trace,
|
||||
resourcePath,
|
||||
legacyKey,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
export { getTrace } from "./get-trace";
|
||||
export { getXRay } from "./get-xray";
|
||||
export { useQuerySubscription } from "./use-query-subscription";
|
||||
@@ -1,9 +0,0 @@
|
||||
import { REFINE_PACKAGE_FILEPATH_REGEXP } from "./constants";
|
||||
|
||||
export const isRefineStack = (filename?: string) => {
|
||||
if (!filename) return false;
|
||||
|
||||
const match = filename.match(REFINE_PACKAGE_FILEPATH_REGEXP);
|
||||
|
||||
return !!match;
|
||||
};
|
||||
@@ -1,67 +0,0 @@
|
||||
import { DevtoolsEvent, send } from "@refinedev/devtools-shared";
|
||||
import { Mutation, Query } from "@tanstack/react-query";
|
||||
|
||||
import { createIdentifier } from "./create-identifier";
|
||||
import { XRayResponse } from "./get-xray";
|
||||
|
||||
export const createMutationListener =
|
||||
(ws: WebSocket) => (mutation?: Mutation) => {
|
||||
if (!mutation?.meta?.trace) return;
|
||||
|
||||
const meta: XRayResponse = mutation?.meta as any;
|
||||
|
||||
new Promise<void>((resolve) => {
|
||||
send(ws, DevtoolsEvent.ACTIVITY, {
|
||||
type: "mutation",
|
||||
identifier: createIdentifier(
|
||||
mutation?.options.mutationKey,
|
||||
mutation?.meta?.trace as any,
|
||||
),
|
||||
key: mutation?.options.mutationKey as any,
|
||||
status: mutation?.state.status,
|
||||
state: mutation?.state,
|
||||
variables: mutation?.state?.variables,
|
||||
...meta,
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
|
||||
// console.table({
|
||||
// type: "mutation",
|
||||
// key: mutation?.options.mutationKey,
|
||||
// id: mutation?.mutationId,
|
||||
// status: mutation?.state.status,
|
||||
// trace: mutation?.meta?.trace,
|
||||
// state: mutation?.state,
|
||||
// variables: mutation?.state?.variables,
|
||||
// });
|
||||
};
|
||||
|
||||
export const createQueryListener = (ws: WebSocket) => (query: Query) => {
|
||||
if (!query?.meta?.trace) return;
|
||||
|
||||
const meta: XRayResponse = query?.meta as any;
|
||||
|
||||
new Promise<void>((resolve) => {
|
||||
send(ws, DevtoolsEvent.ACTIVITY, {
|
||||
type: "query",
|
||||
identifier: createIdentifier(
|
||||
query.queryKey,
|
||||
query.meta?.trace as any,
|
||||
),
|
||||
key: query.queryKey as any,
|
||||
status: query.state.status,
|
||||
state: query.state,
|
||||
...meta,
|
||||
});
|
||||
resolve();
|
||||
});
|
||||
|
||||
// console.table({
|
||||
// type: "query",
|
||||
// key: query.queryKey,
|
||||
// status: query.state.status,
|
||||
// trace: query.meta?.trace,
|
||||
// state: query.state,
|
||||
// });
|
||||
};
|
||||
@@ -1,55 +0,0 @@
|
||||
import { DevToolsContext } from "@refinedev/devtools-shared";
|
||||
import { QueryClient } from "@tanstack/react-query";
|
||||
import React, { useContext } from "react";
|
||||
import { createQueryListener, createMutationListener } from "./listeners";
|
||||
|
||||
export const useQuerySubscription =
|
||||
__DEV_CONDITION__ !== "development"
|
||||
? () => ({})
|
||||
: (queryClient: QueryClient) => {
|
||||
const { ws } = useContext(DevToolsContext);
|
||||
const queryCacheSubscription = React.useRef<() => void>();
|
||||
const mutationCacheSubscription = React.useRef<() => void>();
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!ws) return () => 0;
|
||||
|
||||
const queryCache = queryClient.getQueryCache();
|
||||
|
||||
const queryListener = createQueryListener(ws);
|
||||
|
||||
queryCache.getAll().forEach(queryListener);
|
||||
|
||||
queryCacheSubscription.current = queryCache.subscribe(
|
||||
({ query, type }) =>
|
||||
(type === "added" || type === "updated") &&
|
||||
queryListener(query),
|
||||
);
|
||||
|
||||
return () => {
|
||||
queryCacheSubscription.current?.();
|
||||
};
|
||||
}, [ws, queryClient]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!ws) return () => 0;
|
||||
|
||||
const mutationCache = queryClient.getMutationCache();
|
||||
|
||||
const mutationListener = createMutationListener(ws);
|
||||
|
||||
mutationCache.getAll().forEach(mutationListener);
|
||||
|
||||
mutationCacheSubscription.current = mutationCache.subscribe(
|
||||
({ mutation, type }) =>
|
||||
(type === "added" || type === "updated") &&
|
||||
mutationListener(mutation),
|
||||
);
|
||||
|
||||
return () => {
|
||||
mutationCacheSubscription.current?.();
|
||||
};
|
||||
}, [ws, queryClient]);
|
||||
|
||||
return {};
|
||||
};
|
||||
Reference in New Issue
Block a user