Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dashboard cloning should put chart right after #8605

Closed
Vadman97 opened this issue May 17, 2024 — with Linear · 2 comments
Closed

dashboard cloning should put chart right after #8605

Vadman97 opened this issue May 17, 2024 — with Linear · 2 comments

Comments

Copy link
Member

No description provided.

Copy link

linear bot commented May 17, 2024

Copy link

greptile-apps bot commented May 17, 2024

To implement dashboard cloning with the chart placed right after, follow these steps:

  1. Update Dashboard.tsx:

    • Modify the handleDragEnd function to handle the new chart position after cloning.
    const handleDragEnd = (event: any) => {
        const { active, over } = event;
        if (active.id !== over.id) {
            setGraphs((graphs) => {
                if (graphs === undefined) {
                    return undefined;
                }
                const oldIndex = graphs.findIndex((g) => g.id === active.id);
                const newIndex = graphs.findIndex((g) => g.id === over.id);
                return arrayMove(graphs, oldIndex, newIndex + 1); // Adjust the newIndex to place right after
            });
        }
    };
  2. Update CreateDashboardModal.tsx:

    • Ensure the new dashboard is created with the correct layout.
    const onCreateNewDashboard = () => {
        if (!newDashboard) return;
        updateDashboard({
            name: newDashboard,
            metrics: [],
            layout: JSON.stringify(DEFAULT_METRICS_LAYOUT),
        }).then((r) => {
            const newId = r.data?.upsertDashboard || '';
            navigate(`/${project_id}/dashboards/${newId}`);
        });
    };
  3. Update GraphingEditor.tsx:

    • Adjust the onSave function to ensure the new graph is placed right after the cloned one.
    const onSave = () => {
        let display: string | undefined;
        let nullHandling: string | undefined;
        switch (viewType) {
            case 'Line chart':
                display = lineDisplay;
                nullHandling = lineNullHandling;
                break;
            case 'Bar chart':
                display = barDisplay;
                break;
            case 'Table':
                nullHandling = tableNullHandling;
                break;
        }
        const graphInput: GraphInput = {
            visualizationId: dashboard_id!,
            bucketByKey: bucketByEnabled ? bucketByKey : null,
            bucketCount: bucketByEnabled ? bucketCount : null,
            display,
            functionType,
            groupByKey: groupByEnabled ? groupByKey : null,
            limit: groupByEnabled ? limit : null,
            limitFunctionType: groupByEnabled ? limitFunctionType : null,
            limitMetric: groupByEnabled ? limitMetric : null,
            metric,
            nullHandling,
            productType,
            query: debouncedQuery,
            title: metricViewTitle,
            type: viewType,
        };
        if (isEdit) {
            graphInput.id = graph_id;
        }
        upsertGraph({
            variables: {
                graph: graphInput,
            },
            optimisticResponse: {
                upsertGraph: {
                    ...graphInput,
                    id: graphInput.id ?? `temp-${tempId}`,
                    __typename: 'Graph',
                },
            },
            update(cache, result) {
                if (isEdit) {
                    return;
                }
                const vizId = cache.identify({
                    id: dashboard_id,
                    __typename: 'Visualization',
                });
                const graphId = cache.identify({
                    id: result.data?.upsertGraph.id,
                    __typename: 'Graph',
                });
                cache.modify({
                    id: vizId,
                    fields: {
                        graphs(existing: any[] = []) {
                            return existing.concat([{ __ref: graphId }]);
                        },
                    },
                });
            },
        })
            .then(() => {
                message.success(`Metric view ${isEdit ? 'updated' : 'created'}`);
            })
            .catch(() => {
                message.error('Failed to create metric view');
            });
        navigate({
            pathname: `../${dashboard_id}`,
            search: location.search,
        });
    };

References

/frontend/src/pages/Graphing/Dashboard.tsx
/frontend/src/pages/Dashboards/components/CreateDashboardModal/CreateDashboardModal.tsx
/frontend/src/pages/Graphing/GraphingEditor.tsx
/frontend/src/pages/Dashboards/pages/Dashboard/DashboardPage.tsx
/frontend/src/pages/Dashboards/DashboardsRouter.tsx

Ask Greptile

@linear linear bot closed this as completed Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant