tag-tabs.tsx


Overview

The tag-tabs.tsx file defines a React functional component named TagTabs that provides a user interface element for switching between two views of tag data: a tag cloud and a tag table. It leverages the Segmented control from the Ant Design (antd) library to present tab-like segmented buttons. The component dynamically renders either a TagWordCloud or TagTable component depending on the user's selection.

This component is designed to be part of a knowledge configuration or tag visualization module, supporting internationalization via react-i18next. It manages its own internal state to track the selected tab and updates the displayed component accordingly.


Detailed Explanation

Enum: TagType

enum TagType {
  Cloud = 'cloud',
  Table = 'table',
}

Constant: TagContentMap

const TagContentMap = {
  [TagType.Cloud]: <TagWordCloud></TagWordCloud>,
  [TagType.Table]: <TagTable></TagTable>,
};

Component: TagTabs

export function TagTabs() {
  const [value, setValue] = useState<TagType>(TagType.Cloud);
  const { t } = useTranslation();

  const options: SegmentedLabeledOption[] = [TagType.Cloud, TagType.Table].map(
    (x) => ({
      label: t(`knowledgeConfiguration.tag${upperFirst(x)}`),
      value: x,
    }),
  );

  return (
    <section className="mt-4">
      <Segmented
        value={value}
        options={options}
        onChange={(val) => setValue(val as TagType)}
      />
      {TagContentMap[value]}
    </section>
  );
}

Description

Parameters

State

Return Value

Usage Example

import { TagTabs } from './tag-tabs';

function App() {
  return (
    <div>
      <h1>Tag Visualization</h1>
      <TagTabs />
    </div>
  );
}

Important Implementation Details


Interaction with Other Parts of the System


Mermaid Component Diagram

componentDiagram
    TagTabs <|-- Antd_Segmented : uses
    TagTabs --> TagWordCloud : renders conditionally
    TagTabs --> TagTable : renders conditionally
    TagTabs ..> react-i18next : uses for translation

Summary

tag-tabs.tsx is a concise, well-structured component implementing a tabbed interface for switching between two tag visualization components. It leverages modern React hooks, a popular UI framework, and internationalization, making it highly reusable and adaptable in a multilingual application context. This file acts as a controller of view state for tag display, delegating actual rendering to TagWordCloud and TagTable.