Skip to content

Commit fc5e5bd

Browse files
committed
first commit of swap-vanity-urls
1 parent 7206242 commit fc5e5bd

File tree

5 files changed

+1468
-0
lines changed

5 files changed

+1468
-0
lines changed

extensions/swap-vanity-urls/.Rprofile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source("renv/activate.R")

extensions/swap-vanity-urls/app.R

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

0 commit comments

Comments
 (0)