|
| 1 | +/** |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * Triggered when the add-on is opened from the Gmail homepage. |
| 19 | + * |
| 20 | + * @param {!Object} e - The event object. |
| 21 | + * @returns {!Card} - The homepage card. |
| 22 | + */ |
| 23 | +function onHomepageTrigger(e) { |
| 24 | + return buildHomepageCard(); |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Builds the main card displayed on the Gmail homepage. |
| 29 | + * |
| 30 | + * @returns {!Card} - The homepage card. |
| 31 | + */ |
| 32 | +function buildHomepageCard() { |
| 33 | + // Create a new card builder |
| 34 | + const cardBuilder = CardService.newCardBuilder(); |
| 35 | + |
| 36 | + // Create a card header |
| 37 | + const cardHeader = CardService.newCardHeader(); |
| 38 | + cardHeader.setImageUrl('https://fonts.gstatic.com/s/i/googlematerialicons/label_important/v20/googblue-24dp/1x/gm_label_important_googblue_24dp.png'); |
| 39 | + cardHeader.setImageStyle(CardService.ImageStyle.CIRCLE); |
| 40 | + cardHeader.setTitle("Email Classifier"); |
| 41 | + |
| 42 | + // Add the header to the card |
| 43 | + cardBuilder.setHeader(cardHeader); |
| 44 | + |
| 45 | + // Create a card section |
| 46 | + const cardSection = CardService.newCardSection(); |
| 47 | + |
| 48 | + // Create buttons for generating sample emails and analyzing sentiment |
| 49 | + const buttonSet = CardService.newButtonSet(); |
| 50 | + |
| 51 | + // Create "Classify emails" button |
| 52 | + const classifyButton = createFilledButton({ |
| 53 | + text: 'Classify emails', |
| 54 | + functionName: 'main', |
| 55 | + color: '#007bff', |
| 56 | + icon: 'new_label' |
| 57 | + }); |
| 58 | + buttonSet.addButton(classifyButton); |
| 59 | + |
| 60 | + // Create "Create Labels" button |
| 61 | + const createLabelsButtton = createFilledButton({ |
| 62 | + text: 'Create labels', |
| 63 | + functionName: 'createLabels', |
| 64 | + color: '#34A853', |
| 65 | + icon: 'add' |
| 66 | + }); |
| 67 | + |
| 68 | + // Create "Remove Labels" button |
| 69 | + const removeLabelsButtton = createFilledButton({ |
| 70 | + text: 'Remove labels', |
| 71 | + functionName: 'removeLabels', |
| 72 | + color: '#FF0000', |
| 73 | + icon: 'delete' |
| 74 | + }); |
| 75 | + |
| 76 | + if (labelsCreated()) { |
| 77 | + buttonSet.addButton(removeLabelsButtton); |
| 78 | + } else { |
| 79 | + buttonSet.addButton(createLabelsButtton); |
| 80 | + } |
| 81 | + |
| 82 | + // Add the button set to the section |
| 83 | + cardSection.addWidget(buttonSet); |
| 84 | + |
| 85 | + // Add the section to the card |
| 86 | + cardBuilder.addSection(cardSection); |
| 87 | + |
| 88 | + // Build and return the card |
| 89 | + return cardBuilder.build(); |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Creates a filled text button with the specified text, function, and color. |
| 94 | + * |
| 95 | + * @param {{text: string, functionName: string, color: string, icon: string}} options |
| 96 | + * - text: The text to display on the button. |
| 97 | + * - functionName: The name of the function to call when the button is clicked. |
| 98 | + * - color: The background color of the button. |
| 99 | + * - icon: The material icon to display on the button. |
| 100 | + * @returns {!TextButton} - The created text button. |
| 101 | + */ |
| 102 | +function createFilledButton({text, functionName, color, icon}) { |
| 103 | + // Create a new text button |
| 104 | + const textButton = CardService.newTextButton(); |
| 105 | + |
| 106 | + // Set the button text |
| 107 | + textButton.setText(text); |
| 108 | + |
| 109 | + // Set the action to perform when the button is clicked |
| 110 | + const action = CardService.newAction(); |
| 111 | + action.setFunctionName(functionName); |
| 112 | + action.setLoadIndicator(CardService.LoadIndicator.SPINNER); |
| 113 | + textButton.setOnClickAction(action); |
| 114 | + |
| 115 | + // Set the button style to filled |
| 116 | + textButton.setTextButtonStyle(CardService.TextButtonStyle.FILLED); |
| 117 | + |
| 118 | + // Set the background color |
| 119 | + textButton.setBackgroundColor(color); |
| 120 | + |
| 121 | + textButton.setMaterialIcon(CardService.newMaterialIcon().setName(icon)); |
| 122 | + |
| 123 | + return textButton; |
| 124 | +} |
| 125 | + |
| 126 | +/** |
| 127 | + * Creates a notification response with the specified text. |
| 128 | + * |
| 129 | + * @param {string} notificationText - The text to display in the notification. |
| 130 | + * @returns {!ActionResponse} - The created action response. |
| 131 | + */ |
| 132 | +function buildNotificationResponse(notificationText) { |
| 133 | + // Create a new notification |
| 134 | + const notification = CardService.newNotification(); |
| 135 | + notification.setText(notificationText); |
| 136 | + |
| 137 | + // Create a new action response builder |
| 138 | + const actionResponseBuilder = CardService.newActionResponseBuilder(); |
| 139 | + |
| 140 | + // Set the notification for the action response |
| 141 | + actionResponseBuilder.setNotification(notification); |
| 142 | + |
| 143 | + // Build and return the action response |
| 144 | + return actionResponseBuilder.build(); |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * Creates a card to display the spreadsheet link. |
| 149 | + * |
| 150 | + * @param {string} spreadsheetUrl - The URL of the spreadsheet. |
| 151 | + * @returns {!ActionResponse} - The created action response. |
| 152 | + */ |
| 153 | +function showSpreadsheetLink(spreadsheetUrl) { |
| 154 | + const updatedCardBuilder = CardService.newCardBuilder(); |
| 155 | + |
| 156 | + updatedCardBuilder.setHeader(CardService.newCardHeader().setTitle('Sheet generated!')); |
| 157 | + |
| 158 | + const updatedSection = CardService.newCardSection() |
| 159 | + .addWidget(CardService.newTextParagraph() |
| 160 | + .setText('Click to open the sheet:') |
| 161 | + ) |
| 162 | + .addWidget(CardService.newTextButton() |
| 163 | + .setText('Open Sheet') |
| 164 | + .setOpenLink(CardService.newOpenLink() |
| 165 | + .setUrl(spreadsheetUrl) |
| 166 | + .setOpenAs(CardService.OpenAs.FULL_SCREEN) // Opens in a new browser tab/window |
| 167 | + .setOnClose(CardService.OnClose.NOTHING) // Does nothing when the tab is closed |
| 168 | + ) |
| 169 | + ) |
| 170 | + .addWidget(CardService.newTextButton() // Optional: Add a button to go back or refresh |
| 171 | + .setText('Go Back') |
| 172 | + .setOnClickAction(CardService.newAction() |
| 173 | + .setFunctionName('onHomepageTrigger')) // Go back to the initial state |
| 174 | + ); |
| 175 | + |
| 176 | + updatedCardBuilder.addSection(updatedSection); |
| 177 | + const newNavigation = CardService.newNavigation().updateCard(updatedCardBuilder.build()); |
| 178 | + |
| 179 | + return CardService.newActionResponseBuilder() |
| 180 | + .setNavigation(newNavigation) // This updates the current card in the UI |
| 181 | + .build(); |
| 182 | +} |
0 commit comments