Skip to content

Commit

Permalink
Merge pull request #4125 from systeminit/victor/eng-2597-hydrate-canc…
Browse files Browse the repository at this point in the history
…ontribute-flag

feat: populate canContribute for schema variants
  • Loading branch information
vbustamante authored Jul 10, 2024
2 parents 357dbb7 + 73467b0 commit 220a1ef
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
5 changes: 0 additions & 5 deletions app/web/src/store/asset.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ export const useAssetStore = () => {
},
onSuccess: (variant) => {
const v = variant as SchemaVariantListEntry;
v.canContribute = false;
v.canUpdate = false;
const savedAssetIdx = this.variantList.findIndex(
(a) => a.schemaVariantId === variant.schemaVariantId,
Expand Down Expand Up @@ -436,7 +435,6 @@ export const useAssetStore = () => {
onSuccess: (response) => {
this.variantList = response.map((v) => {
const e = v as SchemaVariantListEntry;
e.canContribute = false;
e.canUpdate = false;
return e;
});
Expand All @@ -462,7 +460,6 @@ export const useAssetStore = () => {
},
onSuccess: (variant) => {
const v = variant as SchemaVariantListEntry;
v.canContribute = false;
v.canUpdate = false;
const savedAssetIdx = this.variantList.findIndex(
(a) => a.schemaVariantId === variant.schemaVariantId,
Expand Down Expand Up @@ -499,7 +496,6 @@ export const useAssetStore = () => {
callback: (variant, metadata) => {
if (metadata.change_set_id !== changeSetId) return;
const v = variant as SchemaVariantListEntry;
v.canContribute = false;
v.canUpdate = false;

const savedAssetIdx = this.variantList.findIndex(
Expand Down Expand Up @@ -569,7 +565,6 @@ export const useAssetStore = () => {
callback: (variant, metadata) => {
if (metadata.change_set_id !== changeSetId) return;
const v = variant as SchemaVariantListEntry;
v.canContribute = false;
v.canUpdate = false;
const savedAssetIdx = this.variantList.findIndex(
(a) => a.schemaVariantId === variant.schemaVariantId,
Expand Down
10 changes: 6 additions & 4 deletions lib/dal/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::workspace_snapshot::node_weight::category_node_weight::CategoryNodeKi
use crate::workspace_snapshot::node_weight::{NodeWeight, NodeWeightError};
use crate::workspace_snapshot::WorkspaceSnapshotError;
use crate::{
pk, ChangeSetError, DalContext, Func, FuncError, Schema, SchemaError, SchemaId, SchemaVariant,
pk, ChangeSetError, DalContext, Func, FuncError, Schema, SchemaError, SchemaVariant,
SchemaVariantError, Timestamp, TransactionsError,
};

Expand Down Expand Up @@ -240,14 +240,16 @@ impl Module {
Self::find(ctx, |module| module.schema_id() == Some(module_schema_id)).await
}

pub async fn find_for_schema_id(
/// Find [Module](Self) based on the id of an entity that it contains. May return [None](None) if
/// entity is not linked to a [Module](Self)
pub async fn find_for_member_id(
ctx: &DalContext,
schema_id: SchemaId,
id: impl Into<Ulid>,
) -> ModuleResult<Option<Self>> {
let workspace_snapshot = ctx.workspace_snapshot()?;

for source_idx in workspace_snapshot
.incoming_sources_for_edge_weight_kind(schema_id, EdgeWeightKindDiscriminants::Use)
.incoming_sources_for_edge_weight_kind(id, EdgeWeightKindDiscriminants::Use)
.await?
{
let node_weight = workspace_snapshot.get_node_weight(source_idx).await?;
Expand Down
38 changes: 32 additions & 6 deletions lib/dal/src/schema/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod value_from;
pub mod authoring;

use crate::action::prototype::{ActionKind, ActionPrototype};
use crate::module::Module;
pub use json::SchemaVariantJson;
pub use json::SchemaVariantMetadataJson;
pub use metadata_view::SchemaVariantMetadataView;
Expand Down Expand Up @@ -116,6 +117,8 @@ pub enum SchemaVariantError {
LeafMapPropNotFound(PropId),
#[error("schema variant missing asset func id; schema_variant_id={0}")]
MissingAssetFuncId(SchemaVariantId),
#[error("module error: {0}")]
Module(String),
#[error("more than one schema found for schema variant: {0}")]
MoreThanOneSchemaFound(SchemaVariantId),
#[error("node weight error: {0}")]
Expand Down Expand Up @@ -215,9 +218,8 @@ impl SchemaVariant {
.ok_or(SchemaVariantError::MissingAssetFuncId(self.id()))?
.into();

let (output_sockets, input_sockets) =
SchemaVariant::list_all_sockets(ctx, self.id()).await?;
let func_ids: Vec<_> = SchemaVariant::all_func_ids(ctx, self.id())
let (output_sockets, input_sockets) = Self::list_all_sockets(ctx, self.id()).await?;
let func_ids: Vec<_> = Self::all_func_ids(ctx, self.id())
.await?
.into_iter()
.map(|func_id| func_id.into())
Expand All @@ -233,6 +235,8 @@ impl SchemaVariant {
front_end_props.push(new_prop);
}

let can_contribute = Self::can_be_contributed_by_id(ctx, self.id).await?;

Ok(frontend_types::SchemaVariant {
schema_id: schema_id.into(),
schema_name: schema.name().to_owned(),
Expand All @@ -258,6 +262,7 @@ impl SchemaVariant {
timestamp: self.timestamp.into(),
props: front_end_props,
can_create_new_components: is_default || !self.is_locked,
can_contribute,
})
}
}
Expand Down Expand Up @@ -732,6 +737,20 @@ impl SchemaVariant {
}
}

/// A schema variant can be contributed if it is default, locked and does not belong to
/// a module, which means it has been updated locally
pub async fn can_be_contributed_by_id(
ctx: &DalContext,
id: SchemaVariantId,
) -> SchemaVariantResult<bool> {
Ok(Self::is_default_by_id(ctx, id).await?
&& Self::is_locked_by_id(ctx, id).await?
&& Module::find_for_member_id(ctx, id)
.await
.map_err(|e| SchemaVariantError::Module(e.to_string()))?
.is_none())
}

pub async fn get_latest_for_schema(
ctx: &DalContext,
schema_id: SchemaId,
Expand Down Expand Up @@ -865,10 +884,17 @@ impl SchemaVariant {
self.is_locked
}

pub async fn is_default(&self, ctx: &DalContext) -> SchemaVariantResult<bool> {
let schema_id = Self::schema_id_for_schema_variant_id(ctx, self.id).await?;
pub async fn is_default_by_id(
ctx: &DalContext,
id: SchemaVariantId,
) -> SchemaVariantResult<bool> {
let schema_id = Self::schema_id_for_schema_variant_id(ctx, id).await?;

Ok(Self::get_default_id_for_schema(ctx, schema_id).await? == self.id)
Ok(Self::get_default_id_for_schema(ctx, schema_id).await? == id)
}

pub async fn is_default(&self, ctx: &DalContext) -> SchemaVariantResult<bool> {
Self::is_default_by_id(ctx, self.id).await
}

pub async fn get_asset_func(&self, ctx: &DalContext) -> SchemaVariantResult<Func> {
Expand Down
2 changes: 1 addition & 1 deletion lib/sdf-server/src/server/service/module/export_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub async fn export_module(
let (based_on_hash, module_schema_id) = if schema_ids.len() == 1 {
match schema_ids.first().copied() {
None => (None, None),
Some(schema_id) => match Module::find_for_schema_id(&ctx, schema_id).await? {
Some(schema_id) => match Module::find_for_member_id(&ctx, schema_id).await? {
Some(module) => (Some(module.root_hash().to_string()), module.schema_id()),
None => (None, None),
},
Expand Down
2 changes: 1 addition & 1 deletion lib/sdf-server/src/server/service/v2/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use axum::{
routing::get,
Json, Router,
};
use dal::{ChangeSetId, Schema, SchemaVariant, SchemaVariantId, WorkspacePk};
use thiserror::Error;

use dal::{ChangeSetId, Schema, SchemaVariant, SchemaVariantId, WorkspacePk};
use si_frontend_types as frontend_types;

use crate::{
Expand Down
1 change: 1 addition & 0 deletions lib/si-frontend-types-rs/src/schema_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct SchemaVariant {
#[serde(flatten)]
pub timestamp: Timestamp,
pub can_create_new_components: bool, // if yes, show in modeling screen, if not, only show in customize
pub can_contribute: bool,
}

#[remain::sorted]
Expand Down

0 comments on commit 220a1ef

Please sign in to comment.