Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Guild Import Modal #499

Open
wants to merge 7 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions apps/protocol-frontend/src/components/GuildImportModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Box, Button, Heading, Spinner, Stack } from '@chakra-ui/react';
import { ControlledSelect, SelectOption as Option } from '@govrn/protocol-ui';
import { useMemo, useState } from 'react';
import { useGuildXYZListGuilds } from '../hooks/useGuildXYZListGuilds';
import { useOverlay } from '../contexts/OverlayContext';

interface GuildImportProps {
onSuccess: (guildId: number) => void;
}

const GuildImportTitle = ({ title }: { title: string }) => {
return (
<Heading as="h5" size="sm" width="80%">
{title}
</Heading>
);
};

const GuildImportModalError = ({ message }: { message: string }) => {
return (
<Stack direction="column" width="100%" height="100%" spacing={8} py={8}>
<GuildImportTitle title="Oops!" />
<Box width="80%">
<p>{message}</p>
</Box>
</Stack>
);
};

export const GuildImportModal = ({ onSuccess }: GuildImportProps) => {
const { setModals } = useOverlay();

const [selectedGuild, setSelectedGuild] = useState<Option<number> | null>(
null,
);

const {
data: guildList,
isLoading: isGuildListLoading,
isError,
error,
} = useGuildXYZListGuilds();

const daoListOptions = useMemo(() => {
return (
guildList?.map(guild => ({
value: guild.id,
label: guild.name,
image: guild.imageUrl,
})) || []
);
}, [guildList]);

function handleGuildImport() {
console.log(
`::: publish message ${guildList?.find(
g => g.id === (selectedGuild ?? -1),
)}`,
);
onSuccess(selectedGuild?.value ?? -1);
setModals({});
}

if (isGuildListLoading) {
return (
<Stack direction="column" width="100%" height="100%" spacing={8} py={8}>
<GuildImportTitle
title={'Connecting to Guild.xyz to load joined Guilds...'}
/>
<Spinner alignContent="center" alignSelf="center" />
</Stack>
);
}

if (isError && error instanceof Error) {
return (
<GuildImportModalError
message={error?.message ?? 'Failed to load joined guilds.'}
/>
);
}

if (guildList?.length === 0) {
return (
<GuildImportModalError
message={'You have not joined any guilds on Guild.xyz.'}
/>
);
}

return (
<Stack direction="column" width="100%" height="100%" spacing={8} py={8}>
<GuildImportTitle title="Success! You are connected to Guild.xyz and can import your DAO here" />
<Box width="50%" height="100%">
<ControlledSelect
isDisabled={isGuildListLoading}
label="Select a DAO to Join"
isMulti={false}
onChange={dao => {
if (dao instanceof Array || !dao) {
return;
}
setSelectedGuild(dao);
}}
value={selectedGuild ?? null}
options={daoListOptions}
isSearchable={false}
isClearable
/>
</Box>

<Button
variant="primary"
onClick={handleGuildImport}
isLoading={isGuildListLoading}
disabled={selectedGuild === null}
width={{ base: '100%', lg: 'min-content' }}
>
Import
</Button>
</Stack>
);
};
Loading