4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | use serde::Deserialize; |
| 15 | use topcoat::{ |
| 16 | Result, |
| 17 | context::Cx, |
| 18 | router::{StatusCode, content::Form, error::not_found, page, query_params}, |
| 19 | view::{attributes, component, view}, |
| 20 | }; |
| 21 | |
| 22 | use crate::{ |
| 23 | application::{ |
| 24 | Error, RepoView, |
| 25 | repo::{RepoEdit, delete_repo, update_repo}, |
| 26 | }, |
| 27 | components::{ |
| 28 | button::{ButtonVariant, button}, |
| 29 | flash::{FlashKind, flash}, |
| 30 | input::input, |
| 31 | label::label, |
| 32 | select::select, |
| 33 | textarea::textarea, |
| 34 | }, |
| 35 | domain::{DomainError, RepoName, Repository, Visibility}, |
| 36 | }; |
| 37 | |
| 38 | use super::{ |
| 39 | context::{current_actor, location, memberships, orgs, repos, server_error, storage}, |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 40 | layout::narrow, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 41 | repo::repo_for, |
| 42 | }; |
| 43 | |
| 44 | #[derive(Debug, Deserialize)] |
| 45 | struct SettingsForm { |
| 46 | description: String, |
| 47 | visibility: String, |
ef23868feat: rebuild the profile page on flat navigation7d | 48 | |
| 49 | |
| 50 | pinned: Option<String>, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 51 | } |
| 52 | |
| 53 | #[derive(Debug, Deserialize)] |
| 54 | struct DeleteForm { |
| 55 | confirm: String, |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | #[query_params(error = bad_request)] |
| 62 | struct Saved { |
| 63 | saved: Option<String>, |
| 64 | } |
| 65 | |
| 66 | |
| 67 | fn optional(value: &str) -> Option<String> { |
| 68 | Some(value.trim().to_owned()).filter(|value| !value.is_empty()) |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | async fn owned_repo(cx: &Cx) -> Result<RepoView> { |
| 77 | let repo = repo_for(cx).await?; |
| 78 | |
| 79 | if !repo.viewer_is_owner { |
| 80 | return Err(not_found().into()); |
| 81 | } |
| 82 | |
| 83 | Ok(repo) |
| 84 | } |
| 85 | |
| 86 | #[page("/{handle}/repos/{name}/settings")] |
| 87 | async fn repo_settings_page(cx: &Cx) -> Result { |
| 88 | let repo = owned_repo(cx).await?; |
| 89 | let saved = query_params::<Saved>(cx)?.saved.is_some(); |
| 90 | |
| 91 | view! { |
| 92 | settings_view( |
| 93 | handle: repo.handle.as_str(), |
| 94 | name: repo.name.as_str(), |
ef23868feat: rebuild the profile page on flat navigation7d | 95 | fields: Fields { |
| 96 | description: repo.description.as_deref().unwrap_or(""), |
| 97 | visibility: repo.visibility, |
| 98 | pinned: repo.pinned, |
| 99 | }, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 100 | saved: saved, |
| 101 | error: "", |
| 102 | ) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | #[page(POST "/{handle}/repos/{name}/settings")] |
| 115 | async fn save(cx: &Cx, Form(submitted): Form<SettingsForm>) -> Result { |
| 116 | let repo = owned_repo(cx).await?; |
| 117 | |
| 118 | |
| 119 | |
| 120 | let visibility = submitted |
| 121 | .visibility |
| 122 | .parse::<Visibility>() |
| 123 | .map_err(|_| topcoat::router::error::bad_request("unknown visibility"))?; |
| 124 | |
| 125 | let outcome = update_repo( |
| 126 | ¤t_actor(cx).await?, |
| 127 | &repo.handle, |
| 128 | &repo.name, |
| 129 | &RepoEdit { |
| 130 | description: optional(&submitted.description), |
| 131 | visibility, |
ef23868feat: rebuild the profile page on flat navigation7d | 132 | pinned: submitted.pinned.is_some(), |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 133 | }, |
| 134 | &orgs(cx), |
| 135 | &memberships(cx), |
| 136 | &repos(cx), |
| 137 | ) |
| 138 | .await; |
| 139 | |
| 140 | let message = match outcome { |
| 141 | Ok(_) => { |
| 142 | return view! { |
| 143 | (StatusCode::SEE_OTHER) |
| 144 | (location(&format!( |
| 145 | "/{}/repos/{}/settings?saved", |
| 146 | repo.handle, repo.name |
| 147 | ))?) |
| 148 | }; |
| 149 | } |
| 150 | |
| 151 | Err(Error::Domain(DomainError::Validation { field, reason })) => { |
| 152 | format!("That {field} is no good: {reason}.") |
| 153 | } |
| 154 | |
| 155 | Err(Error::Domain(DomainError::Forbidden | DomainError::NotFound { .. })) => { |
| 156 | return Err(not_found().into()); |
| 157 | } |
| 158 | |
| 159 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 160 | }; |
| 161 | |
| 162 | view! { |
| 163 | settings_view( |
| 164 | handle: repo.handle.as_str(), |
| 165 | name: repo.name.as_str(), |
ef23868feat: rebuild the profile page on flat navigation7d | 166 | fields: Fields { |
| 167 | description: submitted.description.as_str(), |
| 168 | visibility, |
| 169 | pinned: submitted.pinned.is_some(), |
| 170 | }, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 171 | saved: false, |
| 172 | error: message.as_str(), |
| 173 | ) |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | #[page(POST "/{handle}/repos/{name}/settings/delete")] |
| 187 | async fn delete(cx: &Cx, Form(submitted): Form<DeleteForm>) -> Result { |
| 188 | let repo = owned_repo(cx).await?; |
| 189 | |
| 190 | let confirmed = RepoName::new(&submitted.confirm).is_ok_and(|typed| typed == repo.name); |
| 191 | |
| 192 | if !confirmed { |
| 193 | let message = format!( |
| 194 | "Type {} exactly to confirm. Nothing was deleted.", |
| 195 | repo.name |
| 196 | ); |
| 197 | |
| 198 | return view! { |
| 199 | settings_view( |
| 200 | handle: repo.handle.as_str(), |
| 201 | name: repo.name.as_str(), |
ef23868feat: rebuild the profile page on flat navigation7d | 202 | fields: Fields { |
| 203 | description: repo.description.as_deref().unwrap_or(""), |
| 204 | visibility: repo.visibility, |
| 205 | pinned: repo.pinned, |
| 206 | }, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 207 | saved: false, |
| 208 | error: message.as_str(), |
| 209 | ) |
| 210 | }; |
| 211 | } |
| 212 | |
| 213 | match delete_repo( |
| 214 | ¤t_actor(cx).await?, |
| 215 | &repo.handle, |
| 216 | &repo.name, |
| 217 | &orgs(cx), |
| 218 | &memberships(cx), |
| 219 | &repos(cx), |
| 220 | &storage(cx), |
| 221 | ) |
| 222 | .await |
| 223 | { |
| 224 | Ok(()) => {} |
| 225 | Err(Error::Domain(DomainError::Forbidden | DomainError::NotFound { .. })) => { |
| 226 | return Err(not_found().into()); |
| 227 | } |
| 228 | Err(other) => return Err(server_error(std::io::Error::other(other.to_string()))), |
| 229 | } |
| 230 | |
| 231 | view! { |
| 232 | (StatusCode::SEE_OTHER) |
| 233 | (location(&format!("/{}", repo.handle))?) |
| 234 | } |
| 235 | } |
| 236 | |
ef23868feat: rebuild the profile page on flat navigation7d | 237 | |
| 238 | |
| 239 | #[derive(Debug, Clone, Copy)] |
| 240 | struct Fields<'a> { |
| 241 | description: &'a str, |
| 242 | visibility: Visibility, |
| 243 | pinned: bool, |
| 244 | } |
| 245 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | #[component] |
| 251 | async fn settings_view( |
| 252 | handle: &str, |
| 253 | name: &str, |
ef23868feat: rebuild the profile page on flat navigation7d | 254 | fields: Fields<'_>, |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 255 | saved: bool, |
| 256 | error: &str, |
| 257 | ) -> Result { |
ef23868feat: rebuild the profile page on flat navigation7d | 258 | let Fields { |
| 259 | description, |
| 260 | visibility, |
| 261 | pinned, |
| 262 | } = fields; |
| 263 | |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 264 | view! { |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 265 | narrow( |
| 266 | <h1 class="text-xl font-semibold tracking-tight">"Repository settings"</h1> |
| 267 | <p class="mt-1 font-mono text-sm text-muted-foreground"> |
| 268 | "@" (handle) " / " (name) |
| 269 | </p> |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 270 | |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 271 | if saved { |
| 272 | <div class="mt-6"> |
| 273 | flash(kind: FlashKind::Success, "Repository updated.") |
| 274 | </div> |
| 275 | } |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 276 | |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 277 | if !error.is_empty() { |
| 278 | <div class="mt-6"> |
| 279 | flash(kind: FlashKind::Error, (error)) |
ef23868feat: rebuild the profile page on flat navigation7d | 280 | </div> |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 281 | } |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 282 | |
| 283 | <form |
| 284 | method="post" |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 285 | action=(format!("/{handle}/repos/{name}/settings")) |
| 286 | class="mt-6 space-y-5" |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 287 | > |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 288 | <div class="space-y-2"> |
| 289 | label(attrs: attributes! { for="description" }, "Description") |
| 290 | textarea( |
| 291 | attrs: attributes! { |
| 292 | id="description" |
| 293 | name="description" |
| 294 | rows="2" |
| 295 | maxlength=(Repository::MAX_DESCRIPTION_LEN.to_string()) |
| 296 | placeholder="A sentence for your profile." |
| 297 | }, |
| 298 | (description) |
| 299 | ) |
| 300 | <p class="text-xs text-muted-foreground"> |
| 301 | "Optional. At most " |
| 302 | (Repository::MAX_DESCRIPTION_LEN.to_string()) " characters." |
| 303 | </p> |
| 304 | </div> |
| 305 | |
| 306 | <div class="space-y-2"> |
| 307 | label(attrs: attributes! { for="visibility" }, "Visibility") |
| 308 | select( |
| 309 | attrs: attributes! { id="visibility" name="visibility" }, |
| 310 | <option value="public" selected=(visibility.is_public())>"Public"</option> |
| 311 | <option value="private" selected=(!visibility.is_public())>"Private"</option> |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 312 | ) |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 313 | <p class="text-xs text-muted-foreground"> |
| 314 | "A private repository is hidden from your profile and needs a token " |
| 315 | "to clone." |
| 316 | </p> |
| 317 | </div> |
| 318 | |
| 319 | <div class="space-y-2"> |
| 320 | <div class="flex items-start gap-2"> |
| 321 | <input |
| 322 | id="pinned" |
| 323 | name="pinned" |
| 324 | type="checkbox" |
| 325 | value="on" |
| 326 | checked=(pinned) |
| 327 | class="mt-0.5" |
| 328 | /> |
| 329 | label(attrs: attributes! { for="pinned" }, "Lead my profile with this") |
| 330 | </div> |
| 331 | <p class="text-xs text-muted-foreground"> |
| 332 | "One repository at a time. Choosing this one lets go of whichever " |
| 333 | "was chosen before." |
| 334 | </p> |
| 335 | </div> |
| 336 | |
| 337 | <div class="flex items-center gap-3"> |
| 338 | button(attrs: attributes! { type="submit" }, "Save") |
| 339 | <a |
| 340 | href=(format!("/{handle}/repos/{name}")) |
| 341 | class="text-sm text-muted-foreground hover:text-foreground" |
| 342 | >"Back to repository"</a> |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 343 | </div> |
| 344 | </form> |
5d3dfa5feat: a global top bar, and pages choose their own width22h | 345 | |
| 346 | <section class="mt-10 rounded-lg border border-destructive/30 p-4"> |
| 347 | <h2 class="text-xs font-medium uppercase tracking-wider text-destructive"> |
| 348 | "Delete this repository" |
| 349 | </h2> |
| 350 | <p class="mt-2 text-sm text-muted-foreground"> |
| 351 | "This cannot be undone. The commits, branches and tags go with it, and " |
| 352 | "anyone with a clone keeps their copy while this instance keeps nothing." |
| 353 | </p> |
| 354 | |
| 355 | <form |
| 356 | method="post" |
| 357 | action=(format!("/{handle}/repos/{name}/settings/delete")) |
| 358 | class="mt-4 space-y-2" |
| 359 | > |
| 360 | label( |
| 361 | attrs: attributes! { for="confirm" }, |
| 362 | "Type " (name) " to confirm" |
| 363 | ) |
| 364 | input(attrs: attributes! { |
| 365 | id="confirm" |
| 366 | name="confirm" |
| 367 | type="text" |
| 368 | value="" |
| 369 | placeholder=(name) |
| 370 | required=(true) |
| 371 | autocomplete="off" |
| 372 | }) |
| 373 | <div class="pt-1"> |
| 374 | button( |
| 375 | attrs: attributes! { type="submit" }, |
| 376 | variant: ButtonVariant::Destructive, |
| 377 | "Delete repository" |
| 378 | ) |
| 379 | </div> |
| 380 | </form> |
| 381 | </section> |
| 382 | ) |
4ef3945feat: repository settings, README rendering, branch switcher, raw files7d | 383 | } |
| 384 | } |