diff --git a/n8n-batch-ui.json b/n8n-batch-ui.json new file mode 100644 index 0000000..e0eefef --- /dev/null +++ b/n8n-batch-ui.json @@ -0,0 +1,575 @@ +{ + "name": "Batch UI", + "nodes": [ + { + "parameters": { + "inputSource": "passthrough", + "schema": { + "schemaType": "fromFields", + "fields": [ + { + "fieldName": "chatId", + "fieldType": "number" + }, + { + "fieldName": "messageId", + "fieldType": "number" + }, + { + "fieldName": "callbackData", + "fieldType": "string" + }, + { + "fieldName": "queryId", + "fieldType": "string" + }, + { + "fieldName": "action", + "fieldType": "string" + }, + { + "fieldName": "batchPage", + "fieldType": "number" + }, + { + "fieldName": "selectedCsv", + "fieldType": "string" + }, + { + "fieldName": "toggleName", + "fieldType": "string" + } + ] + } + }, + "id": "batch-ui-trigger", + "name": "When executed by another workflow", + "type": "n8n-nodes-base.executeWorkflowTrigger", + "typeVersion": 1.1, + "position": [ + 240, + 300 + ] + }, + { + "parameters": { + "rules": { + "values": [ + { + "id": "route-mode", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-mode", + "leftValue": "={{ $json.action }}", + "rightValue": "mode", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "mode" + }, + { + "id": "route-toggle", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-toggle", + "leftValue": "={{ $json.action }}", + "rightValue": "toggle", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "toggle" + }, + { + "id": "route-nav", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-nav", + "leftValue": "={{ $json.action }}", + "rightValue": "nav", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "nav" + }, + { + "id": "route-exec", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-exec", + "leftValue": "={{ $json.action }}", + "rightValue": "exec", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "exec" + }, + { + "id": "route-clear", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-clear", + "leftValue": "={{ $json.action }}", + "rightValue": "clear", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "clear" + }, + { + "id": "route-cancel", + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "loose" + }, + "conditions": [ + { + "id": "is-cancel", + "leftValue": "={{ $json.action }}", + "rightValue": "cancel", + "operator": { + "type": "string", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "renameOutput": true, + "outputKey": "cancel" + } + ] + }, + "options": { + "fallbackOutput": "none" + } + }, + "id": "switch-route-batch-action", + "name": "Route Batch UI Action", + "type": "n8n-nodes-base.switch", + "typeVersion": 3.2, + "position": [ + 460, + 300 + ] + }, + { + "parameters": { + "url": "http://docker-socket-proxy:2375/containers/json?all=true", + "options": { + "timeout": 5000 + } + }, + "id": "http-fetch-containers-mode", + "name": "Fetch Containers For Mode", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 680, + 100 + ] + }, + { + "parameters": { + "jsCode": "// Build batch selection keyboard with toggle checkmarks and pagination\nconst containers = $input.all();\nconst triggerData = $('When executed by another workflow').item.json;\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\nconst page = triggerData.batchPage || 0;\nconst selectedCsv = triggerData.selectedCsv || '';\n\n// Parse selection\nconst selectedSet = new Set(selectedCsv ? selectedCsv.split(',') : []);\nconst selectedCount = selectedSet.size;\n\n// Extract container data\nlet allContainers = [];\nfor (const item of containers) {\n if (Array.isArray(item.json)) {\n allContainers = allContainers.concat(item.json);\n } else {\n allContainers.push(item.json);\n }\n}\n\n// Sort: running first, then alphabetically\nconst sortedContainers = allContainers\n .map(c => ({\n name: (c.Names && c.Names[0]) ? c.Names[0].replace(/^\\//, '') : 'unknown',\n state: c.State,\n id: c.Id.substring(0, 12)\n }))\n .sort((a, b) => {\n if (a.state === 'running' && b.state !== 'running') return -1;\n if (a.state !== 'running' && b.state === 'running') return 1;\n return a.name.localeCompare(b.name);\n });\n\n// Pagination: 6 containers per page\nconst containersPerPage = 6;\nconst totalPages = Math.ceil(sortedContainers.length / containersPerPage);\nconst start = page * containersPerPage;\nconst displayContainers = sortedContainers.slice(start, start + containersPerPage);\n\n// Build keyboard with checkmarks\nconst keyboard = displayContainers.map(c => {\n const isSelected = selectedSet.has(c.name);\n const icon = c.state === 'running' ? '\\u{1F7E2}' : '\\u26AA';\n const checkmark = isSelected ? '\\u2713 ' : '';\n return [\n {\n text: `${checkmark}${icon} ${c.name}`,\n callback_data: `batch:toggle:${page}:${selectedCsv}:${c.name}`\n }\n ];\n});\n\n// Add navigation row if needed\nif (totalPages > 1) {\n const navRow = [];\n if (page > 0) {\n navRow.push({ text: '\\u25C0\\uFE0F Previous', callback_data: `batch:nav:${page - 1}:${selectedCsv}` });\n }\n navRow.push({ text: `${page + 1}/${totalPages}`, callback_data: 'noop' });\n if (page < totalPages - 1) {\n navRow.push({ text: 'Next \\u25B6\\uFE0F', callback_data: `batch:nav:${page + 1}:${selectedCsv}` });\n }\n keyboard.push(navRow);\n}\n\n// Add action buttons if selection exists\nif (selectedCount > 0) {\n keyboard.push([\n { text: `Start (${selectedCount})`, callback_data: `batch:exec:start:${selectedCsv}` },\n { text: `Stop (${selectedCount})`, callback_data: `batch:exec:stop:${selectedCsv}` }\n ]);\n keyboard.push([\n { text: 'Clear', callback_data: 'batch:clear' },\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n} else {\n keyboard.push([\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n}\n\nconst totalCount = sortedContainers.length;\nlet message = selectedCount > 0 \n ? `Selected: ${selectedCount} container${selectedCount !== 1 ? 's' : ''}\\n(Tap to select/deselect)`\n : 'Select containers for batch action:\\n(Tap to select/deselect)';\nif (totalPages > 1) {\n message += `\\n\\nShowing ${start + 1}-${Math.min(start + containersPerPage, totalCount)} of ${totalCount}`;\n}\n\nreturn {\n json: {\n success: true,\n action: 'keyboard',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n text: message,\n keyboard: {\n inline_keyboard: keyboard\n },\n selectedCsv: selectedCsv,\n selectedCount: selectedCount,\n answerText: selectedCount > 0 ? `${selectedCount} selected` : 'Select containers...'\n }\n};" + }, + "id": "code-build-batch-keyboard", + "name": "Build Batch Keyboard", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 900, + 100 + ] + }, + { + "parameters": { + "jsCode": "// Handle container toggle in batch selection\nconst triggerData = $('When executed by another workflow').item.json;\nconst selectedCsv = triggerData.selectedCsv || '';\nconst toggleName = triggerData.toggleName;\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\nconst page = triggerData.batchPage || 0;\n\n// Parse current selection\nconst selectedSet = new Set(selectedCsv ? selectedCsv.split(',') : []);\n\n// Toggle the container\nif (selectedSet.has(toggleName)) {\n selectedSet.delete(toggleName);\n} else {\n selectedSet.add(toggleName);\n}\n\n// Convert back to CSV\nconst newSelected = Array.from(selectedSet).filter(n => n).join(',');\n\n// Calculate callback size limit (64 bytes)\nconst callbackPrefix = 'batch:toggle:';\nconst longestName = 20;\nconst maxCsvLength = 64 - callbackPrefix.length - longestName - 2;\n\n// Check if we're at limit\nif (newSelected.length > maxCsvLength) {\n return {\n json: {\n success: false,\n action: 'limit_reached',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n batchPage: page,\n selectedCsv: selectedCsv,\n answerText: 'Maximum selection reached',\n showAlert: true\n }\n };\n}\n\nreturn {\n json: {\n success: true,\n action: 'toggle_update',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n batchPage: page,\n selectedCsv: newSelected,\n selectedCount: selectedSet.size,\n needsKeyboardUpdate: true\n }\n};" + }, + "id": "code-handle-toggle", + "name": "Handle Toggle", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 680, + 200 + ] + }, + { + "parameters": { + "conditions": { + "options": { + "caseSensitive": true, + "typeValidation": "strict" + }, + "conditions": [ + { + "id": "needs-update", + "leftValue": "={{ $json.needsKeyboardUpdate }}", + "rightValue": true, + "operator": { + "type": "boolean", + "operation": "equals" + } + } + ], + "combinator": "and" + }, + "options": {} + }, + "id": "if-needs-keyboard-update", + "name": "Needs Keyboard Update?", + "type": "n8n-nodes-base.if", + "typeVersion": 2.1, + "position": [ + 900, + 200 + ] + }, + { + "parameters": { + "url": "http://docker-socket-proxy:2375/containers/json?all=true", + "options": { + "timeout": 5000 + } + }, + "id": "http-fetch-containers-toggle", + "name": "Fetch Containers For Update", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 1120, + 100 + ] + }, + { + "parameters": { + "jsCode": "// Rebuild batch selection keyboard with updated checkmarks\nconst containers = $input.all();\nconst toggleData = $('Handle Toggle').item.json;\nconst selectedCsv = toggleData.selectedCsv || '';\nconst selectedCount = toggleData.selectedCount || 0;\nconst chatId = toggleData.chatId;\nconst messageId = toggleData.messageId;\nconst queryId = toggleData.queryId;\nconst page = toggleData.batchPage || 0;\n\n// Parse selection\nconst selectedSet = new Set(selectedCsv ? selectedCsv.split(',') : []);\n\n// Extract container data\nlet allContainers = [];\nfor (const item of containers) {\n if (Array.isArray(item.json)) {\n allContainers = allContainers.concat(item.json);\n } else {\n allContainers.push(item.json);\n }\n}\n\n// Sort: running first, then alphabetically\nconst sortedContainers = allContainers\n .map(c => ({\n name: (c.Names && c.Names[0]) ? c.Names[0].replace(/^\\//, '') : 'unknown',\n state: c.State,\n id: c.Id.substring(0, 12)\n }))\n .sort((a, b) => {\n if (a.state === 'running' && b.state !== 'running') return -1;\n if (a.state !== 'running' && b.state === 'running') return 1;\n return a.name.localeCompare(b.name);\n });\n\n// Pagination\nconst containersPerPage = 6;\nconst totalPages = Math.ceil(sortedContainers.length / containersPerPage);\nconst start = page * containersPerPage;\nconst displayContainers = sortedContainers.slice(start, start + containersPerPage);\n\n// Build keyboard with checkmarks\nconst keyboard = displayContainers.map(c => {\n const isSelected = selectedSet.has(c.name);\n const icon = c.state === 'running' ? '\\u{1F7E2}' : '\\u26AA';\n const checkmark = isSelected ? '\\u2713 ' : '';\n return [\n {\n text: `${checkmark}${icon} ${c.name}`,\n callback_data: `batch:toggle:${page}:${selectedCsv}:${c.name}`\n }\n ];\n});\n\n// Add navigation row\nif (totalPages > 1) {\n const navRow = [];\n if (page > 0) {\n navRow.push({ text: '\\u25C0\\uFE0F Previous', callback_data: `batch:nav:${page - 1}:${selectedCsv}` });\n }\n navRow.push({ text: `${page + 1}/${totalPages}`, callback_data: 'noop' });\n if (page < totalPages - 1) {\n navRow.push({ text: 'Next \\u25B6\\uFE0F', callback_data: `batch:nav:${page + 1}:${selectedCsv}` });\n }\n keyboard.push(navRow);\n}\n\n// Add action buttons if selection exists\nif (selectedCount > 0) {\n keyboard.push([\n { text: `Start (${selectedCount})`, callback_data: `batch:exec:start:${selectedCsv}` },\n { text: `Stop (${selectedCount})`, callback_data: `batch:exec:stop:${selectedCsv}` }\n ]);\n keyboard.push([\n { text: 'Clear', callback_data: 'batch:clear' },\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n} else {\n keyboard.push([\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n}\n\nconst totalCount = sortedContainers.length;\nlet message = selectedCount > 0 \n ? `Selected: ${selectedCount} container${selectedCount !== 1 ? 's' : ''}\\n(Tap to select/deselect)`\n : 'Select containers for batch action:\\n(Tap to select/deselect)';\nif (totalPages > 1) {\n message += `\\n\\nShowing ${start + 1}-${Math.min(start + containersPerPage, totalCount)} of ${totalCount}`;\n}\n\nreturn {\n json: {\n success: true,\n action: 'keyboard',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n text: message,\n keyboard: {\n inline_keyboard: keyboard\n },\n selectedCsv: selectedCsv,\n answerText: ''\n }\n};" + }, + "id": "code-rebuild-keyboard", + "name": "Rebuild Keyboard After Toggle", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1340, + 100 + ] + }, + { + "parameters": { + "jsCode": "// Handle batch:exec callback - determine if confirmation needed\nconst triggerData = $('When executed by another workflow').item.json;\nconst callbackData = triggerData.callbackData || '';\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\n\n// Parse callback: batch:exec:{action}:{selectedCsv}\nconst parts = callbackData.split(':');\nconst batchAction = parts[2] || 'start';\nconst selectedCsv = parts.slice(3).join(':');\n\n// Split names\nconst containerNames = selectedCsv.split(',').filter(n => n);\nconst count = containerNames.length;\n\n// Check if stop action (needs confirmation per CONTEXT)\nconst needsConfirmation = batchAction === 'stop';\n\nif (needsConfirmation) {\n // Build stop confirmation keyboard\n const timestamp = Math.floor(Date.now() / 1000);\n const message = `Stop ${count} container${count !== 1 ? 's' : ''}?\\n\\n${containerNames.map(n => '\\u2022 ' + n).join('\\n')}`;\n \n return {\n json: {\n success: true,\n action: 'confirmation',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n text: message,\n keyboard: {\n inline_keyboard: [\n [\n { text: '\\u2705 Confirm Stop', callback_data: `bstop:confirm:${selectedCsv}:${timestamp}:kb` },\n { text: '\\u274c Cancel', callback_data: 'batch:cancel' }\n ]\n ]\n },\n answerText: 'Confirm stop...'\n }\n };\n}\n\n// Immediate execution (non-stop actions)\nreturn {\n json: {\n success: true,\n action: 'execute',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n batchAction: batchAction,\n containerNames: containerNames,\n selectedCsv: selectedCsv,\n count: count,\n fromKeyboard: true,\n answerText: `Starting batch ${batchAction}...`\n }\n};" + }, + "id": "code-handle-exec", + "name": "Handle Exec", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 680, + 400 + ] + }, + { + "parameters": { + "jsCode": "// Handle batch:nav callback - prepare for keyboard rebuild with new page\nconst triggerData = $('When executed by another workflow').item.json;\nconst page = triggerData.batchPage || 0;\nconst selectedCsv = triggerData.selectedCsv || '';\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\n\nreturn {\n json: {\n success: true,\n action: 'nav_update',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n batchPage: page,\n selectedCsv: selectedCsv,\n selectedCount: selectedCsv ? selectedCsv.split(',').filter(n => n).length : 0,\n needsKeyboardUpdate: true\n }\n};" + }, + "id": "code-handle-nav", + "name": "Handle Nav", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 680, + 300 + ] + }, + { + "parameters": { + "url": "http://docker-socket-proxy:2375/containers/json?all=true", + "options": { + "timeout": 5000 + } + }, + "id": "http-fetch-containers-nav", + "name": "Fetch Containers For Nav", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 900, + 300 + ] + }, + { + "parameters": { + "jsCode": "// Rebuild batch selection keyboard for navigation\nconst containers = $input.all();\nconst navData = $('Handle Nav').item.json;\nconst selectedCsv = navData.selectedCsv || '';\nconst selectedCount = navData.selectedCount || 0;\nconst chatId = navData.chatId;\nconst messageId = navData.messageId;\nconst queryId = navData.queryId;\nconst page = navData.batchPage || 0;\n\n// Parse selection\nconst selectedSet = new Set(selectedCsv ? selectedCsv.split(',') : []);\n\n// Extract container data\nlet allContainers = [];\nfor (const item of containers) {\n if (Array.isArray(item.json)) {\n allContainers = allContainers.concat(item.json);\n } else {\n allContainers.push(item.json);\n }\n}\n\n// Sort: running first, then alphabetically\nconst sortedContainers = allContainers\n .map(c => ({\n name: (c.Names && c.Names[0]) ? c.Names[0].replace(/^\\//, '') : 'unknown',\n state: c.State,\n id: c.Id.substring(0, 12)\n }))\n .sort((a, b) => {\n if (a.state === 'running' && b.state !== 'running') return -1;\n if (a.state !== 'running' && b.state === 'running') return 1;\n return a.name.localeCompare(b.name);\n });\n\n// Pagination\nconst containersPerPage = 6;\nconst totalPages = Math.ceil(sortedContainers.length / containersPerPage);\nconst start = page * containersPerPage;\nconst displayContainers = sortedContainers.slice(start, start + containersPerPage);\n\n// Build keyboard with checkmarks\nconst keyboard = displayContainers.map(c => {\n const isSelected = selectedSet.has(c.name);\n const icon = c.state === 'running' ? '\\u{1F7E2}' : '\\u26AA';\n const checkmark = isSelected ? '\\u2713 ' : '';\n return [\n {\n text: `${checkmark}${icon} ${c.name}`,\n callback_data: `batch:toggle:${page}:${selectedCsv}:${c.name}`\n }\n ];\n});\n\n// Add navigation row\nif (totalPages > 1) {\n const navRow = [];\n if (page > 0) {\n navRow.push({ text: '\\u25C0\\uFE0F Previous', callback_data: `batch:nav:${page - 1}:${selectedCsv}` });\n }\n navRow.push({ text: `${page + 1}/${totalPages}`, callback_data: 'noop' });\n if (page < totalPages - 1) {\n navRow.push({ text: 'Next \\u25B6\\uFE0F', callback_data: `batch:nav:${page + 1}:${selectedCsv}` });\n }\n keyboard.push(navRow);\n}\n\n// Add action buttons if selection exists\nif (selectedCount > 0) {\n keyboard.push([\n { text: `Start (${selectedCount})`, callback_data: `batch:exec:start:${selectedCsv}` },\n { text: `Stop (${selectedCount})`, callback_data: `batch:exec:stop:${selectedCsv}` }\n ]);\n keyboard.push([\n { text: 'Clear', callback_data: 'batch:clear' },\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n} else {\n keyboard.push([\n { text: 'Cancel', callback_data: 'batch:cancel' }\n ]);\n}\n\nconst totalCount = sortedContainers.length;\nlet message = selectedCount > 0 \n ? `Selected: ${selectedCount} container${selectedCount !== 1 ? 's' : ''}\\n(Tap to select/deselect)`\n : 'Select containers for batch action:\\n(Tap to select/deselect)';\nif (totalPages > 1) {\n message += `\\n\\nShowing ${start + 1}-${Math.min(start + containersPerPage, totalCount)} of ${totalCount}`;\n}\n\nreturn {\n json: {\n success: true,\n action: 'keyboard',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n text: message,\n keyboard: {\n inline_keyboard: keyboard\n },\n selectedCsv: selectedCsv,\n answerText: ''\n }\n};" + }, + "id": "code-rebuild-keyboard-nav", + "name": "Rebuild Keyboard For Nav", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1120, + 300 + ] + }, + { + "parameters": { + "jsCode": "// Handle batch:clear callback - reset selection to empty\nconst triggerData = $('When executed by another workflow').item.json;\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\n\nreturn {\n json: {\n success: true,\n action: 'clear_update',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n batchPage: 0,\n selectedCsv: '',\n selectedCount: 0,\n needsKeyboardUpdate: true\n }\n};" + }, + "id": "code-handle-clear", + "name": "Handle Clear", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 680, + 500 + ] + }, + { + "parameters": { + "url": "http://docker-socket-proxy:2375/containers/json?all=true", + "options": { + "timeout": 5000 + } + }, + "id": "http-fetch-containers-clear", + "name": "Fetch Containers For Clear", + "type": "n8n-nodes-base.httpRequest", + "typeVersion": 4.2, + "position": [ + 900, + 500 + ] + }, + { + "parameters": { + "jsCode": "// Rebuild batch selection keyboard after clear\nconst containers = $input.all();\nconst clearData = $('Handle Clear').item.json;\nconst chatId = clearData.chatId;\nconst messageId = clearData.messageId;\nconst queryId = clearData.queryId;\nconst page = 0; // Reset to first page\n\n// Extract container data\nlet allContainers = [];\nfor (const item of containers) {\n if (Array.isArray(item.json)) {\n allContainers = allContainers.concat(item.json);\n } else {\n allContainers.push(item.json);\n }\n}\n\n// Sort: running first, then alphabetically\nconst sortedContainers = allContainers\n .map(c => ({\n name: (c.Names && c.Names[0]) ? c.Names[0].replace(/^\\//, '') : 'unknown',\n state: c.State,\n id: c.Id.substring(0, 12)\n }))\n .sort((a, b) => {\n if (a.state === 'running' && b.state !== 'running') return -1;\n if (a.state !== 'running' && b.state === 'running') return 1;\n return a.name.localeCompare(b.name);\n });\n\n// Pagination\nconst containersPerPage = 6;\nconst totalPages = Math.ceil(sortedContainers.length / containersPerPage);\nconst start = page * containersPerPage;\nconst displayContainers = sortedContainers.slice(start, start + containersPerPage);\n\n// Build keyboard (no selection)\nconst keyboard = displayContainers.map(c => {\n const icon = c.state === 'running' ? '\\u{1F7E2}' : '\\u26AA';\n return [\n {\n text: `${icon} ${c.name}`,\n callback_data: `batch:toggle:${page}::${c.name}`\n }\n ];\n});\n\n// Add navigation row\nif (totalPages > 1) {\n const navRow = [];\n if (page > 0) {\n navRow.push({ text: '\\u25C0\\uFE0F Previous', callback_data: `batch:nav:${page - 1}:` });\n }\n navRow.push({ text: `${page + 1}/${totalPages}`, callback_data: 'noop' });\n if (page < totalPages - 1) {\n navRow.push({ text: 'Next \\u25B6\\uFE0F', callback_data: `batch:nav:${page + 1}:` });\n }\n keyboard.push(navRow);\n}\n\n// Add cancel button only\nkeyboard.push([\n { text: 'Cancel', callback_data: 'batch:cancel' }\n]);\n\nconst totalCount = sortedContainers.length;\nlet message = 'Select containers for batch action:\\n(Tap to select/deselect)';\nif (totalPages > 1) {\n message += `\\n\\nShowing ${start + 1}-${Math.min(start + containersPerPage, totalCount)} of ${totalCount}`;\n}\n\nreturn {\n json: {\n success: true,\n action: 'keyboard',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n text: message,\n keyboard: {\n inline_keyboard: keyboard\n },\n selectedCsv: '',\n answerText: 'Selection cleared'\n }\n};" + }, + "id": "code-rebuild-keyboard-clear", + "name": "Rebuild Keyboard After Clear", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 1120, + 500 + ] + }, + { + "parameters": { + "jsCode": "// Handle batch:cancel callback - return to container list\nconst triggerData = $('When executed by another workflow').item.json;\nconst chatId = triggerData.chatId;\nconst messageId = triggerData.messageId;\nconst queryId = triggerData.queryId;\n\nreturn {\n json: {\n success: true,\n action: 'cancel',\n queryId: queryId,\n chatId: chatId,\n messageId: messageId,\n page: 0,\n answerText: 'Batch selection cancelled'\n }\n};" + }, + "id": "code-handle-cancel", + "name": "Handle Cancel", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 680, + 600 + ] + } + ], + "connections": { + "When executed by another workflow": { + "main": [ + [ + { + "node": "Route Batch UI Action", + "type": "main", + "index": 0 + } + ] + ] + }, + "Route Batch UI Action": { + "main": [ + [ + { + "node": "Fetch Containers For Mode", + "type": "main", + "index": 0 + } + ], + [ + { + "node": "Handle Toggle", + "type": "main", + "index": 0 + } + ], + [ + { + "node": "Handle Nav", + "type": "main", + "index": 0 + } + ], + [ + { + "node": "Handle Exec", + "type": "main", + "index": 0 + } + ], + [ + { + "node": "Handle Clear", + "type": "main", + "index": 0 + } + ], + [ + { + "node": "Handle Cancel", + "type": "main", + "index": 0 + } + ] + ] + }, + "Fetch Containers For Mode": { + "main": [ + [ + { + "node": "Build Batch Keyboard", + "type": "main", + "index": 0 + } + ] + ] + }, + "Handle Toggle": { + "main": [ + [ + { + "node": "Needs Keyboard Update?", + "type": "main", + "index": 0 + } + ] + ] + }, + "Needs Keyboard Update?": { + "main": [ + [ + { + "node": "Fetch Containers For Update", + "type": "main", + "index": 0 + } + ] + ] + }, + "Fetch Containers For Update": { + "main": [ + [ + { + "node": "Rebuild Keyboard After Toggle", + "type": "main", + "index": 0 + } + ] + ] + }, + "Handle Nav": { + "main": [ + [ + { + "node": "Fetch Containers For Nav", + "type": "main", + "index": 0 + } + ] + ] + }, + "Fetch Containers For Nav": { + "main": [ + [ + { + "node": "Rebuild Keyboard For Nav", + "type": "main", + "index": 0 + } + ] + ] + }, + "Handle Clear": { + "main": [ + [ + { + "node": "Fetch Containers For Clear", + "type": "main", + "index": 0 + } + ] + ] + }, + "Fetch Containers For Clear": { + "main": [ + [ + { + "node": "Rebuild Keyboard After Clear", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "settings": { + "executionOrder": "v1", + "callerPolicy": "any" + } +}