import { IssueSource } from "@shared/schema"; import { ellipsis } from "@shared/styles"; import { observer } from "mobx-react"; import React from "react"; import { Trans, useTranslation } from "react-i18next"; import { toast } from "sonner"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import Button from "~/components/Button"; import Flex from "~/components/Flex"; import Modal from "~/components/Modal"; import PluginIcon from "~/components/PluginIcon"; import Text from "~/components/Text"; import useBoolean from "~/hooks/useBoolean"; import useRequest from "~/hooks/useRequest"; import { client } from "~/utils/ApiClient"; type Props = { issueTitle: string; open: boolean; onCreate: (source: IssueSource) => Promise; onClose: () => void; }; export const CreateIssueDialog = observer( ({ issueTitle, open, onCreate, onClose }: Props) => { const { t } = useTranslation(); const [isCreating, setCreating, unsetCreating] = useBoolean(); const [selectedSource, selectSource] = React.useState(); const { data: sources, loading, request, } = useRequest( React.useCallback(async () => { try { const res = await client.post("/issues.list_sources"); return res.data; } catch (err) { toast.error(t("Couldn't load issue sources, try again?")); throw err; } }, [t]) ); const handleCreateIssue = React.useCallback(async () => { setCreating(); await onCreate(selectedSource!); unsetCreating(); }, [selectedSource, onCreate, setCreating, unsetCreating]); React.useEffect(() => { if (open) { void request(); } else { selectSource(undefined); } }, [open, request]); return ( {loading ? ( "Loading..." ) : !sources?.length ? ( "No source available" ) : ( {sources.map((source) => ( ))} )}
{selectedSource ? ( , }} /> ) : ( t("Select a source to create issue") )}
); } ); const SourceItem = ({ source, selected, onSelect, }: { source: IssueSource; selected: boolean; onSelect: (source: IssueSource) => void; }) => ( onSelect(source)} $selected={selected} > {source.name} {source.owner.name} ); export const FlexContainer = styled(Flex)` margin-left: -24px; margin-right: -24px; margin-bottom: -24px; outline: none; `; const ListContainer = styled.div` height: 65vh; padding: 0 24px 12px; overflow: scroll; ${breakpoint("tablet")` height: 40vh; `} `; const SourceAccount = styled(Text)``; const SourceItemWrapper = styled(Flex)<{ $selected: boolean }>` font-size: 16px; cursor: var(--pointer); padding: 12px; border-radius: 6px; ${(props) => props.$selected && ` background: ${props.theme.accent}; color: ${props.theme.white}; ${SourceAccount} { color: ${props.theme.white}; } `} ${breakpoint("tablet")` padding: 4px 6px; font-size: 15px; `} `; const Footer = styled(Flex)` height: 64px; border-top: 1px solid ${(props) => props.theme.horizontalRule}; padding: 0 24px; `; const StyledText = styled(Text)` ${ellipsis()} margin-bottom: 0; `;