(function () {
"use strict";
var ICON_SVG = {
doc: '',
sheet:
'',
slides:
'',
paper:
'',
};
var KEYBOARD_SHORTCUTS = {
1: "#chat",
2: "#drive",
3: "#tasks",
4: "#mail",
5: "#calendar",
6: "#meet",
};
function getIconForType(type) {
return ICON_SVG[type] || ICON_SVG.doc;
}
function escapeHtml(text) {
var div = document.createElement("div");
div.textContent = text;
return div.innerHTML;
}
function renderRecentDocuments(items) {
if (!items || items.length === 0) {
return;
}
var container = document.getElementById("recent-documents");
if (!container) {
return;
}
var html = items
.slice(0, 4)
.map(function (item) {
var safeUrl = escapeHtml(item.url || "");
var safeType = escapeHtml(item.type || "doc");
var safeName = escapeHtml(item.name || "");
var safeMeta = escapeHtml(item.meta || "");
return (
'
' +
'
' +
getIconForType(item.type) +
"
" +
'
' +
'' +
safeName +
"" +
'' +
safeMeta +
"" +
"
" +
"
"
);
})
.join("");
container.innerHTML = html;
container.querySelectorAll(".recent-card").forEach(function (card) {
card.addEventListener("click", function () {
var url = this.getAttribute("data-url");
if (url) {
window.location.href = url;
}
});
});
}
function loadRecentDocuments() {
fetch("/api/activity/recent")
.then(function (response) {
if (!response.ok) {
throw new Error("Failed to fetch recent documents");
}
return response.json();
})
.then(function (items) {
renderRecentDocuments(items);
})
.catch(function () {
console.log("Using placeholder recent documents");
});
}
function setupHomeSearch() {
var homeSearch = document.getElementById("home-search");
if (homeSearch) {
homeSearch.addEventListener("focus", function () {
var omnibox = document.getElementById("omniboxInput");
if (omnibox) {
omnibox.focus();
}
});
}
}
function setupKeyboardShortcuts() {
document.addEventListener("keydown", function (e) {
if (e.altKey && !e.ctrlKey && !e.shiftKey) {
var target = KEYBOARD_SHORTCUTS[e.key];
if (target) {
e.preventDefault();
var link = document.querySelector('a[href="' + target + '"]');
if (link) {
link.click();
}
}
}
});
}
function initHome() {
loadRecentDocuments();
setupHomeSearch();
}
function isHomeVisible() {
return document.querySelector(".home-container") !== null;
}
setupKeyboardShortcuts();
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", function () {
if (isHomeVisible()) {
initHome();
}
});
} else {
if (isHomeVisible()) {
initHome();
}
}
document.body.addEventListener("htmx:afterSwap", function (evt) {
if (evt.detail.target && evt.detail.target.id === "main-content") {
if (isHomeVisible()) {
initHome();
}
}
});
})();