forked from podman-desktop/podman-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add purple dot when new content is available in dashboard (podm…
…an-desktop#4043) (podman-desktop#4782) * feat: add purple dot when new content is available in dashboard (podman-desktop#4043) Signed-off-by: lstocchi <[email protected]> * fix: create base component Signed-off-by: lstocchi <[email protected]> --------- Signed-off-by: lstocchi <[email protected]>
- Loading branch information
Showing
5 changed files
with
296 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/renderer/src/lib/dashboard/NewContentOnDashboardBadge.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import '@testing-library/jest-dom/vitest'; | ||
import { test, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import NewContentOnDashboardBadge from './NewContentOnDashboardBadge.svelte'; | ||
import { notificationQueue } from '/@/stores/notifications'; | ||
import type { NotificationCard } from '../../../../main/src/plugin/api/notification'; | ||
import type { ProviderStatus } from '@podman-desktop/api'; | ||
import type { ProviderContainerConnectionInfo, ProviderInfo } from '../../../../main/src/plugin/api/provider-info'; | ||
import { providerInfos } from '/@/stores/providers'; | ||
import { router } from 'tinro'; | ||
|
||
async function waitRender(): Promise<void> { | ||
const result = render(NewContentOnDashboardBadge); | ||
// wait that result.component.$$.ctx[3] is set | ||
while (result.component.$$.ctx[3] === undefined) { | ||
await new Promise(resolve => setTimeout(resolve, 100)); | ||
} | ||
} | ||
|
||
const notification1: NotificationCard = { | ||
id: 1, | ||
extensionId: 'extension', | ||
title: '1', | ||
body: '1', | ||
type: 'info', | ||
highlight: true, | ||
}; | ||
|
||
const pStatus: ProviderStatus = 'started'; | ||
const pInfo: ProviderContainerConnectionInfo = { | ||
name: 'test', | ||
status: 'started', | ||
endpoint: { | ||
socketPath: '', | ||
}, | ||
type: 'podman', | ||
}; | ||
const providerInfo = { | ||
id: 'test', | ||
internalId: 'id', | ||
name: '', | ||
containerConnections: [pInfo], | ||
kubernetesConnections: undefined, | ||
status: pStatus, | ||
containerProviderConnectionCreation: false, | ||
containerProviderConnectionInitialization: false, | ||
kubernetesProviderConnectionCreation: false, | ||
kubernetesProviderConnectionInitialization: false, | ||
links: undefined, | ||
detectionChecks: undefined, | ||
warnings: undefined, | ||
images: undefined, | ||
installationSupport: undefined, | ||
} as unknown as ProviderInfo; | ||
|
||
test('Expect to do not display any dot if active page is Dashboard', async () => { | ||
notificationQueue.set([]); | ||
providerInfos.set([]); | ||
await waitRender(); | ||
|
||
notificationQueue.set([notification1]); | ||
providerInfos.set([providerInfo]); | ||
|
||
await new Promise(resolve => setTimeout(resolve, 200)); | ||
|
||
const dot = screen.queryByLabelText('New content available'); | ||
expect(dot).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('Expect to do not display any dot if active page is not Dashboard but there are no updates', async () => { | ||
notificationQueue.set([]); | ||
providerInfos.set([]); | ||
router.goto('/pods'); | ||
|
||
await waitRender(); | ||
|
||
const dot = screen.queryByLabelText('New content available'); | ||
expect(dot).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('Expect to display the dot if active page is not Dashboard and there is a new notification', async () => { | ||
notificationQueue.set([]); | ||
providerInfos.set([]); | ||
router.goto('/pods'); | ||
await waitRender(); | ||
|
||
notificationQueue.set([notification1]); | ||
|
||
await new Promise(resolve => setTimeout(resolve, 200)); | ||
|
||
const dot = screen.getByLabelText('New content available'); | ||
expect(dot).toBeInTheDocument(); | ||
}); | ||
|
||
test('Expect to display the dot if active page is not Dashboard and there is a new provider', async () => { | ||
notificationQueue.set([]); | ||
providerInfos.set([]); | ||
router.goto('/pods'); | ||
await waitRender(); | ||
|
||
providerInfos.set([providerInfo]); | ||
|
||
await new Promise(resolve => setTimeout(resolve, 200)); | ||
|
||
const dot = screen.getByLabelText('New content available'); | ||
expect(dot).toBeInTheDocument(); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/renderer/src/lib/dashboard/NewContentOnDashboardBadge.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<script lang="ts"> | ||
import { onDestroy, onMount } from 'svelte'; | ||
import { providerInfos } from '/@/stores/providers'; | ||
import { notificationQueue } from '/@/stores/notifications'; | ||
import type { Unsubscriber } from 'svelte/motion'; | ||
import NewContentBadge from '../ui/NewContentBadge.svelte'; | ||
let providersId: string[] = []; | ||
let notificationCount: number = 0; | ||
let hasNewProviders = false; | ||
let hasNewNotifications = false; | ||
$: hasNew = hasNewProviders || hasNewNotifications; | ||
let providersUnsubscribe: Unsubscriber; | ||
let notificationsUnsubscribe: Unsubscriber; | ||
onMount(() => { | ||
// if there is a new provider we display the dot | ||
providersUnsubscribe = providerInfos.subscribe(updatedProviders => { | ||
const updatedProvidersId = updatedProviders.map(prov => prov.internalId).sort(); | ||
if (!hasNewProviders) { | ||
// if the user is in the dashboard page we do not check for new providers | ||
hasNewProviders = hasNewProvider(providersId, updatedProvidersId); | ||
} | ||
providersId = updatedProvidersId; | ||
}); | ||
// if there is a new notification we display the dot | ||
notificationsUnsubscribe = notificationQueue.subscribe(notifications => { | ||
if (!hasNewNotifications) { | ||
// if the user is in the dashboard page we do not check for new notifications | ||
hasNewNotifications = notifications.length > notificationCount; | ||
} | ||
notificationCount = notifications.length; | ||
}); | ||
}); | ||
onDestroy(() => { | ||
providersUnsubscribe?.(); | ||
notificationsUnsubscribe?.(); | ||
}); | ||
function hasNewProvider(oldProvidersId: string[], newProvidersId: string[]): boolean { | ||
if (oldProvidersId.length < newProvidersId.length) { | ||
return true; | ||
} | ||
for (let [index, id] of newProvidersId.entries()) { | ||
if (id !== oldProvidersId[index]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
function onHide() { | ||
hasNewProviders = false; | ||
hasNewNotifications = false; | ||
} | ||
</script> | ||
|
||
<div class="absolute top-0 right-[-9px]"> | ||
<NewContentBadge pagePath="/" show="{hasNew}" onHide="{onHide}" /> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/********************************************************************** | ||
* Copyright (C) 2023 Red Hat, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
***********************************************************************/ | ||
import '@testing-library/jest-dom/vitest'; | ||
import { test, expect } from 'vitest'; | ||
import { render, screen } from '@testing-library/svelte'; | ||
import NewContentBadge from './NewContentBadge.svelte'; | ||
import { router } from 'tinro'; | ||
|
||
test('Expect to do not display any dot if active page is same from pagePath', async () => { | ||
router.goto('/'); | ||
render(NewContentBadge, { | ||
pagePath: '/', | ||
show: true, | ||
}); | ||
|
||
const dot = screen.queryByLabelText('New content available'); | ||
expect(dot).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('Expect to do not display any dot if active page is in pagePath but show prop is false', async () => { | ||
router.goto('/'); | ||
render(NewContentBadge, { | ||
pagePath: '/', | ||
show: false, | ||
}); | ||
|
||
const dot = screen.queryByLabelText('New content available'); | ||
expect(dot).not.toBeInTheDocument(); | ||
}); | ||
|
||
test('Expect to display the dot if active page is not pagePath and there is new content', async () => { | ||
router.goto('/test'); | ||
render(NewContentBadge, { | ||
pagePath: '/', | ||
show: true, | ||
}); | ||
|
||
const dot = screen.getByLabelText('New content available'); | ||
expect(dot).toBeInTheDocument(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<script lang="ts"> | ||
import { onDestroy, onMount } from 'svelte'; | ||
import { router } from 'tinro'; | ||
import type { Unsubscriber } from 'svelte/motion'; | ||
export let pagePath: string; | ||
export let show: boolean = false; | ||
export let onHide: () => void = () => {}; | ||
let isInPage = $router.path === pagePath; | ||
let hasNew: boolean; | ||
$: hasNew = !isInPage && show; | ||
let routerUnsubscribe: Unsubscriber; | ||
onMount(() => { | ||
// listen to router change, so we can reset the changes and update the dot visibility | ||
routerUnsubscribe = router.subscribe(route => { | ||
isInPage = route.path === pagePath; | ||
if (isInPage) { | ||
onHide(); | ||
} | ||
}); | ||
}); | ||
onDestroy(() => { | ||
routerUnsubscribe?.(); | ||
}); | ||
</script> | ||
|
||
{#if hasNew} | ||
<div aria-label="New content available" class="w-[6px] h-[6px] bg-purple-500 rounded-full"></div> | ||
{/if} |