|
| 1 | +library(shiny) |
| 2 | +library(bslib) |
| 3 | +library(bsicons) |
| 4 | +library(connectapi) |
| 5 | +library(dplyr) |
| 6 | +library(shinyjs) |
| 7 | + |
| 8 | +ui <- page_sidebar( |
| 9 | + useShinyjs(), |
| 10 | + title = "Swap Vanity URLs", |
| 11 | + sidebar = sidebar( |
| 12 | + # You would typically put filter controls here |
| 13 | + div("Select two content items to swap their vanity URLs."), |
| 14 | + checkboxInput("only_vanities", "Show only content with vanity URLs", value = FALSE) |
| 15 | + ), |
| 16 | + |
| 17 | + card( |
| 18 | + card_header( |
| 19 | + div( |
| 20 | + class = "d-flex justify-content-between align-items-center", |
| 21 | + span("Content Items"), |
| 22 | + actionButton("swapButton", "Swap Vanity URLs", class = "btn-primary") |
| 23 | + ) |
| 24 | + ), |
| 25 | + div( |
| 26 | + style = "padding: 15px;", |
| 27 | + uiOutput("content_list") |
| 28 | + ) |
| 29 | + ) |
| 30 | +) |
| 31 | + |
| 32 | +server <- function(input, output, session) { |
| 33 | + data_version <- reactiveVal(0) |
| 34 | + |
| 35 | + # Connect client as a reactive value |
| 36 | + reactive_client <- reactive(suppressMessages(connect())) |
| 37 | + |
| 38 | + # Reactive to get content data |
| 39 | + content_data <- reactive({ |
| 40 | + data_version() |
| 41 | + |
| 42 | + client <- reactive_client() |
| 43 | + |
| 44 | + # FIXDME: Filter for a single user's content, hard-coded |
| 45 | + content <- get_content(client, owner_guid = Sys.getenv("OWNER_GUID")) |
| 46 | + vanities <- get_vanity_urls(client) |
| 47 | + |
| 48 | + if (input$only_vanities) { |
| 49 | + joined_content <- inner_join(content, vanities, join_by("guid" == "content_guid")) |
| 50 | + } else { |
| 51 | + joined_content <- left_join(content, vanities, join_by("guid" == "content_guid")) |
| 52 | + } |
| 53 | + select(joined_content, guid, title, path, dashboard_url) |
| 54 | + }) |
| 55 | + |
| 56 | + # Reactive to track the number of selected items |
| 57 | + selected_count <- reactive({ |
| 58 | + sum(unlist(reactiveValuesToList(input)[grep("^select_", names(input))])) |
| 59 | + }) |
| 60 | + |
| 61 | + # Update button state based on count |
| 62 | + observe({ |
| 63 | + toggleState("swapButton", selected_count() == 2) |
| 64 | + }) |
| 65 | + |
| 66 | + # Generate the list UI |
| 67 | + output$content_list <- renderUI({ |
| 68 | + data <- content_data() |
| 69 | + |
| 70 | + client <- reactive_client() |
| 71 | + |
| 72 | + items <- lapply(seq_len(nrow(data)), function(i) { |
| 73 | + div( |
| 74 | + class = "d-flex border-bottom py-3", |
| 75 | + # Checkbox column |
| 76 | + div( |
| 77 | + class = "me-3", |
| 78 | + checkboxInput(paste0("select_", data$guid[i]), "", width = "auto") |
| 79 | + ), |
| 80 | + # Content column |
| 81 | + div( |
| 82 | + class = "flex-grow-1 d-flex flex-column", # Flex column layout |
| 83 | + style = "min-width: 0;", |
| 84 | + # Title row |
| 85 | + div( |
| 86 | + h5(data$title[i], class = "mb-1") |
| 87 | + ), |
| 88 | + # URLs |
| 89 | + div( |
| 90 | + class = "d-flex flex-column gap-1", |
| 91 | + # Vanity Path Row |
| 92 | + div( |
| 93 | + class = "small text-truncate", |
| 94 | + tags$span("Vanity Path: "), |
| 95 | + if (is.na(data$path[i])) { |
| 96 | + "None" |
| 97 | + } else { |
| 98 | + a(data$path[i], href = (client$server_url(data$path[i])), target = "_blank", |
| 99 | + class = "text-decoration-none") |
| 100 | + } |
| 101 | + ), |
| 102 | + # Open Dashboard Row |
| 103 | + div( |
| 104 | + class = "small", |
| 105 | + a( |
| 106 | + div(bs_icon("arrow-up-right-circle"), "Open Dashboard "), |
| 107 | + href = data$dashboard_url[i], |
| 108 | + target = "_blank", class = "text-decoration-none" |
| 109 | + ) |
| 110 | + ) |
| 111 | + ) |
| 112 | + ) |
| 113 | + ) |
| 114 | + }) |
| 115 | + |
| 116 | + div(class = "list-group", items) |
| 117 | + }) |
| 118 | + |
| 119 | + # When the button is pressed, swap the vanity URLs. |
| 120 | + observeEvent(input$swapButton, { |
| 121 | + input_list <- reactiveValuesToList(input) |
| 122 | + checkboxes <- input_list[grep("^select_", names(input_list))] |
| 123 | + selected <- names(checkboxes[checkboxes == TRUE]) |
| 124 | + selected_guids <- sub("^select_", "", selected) |
| 125 | + |
| 126 | + client <- reactive_client() |
| 127 | + |
| 128 | + item_1 <- content_item(client, selected_guids[1]) |
| 129 | + item_2 <- content_item(client, selected_guids[2]) |
| 130 | + |
| 131 | + swap_vanity_url(item_1, item_2) |
| 132 | + |
| 133 | + data_version(data_version() + 1) |
| 134 | + }) |
| 135 | +} |
| 136 | + |
| 137 | +shinyApp(ui, server) |
0 commit comments