Skip to content

Commit

Permalink
Merge branch 'main' of github.com:etalab/annuaire-entreprises-site
Browse files Browse the repository at this point in the history
  • Loading branch information
XavierJp committed Jul 25, 2024
2 parents 6d5d7a8 + e6c9ba9 commit 4083dc1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 30 deletions.
12 changes: 10 additions & 2 deletions components/header/changelog-notification.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
content: '';
position: absolute;
top: 5px;
right: 5px;
right: 2px;
width: 10px;
height: 10px;
background-color: #ff0000;
Expand All @@ -11,6 +11,14 @@
}

.changelogNotification {
top: -2px;
position: relative;
}

@media only screen and (min-width: 1px) and (max-width: 992px) {
.notificationText {
display: none;
}
.changelogNotification::before {
top: 0;
}
}
38 changes: 16 additions & 22 deletions components/header/changelog-notification.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use client';

import { useEffect } from 'react';
import FadeIn from '#components-ui/animation/fade-in';
import { Icon } from '#components-ui/icon/wrapper';
import InformationTooltip from '#components-ui/information-tooltip';
import { changelogData } from '#models/historique-modifications';
import { EScope, hasRights } from '#models/user/rights';
import { formatDate } from '#utils/helpers';
import { useStorage } from 'hooks';
import useSession from 'hooks/use-session';
import style from './changelog-notification.module.css';
Expand All @@ -31,7 +30,7 @@ export default function ChangelogNotification() {
const [previousDateOfLastNews, saveDateOfLastNews] = useStorage(
'local',
NEW_SINCE_LAST_VISIT_ID,
dateOfLastNews
null
);

useEffect(() => {
Expand All @@ -40,32 +39,27 @@ export default function ChangelogNotification() {
}
}, []);

if (!previousDateOfLastNews) {
saveDateOfLastNews(formatDate(new Date()));
}

if (
!dateOfLastNews ||
!previousDateOfLastNews ||
!dateOfLastNews ||
convertToISO(previousDateOfLastNews) >= convertToISO(dateOfLastNews)
) {
return null;
}

return (
<li>
<FadeIn>
<InformationTooltip
verticalOrientation="bottom"
label="Découvrir les nouveautés"
width={200}
tabIndex={undefined}
ariaRelation="labelledby"
>
<a
href="/historique-des-modifications"
className={style.changelogNotification + ' fr-btn fr-btn--sm'}
>
<Icon slug="present" />
</a>
</InformationTooltip>
</FadeIn>
</li>
<a
href="/historique-des-modifications"
className={style.changelogNotification + ' fr-link'}
title="Découvrir les dernières évolutions de l’Annuaire des Entreprises"
>
<Icon slug="present">
<span className={style.notificationText}>Nouveautés</span>
</Icon>
</a>
);
}
5 changes: 4 additions & 1 deletion components/header/header-core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const HeaderCore: React.FC<IProps> = ({
</div>
) : null}
<div className={styles.menuMobile}>
<ChangelogNotificationWithoutSSR />
<Menu
session={session}
useAgentCTA={useAgentCTA}
Expand All @@ -113,7 +114,9 @@ export const HeaderCore: React.FC<IProps> = ({
<div className="fr-header__tools">
<div className="fr-header__tools-links">
<ul className="fr-links-group">
<ChangelogNotificationWithoutSSR />
<li>
<ChangelogNotificationWithoutSSR />
</li>
<li>
<Menu
session={session}
Expand Down
2 changes: 2 additions & 0 deletions components/header/header-core/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
order: 3;
padding: 1rem;
margin-left: auto;
gap: 1rem;
display: flex;
}

@media screen and (min-width: 993px) {
Expand Down
35 changes: 30 additions & 5 deletions hooks/use-storage.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
import { useState } from 'react';

function isStorageAvailable(type: 'session' | 'local') {
const test = 'test';

if (typeof window === 'undefined') {
return false;
}

const store = getStore(type);
try {
store.setItem(test, test);
store.removeItem(test);
return true;
} catch (e) {
return false;
}
}

const getStore = (type: 'session' | 'local') => {
return type === 'session' ? window.sessionStorage : window.localStorage;
};

export const useStorage = (
type: 'session' | 'local',
key: string,
initialValue: any
) => {
const storageAvailable = isStorageAvailable(type);

// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState(() => {
if (typeof window === 'undefined') {
if (!storageAvailable) {
return initialValue;
}

try {
const store =
type === 'session' ? window.sessionStorage : window.localStorage;
const store = getStore(type);
// Get from local storage by key
const item = store.getItem(key);
// Parse stored json or if none return initialValue
Expand All @@ -25,6 +47,10 @@ export const useStorage = (
}
});

if (!storageAvailable) {
return [storedValue, () => {}];
}

// Return a wrapped version of useState's setter function that ...
// ... persists the new value to localStorage.
const setValue = (value: any) => {
Expand All @@ -36,8 +62,7 @@ export const useStorage = (
setStoredValue(valueToStore);
// Save to local storage
if (typeof window !== 'undefined') {
const store =
type === 'session' ? window.sessionStorage : window.localStorage;
const store = getStore(type);
store.setItem(key, JSON.stringify(valueToStore));
}
} catch (error) {
Expand Down

0 comments on commit 4083dc1

Please sign in to comment.