fix: crash bugs in TeamInvoices + faster load

Bug fixes:
- TeamInvoices: add useAuth import/currentUser (invoice-create crash)
- TeamInvoices: setChartYear -> setExportYear (year-dropdown crash)
- TeamDashboard: drop redundant setState-in-effect (cascading renders)
- Companies: delete dead _UnusedClientCompanies (illegal hook calls)
- annotate intentional empty PDF-fallback catches

Load speed:
- preconnect/dns-prefetch to Supabase origin
- lazy-load heic-to in Converters: page chunk 2737KB -> 9KB
- split recharts into its own 'charts' vendor chunk

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Krao Hasanee
2026-06-08 12:59:11 -04:00
parent 85625f4d95
commit 04e0911e9f
101 changed files with 11786 additions and 7445 deletions
-83
View File
@@ -1,83 +0,0 @@
// One-time: creates 00 Project Files folder inside every existing project folder in FileBrowser
// Run: node scripts/backfill-project-files-folder.mjs
import { readFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createClient } from '@supabase/supabase-js';
const __dir = dirname(fileURLToPath(import.meta.url));
const envFile = resolve(__dir, '../.env.backfill');
const env = {};
readFileSync(envFile, 'utf8').split('\n').forEach(line => {
const m = line.match(/^([^#=]+)=(.*)$/);
if (m) env[m[1].trim()] = m[2].trim().replace(/^["']|["']$/g, '');
});
const SUPABASE_URL = env.VITE_SUPABASE_URL || env.SUPABASE_URL;
const SERVICE_ROLE_KEY = env.SUPABASE_SERVICE_ROLE_KEY;
const FB_URL = (env.FILEBROWSER_URL || '').replace(/\/+$/, '');
const FB_TOKEN = env.FILEBROWSER_TOKEN;
const CLIENT_ROOT = env.FILEBROWSER_CLIENT_ROOT || '/fourgebranding/Clients';
const FB_SOURCE = 'files';
if (!SUPABASE_URL || !SERVICE_ROLE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
if (!FB_URL || !FB_TOKEN) { console.error('Missing FileBrowser env'); process.exit(1); }
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, { auth: { persistSession: false } });
function safeName(v) {
return String(v || '').trim()
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
.replace(/\s+/g, ' ')
.replace(/^-+|-+$/g, '');
}
function joinPath(...parts) {
const raw = parts.join('/');
const clean = raw.split('/').filter(p => p && p !== '.');
return `/${clean.join('/')}`;
}
async function mkdir(path) {
const qs = new URLSearchParams({ source: FB_SOURCE, path, isDir: 'true' }).toString();
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
method: 'POST',
headers: { Authorization: `Bearer ${FB_TOKEN}` },
});
if (!res.ok) {
const text = await res.text();
if (!text.includes('already') && !text.includes('exist')) {
console.warn(` mkdir ${path}: ${res.status} ${text.slice(0, 80)}`);
}
}
}
async function main() {
const { data: projects, error } = await admin
.from('projects')
.select('id, name, company:companies(name)');
if (error) { console.error('Query failed:', error.message); process.exit(1); }
console.log(`Found ${projects.length} projects`);
for (const p of projects) {
const company = safeName(p.company?.name || '');
const project = safeName(p.name || '');
if (!company || !project) { console.log(` Skipping (missing name): ${p.id}`); continue; }
const projectDir = joinPath(CLIENT_ROOT, company, 'Projects', project);
const targetDir = joinPath(projectDir, '00 Project Files');
await mkdir(joinPath(CLIENT_ROOT, company));
await mkdir(joinPath(CLIENT_ROOT, company, 'Projects'));
await mkdir(projectDir);
await mkdir(targetDir);
console.log(`${company} / ${project} / 00 Project Files`);
}
console.log('\nDone.');
}
main().catch(err => { console.error(err); process.exit(1); });
-127
View File
@@ -1,127 +0,0 @@
#!/usr/bin/env node
// Backfill FileBrowser folders for all existing projects and tasks.
// Project: Clients/{company}/Projects/{project}/00 Project Files/ + 00 Project Info/
// Task: Clients/{company}/Projects/{project}/{task}/Working Files/ + Request Info/
// Run: node --env-file=.env.backfill scripts/backfill-project-folders.mjs
const FB_SOURCE = 'files';
const FILEBROWSER_URL = (process.env.FILEBROWSER_URL || 'https://fourgebranding.krao.us').replace(/\/+$/, '');
const FILEBROWSER_TOKEN = process.env.FILEBROWSER_TOKEN;
const CLIENT_ROOT = process.env.FILEBROWSER_CLIENT_ROOT || '/fourgebranding/Clients';
const SUPABASE_URL = process.env.VITE_SUPABASE_URL || process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_SERVICE_ROLE_KEY;
if (!FILEBROWSER_TOKEN) { console.error('Missing FILEBROWSER_TOKEN'); process.exit(1); }
if (!SUPABASE_URL || !SUPABASE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
function safeName(value) {
return String(value || '')
.trim()
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
.replace(/\s+/g, ' ')
.replace(/^-+|-+$/g, '');
}
function normalizePath(path) {
const parts = String(path || '/').split('/').filter(p => p && p !== '.' && p !== '..');
return `/${parts.join('/')}`;
}
function joinPath(...parts) {
return normalizePath(parts.join('/'));
}
async function mkdir(path) {
const qs = new URLSearchParams({ source: FB_SOURCE, path, isDir: 'true' }).toString();
const res = await fetch(`${FILEBROWSER_URL}/api/resources?${qs}`, {
method: 'POST',
headers: { Authorization: `Bearer ${FILEBROWSER_TOKEN}` },
});
// 200 = created, 409 = already exists — both fine
if (!res.ok && res.status !== 409) {
const text = await res.text();
throw new Error(`mkdir ${path} failed (${res.status}): ${text}`);
}
return res.status;
}
async function supabaseFetch(path) {
const res = await fetch(`${SUPABASE_URL}/rest/v1/${path}`, {
headers: {
apikey: SUPABASE_KEY,
Authorization: `Bearer ${SUPABASE_KEY}`,
},
});
if (!res.ok) throw new Error(`Supabase ${path}: ${await res.text()}`);
return res.json();
}
async function run() {
console.log('Fetching projects from Supabase...');
const projects = await supabaseFetch('projects?select=id,name,company:companies(name)&order=created_at.asc');
console.log(`Found ${projects.length} projects.`);
console.log('Fetching tasks from Supabase...');
const tasks = await supabaseFetch('tasks?select=id,title,project:projects(name,company:companies(name))&order=submitted_at.asc');
console.log(`Found ${tasks.length} tasks.\n`);
let created = 0;
let existing = 0;
let errors = 0;
// ── Projects ──────────────────────────────────────────────────────────────
console.log('=== PROJECTS ===');
for (const project of projects) {
const companyName = project.company?.name;
if (!companyName) { console.log(` SKIP ${project.name} — no company`); continue; }
const companyDir = joinPath(CLIENT_ROOT, safeName(companyName));
const projectsDir = joinPath(companyDir, 'Projects');
const projectDir = joinPath(projectsDir, safeName(project.name));
try {
await mkdir(companyDir);
await mkdir(projectsDir);
const s = await mkdir(projectDir);
await mkdir(joinPath(projectDir, '00 Project Files'));
await mkdir(joinPath(projectDir, '00 Project Info'));
if (s === 409) { console.log(` EXISTS ${companyName} / ${project.name}`); existing++; }
else { console.log(` CREATED ${companyName} / ${project.name}`); created++; }
} catch (err) {
console.error(` ERROR ${companyName} / ${project.name}: ${err.message}`);
errors++;
}
}
// ── Tasks ─────────────────────────────────────────────────────────────────
console.log('\n=== TASKS ===');
for (const task of tasks) {
const projectName = task.project?.name;
const companyName = task.project?.company?.name;
if (!projectName || !companyName) { console.log(` SKIP ${task.title} — missing project/company`); continue; }
const projectDir = joinPath(CLIENT_ROOT, safeName(companyName), 'Projects', safeName(projectName));
const taskDir = joinPath(projectDir, safeName(task.title));
try {
// Ensure parent exists (idempotent)
await mkdir(joinPath(CLIENT_ROOT, safeName(companyName)));
await mkdir(joinPath(CLIENT_ROOT, safeName(companyName), 'Projects'));
await mkdir(projectDir);
const s = await mkdir(taskDir);
await mkdir(joinPath(taskDir, 'Working Files'));
await mkdir(joinPath(taskDir, 'Request Info'));
if (s === 409) { console.log(` EXISTS ${companyName} / ${projectName} / ${task.title}`); existing++; }
else { console.log(` CREATED ${companyName} / ${projectName} / ${task.title}`); created++; }
} catch (err) {
console.error(` ERROR ${companyName} / ${projectName} / ${task.title}: ${err.message}`);
errors++;
}
}
console.log(`\nDone. Created: ${created} Already existed: ${existing} Errors: ${errors}`);
}
run().catch(err => { console.error(err); process.exit(1); });
-212
View File
@@ -1,212 +0,0 @@
// One-time script: copies existing submission files from Supabase Storage to FileBrowser
// Run: node scripts/backfill-request-files.mjs
import { readFileSync } from 'fs';
import { createClient } from '@supabase/supabase-js';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dir = dirname(fileURLToPath(import.meta.url));
const envFile = resolve(__dir, '../.env.backfill');
// Parse env file
const env = {};
readFileSync(envFile, 'utf8').split('\n').forEach(line => {
const m = line.match(/^([^#=]+)=(.*)$/);
if (m) env[m[1].trim()] = m[2].trim().replace(/^["']|["']$/g, '');
});
const SUPABASE_URL = env.VITE_SUPABASE_URL || env.SUPABASE_URL;
const SERVICE_ROLE_KEY = env.SUPABASE_SERVICE_ROLE_KEY;
const FB_URL = (env.FILEBROWSER_URL || '').replace(/\/+$/, '');
const FB_TOKEN = env.FILEBROWSER_TOKEN;
const CLIENT_ROOT = env.FILEBROWSER_CLIENT_ROOT || '/Clients';
const FB_SOURCE = 'srv';
if (!SUPABASE_URL || !SERVICE_ROLE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
if (!FB_URL || !FB_TOKEN) { console.error('Missing FileBrowser env'); process.exit(1); }
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, {
auth: { persistSession: false, autoRefreshToken: false },
});
function normalizePath(path) {
const parts = String(path || '/').trim().split('/').filter(Boolean);
const clean = [];
for (const p of parts) {
if (p === '.') continue;
if (p === '..') throw new Error('path traversal');
clean.push(p);
}
return `/${clean.join('/')}`;
}
function joinPath(...parts) { return normalizePath(parts.join('/')); }
function safeName(v) {
return String(v || '').trim()
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
.replace(/\s+/g, ' ')
.replace(/^-+|-+$/g, '');
}
async function fbFetch(method, endpoint, { params = {}, headers = {}, body } = {}) {
const qs = new URLSearchParams({ source: FB_SOURCE, ...params }).toString();
const res = await fetch(`${FB_URL}${endpoint}?${qs}`, {
method,
headers: { Authorization: `Bearer ${FB_TOKEN}`, ...headers },
body,
});
if (!res.ok) {
const text = await res.text();
throw new Error(`FB ${res.status}: ${text.slice(0, 200)}`);
}
return res;
}
async function mkdir(path) {
await fbFetch('POST', '/api/resources', { params: { path, isDir: 'true' } }).catch(() => {});
}
async function fbExists(path) {
try {
await fbFetch('GET', '/api/resources', { params: { path } });
return true;
} catch (e) {
if (String(e.message || '').includes('404')) return false;
throw e;
}
}
async function ensureTaskStructure(companyName, projectName, taskTitle) {
const companyDir = joinPath(CLIENT_ROOT, safeName(companyName));
const projectsDir = joinPath(companyDir, 'Projects');
const projectDir = joinPath(projectsDir, safeName(projectName));
const taskDir = joinPath(projectDir, safeName(taskTitle));
const requestInfoDir = joinPath(taskDir, 'Request Info');
const workingFilesDir = joinPath(taskDir, 'Working Files');
const oldBooksDir = joinPath(taskDir, 'Old Books');
await mkdir(companyDir);
await mkdir(projectsDir);
await mkdir(projectDir);
await mkdir(joinPath(projectDir, '00 Project Files'));
await mkdir(taskDir);
await mkdir(requestInfoDir);
await mkdir(workingFilesDir);
await mkdir(oldBooksDir);
return { requestInfoDir };
}
async function main() {
const { data: tasks, error: taskError } = await admin
.from('tasks')
.select(`
id, title,
project:projects!inner(
id, name,
company:companies!inner(name)
)
`);
if (taskError) { console.error('Task query failed:', taskError.message); process.exit(1); }
let ensuredTasks = 0;
for (const task of tasks || []) {
const project = task?.project;
const company = project?.company;
if (!task?.title || !project?.name || !company?.name) continue;
await ensureTaskStructure(company.name, project.name, task.title);
ensuredTasks++;
}
const { data: rows, error } = await admin
.from('submission_files')
.select(`
id, name, storage_path,
submission:submissions!inner(
id, version_number,
task:tasks!inner(
id, title,
project:projects!inner(
id, name,
company:companies!inner(name)
)
)
)
`);
if (error) { console.error('Query failed:', error.message); process.exit(1); }
// Group by task + version
const groups = new Map();
for (const row of rows || []) {
const sub = row.submission;
const task = sub?.task;
const project = task?.project;
const company = project?.company;
if (!task || !project || !company) continue;
const key = `${task.id}::${sub.version_number}`;
if (!groups.has(key)) {
groups.set(key, {
companyName: company.name,
projectName: project.name,
taskTitle: task.title,
versionNumber: sub.version_number,
files: [],
});
}
groups.get(key).files.push({ name: row.name, storage_path: row.storage_path });
}
console.log(`Ensured structure for ${ensuredTasks} tasks`);
console.log(`Found ${groups.size} task/revision groups, ${rows.length} total files`);
let processed = 0, skipped = 0, errors = 0;
for (const [key, group] of groups) {
if (group.files.length === 0) { skipped++; continue; }
const revFolder = `R${String(group.versionNumber).padStart(2, '0')}`;
const { requestInfoDir } = await ensureTaskStructure(group.companyName, group.projectName, group.taskTitle);
const revDir = joinPath(requestInfoDir, revFolder);
console.log(`\n[${group.companyName}] ${group.projectName} / ${group.taskTitle} / ${revFolder} (${group.files.length} files)`);
await mkdir(revDir);
for (const file of group.files) {
try {
const { data: blob, error: downloadErr } = await admin.storage
.from('submissions')
.download(file.storage_path);
if (downloadErr || !blob) throw new Error(`download failed: ${downloadErr?.message}`);
const fileBuffer = await blob.arrayBuffer();
const form = new FormData();
form.append('file', new Blob([fileBuffer]), file.name);
const fbFilePath = joinPath(revDir, file.name);
if (await fbExists(fbFilePath)) {
console.log(` - skip existing ${file.name}`);
skipped++;
continue;
}
await fbFetch('POST', '/api/resources', {
params: { path: fbFilePath },
body: form,
});
console.log(`${file.name}`);
processed++;
} catch (err) {
console.error(`${file.name}: ${err.message}`);
errors++;
}
}
}
console.log(`\nDone. Processed: ${processed}, Skipped: ${skipped}, Errors: ${errors}`);
}
main().catch(err => { console.error(err); process.exit(1); });
@@ -1,103 +0,0 @@
// One-time: removes leftover '.Project Files' and 'Project Files' folders
// that may still exist alongside the renamed '00 Project Files'
// Run: node scripts/cleanup-old-project-files-folders.mjs
import { readFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createClient } from '@supabase/supabase-js';
const __dir = dirname(fileURLToPath(import.meta.url));
const envFile = resolve(__dir, '../.env.backfill');
const env = {};
readFileSync(envFile, 'utf8').split('\n').forEach(line => {
const m = line.match(/^([^#=]+)=(.*)$/);
if (m) env[m[1].trim()] = m[2].trim().replace(/^["']|["']$/g, '');
});
const SUPABASE_URL = env.VITE_SUPABASE_URL || env.SUPABASE_URL;
const SERVICE_ROLE_KEY = env.SUPABASE_SERVICE_ROLE_KEY;
const FB_URL = (env.FILEBROWSER_URL || '').replace(/\/+$/, '');
const FB_TOKEN = env.FILEBROWSER_TOKEN;
const CLIENT_ROOT = env.FILEBROWSER_CLIENT_ROOT || '/fourgebranding/Clients';
const FB_SOURCE = 'files';
if (!SUPABASE_URL || !SERVICE_ROLE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
if (!FB_URL || !FB_TOKEN) { console.error('Missing FileBrowser env'); process.exit(1); }
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, { auth: { persistSession: false } });
function safeName(v) {
return String(v || '').trim()
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
.replace(/\s+/g, ' ')
.replace(/^-+|-+$/g, '');
}
function joinPath(...parts) {
const clean = parts.join('/').split('/').filter(p => p && p !== '.');
return `/${clean.join('/')}`;
}
async function fbList(path) {
const qs = new URLSearchParams({ source: FB_SOURCE, path }).toString();
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
headers: { Authorization: `Bearer ${FB_TOKEN}` },
});
if (!res.ok) return null;
const data = await res.json().catch(() => null);
return data?.folders || [];
}
async function fbDelete(path) {
const qs = new URLSearchParams({ source: FB_SOURCE, path }).toString();
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
method: 'DELETE',
headers: { Authorization: `Bearer ${FB_TOKEN}` },
});
if (!res.ok) {
const text = await res.text();
throw new Error(`${res.status}: ${text.slice(0, 100)}`);
}
}
const OLD_NAMES = ['.Project Files', 'Project Files'];
async function main() {
const { data: projects, error } = await admin
.from('projects')
.select('id, name, company:companies(name)');
if (error) { console.error('Query failed:', error.message); process.exit(1); }
console.log(`Checking ${projects.length} projects...\n`);
let cleaned = 0;
for (const p of projects) {
const company = safeName(p.company?.name || '');
const project = safeName(p.name || '');
if (!company || !project) continue;
const projectDir = joinPath(CLIENT_ROOT, company, 'Projects', project);
const entries = await fbList(projectDir);
if (!entries) { console.log(` ? could not list: ${company} / ${project}`); continue; }
const names = entries.map(e => e.name);
for (const oldName of OLD_NAMES) {
if (names.includes(oldName)) {
const oldPath = joinPath(projectDir, oldName);
try {
await fbDelete(oldPath);
console.log(` ✓ deleted "${oldName}": ${company} / ${project}`);
cleaned++;
} catch (err) {
console.error(` ✗ failed to delete "${oldName}" in ${company} / ${project}: ${err.message}`);
}
}
}
}
console.log(`\nDone. Cleaned ${cleaned} old folder(s).`);
}
main().catch(err => { console.error(err); process.exit(1); });
+180
View File
@@ -0,0 +1,180 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Task Tracker PDF Generator</title>
<script src="https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2/dist/umd/supabase.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf-autotable/3.8.2/jspdf.plugin.autotable.min.js"></script>
<style>
body { font-family: -apple-system, sans-serif; background: #111; color: #fff; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; flex-direction: column; gap: 16px; }
button { background: #F5A523; color: #000; border: none; border-radius: 8px; padding: 12px 28px; font-size: 14px; font-weight: 600; cursor: pointer; }
button:disabled { opacity: 0.5; cursor: default; }
#status { font-size: 13px; color: #888; }
select { background: #222; color: #fff; border: 1px solid #333; border-radius: 8px; padding: 8px 12px; font-size: 13px; min-width: 220px; }
</style>
</head>
<body>
<div style="font-size:22px;font-weight:700;letter-spacing:-0.5px">Task Tracker PDF</div>
<select id="companyFilter"><option value="">All Companies</option></select>
<button id="btn" onclick="generate()">Generate &amp; Download PDF</button>
<div id="status">Ready</div>
<script>
const SUPABASE_URL = 'https://fqflxxqvennhvoeywrdw.supabase.co';
const SUPABASE_ANON_KEY = 'sb_publishable_qNNIKtnu1dUIVKelq9aYYQ_TfHgzhyR';
const { createClient } = supabase;
const sb = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);
const STATUS_LABELS = {
not_started: 'Not Started',
in_progress: 'In Progress',
on_hold: 'On Hold',
client_review: 'In Review',
client_approved: 'Approved',
invoiced: 'Invoiced',
paid: 'Paid',
};
const versionLabel = (v) => `R${String(v ?? 0).padStart(2, '0')}`;
const isInvoiced = (t) => t.invoiced || t.status === 'invoiced' || t.status === 'paid';
// Populate company dropdown on load
(async () => {
const { data } = await sb
.from('tasks')
.select('project:projects(company:companies(id, name))')
.limit(1000);
const seen = new Map();
for (const t of data || []) {
const c = t.project?.company;
if (c && !seen.has(c.id)) seen.set(c.id, c);
}
const sel = document.getElementById('companyFilter');
[...seen.values()]
.sort((a, b) => a.name.localeCompare(b.name))
.forEach(c => {
const opt = document.createElement('option');
opt.value = c.id;
opt.textContent = c.name;
sel.appendChild(opt);
});
})();
async function generate() {
const btn = document.getElementById('btn');
const status = document.getElementById('status');
const selectedCompany = document.getElementById('companyFilter').value;
const selectedCompanyName = document.getElementById('companyFilter').selectedOptions[0]?.text || 'All Companies';
btn.disabled = true;
status.textContent = 'Fetching data from Supabase…';
try {
const { data, error } = await sb
.from('tasks')
.select('id, title, status, current_version, invoiced, submitted_at, project:projects(id, name, company:companies(id, name))')
.order('submitted_at', { ascending: false });
if (error) throw error;
let rows = data || [];
if (selectedCompany) {
rows = rows.filter(t => t.project?.company?.id === selectedCompany);
}
status.textContent = `Building PDF for ${rows.length} tasks…`;
const { jsPDF } = window.jspdf;
const doc = new jsPDF({ orientation: 'portrait', unit: 'pt', format: 'letter' });
// Header
doc.setFont('helvetica', 'bold');
doc.setFontSize(18);
doc.setTextColor(245, 165, 35);
doc.text('Fourge Branding', 40, 48);
doc.setFont('helvetica', 'normal');
doc.setFontSize(11);
doc.setTextColor(120, 120, 120);
doc.text('Task Tracker', 40, 64);
doc.setFontSize(9);
const now = new Date().toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
doc.text(`Generated ${now} · ${selectedCompanyName} · ${rows.length} tasks`, 40, 78);
// Table
doc.autoTable({
startY: 96,
head: [['TASK NAME', 'VERSION', 'STATUS', 'INVOICED']],
body: rows.map(t => [
[
t.title || '—',
t.project?.company?.name && t.project?.name
? `${t.project.company.name} · ${t.project.name}`
: (t.project?.name || ''),
],
versionLabel(t.current_version),
STATUS_LABELS[t.status] || t.status || '—',
isInvoiced(t) ? 'Yes' : '—',
]),
styles: {
fontSize: 9,
cellPadding: { top: 5, right: 8, bottom: 5, left: 8 },
overflow: 'linebreak',
textColor: [30, 30, 30],
},
headStyles: {
fillColor: [245, 165, 35],
textColor: [0, 0, 0],
fontStyle: 'bold',
fontSize: 8,
},
alternateRowStyles: {
fillColor: [248, 248, 248],
},
columnStyles: {
0: { cellWidth: 220 },
1: { cellWidth: 55, halign: 'center' },
2: { cellWidth: 90 },
3: { cellWidth: 60, halign: 'center' },
},
willDrawCell(data) {
if (data.column.index === 0 && data.section === 'body') {
const [title, sub] = Array.isArray(data.cell.raw) ? data.cell.raw : [data.cell.raw, ''];
const x = data.cell.x + 8;
let y = data.cell.y + 13;
doc.setFont('helvetica', 'bold');
doc.setFontSize(9);
doc.setTextColor(30, 30, 30);
doc.text(String(title), x, y, { maxWidth: 204 });
if (sub) {
const titleLines = doc.splitTextToSize(String(title), 204).length;
y += titleLines * 11;
doc.setFont('helvetica', 'normal');
doc.setFontSize(8);
doc.setTextColor(140, 140, 140);
doc.text(String(sub), x, y, { maxWidth: 204 });
}
data.cell.text = [];
}
},
margin: { left: 40, right: 40 },
});
const slug = selectedCompany ? selectedCompanyName.replace(/\s+/g, '-').toLowerCase() : 'all';
const filename = `task-tracker-${slug}-${new Date().toISOString().slice(0, 10)}.pdf`;
doc.save(filename);
status.textContent = `Downloaded: ${filename}`;
} catch (err) {
status.textContent = `Error: ${err.message}`;
console.error(err);
} finally {
btn.disabled = false;
}
}
</script>
</body>
</html>
-104
View File
@@ -1,104 +0,0 @@
// One-time: renames .00 Project Files → 00 Project Files for all existing project folders
// Run: node scripts/rename-project-files-folder.mjs
import { readFileSync } from 'fs';
import { resolve, dirname } from 'path';
import { fileURLToPath } from 'url';
import { createClient } from '@supabase/supabase-js';
const __dir = dirname(fileURLToPath(import.meta.url));
const envFile = resolve(__dir, '../.env.backfill');
const env = {};
readFileSync(envFile, 'utf8').split('\n').forEach(line => {
const m = line.match(/^([^#=]+)=(.*)$/);
if (m) env[m[1].trim()] = m[2].trim().replace(/^["']|["']$/g, '');
});
const SUPABASE_URL = env.VITE_SUPABASE_URL || env.SUPABASE_URL;
const SERVICE_ROLE_KEY = env.SUPABASE_SERVICE_ROLE_KEY;
const FB_URL = (env.FILEBROWSER_URL || '').replace(/\/+$/, '');
const FB_TOKEN = env.FILEBROWSER_TOKEN;
const CLIENT_ROOT = env.FILEBROWSER_CLIENT_ROOT || '/fourgebranding/Clients';
const FB_SOURCE = 'files';
if (!SUPABASE_URL || !SERVICE_ROLE_KEY) { console.error('Missing Supabase env'); process.exit(1); }
if (!FB_URL || !FB_TOKEN) { console.error('Missing FileBrowser env'); process.exit(1); }
const admin = createClient(SUPABASE_URL, SERVICE_ROLE_KEY, { auth: { persistSession: false } });
function safeName(v) {
return String(v || '').trim()
.replace(/[\\/:*?"<>|#%{}^~[\]`]+/g, '-')
.replace(/\s+/g, ' ')
.replace(/^-+|-+$/g, '');
}
function joinPath(...parts) {
const clean = parts.join('/').split('/').filter(p => p && p !== '.');
return `/${clean.join('/')}`;
}
async function fbRename(fromPath, toPath) {
const qs = new URLSearchParams({ source: FB_SOURCE }).toString();
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
method: 'PATCH',
headers: { Authorization: `Bearer ${FB_TOKEN}`, 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'rename',
items: [{ fromSource: FB_SOURCE, fromPath, toSource: FB_SOURCE, toPath }],
overwrite: false,
}),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`${res.status}: ${text.slice(0, 120)}`);
}
}
async function fbMkdir(path) {
const qs = new URLSearchParams({ source: FB_SOURCE, path, isDir: 'true' }).toString();
const res = await fetch(`${FB_URL}/api/resources?${qs}`, {
method: 'POST',
headers: { Authorization: `Bearer ${FB_TOKEN}` },
});
if (!res.ok) {
const text = await res.text();
if (!text.includes('already') && !text.includes('exist')) throw new Error(`mkdir ${path}: ${res.status}`);
}
}
async function main() {
const { data: projects, error } = await admin
.from('projects')
.select('id, name, company:companies(name)');
if (error) { console.error('Query failed:', error.message); process.exit(1); }
console.log(`Found ${projects.length} projects`);
for (const p of projects) {
const company = safeName(p.company?.name || '');
const project = safeName(p.name || '');
if (!company || !project) continue;
const projectDir = joinPath(CLIENT_ROOT, company, 'Projects', project);
const oldPath = joinPath(projectDir, 'Project Files');
const newPath = joinPath(projectDir, '00 Project Files');
try {
await fbRename(oldPath, newPath);
console.log(` ✓ renamed: ${company} / ${project}`);
} catch (err) {
// If rename fails (source doesn't exist), ensure new folder exists
try {
await fbMkdir(newPath);
console.log(` + created: ${company} / ${project} (no dot folder found)`);
} catch (e2) {
console.log(` ~ exists: ${company} / ${project}`);
}
}
}
console.log('\nDone.');
}
main().catch(err => { console.error(err); process.exit(1); });