@towns-protocol/react-sdk
Version:
React Hooks for Towns Protocol SDK
71 lines • 3.2 kB
JavaScript
import { useMemo } from 'react';
import { useAction } from './internals/useAction';
import { useObservable } from './useObservable';
import { useSyncAgent } from './useSyncAgent';
import { getRoom } from './utils';
const getMyMember = (sync, streamId) => getRoom(sync, streamId).members.myself;
/**
* Hook to get the data of the current user in a stream.
* @param streamId - The id of the stream to get the current user of.
* @param config - Configuration options for the observable.
* @returns The MemberModel of the current user.
*/
export const useMyMember = (streamId, config) => {
const sync = useSyncAgent();
const myself = useMemo(() => getMyMember(sync, streamId), [sync, streamId]);
const { data } = useObservable(myself.member, config);
return {
...data,
};
};
/**
* Hook to set the ENS address of the current user in a stream.
* You should be validating if the ENS address belongs to the user before setting it.
* @param streamId - The id of the stream to set the ENS address of.
* @param config - Configuration options for the action.
* @returns The `setEnsAddress` action and its loading state.
*/
export const useSetEnsAddress = (streamId, config) => {
const sync = useSyncAgent();
const member = useMemo(() => getMyMember(sync, streamId), [sync, streamId]);
const { action: setEnsAddress, ...rest } = useAction(member, 'setEnsAddress', config);
return { setEnsAddress, ...rest };
};
/**
* Hook to set the username of the current user in a stream.
* @param streamId - The id of the stream to set the username of.
* @param config - Configuration options for the action.
* @returns The `setUsername` action and its loading state.
*/
export const useSetUsername = (streamId, config) => {
const sync = useSyncAgent();
const member = useMemo(() => getMyMember(sync, streamId), [sync, streamId]);
const { action: setUsername, ...rest } = useAction(member, 'setUsername', config);
return { setUsername, ...rest };
};
/**
* Hook to set the display name of the current user in a stream.
* @param streamId - The id of the stream to set the display name of.
* @param config - Configuration options for the action.
* @returns The `setDisplayName` action and its loading state.
*/
export const useSetDisplayName = (streamId, config) => {
const sync = useSyncAgent();
const member = useMemo(() => getMyMember(sync, streamId), [sync, streamId]);
const { action: setDisplayName, ...rest } = useAction(member, 'setDisplayName', config);
return { setDisplayName, ...rest };
};
/**
* Hook to set the NFT of the current user in a stream.
* You should be validating if the NFT belongs to the user before setting it.
* @param streamId - The id of the stream to set the NFT of.
* @param config - Configuration options for the action.
* @returns The `setNft` action and its loading state.
*/
export const useSetNft = (streamId, config) => {
const sync = useSyncAgent();
const member = useMemo(() => getMyMember(sync, streamId), [sync, streamId]);
const { action: setNft, ...rest } = useAction(member, 'setNft', config);
return { setNft, ...rest };
};
//# sourceMappingURL=useMyMember.js.map