Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3fd779d
Add time switch
kowalczyk-krzysztof Apr 12, 2025
09af89f
Change time in locator
kowalczyk-krzysztof Apr 12, 2025
df4c0f7
Improve FormattedMessage
kowalczyk-krzysztof Apr 12, 2025
e14115b
[CI] Auto-commit changed files from 'node scripts/notice'
kibanamachine Apr 14, 2025
7241d72
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 14, 2025
eed8aa6
Fix stale params
kowalczyk-krzysztof Apr 14, 2025
95e57df
Simplify condition
kowalczyk-krzysztof Apr 15, 2025
6692c2f
Use i18n components for date formatting
kowalczyk-krzysztof Apr 15, 2025
84cbe37
Update relative time handling
kowalczyk-krzysztof Apr 15, 2025
449b785
Code cleanup
kowalczyk-krzysztof Apr 15, 2025
b140410
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 15, 2025
b2dc797
Code cleanup
kowalczyk-krzysztof Apr 15, 2025
bae1a1c
Add unit tests
kowalczyk-krzysztof Apr 16, 2025
8e65219
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 16, 2025
0255f73
Move time utils to lib
kowalczyk-krzysztof Apr 16, 2025
d75ebab
Add Dashboard shareableUrlLocatorParams
kowalczyk-krzysztof Apr 16, 2025
d3797f3
Handle now edge case
kowalczyk-krzysztof Apr 16, 2025
fe1c658
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 17, 2025
9f04ed0
Add component unit tests
kowalczyk-krzysztof Apr 18, 2025
62cbd7f
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 22, 2025
f4395ac
dashboard locator
kowalczyk-krzysztof Apr 23, 2025
ef41169
Design update
kowalczyk-krzysztof Apr 23, 2025
83e410c
Change tests to RTL
kowalczyk-krzysztof Apr 23, 2025
828d006
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 24, 2025
a163a80
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 28, 2025
348266c
Merge branch 'main' into feat/share-modal-time-range
kowalczyk-krzysztof Apr 28, 2025
1926ac9
Add rounding
kowalczyk-krzysztof Apr 28, 2025
d299817
Update getUpdatedParams condition
kowalczyk-krzysztof Apr 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { i18n } from '@kbn/i18n';
import { getStateFromKbnUrl, setStateToKbnUrl, unhashUrl } from '@kbn/kibana-utils-plugin/public';

import { FormattedMessage } from '@kbn/i18n-react';
import { LocatorPublic } from '@kbn/share-plugin/common';
import { convertPanelMapToPanelsArray } from '../../../../common/lib/dashboard_panel_converters';
import { DashboardPanelMap } from '../../../../common';
import {
Expand Down Expand Up @@ -270,5 +271,11 @@ export function ShowShareModal({
},
},
toasts: coreServices.notifications.toasts,
shareableUrlLocatorParams: {
locator: shareService.url.locators.get(
DASHBOARD_APP_LOCATOR
) as LocatorPublic<DashboardLocatorParams>,
params: locatorParams,
},
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const LinkTabContent: ILinkTab['content'] = ({ state, dispatch }) => {
export const linkTab: ILinkTab = {
id: 'link',
name: i18n.translate('share.contextMenu.permalinksTab', {
defaultMessage: 'Links',
defaultMessage: 'Link',
}),
description: i18n.translate('share.dashboard.link.description', {
defaultMessage: 'Share a direct link to this search.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
EuiFlexItem,
EuiForm,
EuiSpacer,
EuiText,
EuiSwitchEvent,
EuiToolTip,
} from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
import React, { useCallback, useState, useRef, useEffect } from 'react';
import { TimeTypeSection } from './time_type_section';
import type { IShareContext } from '../../context';
import type { LinkShareConfig, LinkShareUIConfig } from '../../../types';

Expand Down Expand Up @@ -54,9 +55,11 @@ export const LinkContent = ({
const [snapshotUrl, setSnapshotUrl] = useState<string>('');
const [isTextCopied, setTextCopied] = useState(false);
const [isLoading, setIsLoading] = useState(false);
const [isAbsoluteTime, setIsAbsoluteTime] = useState(true);
const urlParamsRef = useRef<UrlParams | undefined>(undefined);
const urlToCopy = useRef<string | undefined>(undefined);
const copiedTextToolTipCleanupIdRef = useRef<ReturnType<typeof setTimeout>>();
const timeRange = shareableUrlLocatorParams?.params?.timeRange;

const { delegatedShareUrlHandler, draftModeCallOut: DraftModeCallout } = objectConfig;

Expand Down Expand Up @@ -84,12 +87,15 @@ export const LinkContent = ({

const createShortUrl = useCallback(async () => {
if (shareableUrlLocatorParams) {
const shortUrl = await shortUrlService.createWithLocator(shareableUrlLocatorParams);
const shortUrl = await shortUrlService.createWithLocator(
shareableUrlLocatorParams,
isAbsoluteTime
);
return shortUrl.locator.getUrl(shortUrl.params, { absolute: true });
} else {
return (await shortUrlService.createFromLongUrl(snapshotUrl)).url;
return (await shortUrlService.createFromLongUrl(snapshotUrl, isAbsoluteTime)).url;
}
}, [shareableUrlLocatorParams, shortUrlService, snapshotUrl]);
}, [shareableUrlLocatorParams, shortUrlService, snapshotUrl, isAbsoluteTime]);

const copyUrlHelper = useCallback(async () => {
setIsLoading(true);
Expand Down Expand Up @@ -117,16 +123,21 @@ export const LinkContent = ({
setIsLoading(false);
}, [snapshotUrl, delegatedShareUrlHandler, allowShortUrl, createShortUrl]);

const changeTimeType = (e: EuiSwitchEvent) => {
setIsAbsoluteTime(e.target.checked);
if (urlToCopy?.current && e.target.checked !== isAbsoluteTime) {
urlToCopy.current = undefined;
}
};

return (
<>
<EuiForm>
<EuiText size="s">
<FormattedMessage
id="share.link.helpText"
defaultMessage="Share a direct link to this {objectType}."
values={{ objectType }}
/>
</EuiText>
<TimeTypeSection
timeRange={timeRange}
isAbsoluteTime={isAbsoluteTime}
changeTimeType={changeTimeType}
/>
{isDirty && DraftModeCallout && (
<>
<EuiSpacer size="m" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import React, { ComponentProps } from 'react';
import { __IntlProvider as IntlProvider } from '@kbn/i18n-react';
import { render, screen } from '@testing-library/react';
import { TimeTypeSection } from './time_type_section';
import * as timeUtils from '../../../lib/time_utils';

const renderComponent = (props: ComponentProps<typeof TimeTypeSection>) => {
render(
<IntlProvider locale="en">
<TimeTypeSection {...props} />
</IntlProvider>
);
};

describe('TimeTypeSection', () => {
beforeEach(() => {
jest.clearAllMocks();
jest
.spyOn(timeUtils, 'convertRelativeTimeStringToAbsoluteTimeDate')
.mockReturnValue(new Date());
jest
.spyOn(timeUtils, 'getRelativeTimeValueAndUnitFromTimeString')
.mockImplementation((time) => {
if (time === 'now') return { value: 0, unit: 'second', roundingUnit: undefined };
if (time === 'now-1m') return { value: -1, unit: 'minute', roundingUnit: undefined };
if (time === 'now-30m') return { value: -30, unit: 'minute', roundingUnit: undefined };
return { value: 0, unit: 'second', roundingUnit: undefined };
});
jest.spyOn(timeUtils, 'isTimeRangeAbsoluteTime').mockReturnValue(false);
});

it('renders null when timeRange is not provided', () => {
const changeTimeType = jest.fn();

renderComponent({
isAbsoluteTime: false,
changeTimeType,
});

const timeRangeSwitch = screen.queryByRole('switch');

expect(timeRangeSwitch).not.toBeInTheDocument();
});

it('renders with absolute time range', () => {
const timeRange = { from: '2022-01-01T00:00:00.000Z', to: '2022-01-02T00:00:00.000Z' };
const changeTimeType = jest.fn();

renderComponent({
timeRange,
isAbsoluteTime: true,
changeTimeType,
});

const timeRangeSwitch = screen.getByRole('switch');

expect(timeRangeSwitch).toBeChecked();

const absoluteTimeInfoText = screen.getByTestId('absoluteTimeInfoText');

expect(absoluteTimeInfoText).toBeInTheDocument();
});

it('renders with relative time range (from now to specific time)', () => {
const timeRange = { from: 'now', to: 'now+15m' };
const changeTimeType = jest.fn();

renderComponent({
timeRange,
isAbsoluteTime: false,
changeTimeType,
});

const timeRangeSwitch = screen.getByRole('switch');

expect(timeRangeSwitch).not.toBeChecked();

const relativeTimeFromNowInfoText = screen.getByTestId('relativeTimeInfoTextFromNow');

expect(relativeTimeFromNowInfoText).toBeInTheDocument();
});

it('renders with relative time range (from specific time to now)', () => {
const timeRange = { from: 'now-30m', to: 'now' };
const changeTimeType = jest.fn();

renderComponent({
timeRange,
isAbsoluteTime: false,
changeTimeType,
});

const timeRangeSwitch = screen.getByRole('switch');

expect(timeRangeSwitch).not.toBeChecked();

const relativeTimeToNowInfoText = screen.getByTestId('relativeTimeInfoTextToNow');

expect(relativeTimeToNowInfoText).toBeInTheDocument();
});

it('renders with relative time range (between two relative times)', () => {
const timeRange = { from: 'now-30m', to: 'now-1m' };
const changeTimeType = jest.fn();

renderComponent({
timeRange,
isAbsoluteTime: false,
changeTimeType,
});

const timeRangeSwitch = screen.getByRole('switch');

expect(timeRangeSwitch).not.toBeChecked();

const relativeTimeInfoText = screen.getByTestId('relativeTimeInfoTextDefault');

expect(relativeTimeInfoText).toBeInTheDocument();
});

it('disables switch when timeRange is already absolute', () => {
const timeRange = { from: '2022-01-01T00:00:00.000Z', to: '2022-01-02T00:00:00.000Z' };
const changeTimeType = jest.fn();

jest.spyOn(timeUtils, 'isTimeRangeAbsoluteTime').mockReturnValue(true);

renderComponent({
timeRange,
isAbsoluteTime: false,
changeTimeType,
});

const timeRangeSwitch = screen.queryByRole('switch');

expect(timeRangeSwitch).not.toBeInTheDocument();
});
});
Loading