-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrefactor.js
More file actions
59 lines (50 loc) · 3.74 KB
/
Copy pathrefactor.js
File metadata and controls
59 lines (50 loc) · 3.74 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
const fs = require('fs');
let code = fs.readFileSync('frontend/app/companies/[id]/page.tsx', 'utf8');
// 1. Add toast import
if (!code.includes('react-hot-toast')) {
code = code.replace(
/import \{ useSession, signOut \} from 'next-auth\/react';/,
`import { useSession, signOut } from 'next-auth/react';\nimport toast from 'react-hot-toast';`
);
}
// 2. Add useCallback
code = code.replace(/import \{ useState, useEffect, useRef \} from 'react';/, "import { useState, useEffect, useRef, useCallback } from 'react';");
// 3. Extract fetch data logic to a useCallback function
const fetchPattern = /useEffect\(\(\) => \{[\s\S]*?const fetchCompanyAndTemplates = async \(\) => \{([\s\S]*?)\};\s+fetchCompanyAndTemplates\(\);[\s\S]*?\}, \[params\.id, router, status\]\);/;
const fetchMatch = code.match(fetchPattern);
if (fetchMatch) {
const fetchBody = fetchMatch[1];
const fetchDataDefinition = `
const fetchData = useCallback(async () => {
${fetchBody}
}, [params.id, router, user?.role]);
useEffect(() => {
if (status === 'authenticated') {
fetchData();
}
}, [status, fetchData]);
`;
code = code.replace(fetchPattern, fetchDataDefinition);
}
// 4. Replace alerts with toasts
code = code.replace(/alert\('Failed to load data: ' \+ error\.message\);/g, "toast.error('Failed to load data: ' + error.message);");
code = code.replace(/alert\('Company assignment updated successfully!'\);/g, "toast.success('Company assignment updated successfully!');");
code = code.replace(/alert\(error\.message \|\| 'Failed to assign company'\);/g, "toast.error(error.message || 'Failed to assign company');");
code = code.replace(/alert\('Lock acquired! You can now compose the first email \(5 min limit\)\.'\);/g, "toast.success('Lock acquired! You can now compose the first email (5 min limit).');");
code = code.replace(/alert\(error\.message \|\| 'Failed to acquire lock\.'\);/g, "toast.error(error.message || 'Failed to acquire lock.');");
code = code.replace(/alert\('Lock released\.'\);/g, "toast.success('Lock released.');");
code = code.replace(/alert\(error\.message \|\| 'Failed to release lock\.'\);/g, "toast.error(error.message || 'Failed to release lock.');");
code = code.replace(/alert\('Email sent successfully!'\);/g, "toast.success('Email sent successfully!');");
code = code.replace(/alert\(error\.message \|\| 'Failed to send email'\);/g, "toast.error(error.message || 'Failed to send email');");
code = code.replace(/alert\(error\.message \|\| 'Failed to generate intro'\);/g, "toast.error(error.message || 'Failed to generate intro');");
code = code.replace(/alert\(error\.message \|\| 'Failed to draft email'\);/g, "toast.error(error.message || 'Failed to draft email');");
code = code.replace(/alert\(error\.message \|\| 'Failed to save description'\);/g, "toast.error(error.message || 'Failed to save description');");
code = code.replace(/alert\(error\.message \|\| 'Failed to generate summary'\);/g, "toast.error(error.message || 'Failed to generate summary');");
code = code.replace(/alert\(error\.message \|\| 'Failed to add note'\);/g, "toast.error(error.message || 'Failed to add note');");
code = code.replace(/alert\(error\.message \|\| 'Failed to generate suggestion'\);/g, "toast.error(error.message || 'Failed to generate suggestion');");
code = code.replace(/alert\('Follow-up scheduled successfully!'\);/g, "toast.success('Follow-up scheduled successfully!');");
code = code.replace(/alert\(error\.message \|\| 'Failed to schedule follow-up'\);/g, "toast.error(error.message || 'Failed to schedule follow-up');");
// 5. Replace window.location.reload() with fetchData()
code = code.replace(/window\.location\.reload\(\);/g, "await fetchData();");
fs.writeFileSync('frontend/app/companies/[id]/page.tsx', code, 'utf8');
console.log('Script done.');