-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuseScripts.ts
More file actions
141 lines (125 loc) · 3.68 KB
/
Copy pathuseScripts.ts
File metadata and controls
141 lines (125 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { useCallback, useMemo } from "react";
import { useAppDispatch, useAppSelector } from "../../../store";
import {
addUserScript,
clearExecutionLogs,
deleteUserScript,
logScriptExecution,
setError,
toggleUserScript,
updateUserScript,
} from "../scriptsSlice";
import { ScriptExecution, UserScript } from "../types";
import { createPatternRegex } from "../utils/patternUtils";
export const useScripts = () => {
const dispatch = useAppDispatch();
const scriptsState = useAppSelector((state) => state.scripts);
const settings = useAppSelector((state) => state.settings);
// Script CRUD operations
const createScript = (scriptData: {
url?: string;
code?: string;
name: string;
description?: string;
urlPatterns: string[];
runAt: "document-start" | "document-ready" | "document-end";
}) => {
return dispatch(addUserScript(scriptData));
};
const updateScript = (id: string, updates: Partial<UserScript>) => {
dispatch(updateUserScript(id, updates));
};
const removeScript = (id: string) => {
dispatch(deleteUserScript(id));
};
const toggleScript = (id: string) => {
dispatch(toggleUserScript(id));
};
// Execution logging
const logExecution = useCallback(
(execution: ScriptExecution) => {
if (settings.logExecutions) {
dispatch(logScriptExecution(execution));
}
},
[dispatch, settings.logExecutions]
);
const clearLogs = () => {
dispatch(clearExecutionLogs());
};
const clearError = () => {
dispatch(setError(null));
};
const patternRegexCache = useMemo(() => {
const cache: Record<string, RegExp> = {};
Object.values(scriptsState.userScripts).forEach((script) => {
script.urlPatterns.forEach((pattern) => {
if (!cache[pattern]) {
try {
cache[pattern] = createPatternRegex(pattern);
} catch {
// Invalid pattern, skip caching
}
}
});
});
return cache;
}, [scriptsState.userScripts]);
const getScriptsForUrl = useCallback(
(url: string) => {
if (!settings.scriptsEnabled) return [];
return Object.values(scriptsState.userScripts).filter(
(script) =>
script.enabled &&
script.urlPatterns.some((pattern) => {
const regex = patternRegexCache[pattern];
return regex?.test(url) ?? false;
})
);
},
[scriptsState.userScripts, settings.scriptsEnabled, patternRegexCache]
);
const getScriptsByRunAt = useCallback(
(url: string, runAt: UserScript["runAt"]) => {
return getScriptsForUrl(url).filter((script) => script.runAt === runAt);
},
[getScriptsForUrl]
);
// Computed values
const scripts = Object.values(scriptsState.userScripts);
const enabledScripts = scripts.filter((script) => script.enabled);
const scriptCount = scripts.length;
const enabledScriptCount = enabledScripts.length;
const executionLogs = Object.values(scriptsState.executions);
const recentExecutions = executionLogs
.sort(
(a, b) =>
new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime()
)
.slice(0, 50); // Keep only last 50 executions
return {
// State
userScripts: scriptsState.userScripts,
executions: scriptsState.executions,
isLoading: scriptsState.isLoading,
error: scriptsState.error,
// Computed values
scripts,
enabledScripts,
scriptCount,
enabledScriptCount,
executionLogs,
recentExecutions,
scriptsEnabled: settings.scriptsEnabled,
// Actions
createScript,
updateScript,
removeScript,
toggleScript,
logExecution,
clearLogs,
clearError,
getScriptsForUrl,
getScriptsByRunAt,
};
};