Skip to content

Commit

Permalink
Merge pull request #1706 from visualize-admin/feat/tab-labels
Browse files Browse the repository at this point in the history
feat: Add chart and dashboard labels
  • Loading branch information
bprusinowski authored Sep 3, 2024
2 parents b6e3d81 + 6e5c257 commit d989964
Show file tree
Hide file tree
Showing 23 changed files with 288 additions and 29 deletions.
6 changes: 6 additions & 0 deletions app/charts/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ describe("chart type switch", () => {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
fields: {
x: {
Expand Down
6 changes: 6 additions & 0 deletions app/charts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ export const META: Meta = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
};

type GetInitialConfigOptions = {
Expand Down
56 changes: 34 additions & 22 deletions app/components/chart-selection-tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { ChartTypeSelector } from "@/configurator/components/chart-type-selector
import { getIconName } from "@/configurator/components/ui-helpers";
import { useFlag } from "@/flags";
import { Icon, IconName } from "@/icons";
import { defaultLocale, useLocale } from "@/locales";
import { useLocale } from "@/locales";
import useEvent from "@/utils/use-event";

type TabsState = {
Expand Down Expand Up @@ -92,19 +92,16 @@ export const ChartSelectionTabs = () => {
}

const chartConfig = getChartConfig(state);
const data: TabDatum[] = state.chartConfigs.map((d) => {
return {
key: d.key,
chartType: d.chartType,
active: d.key === chartConfig.key,
label:
d.meta.title[locale] !== ""
? d.meta.title[locale]
: d.meta.title[defaultLocale] !== ""
? d.meta.title[defaultLocale]
: undefined,
};
});
const data: TabDatum[] = state.chartConfigs.map(
({ key, chartType, meta }) => {
return {
key,
chartType,
active: key === chartConfig.key,
label: meta.label[locale] !== "" ? meta.label[locale] : undefined,
};
}
);

return (
<TabsStateProvider>
Expand Down Expand Up @@ -620,6 +617,12 @@ const TabContent = (props: {
onSwitchClick,
} = props;
const classes = useIconStyles({ active, dragging });
const [_, dispatch] = useConfiguratorState();
const showAddLabel = editable && !label;
const addLabel = t({
id: "chart-selection-tabs.add-label",
message: "Add label",
});
return (
<Flex className={classes.root}>
<Button
Expand All @@ -629,24 +632,33 @@ const TabContent = (props: {
>
<Icon name={iconName} />
</Button>
{label ? (
<Tooltip title={label} enterDelay={750}>
{label || showAddLabel ? (
<Tooltip title={label || addLabel} enterDelay={750}>
<Button
variant="text"
color="primary"
className={classes.label}
onClick={onSwitchClick}
onClick={() => {
onSwitchClick?.();
if (editable) {
dispatch({
type: "CHART_ACTIVE_FIELD_CHANGED",
value: "label",
});
}
}}
sx={{
color: (t) =>
label ? "inherit" : `${t.palette.grey[500]} !important`,
}}
>
{label}
{label || `[ ${addLabel} ]`}
</Button>
</Tooltip>
) : null}
{editable && (
<Button
variant="text"
onClick={(e) => {
onChevronDownClick?.(e, chartKey);
}}
onClick={(e) => onChevronDownClick?.(e, chartKey)}
className={classes.editIconWrapper}
>
<Icon name="chevronDown" />
Expand Down
15 changes: 13 additions & 2 deletions app/config-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export const extractSingleFilters = (filters: Filters): SingleFilters => {
) as SingleFilters;
};

// Meta
const Title = t.type({
de: t.string,
fr: t.string,
Expand All @@ -126,7 +125,19 @@ const Description = t.type({
it: t.string,
en: t.string,
});
const Meta = t.type({ title: Title, description: Description });
export type Description = t.TypeOf<typeof Description>;
const Label = t.type({
de: t.string,
fr: t.string,
it: t.string,
en: t.string,
});
export type Label = t.TypeOf<typeof Label>;
const Meta = t.type({
title: Title,
description: Description,
label: Label,
});
export type Meta = t.TypeOf<typeof Meta>;
export type MetaKey = keyof Meta;

Expand Down
12 changes: 12 additions & 0 deletions app/configurator/components/add-dataset-dialog.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export const photovoltaikChartStateMock: ConfiguratorStateConfiguringChart = {
fr: "",
it: "",
},
label: {
de: "",
en: "",
fr: "",
it: "",
},
},
},
chartConfigs: [
Expand All @@ -43,6 +49,12 @@ export const photovoltaikChartStateMock: ConfiguratorStateConfiguringChart = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down
2 changes: 1 addition & 1 deletion app/configurator/components/annotation-options.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const AnnotationOptions = (props: AnnotationOptionsProps) => {
tabIndex={-1}
sx={{ overflowX: "hidden", overflowY: "auto" }}
>
<ControlSection>
<ControlSection hideTopBorder>
<SectionTitle>{getFieldLabel(activeField)}</SectionTitle>
<ControlSectionContent gap="none">
{orderedLocales.map((locale) => (
Expand Down
5 changes: 3 additions & 2 deletions app/configurator/components/configurator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
hasChartConfigs,
initChartStateFromChartEdit,
isConfiguring,
MetaKey,
saveChartLocally,
useConfiguratorState,
} from "@/configurator";
Expand Down Expand Up @@ -115,8 +116,8 @@ export const BackButton = ({

export const isAnnotationField = (
field: string | undefined
): field is "title" | "description" => {
return field === "title" || field === "description";
): field is MetaKey => {
return field === "title" || field === "description" || field === "label";
};

const useAssureCorrectDataSource = (stateGuard: ConfiguratorState["state"]) => {
Expand Down
3 changes: 3 additions & 0 deletions app/configurator/components/field-i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const fieldLabels = {
id: "controls.description",
message: "Description",
}),
"controls.label": defineMessage({ id: "controls.label", message: "Label" }),
"controls.column.stacked": defineMessage({
id: "controls.column.stacked",
message: "Stacked",
Expand Down Expand Up @@ -262,6 +263,8 @@ export function getFieldLabel(field: string): string {
return i18n._(fieldLabels["controls.title"]);
case "description":
return i18n._(fieldLabels["controls.description"]);
case "label":
return i18n._(fieldLabels["controls.label"]);

// Encoding Options
case "stacked":
Expand Down
6 changes: 6 additions & 0 deletions app/configurator/configurator-state/initial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export const getInitialConfiguringConfigBasedOnCube = (props: {
fr: "",
it: "",
},
label: {
de: "",
en: "",
fr: "",
it: "",
},
},
activeField: undefined,
},
Expand Down
14 changes: 14 additions & 0 deletions app/configurator/configurator-state/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ export const configStateMock = {
fr: "",
it: "",
},
label: {
de: "",
en: "",
fr: "",
it: "",
},
},
},
chartConfigs: [
Expand All @@ -118,6 +124,12 @@ export const configStateMock = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down Expand Up @@ -1116,6 +1128,7 @@ export const configJoinedCubes: Partial<
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
cubes: [
{
Expand Down Expand Up @@ -1371,6 +1384,7 @@ export const configJoinedCubes: Partial<
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
cubes: [
{
Expand Down
6 changes: 6 additions & 0 deletions app/configurator/configurator-state/reducer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,12 @@ describe("deriveFiltersFromFields", () => {
"fr": "",
"it": "",
},
"label": Object {
"de": "",
"en": "",
"fr": "",
"it": "",
},
"title": Object {
"de": "",
"en": "",
Expand Down
2 changes: 2 additions & 0 deletions app/docs/charts.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const ColumnsStory = {
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
activeField: undefined,
},
Expand Down Expand Up @@ -126,6 +127,7 @@ const ScatterplotStory = {
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
activeField: undefined,
},
Expand Down
6 changes: 6 additions & 0 deletions app/docs/columns.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export const chartConfig: ColumnConfig = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down
13 changes: 13 additions & 0 deletions app/docs/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const states: ConfiguratorState[] = [
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
activeField: undefined,
},
Expand All @@ -43,6 +44,12 @@ export const states: ConfiguratorState[] = [
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down Expand Up @@ -893,6 +900,12 @@ export const tableConfig: TableConfig = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down
6 changes: 6 additions & 0 deletions app/docs/lines.mock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ export const chartConfig: LineConfig = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down
1 change: 1 addition & 0 deletions app/docs/lines.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const LineChartStory = () => (
meta: {
title: { en: "", de: "", fr: "", it: "" },
description: { en: "", de: "", fr: "", it: "" },
label: { en: "", de: "", fr: "", it: "" },
},
activeField: undefined,
},
Expand Down
6 changes: 6 additions & 0 deletions app/docs/scatterplot.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export const chartConfig: ScatterPlotConfig = {
fr: "",
it: "",
},
label: {
en: "",
de: "",
fr: "",
it: "",
},
},
cubes: [
{
Expand Down
Loading

0 comments on commit d989964

Please sign in to comment.