index.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import React, { useLayoutEffect } from 'react';
  6. import { IJsonSchema, JsonSchemaTypeManager, JsonSchemaUtils } from '@flowgram.ai/json-schema';
  7. import { useCurrentScope, useRefresh } from '@flowgram.ai/editor';
  8. import { DisplaySchemaTag } from '@/components/display-schema-tag';
  9. import './styles.css';
  10. interface PropsType {
  11. value?: IJsonSchema;
  12. showIconInTree?: boolean;
  13. displayFromScope?: boolean;
  14. typeManager?: JsonSchemaTypeManager;
  15. style?: React.CSSProperties;
  16. }
  17. export function DisplayOutputs({ value, showIconInTree, displayFromScope, style }: PropsType) {
  18. const scope = useCurrentScope();
  19. const refresh = useRefresh();
  20. useLayoutEffect(() => {
  21. if (!displayFromScope || !scope) {
  22. return () => null;
  23. }
  24. const disposable = scope.output.onListOrAnyVarChange(() => {
  25. refresh();
  26. });
  27. return () => {
  28. disposable.dispose();
  29. };
  30. }, [displayFromScope]);
  31. const properties: IJsonSchema['properties'] = displayFromScope
  32. ? (scope?.output.variables || []).reduce((acm, curr) => {
  33. acm = {
  34. ...acm,
  35. ...(JsonSchemaUtils.astToSchema(curr.type)?.properties || {}),
  36. };
  37. return acm;
  38. }, {})
  39. : value?.properties || {};
  40. const childEntries = Object.entries(properties || {});
  41. return (
  42. <div className="gedit-m-display-outputs-wrapper" style={style}>
  43. {childEntries.map(([key, schema]) => (
  44. <DisplaySchemaTag
  45. key={key}
  46. title={key}
  47. value={schema}
  48. showIconInTree={showIconInTree}
  49. warning={!schema}
  50. />
  51. ))}
  52. </div>
  53. );
  54. }