generated from graasp/graasp-repo
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add preview component * fix: display in copyright text * fix: test
- Loading branch information
Showing
10 changed files
with
148 additions
and
13 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
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
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,14 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import { CopyrightText } from './CopyrightText'; | ||
|
||
const meta = { | ||
component: CopyrightText, | ||
} satisfies Meta<typeof CopyrightText>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default = { | ||
args: {}, | ||
} satisfies Story; |
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,31 @@ | ||
import { useState } from 'react'; | ||
|
||
import { Typography } from '@mui/material'; | ||
|
||
import { usePreviewMode } from '~landing/preview/PreviewModeContext'; | ||
|
||
export function CopyrightText() { | ||
const [clickCounter, setClickCounter] = useState(0); | ||
const { togglePreview, isEnabled: isPreviewEnabled } = usePreviewMode(); | ||
|
||
const handleClick = () => { | ||
if (clickCounter === 2) { | ||
togglePreview(); | ||
// reset counter | ||
setClickCounter(0); | ||
} else { | ||
setClickCounter((s) => s + 1); | ||
} | ||
}; | ||
return ( | ||
<Typography | ||
textAlign="center" | ||
variant="note" | ||
onClick={handleClick} | ||
sx={{ userSelect: 'none' }} | ||
> | ||
© Graasp 2014 - {new Date().getFullYear()} | ||
{isPreviewEnabled ? ' (preview)' : ''} | ||
</Typography> | ||
); | ||
} |
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
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,74 @@ | ||
import { | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
const PREVIEW_STORAGE_KEY = 'graasp-preview'; | ||
|
||
type PreviewContextType = { | ||
togglePreview: () => void; | ||
isEnabled: boolean; | ||
}; | ||
const PreviewContext = createContext<PreviewContextType>({ | ||
isEnabled: false, | ||
togglePreview: () => console.error('no Preview context present'), | ||
}); | ||
|
||
const isPreviewEnabled = () => { | ||
const isPresent = localStorage.getItem(PREVIEW_STORAGE_KEY) != null; | ||
return isPresent; | ||
}; | ||
|
||
export function PreviewContextProvider({ | ||
children, | ||
}: Readonly<{ children: ReactNode }>) { | ||
const [isEnabled, setIsEnabled] = useState(isPreviewEnabled()); | ||
|
||
useEffect(() => { | ||
function listenForStorageChanges(event: StorageEvent) { | ||
if (event.key === PREVIEW_STORAGE_KEY) { | ||
// sync the local state | ||
setIsEnabled(isPreviewEnabled()); | ||
} | ||
// discard the event otherwise | ||
} | ||
window.addEventListener('storage', listenForStorageChanges); | ||
return () => window.removeEventListener('storage', listenForStorageChanges); | ||
}, []); | ||
|
||
const value = useMemo( | ||
() => ({ | ||
togglePreview: () => { | ||
if (isPreviewEnabled()) { | ||
window.localStorage.removeItem(PREVIEW_STORAGE_KEY); | ||
setIsEnabled(false); | ||
} else { | ||
window.localStorage.setItem(PREVIEW_STORAGE_KEY, 'enabled'); | ||
setIsEnabled(true); | ||
} | ||
}, | ||
isEnabled, | ||
}), | ||
[isEnabled], | ||
); | ||
|
||
return ( | ||
<PreviewContext.Provider value={value}>{children}</PreviewContext.Provider> | ||
); | ||
} | ||
|
||
export function usePreviewMode() { | ||
return useContext<PreviewContextType>(PreviewContext); | ||
} | ||
|
||
export function Preview({ children }: { children: ReactNode }) { | ||
const { isEnabled } = usePreviewMode(); | ||
if (isEnabled) { | ||
return children; | ||
} | ||
return null; | ||
} |
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
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
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
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