Skip to content

Commit

Permalink
merge: #3085
Browse files Browse the repository at this point in the history
3085: fix(web, sdf-server): Update code_diff for new components r=stack72 a=stack72

![Screenshot 2023-12-20 at 00 59 33](https://github.com/systeminit/si/assets/227823/026a9c05-4f4a-4a22-b336-84b8fb55317f)


Co-authored-by: stack72 <[email protected]>
  • Loading branch information
si-bors-ng[bot] and stack72 authored Dec 20, 2023
2 parents b50845b + c573df8 commit 5faf9a6
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
2 changes: 2 additions & 0 deletions app/web/src/components/AssetDiffDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
v-if="selectedComponentDiff.current.code"
:code="selectedComponentDiff.current.code"
:codeLanguage="selectedComponentDiff.current.language"
:allowCopy="false"
>
<template #title>
<span class="text-lg">Current</span>
Expand All @@ -27,6 +28,7 @@
v-if="selectedComponentDiff.diffs[0]?.code"
:code="selectedComponentDiff.diffs[0]?.code"
:codeLanguage="selectedComponentDiff.diffs[0]?.language"
:allowCopy="false"
>
<template #title>
<span class="text-lg">Diff</span>
Expand Down
4 changes: 3 additions & 1 deletion app/web/src/components/CodeViewer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<div class="flex">
<SiButtonIcon
v-if="allowCopy"
tooltipText="Copy code to clipboard"
ignoreTextColor
icon="clipboard-copy"
Expand All @@ -21,7 +22,7 @@
</div>
</div>
<SiButtonIcon
v-if="!showTitle"
v-if="!showTitle && allowCopy"
tooltipText="Copy code to clipboard"
ignoreTextColor
icon="clipboard-copy"
Expand Down Expand Up @@ -89,6 +90,7 @@ const props = defineProps({
// // Format: "0.0px" or "0%"
height: { type: String },
showTitle: { type: Boolean },
allowCopy: { type: Boolean, default: true },
title: { type: String },
titleClasses: { type: String, default: "h-10" },
border: { type: Boolean, default: false },
Expand Down
46 changes: 29 additions & 17 deletions lib/dal/src/component/diff.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module contains [`ComponentDiff`].

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::component::ComponentResult;
use crate::{
Expand Down Expand Up @@ -67,7 +68,9 @@ impl ComponentDiff {
}

// Find the "diffs" given the head dal context only if the component exists on head.
let diffs: Vec<CodeView> = if Component::get_by_id(&head_ctx, &component_id)
let mut is_new_component = false;
let prev_json: String;
if Component::get_by_id(&head_ctx, &component_id)
.await?
.is_some()
{
Expand All @@ -83,28 +86,37 @@ impl ComponentDiff {
let mut prev_component_view = ComponentViewProperties::try_from(prev_component_view)?;
prev_component_view.drop_private();

let prev_json = serde_json::to_string_pretty(&prev_component_view)?;
prev_json = serde_json::to_string_pretty(&prev_component_view)?;
} else {
is_new_component = true;
prev_json = serde_json::to_string_pretty(&json!(null))?;
};

let mut lines = Vec::new();
for diff_object in diff::lines(&prev_json, &curr_json) {
let line = match diff_object {
diff::Result::Left(left) => format!("-{left}"),
diff::Result::Both(unchanged, _) => format!(" {unchanged}"),
diff::Result::Right(right) => format!("+{right}"),
};
let mut lines = Vec::new();
for diff_object in diff::lines(&prev_json, &curr_json) {
let line = match diff_object {
diff::Result::Left(left) => format!("-{left}"),
diff::Result::Both(unchanged, _) => format!(" {unchanged}"),
diff::Result::Right(right) => format!("+{right}"),
};
if line != "-null" {
lines.push(line);
}

// FIXME(nick): generate multiple code views if there are multiple code views.
let diff = CodeView::new(CodeLanguage::Diff, Some(lines.join(NEWLINE)), None);
vec![diff]
} else {
vec![]
};
}
let diff = CodeView::new(CodeLanguage::Diff, Some(lines.join(NEWLINE)), None);
let diffs: Vec<CodeView> = vec![diff];

Ok(Self {
component_id,
current: CodeView::new(CodeLanguage::Json, Some(curr_json), None),
current: CodeView::new(
CodeLanguage::Json,
if is_new_component {
Some(curr_json)
} else {
None
},
None,
),
diffs,
})
}
Expand Down

0 comments on commit 5faf9a6

Please sign in to comment.