-
-
Notifications
You must be signed in to change notification settings - Fork 771
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
135 additions
and
45 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 |
---|---|---|
@@ -1,9 +1,9 @@ | ||
'use client' | ||
|
||
import { useAggregation } from '~/hooks/data/use-aggregation' | ||
import { useAggregationQuery } from '~/hooks/data/use-aggregation' | ||
|
||
export default function Home() { | ||
const { data } = useAggregation() | ||
const { data } = useAggregationQuery() | ||
// throw new Error() | ||
return <main>{data?.user.avatar}</main> | ||
} |
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,22 @@ | ||
'use client' | ||
|
||
import Image from 'next/image' | ||
|
||
import { useAggregationSelector } from '~/providers/root/aggregation-data-provider' | ||
|
||
export const SiteOwnerAvatar = () => { | ||
const avatar = useAggregationSelector((data) => data.user.avatar) | ||
|
||
if (!avatar) return | ||
return ( | ||
<div className="absolute bottom-[8px] right-[-5px] overflow-hidden rounded-full border-[1.5px] border-accent/50"> | ||
<Image | ||
src={avatar} | ||
alt="" | ||
width={25} | ||
height={25} | ||
className="rounded-full border-[1.5px] border-transparent" | ||
/> | ||
</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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import type { AggregateRoot } from '@mx-space/api-client' | ||
import type { UseQueryOptions } from '@tanstack/react-query' | ||
|
||
import { aggregation } from '../../queries/definition/aggregation' | ||
|
||
export const useAggregation = () => { | ||
export const useAggregationQuery = ( | ||
options?: UseQueryOptions<AggregateRoot, unknown, AggregateRoot, string[]>, | ||
) => { | ||
return useQuery({ | ||
...aggregation.root(), | ||
...options, | ||
}) | ||
} |
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,41 @@ | ||
import { useCallback, useEffect } from 'react' | ||
import { atom, useAtomValue } from 'jotai' | ||
import { selectAtom } from 'jotai/utils' | ||
import type { AggregateRoot } from '@mx-space/api-client' | ||
import type { FC, PropsWithChildren } from 'react' | ||
|
||
import { useAggregationQuery } from '~/hooks/data/use-aggregation' | ||
import { jotaiStore } from '~/lib/store' | ||
|
||
const aggregationDataAtom = atom<null | AggregateRoot>(null) | ||
|
||
export const AggregationProvider: FC<PropsWithChildren> = ({ children }) => { | ||
const { data } = useAggregationQuery() | ||
|
||
useEffect(() => { | ||
if (!data) return | ||
jotaiStore.set(aggregationDataAtom, data) | ||
}, [data]) | ||
|
||
return children | ||
} | ||
|
||
/** | ||
* Not recommended to use | ||
*/ | ||
export const useAggregationData = () => useAtomValue(aggregationDataAtom) | ||
|
||
export const useAggregationSelector = <T,>( | ||
selector: (atomValue: AggregateRoot) => T, | ||
deps: any[] = [], | ||
): T | null => | ||
useAtomValue( | ||
// @ts-expect-error | ||
selectAtom( | ||
aggregationDataAtom, | ||
useCallback( | ||
(atomValue) => (!atomValue ? null : selector(atomValue)), | ||
deps, | ||
), | ||
), | ||
) |
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