Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

3.6 KB
Raw Blame
'use client';

import { Flexbox, Input, Text, toast } from '@lobehub/ui';
import { Form, useForm } from '@lobehub/ui/form';
import { memo, useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';

import ImperativeModal from '@/components/ImperativeModal';
import InlineErrorText from '@/components/InlineErrorText';
import { type InlineErrorState, toInlineError } from '@/components/InlineErrorText/errorDetail';
import { lambdaClient } from '@/libs/trpc/client';

import { isGithubRepoUrl } from './githubRepoUrl';

interface SubmitRepoModalProps {
  beforeSubmit?: () => Promise<{ actAs?: number } | void>;
  onClose: () => void;
  onSuccess?: () => void;
  open: boolean;
}

export const SubmitRepoModal = memo<SubmitRepoModalProps>(
  ({ open, onClose, onSuccess, beforeSubmit }) => {
    const { t } = useTranslation('discover');

    const form = useForm<{ gitUrl?: string }>();

    const [isSubmitting, setIsSubmitting] = useState(false);
    /** Submit failure, shown above the modal buttons: the modal stays open with the typed URL. */
    const [error, setError] = useState<InlineErrorState | null>(null);

    const handleSubmit = useCallback(async () => {
      setError(null);
      try {
        const { valid } = await form.validate();
        if (!valid) return;
        const gitUrl = form.getValue('gitUrl')?.trim();

        if (!gitUrl) {
          return;
        }

        setIsSubmitting(true);

        const submitContext = await beforeSubmit?.();

        await lambdaClient.market.socialProfile.submitRepo.mutate({
          actAs: submitContext?.actAs,
          gitUrl,
          type: 'skill',
        });

        toast.success(t('user.submitRepoSuccess'));
        onSuccess?.();
        onClose();
        form.reset();
      } catch (err) {
        console.error('[SubmitRepoModal] Failed to submit:', err);
        setError(toInlineError(t('user.submitRepoError'), err));
      } finally {
        setIsSubmitting(false);
      }
    }, [beforeSubmit, form, t, onSuccess, onClose]);

    const handleCancel = useCallback(() => {
      setError(null);
      form.reset();
      onClose();
    }, [form, onClose]);

    return (
      <ImperativeModal
        centered
        cancelText={t('user.cancel')}
        confirmLoading={isSubmitting}
        okText={t('user.submit')}
        open={open}
        title={false}
        width={480}
        onCancel={handleCancel}
        onOk={handleSubmit}
      >
        <Text strong fontSize={20} style={{ display: 'block', marginBottom: 8, marginTop: 16 }}>
          {t('user.submitRepoTitle')}
        </Text>
        <Text style={{ display: 'block', marginBottom: 16 }} type="secondary">
          {t('user.submitRepoDescription')}
        </Text>

        <Form form={form} layout="vertical">
          <Form.Field
            label={t('user.githubUrl')}
            name="gitUrl"
            required={t('user.githubUrlRequired')}
            validate={(value: string) =>
              value && !isGithubRepoUrl(value) ? t('user.githubUrlInvalid') : undefined
            }
          >
            <Input placeholder="https://github.com/username/repo" />
          </Form.Field>
        </Form>

        <Flexbox style={{ marginTop: 8 }}>
          <Text style={{ fontSize: 12 }} type="secondary">
            {t('user.submitRepoHint')}
          </Text>
        </Flexbox>
        {error && (
          <InlineErrorText detail={error.detail} style={{ marginTop: 12 }}>
            {error.summary}
          </InlineErrorText>
        )}
      </ImperativeModal>
    );
  },
);

SubmitRepoModal.displayName = 'SubmitRepoModal';

export default SubmitRepoModal;