-
-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <tukon479@gmail.com>
- Loading branch information
Showing
9 changed files
with
186 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
|
||
export const ProviderComposer: Component<{ | ||
contexts: JSX.Element[] | ||
}> = ({ contexts, children }) => { | ||
return contexts.reduceRight((kids: any, parent: any) => { | ||
return React.cloneElement(parent, { children: kids }) | ||
}, children) | ||
} |
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
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,44 @@ | ||
import { | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useRef, | ||
} from 'react' | ||
import { atom, useAtomValue } from 'jotai' | ||
import { selectAtom } from 'jotai/utils' | ||
import type { Image } from '@mx-space/api-client' | ||
|
||
import { jotaiStore } from '~/lib/store' | ||
|
||
const MarkdownImageRecordProviderInternal = createContext(atom([] as Image[])) | ||
|
||
export const MarkdownImageRecordProvider: Component<{ | ||
images: Image[] | ||
}> = ({ children, images }) => { | ||
const atomRef = useRef(atom([...images] as Image[])).current | ||
|
||
useEffect(() => { | ||
jotaiStore.set(atomRef, [...images]) | ||
}, [images]) | ||
|
||
return ( | ||
<MarkdownImageRecordProviderInternal.Provider value={atomRef}> | ||
{children} | ||
</MarkdownImageRecordProviderInternal.Provider> | ||
) | ||
} | ||
|
||
export const useMarkdownImageRecord = (src: string) => { | ||
return useAtomValue( | ||
selectAtom( | ||
useContext(MarkdownImageRecordProviderInternal), | ||
useCallback( | ||
(value: Image[]) => { | ||
return value.find((image) => image.src === src) | ||
}, | ||
[src], | ||
), | ||
), | ||
) | ||
} |
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