import { initials, loadFailure, loading } from "./components.js";
import { h } from "./dom.js";
import { icon } from "./icons.js";
import { api, message } from "./ipc.js";
import { applyTheme, readTheme } from "./theme.js";
import { dashboard } from "./views/dashboard.js";
import { diagnostics } from "./views/diagnostics.js";
import { identities } from "./views/identities.js";
import { onboarding } from "./views/onboarding.js";
import { quickSwitch } from "./views/quick-switch.js";
import { repositories } from "./views/repositories.js";
import { repositoryDetails } from "./views/repository-details.js";
import { settings } from "./views/settings.js";
import { ssh } from "./views/ssh.js";
import { status } from "./views/status.js";
const VIEWS = [
{ id: "dashboard", label: "Dashboard", icon: "layout-dashboard" },
{ id: "identities", label: "Identities", icon: "users-round" },
{ id: "repositories", label: "Repositories", icon: "git-branch" },
{ id: "ssh", label: "SSH Keys", icon: "key-round" },
{ id: "diagnostics", label: "Diagnostics", icon: "monitor-cog" },
{ id: "settings", label: "Settings", icon: "settings" },
];
const NESTED_VIEWS = new Set(["status", "repository-details"]);
const app = {
version: "",
view: "dashboard",
profiles: [],
schemaVersion: 0,
activeGithubUser: "",
repositories: [],
roots: [],
selectedRepo: "",
theme: readTheme(),
quickSwitch: false,
busy: true,
loadFailed: false,
error: "",
notice: "",
};
const root = document.getElementById("root");
let noticeTimer = 0;
let mounted = { id: null, host: null };
export function signal(text) {
app.notice = text;
window.clearTimeout(noticeTimer);
noticeTimer = window.setTimeout(() => {
app.notice = "";
render();
}, 3500);
render();
}
export function fail(reason) {
app.error = message(reason);
render();
}
export async function reload() {
app.busy = true;
app.error = "";
app.loadFailed = false;
render();
try {
const [profiles, roots] = await Promise.all([activeApi.profiles(), activeApi.roots()]);
app.profiles = profiles;
app.roots = roots;
await readBackendFacts();
} catch (reason) {
app.error = message(reason);
app.loadFailed = true;
} finally {
app.busy = false;
mounted = { id: null, host: null };
render();
}
}
async function readBackendFacts() {
if (!app.schemaVersion) {
try {
const report = await activeApi.doctor();
app.schemaVersion = report?.schema_version ?? 0;
} catch {
app.schemaVersion = 0;
}
}
const hosts = [...new Set(app.profiles.map((entry) => entry.profile.hostname))];
let active = "";
for (const host of hosts) {
try {
const accounts = await activeApi.accounts(host);
const found = accounts.find((account) => account.active && account.valid);
if (found) {
active = found.login;
break;
}
} catch {
}
}
app.activeGithubUser = active;
}
let activeApi = api;
const context = {
app,
get api() {
return activeApi;
},
signal,
fail,
reload,
select(path) {
app.selectedRepo = path;
render();
},
touch() {
render();
},
go(view) {
if (view !== app.view) app.error = "";
app.view = view;
render();
},
openRepository(path) {
if (app.view !== "repository-details" || path !== app.selectedRepo) app.error = "";
app.selectedRepo = path;
app.view = "repository-details";
render();
},
setTheme(theme, accent) {
app.theme = applyTheme(theme, accent);
render();
},
openQuickSwitch() {
if (app.profiles.length === 0 || app.loadFailed) return;
app.quickSwitch = true;
render();
},
closeQuickSwitch() {
app.quickSwitch = false;
render();
},
};
function titlebar() {
return h(
"header",
{ class: "titlebar" },
h(
"div",
{ class: "wordmark" },
h("span", { class: "mark" }, icon("git-branch", 17)),
h("span", null, "GitBound"),
app.version ? h("span", { class: "version" }, `v${app.version}`) : null,
),
h(
"div",
{ class: "titlebar-actions" },
h(
"button",
{
class: "quick-switch-trigger",
"data-k": "shell-quick-switch",
"aria-label": "Quick switch identity",
disabled: app.profiles.length === 0 || app.loadFailed,
onClick: () => context.openQuickSwitch(),
},
icon("arrow-right-left", 15),
h("span", null, "Quick switch"),
h("kbd", null, shortcutLabel()),
),
h("div", { class: "local-only" }, icon("shield-check", 15), " Local configuration only"),
),
);
}
function shortcutLabel() {
return navigator.platform.toLowerCase().includes("mac") ? "⌘K" : "Ctrl K";
}
function sidebar() {
const drifted = app.repositories.some((repo) => repo.status === "drifted");
const active = app.activeGithubUser
? app.profiles.find((entry) =>
entry.profile.github_user.toLowerCase() === app.activeGithubUser.toLowerCase()
)
: null;
return h(
"aside",
{ class: "sidebar", "aria-label": "Primary navigation" },
h(
"nav",
null,
VIEWS.map((item) =>
h(
"button",
{
"aria-label": item.label,
"aria-current": app.view === item.id ? "page" : null,
class: app.view === item.id ? "nav-item active" : "nav-item",
onClick: () => context.go(item.id),
},
icon(item.icon, 17),
h("span", null, item.label),
item.id === "repositories" && drifted
? h("i", { class: "nav-dot", "aria-hidden": "true" })
: null,
)
),
),
h(
"div",
{ class: "sidebar-foot" },
active
? h(
"button",
{
class: "identity-chip",
"data-k": "sidebar-identity",
onClick: () => context.go("identities"),
},
h("span", { class: "avatar", "aria-hidden": "true" }, initials(active.profile.git_name)),
h(
"span",
{ class: "identity-chip-text" },
h("strong", null, active.name),
h("span", null, active.profile.git_email),
),
)
: null,
h(
"div",
{ class: "sidebar-note" },
h("span", { class: "status-dot ok" }),
"Configuration protected",
h(
"span",
{ class: "muted" },
app.schemaVersion
? `Schema ${app.schemaVersion} · no secrets stored`
: "No secrets stored",
),
),
),
);
}
function toasts() {
return [
app.error
? h(
"div",
{ class: "toast error", role: "alert" },
icon("circle-x", 17),
h("span", null, app.error),
h(
"button",
{
"aria-label": "Dismiss error",
onClick: () => {
app.error = "";
render();
},
},
icon("x", 16),
),
)
: null,
app.notice
? h(
"div",
{ class: "toast success", role: "status" },
icon("check", 17),
h("span", null, app.notice),
)
: null,
];
}
const BUILDERS = {
dashboard,
diagnostics,
identities,
repositories,
"repository-details": repositoryDetails,
settings,
ssh,
status,
};
function stub(id) {
const label = VIEWS.find((view) => view.id === id)?.label ?? id;
return h(
"section",
null,
h(
"header",
{ class: "page-header" },
h("div", null, h("h1", null, label), h("p", null, "Not yet ported.")),
),
);
}
function buildView(id) {
const builder = BUILDERS[id];
return builder ? builder(context) : stub(id);
}
function workspace() {
const host = h("main", { class: "workspace" });
if (app.busy) {
host.append(loading());
mounted = { id: null, host: null };
return host;
}
if (app.loadFailed) {
host.append(
app.view === "diagnostics"
? buildView("diagnostics")
: loadFailure(reload, () => context.go("diagnostics")),
);
mounted = { id: null, host: null };
return host;
}
if (app.profiles.length === 0) {
if (mounted.id === "onboarding" && mounted.host) {
host.append(mounted.host);
} else {
const wizard = onboarding(context);
mounted = { id: "onboarding", host: wizard };
host.append(wizard);
}
return host;
}
if (NESTED_VIEWS.has(app.view) && !app.selectedRepo) {
app.view = "repositories";
}
if (mounted.id === app.view && mounted.host) {
host.append(mounted.host);
} else {
const view = buildView(app.view);
mounted = { id: app.view, host: view };
host.append(view);
}
return host;
}
function render() {
root.replaceChildren(
h(
"div",
{ class: "app-shell" },
titlebar(),
sidebar(),
workspace(),
app.quickSwitch ? quickSwitch(context) : null,
toasts(),
),
);
}
document.addEventListener("keydown", (event) => {
if ((event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "k") {
event.preventDefault();
if (app.quickSwitch) context.closeQuickSwitch();
else context.openQuickSwitch();
return;
}
if (event.key === "Escape" && app.quickSwitch) {
event.preventDefault();
context.closeQuickSwitch();
}
});
async function demoFixtures() {
if (!new URLSearchParams(location.search).has("demo")) return null;
try {
return await api.demoFixtures();
} catch {
return null;
}
}
function demoApi(fixtures) {
const noop = async () => {};
return {
...api,
version: async () => "demo",
profiles: async () => fixtures.profiles,
roots: async () => fixtures.roots,
doctor: async () => fixtures.doctor,
scan: async () => fixtures.repositories,
cancelScan: noop,
accounts: async () => fixtures.accounts,
testSsh: async () => fixtures.ssh_test,
ciStatus: async () => fixtures.ci_status,
clone: async (_profile, repository, parent) => `${parent}/${repository.split("/").pop()}`,
audit: async (repository) => ({
repository,
profile: null,
overall: "ok",
checks: [
{
id: "commits",
expected: null,
actual: "3",
status: "ok",
message: "3 commits inspected",
},
{
id: "author_identity",
expected: "oss@mira.dev",
actual: "oss@mira.dev",
status: "ok",
message: "every commit was authored by an allowed address",
},
],
}),
directoryRules: async () => [
{
profile: fixtures.profiles[0]?.name ?? "work",
path: fixtures.roots[0] ?? "/home/mira/work",
condition: `includeIf.gitdir:${fixtures.roots[0] ?? "/home/mira/work"}/.path`,
include: "/home/mira/.config/GitBound/work.gitconfig",
},
],
hookState: async () => ({ pre_commit: "installed", pre_push: "installed" }),
installHooks: async () => ({ pre_commit: "installed", pre_push: "installed" }),
uninstallHooks: async () => ({ pre_commit: "not installed", pre_push: "not installed" }),
inspect: async (repository, network = false) => ({
network_checked: network,
report: {
repository,
profile: fixtures.repositories.find((r) => r.path === repository)?.bound_profile,
overall: "warning",
checks: [
{
id: "git_author",
expected: "Mira Chen",
actual: "Mira Chen",
status: "ok",
message: "Git author matches the bound profile",
},
{
id: "git_email",
expected: "oss@mira.dev",
actual: "oss@mira.dev",
status: "ok",
message: "Git email matches the bound profile",
},
{
id: "remote_owner",
expected: "tauri-apps",
actual: "tauri-apps",
status: "ok",
message: "Remote owner is allowed by the profile",
},
{
id: "github_cli",
expected: "personal",
actual: network ? "mira-acme" : undefined,
status: network ? "warning" : "unverified",
message: network
? "Active account differs; no switch was performed."
: "Run network refresh to check.",
},
],
},
}),
};
}
applyTheme(app.theme.mode, app.theme.accent);
render();
const fixtures = await demoFixtures();
if (fixtures) {
activeApi = demoApi(fixtures);
app.repositories = fixtures.repositories;
app.selectedRepo = fixtures.repositories[1]?.path ?? "";
}
try {
app.version = await activeApi.version();
} catch {
}
await reload();