app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

text.rs (6841B)


      1 use gpui::SharedString;
      2 use radroots_app_core::{AppRuntimeMode, AppRuntimeSnapshot, runtime_mode_label};
      3 use radroots_app_i18n::{AppTextKey, app_text};
      4 
      5 use crate::LabelValueRow;
      6 
      7 #[derive(Clone, Copy, Debug, Eq, PartialEq)]
      8 pub struct SettingsPreferencesGeneralRowState {
      9     pub allow_relay_connections: bool,
     10     pub use_media_servers: bool,
     11     pub use_nip05: bool,
     12     pub launch_at_login: bool,
     13 }
     14 
     15 pub fn app_shared_text(key: AppTextKey) -> SharedString {
     16     app_text(key).into()
     17 }
     18 
     19 pub fn app_shared_label_text(key: AppTextKey) -> SharedString {
     20     format!("{}:", app_text(key)).into()
     21 }
     22 
     23 pub fn runtime_metadata_rows(snapshot: &AppRuntimeSnapshot) -> Vec<LabelValueRow> {
     24     vec![
     25         metadata_row(
     26             AppTextKey::MetadataCorePackage,
     27             snapshot.core.package_name.clone(),
     28         ),
     29         metadata_row(
     30             AppTextKey::MetadataCoreVersion,
     31             snapshot.core.package_version.clone(),
     32         ),
     33         metadata_row(
     34             AppTextKey::MetadataCoreAuthors,
     35             snapshot.core.package_authors.clone(),
     36         ),
     37         metadata_row(
     38             AppTextKey::MetadataRustEdition,
     39             snapshot.core.rust_edition.clone(),
     40         ),
     41         metadata_row(
     42             AppTextKey::MetadataRustToolchain,
     43             snapshot.core.rust_toolchain.clone(),
     44         ),
     45         metadata_row(
     46             AppTextKey::MetadataTargetTriple,
     47             snapshot.build.target_triple.clone(),
     48         ),
     49         metadata_row(
     50             AppTextKey::MetadataBuildProfile,
     51             snapshot.build.build_profile.clone(),
     52         ),
     53         metadata_row(
     54             AppTextKey::MetadataProjection,
     55             snapshot.build.projection_source.clone(),
     56         ),
     57         metadata_row(
     58             AppTextKey::MetadataGitCommit,
     59             snapshot
     60                 .build
     61                 .git_commit
     62                 .clone()
     63                 .unwrap_or_else(|| app_text(AppTextKey::ValueNone)),
     64         ),
     65         metadata_row(AppTextKey::MetadataAppName, snapshot.host.app_name.clone()),
     66         metadata_row(
     67             AppTextKey::MetadataAppId,
     68             snapshot.host.app_identifier.clone(),
     69         ),
     70         metadata_row(
     71             AppTextKey::MetadataAppVersion,
     72             snapshot.host.app_version.clone(),
     73         ),
     74         metadata_row(
     75             AppTextKey::MetadataAppBuild,
     76             snapshot.host.app_build.clone(),
     77         ),
     78         metadata_row(
     79             AppTextKey::MetadataPlatform,
     80             snapshot.host.platform_name.clone(),
     81         ),
     82         metadata_row(
     83             AppTextKey::MetadataOperatingSystem,
     84             snapshot.host.operating_system.clone(),
     85         ),
     86         metadata_row(
     87             AppTextKey::MetadataHostLocale,
     88             snapshot.host.host_locale.clone(),
     89         ),
     90         metadata_row(
     91             AppTextKey::MetadataRuntimeOrigin,
     92             snapshot.host.runtime_origin.clone(),
     93         ),
     94         metadata_row(
     95             AppTextKey::MetadataRuntimeMode,
     96             runtime_mode_text(&snapshot.runtime_mode),
     97         ),
     98         metadata_row(AppTextKey::MetadataRunId, snapshot.run_id.clone()),
     99     ]
    100 }
    101 
    102 pub fn settings_preferences_general_rows(
    103     state: SettingsPreferencesGeneralRowState,
    104 ) -> Vec<LabelValueRow> {
    105     vec![
    106         text_row(
    107             AppTextKey::SettingsGeneralAllowRelayConnections,
    108             enabled_value_key(state.allow_relay_connections),
    109         ),
    110         text_row(
    111             AppTextKey::SettingsGeneralUseMediaServers,
    112             enabled_value_key(state.use_media_servers),
    113         ),
    114         text_row(
    115             AppTextKey::SettingsGeneralUseNip05,
    116             enabled_value_key(state.use_nip05),
    117         ),
    118         text_row(
    119             AppTextKey::SettingsGeneralLaunchAtLogin,
    120             enabled_value_key(state.launch_at_login),
    121         ),
    122     ]
    123 }
    124 
    125 fn metadata_row(label: AppTextKey, value: impl Into<String>) -> LabelValueRow {
    126     LabelValueRow::new(app_shared_text(label), value.into())
    127 }
    128 
    129 fn text_row(label: AppTextKey, value: AppTextKey) -> LabelValueRow {
    130     metadata_row(label, app_text(value))
    131 }
    132 
    133 fn enabled_value_key(enabled: bool) -> AppTextKey {
    134     if enabled {
    135         AppTextKey::ValueEnabled
    136     } else {
    137         AppTextKey::ValueDisabled
    138     }
    139 }
    140 
    141 fn runtime_mode_text(mode: &AppRuntimeMode) -> String {
    142     match mode {
    143         AppRuntimeMode::Production => app_text(AppTextKey::ValueRuntimeModeProduction),
    144         _ => runtime_mode_label(mode).to_owned(),
    145     }
    146 }
    147 
    148 #[cfg(test)]
    149 mod tests {
    150     use radroots_app_core::{
    151         APP_PROJECTION_SOURCE, AppBuildIdentity, AppRuntimeCapture, AppRuntimeMode,
    152         AppRuntimeSnapshot,
    153     };
    154     use radroots_app_i18n::{AppTextKey, app_text};
    155 
    156     use super::{
    157         SettingsPreferencesGeneralRowState, runtime_metadata_rows,
    158         settings_preferences_general_rows,
    159     };
    160 
    161     #[test]
    162     fn runtime_metadata_rows_use_localized_labels() {
    163         let snapshot = AppRuntimeSnapshot::from_capture(
    164             AppBuildIdentity {
    165                 package_name: "radroots_app".to_owned(),
    166                 package_version: "0.1.0".to_owned(),
    167                 build_profile: "debug".to_owned(),
    168                 target_triple: "aarch64-apple-darwin".to_owned(),
    169                 projection_source: APP_PROJECTION_SOURCE.to_owned(),
    170                 git_commit: None,
    171             },
    172             AppRuntimeMode::Development,
    173             AppRuntimeCapture {
    174                 host_locale: "en_US.UTF-8".to_owned(),
    175                 operating_system: "macos".to_owned(),
    176                 run_id: "run-development-123-pid456".to_owned(),
    177             },
    178         );
    179 
    180         let rows = runtime_metadata_rows(&snapshot);
    181 
    182         assert!(
    183             rows.iter()
    184                 .any(|row| row.label == "runtime mode" && row.value == "development")
    185         );
    186         assert!(
    187             rows.iter()
    188                 .any(|row| row.label == "app id" && row.value == "org.radroots.app")
    189         );
    190     }
    191 
    192     #[test]
    193     fn settings_preferences_rows_use_localized_copy() {
    194         let general_rows = settings_preferences_general_rows(SettingsPreferencesGeneralRowState {
    195             allow_relay_connections: false,
    196             use_media_servers: true,
    197             use_nip05: false,
    198             launch_at_login: true,
    199         });
    200 
    201         let allow_relay_label = app_text(AppTextKey::SettingsGeneralAllowRelayConnections);
    202         let enabled_value = app_text(AppTextKey::ValueEnabled);
    203         let disabled_value = app_text(AppTextKey::ValueDisabled);
    204 
    205         assert!(
    206             general_rows
    207                 .iter()
    208                 .any(|row| row.label == allow_relay_label && row.value == disabled_value)
    209         );
    210         assert!(general_rows.iter().any(|row| {
    211             row.label == app_text(AppTextKey::SettingsGeneralLaunchAtLogin)
    212                 && row.value == enabled_value
    213         }));
    214     }
    215 }