|
| 1 | +import React from "react"; |
| 2 | +import { Table } from "antd"; |
| 3 | +import { inject, observer } from "mobx-react"; |
| 4 | +import { flow, types } from "mobx-state-tree"; |
| 5 | +import Papa from "papaparse"; |
| 6 | + |
| 7 | +import { errorBuilder } from "../../core/DataValidator/ConfigValidator"; |
| 8 | +import Registry from "../../core/Registry"; |
| 9 | +import { AnnotationMixin } from "../../mixins/AnnotationMixin"; |
| 10 | +import ProcessAttrsMixin from "../../mixins/ProcessAttrs"; |
| 11 | +import Base from "./Base"; |
| 12 | +import { parseTypeAndOption, parseValue } from "../../utils/data"; |
| 13 | +import messages from "../../utils/messages"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Use the Table tag to display object keys and values in a table. |
| 17 | + * @example |
| 18 | + * <!-- Basic labeling configuration for text in a table --> |
| 19 | + * <View> |
| 20 | + * <Table name="text-1" value="$text"></Table> |
| 21 | + * </View> |
| 22 | + * @name Table |
| 23 | + * @meta_title Table Tag to Display Keys & Values in Tables |
| 24 | + * @meta_description Customize Label Studio by displaying key-value pairs in tasks for machine learning and data science projects. |
| 25 | + * @param {string} valuetype Value to define the data type in Table |
| 26 | + * @param {string} value Data field value containing JSON type for Table |
| 27 | + */ |
| 28 | +const Model = types |
| 29 | + .model({ |
| 30 | + name: types.identifier, |
| 31 | + type: "table", |
| 32 | + value: types.maybeNull(types.string), |
| 33 | + _value: types.frozen([]), |
| 34 | + valuetype: types.optional(types.string, "json"), |
| 35 | + }) |
| 36 | + .views(self => ({ |
| 37 | + get dataSource() { |
| 38 | + const { type } = parseTypeAndOption(self.valuetype); |
| 39 | + |
| 40 | + if (type === "json") { |
| 41 | + return Object.keys(self._value).map(k => { |
| 42 | + let val = self._value[k]; |
| 43 | + |
| 44 | + if (typeof val === "object") val = JSON.stringify(val); |
| 45 | + return { type: k, value: val }; |
| 46 | + }); |
| 47 | + } else { |
| 48 | + return self._value; |
| 49 | + } |
| 50 | + }, |
| 51 | + get columns() { |
| 52 | + if (self.valuetype === "json" || !self._value[0]) { |
| 53 | + return [ |
| 54 | + { title: "Name", dataIndex: "type" }, |
| 55 | + { title: "Value", dataIndex: "value" }, |
| 56 | + ]; |
| 57 | + } else { |
| 58 | + return Object.keys(self._value[0]).map(value => ({ title: value, dataIndex: value })); |
| 59 | + } |
| 60 | + }, |
| 61 | + })) |
| 62 | + .actions(self => ({ |
| 63 | + updateValue: flow(function*(store) { |
| 64 | + const { type, options } = parseTypeAndOption(self.valuetype); |
| 65 | + let originData = parseValue(self.value, store.task.dataObj); |
| 66 | + |
| 67 | + if (options.url) { |
| 68 | + try { |
| 69 | + const response = yield fetch(originData); |
| 70 | + const { ok, status, statusText } = response; |
| 71 | + |
| 72 | + if (!ok) throw new Error(`${status} ${statusText}`); |
| 73 | + |
| 74 | + originData = yield response.text(); |
| 75 | + } catch (error) { |
| 76 | + const message = messages.ERR_LOADING_HTTP({ attr: self.value, error: String(error), url: originData }); |
| 77 | + |
| 78 | + self.annotationStore.addErrors([errorBuilder.generalError(message)]); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + switch (type) { |
| 83 | + case "csv": |
| 84 | + { |
| 85 | + Papa.parse(originData, { |
| 86 | + delimiter: options.separator, |
| 87 | + header: !options.headless, |
| 88 | + download: false, |
| 89 | + complete: ({ data }) => { |
| 90 | + self._value = data; |
| 91 | + }, |
| 92 | + }); |
| 93 | + } |
| 94 | + break; |
| 95 | + default: |
| 96 | + self._value = typeof originData === "string" ? JSON.parse(originData) : originData; |
| 97 | + break; |
| 98 | + } |
| 99 | + }), |
| 100 | + })); |
| 101 | + |
| 102 | +const TableModel = types.compose("TableModel", Base, ProcessAttrsMixin, AnnotationMixin, Model); |
| 103 | + |
| 104 | +const HtxTable = inject("store")( |
| 105 | + observer(({ item }) => { |
| 106 | + return <Table bordered dataSource={item.dataSource} columns={item.columns} pagination={{ hideOnSinglePage: true }} />; |
| 107 | + }), |
| 108 | +); |
| 109 | + |
| 110 | +Registry.addTag("table", TableModel, HtxTable); |
| 111 | +Registry.addObjectType(TableModel); |
| 112 | + |
| 113 | +export { HtxTable, TableModel }; |
0 commit comments