rob-revisited/ide/ui/js/recent.js

74 lines
4.6 KiB
JavaScript

// Recently Changed Files (Navigate > Recently Changed Files, Ctrl+Shift+E): the project's files by their modification
// time on disk, newest first, whoever changed them (the IDE, Claude Code, a terminal, git), with the git status and,
// when the local history knows, whether the IDE saved it or it changed outside. A click opens the file, Diff compares it
// with the version before (local history) or with git's HEAD. PHP: JumPHP\Ide\Search\RecentChanges (files.recent).
import { backend } from './backend.js';
import { el, clear, icon, basename, dirname, fileIcon, debounce, notify, timeAgo, prefs, t } from './ui.js';
const PERIODS = [['3600', 'Last hour'], ['86400', 'Today'], ['604800', 'Last 7 days'], ['0', 'Any time']];
const BADGES = { modified: 'M', added: 'A', untracked: '??', deleted: 'D', renamed: 'R', conflict: 'C' };
const size = (n) => (n < 1024 ? `${n} B` : n < 1048576 ? `${(n / 1024).toFixed(1)} KB` : `${(n / 1048576).toFixed(1)} MB`);
export class RecentChangesPanel {
constructor(app) {
this.app = app;
this.since = prefs.get('recentSince', '86400');
this.ext = '';
this.files = [];
this.list = el('div.tree.recent-list', { tabIndex: 0, style: { flex: '1', overflow: 'auto' } });
const period = el('select.input', { title: t('Changed in'), onchange: (e) => { this.since = e.target.value; prefs.set('recentSince', this.since); this.refresh(); } },
...PERIODS.map(([value, label]) => el('option', { value, selected: value === this.since }, t(label))));
const ext = el('input.input', { placeholder: t('Extensions (php, js)'), spellcheck: false, oninput: debounce((e) => { this.ext = e.target.value; this.refresh(); }, 250) });
this.summary = el('span.recent-summary');
this.node = el('div.recent-changes', { style: { height: '100%', display: 'flex', flexDirection: 'column' } },
el('div.find-bar', {}, el('div.line', {}, period, ext), el('div.line', {}, this.summary)), this.list);
this.refreshSoon = debounce(() => this.refresh(), 400);
backend.on('files.changed', () => { if (this.visible()) this.refreshSoon(); });
}
title() { return 'Recently Changed'; }
actions() { return [el('button', { title: t('Refresh'), onclick: () => this.refresh() }, icon('refresh'))]; }
shown() { this.refresh(); }
visible() { return this.app.open?.[this.app.tools.recent?.side] === 'recent'; }
async refresh() {
try {
this.files = await backend.invoke('files.recent', { since: +this.since, ext: this.ext, limit: 300 });
} catch (e) { this.summary.textContent = e.message; return; }
this.render();
}
render() {
clear(this.list);
const git = this.app.git?.byPath;
this.summary.textContent = t('{n} file(s), newest first', { n: this.files.length });
if (!this.files.length) { this.list.append(el('div.placeholder', {}, t('No file changed in this period.'))); return; }
for (const f of this.files) {
const status = git?.get(f.path)?.status;
const badge = status ? el(`span.recent-badge.${status}`, { title: t(status) }, BADGES[status] || 'M') : null;
const who = f.origin === 'ide' ? el('span.recent-origin', { title: t('Saved in the IDE') }, 'IDE')
: f.origin === 'outside' ? el('span.recent-origin.outside', { title: t('Changed outside the IDE (Claude, a terminal, git…)') }, t('outside')) : null;
const diff = (f.previous || (status && status !== 'untracked' && status !== 'added'))
? el('button.icon-btn.recent-diff', { title: f.previous ? t('Compare with the version before (local history)') : t('Compare with HEAD'), onclick: (e) => { e.stopPropagation(); this.diff(f, status); } }, icon('diff'))
: null;
this.list.append(el('div.row.recent-row', { title: f.path, onclick: () => this.app.editors.open(f.path, { preview: true }), ondblclick: () => this.app.editors.open(f.path) },
fileIcon(f.path), el('span.label', {}, basename(f.path)), badge, el('span.hint', {}, dirname(f.path)), who,
el('span.recent-meta', { title: `${new Date(f.mtime * 1000).toLocaleString()} · ${size(f.size)}` }, timeAgo(f.mtime)), diff));
}
}
/** The file against what it was before: its previous local-history version, else git's HEAD. */
async diff(f, status) {
if (f.previous) {
const original = await backend.invoke('history.local.read', { path: f.path, id: f.previous.id }).catch((e) => { notify(e.message, 'error'); return null; });
if (original === null) return;
const when = new Date(f.previous.time * 1000).toLocaleString();
return this.app.editors.openDiff(f.path, { original, title: `${basename(f.path)} (${t('local history')}: ${when})`, left: when });
}
if (status) return this.app.git.diff(f.path);
}
}