-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c18d047
commit 9f00091
Showing
15 changed files
with
409 additions
and
233 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
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,77 @@ | ||
import { useState, useEffect, useCallback } from "react"; | ||
import { mapValues } from "lodash"; | ||
import { useAppStore } from "@/common/data/stores/app"; | ||
import { SpaceConfig } from "@/common/components/templates/Space"; | ||
import useIsSpaceEditable from "@/common/lib/hooks/useIsSpaceEditable"; | ||
import { SpaceId } from "@/common/data/stores/app/space/spaceStore"; | ||
import { useCurrentSpaceId } from "@/common/lib/hooks/useIsSpaceEditable"; | ||
|
||
const HOMEBASE_ID = "homebase"; | ||
|
||
interface useSpaceConfigReturnValue { | ||
spaceConfig?: SpaceConfig | null; | ||
loading: boolean; | ||
} | ||
|
||
export const useCurrentSpaceConfig = (): useSpaceConfigReturnValue => { | ||
const spaceId = useCurrentSpaceId(); | ||
return useSpaceConfig(spaceId); | ||
}; | ||
|
||
// Gets space config from local data store (localStorage) | ||
const useLocalSpaceConfig = (spaceId?: SpaceId | null): SpaceConfig | null => { | ||
const isEditable = useIsSpaceEditable(spaceId); | ||
|
||
return useAppStore((state) => { | ||
if (spaceId === HOMEBASE_ID) return state.homebase.homebaseConfig; | ||
if (!spaceId || !(spaceId in state.space.localSpaces)) return null; | ||
|
||
const spaceConfig = state.space.localSpaces[spaceId]; | ||
if (!spaceConfig) return null; | ||
|
||
const fidgetInstanceDatums = mapValues( | ||
spaceConfig.fidgetInstanceDatums, | ||
(datum) => ({ | ||
...datum, | ||
config: { | ||
settings: datum.config.settings, | ||
editable: datum.config.editable, | ||
data: {}, // TO DO: Inject fidget data here | ||
}, | ||
}), | ||
); | ||
|
||
// Populate missing fields | ||
return { ...spaceConfig, isEditable, fidgetInstanceDatums }; | ||
}) as SpaceConfig | null; | ||
}; | ||
|
||
export const useSpaceConfig = ( | ||
spaceId?: SpaceId | null, | ||
): useSpaceConfigReturnValue => { | ||
const [loading, setLoading] = useState(false); | ||
const { loadSpace, setCurrentSpaceId } = useAppStore((state) => ({ | ||
loadSpace: state.space.loadSpace, | ||
setCurrentSpaceId: state.currentSpace.setCurrentSpaceId, | ||
})); | ||
const spaceConfig = useLocalSpaceConfig(spaceId); | ||
|
||
const refetch = useCallback(async () => { | ||
if (spaceId) { | ||
setLoading(true); | ||
await loadSpace(spaceId); | ||
setLoading(false); | ||
} | ||
}, [spaceId]); | ||
|
||
useEffect(() => { | ||
if (spaceId) { | ||
setCurrentSpaceId(spaceId); | ||
refetch(); | ||
} | ||
}, [spaceId]); | ||
|
||
return { spaceConfig, loading }; | ||
}; | ||
|
||
export default useCurrentSpaceConfig; |
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,10 @@ | ||
import { useCurrentSpaceConfig } from "@/common/lib/hooks/useCurrentSpaceConfig"; | ||
import DEFAULT_THEME from "@/common/lib/theme/defaultTheme"; | ||
import { UserTheme } from "@/common/lib/theme"; | ||
|
||
export const useCurrentSpaceTheme = (): UserTheme => { | ||
const { spaceConfig } = useCurrentSpaceConfig(); | ||
return spaceConfig?.theme ?? DEFAULT_THEME; | ||
}; | ||
|
||
export default useCurrentSpaceTheme; |
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,34 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useAuthenticatorManager } from "@/authenticators/AuthenticatorManager"; | ||
import useIsSignedIntoFarcaster from "@/common/lib/hooks/useIsSignedIntoFarcaster"; | ||
|
||
const FARCASTER_NOUNSPACE_AUTHENTICATOR_NAME = "farcaster:nounspace"; | ||
|
||
export const useCurrentUserFid = () => { | ||
const [currentUserFid, setCurrentUserFid] = useState<number | null>(null); | ||
const { callMethod: authManagerCallMethod, lastUpdatedAt } = | ||
useAuthenticatorManager(); | ||
const isSignedIntoFarcaster = useIsSignedIntoFarcaster(); | ||
|
||
useEffect(() => { | ||
if (!isSignedIntoFarcaster || !lastUpdatedAt) return; | ||
|
||
const fetchCurrentUserFid = async () => { | ||
const authManagerResp = await authManagerCallMethod({ | ||
requestingFidgetId: "root", | ||
authenticatorId: FARCASTER_NOUNSPACE_AUTHENTICATOR_NAME, | ||
methodName: "getAccountFid", | ||
isLookup: true, | ||
}); | ||
if (authManagerResp.result === "success") { | ||
setCurrentUserFid(authManagerResp.value as number); | ||
} | ||
}; | ||
|
||
fetchCurrentUserFid(); | ||
}, [isSignedIntoFarcaster, lastUpdatedAt]); | ||
|
||
return currentUserFid; | ||
}; | ||
|
||
export default useCurrentUserFid; |
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,7 @@ | ||
import { useAppStore } from "@/common/data/stores/app"; | ||
|
||
export const useEditableSpaces = () => { | ||
return useAppStore((state) => state.space.editableSpaces); | ||
}; | ||
|
||
export default useEditableSpaces; |
Oops, something went wrong.