"use client";
import React, { Fragment, useState } from "react";
import { Avatar } from "components/Avatar";
import { Separator } from "components/Layout";
import { LoginModal } from "components/LoginButton";
import { ButtonPrimary, ButtonTertiary } from "components/Buttons";
import { BaseTextBlock } from "app/(app)/(published)/lish/[did]/[publication]/[rkey]/Blocks/BaseTextBlock";
import { LocalizedDate } from "app/(app)/(published)/lish/[did]/[publication]/LocalizedDate";
import { ComposerChrome, ComposerSubmit } from "components/Composer";
import { TextComposer } from "components/Composer/TextComposer";
import {
docToFacetedText,
facetedTextToDoc,
type FacetedText,
} from "components/Composer/facetedText";
import type { DocumentQuestion, QuestionState } from "src/documentQuestions";
import type { InteractionAuthor } from "src/interactionAuthor";
import {
authorDisplayName,
hasSeveralAuthors,
InteractionAuthorName,
} from "../InteractionAuthorName";
import {
CollapsibleDivider,
ReplyButtonTextInput,
} from "../ReplyBlock/RepliesHeader";
import { DEFAULT_QUESTIONS_BUTTON_TEXT } from "./constants";
import {
AUDIENCE_SUBMITTERS,
audienceNotice,
type QuestionsAudience,
} from "src/questionsAudience";
import { SettingsTiny } from "components/Icons/SettingsTiny";
import { AudienceRestricted } from "../AudienceRestricted";
import { ClosesAtNotice, useSubmissionsClosed } from "../useSubmissionsClosed";
export function QuestionsButtonTextInput(props: {
value: string;
onCommit: (text: string) => void;
}) {
return (
);
}
export function DisabledAskButton(props: { text?: string }) {
return (
{props.text || DEFAULT_QUESTIONS_BUTTON_TEXT}
);
}
type QuestionHelpProps = {
audience: QuestionsAudience;
// Names the tiers of a "tiers" audience
tierNames?: string[];
showPending?: boolean;
closesAt?: string;
};
function questionHelpLines(props: QuestionHelpProps) {
return [
audienceNotice(props.audience, "questions", props.tierNames),
props.closesAt && ,
!props.showPending && "Questions show once the author answers them.",
].filter((line) => !!line);
}
function QuestionHelp(props: QuestionHelpProps) {
let lines = questionHelpLines(props);
if (lines.length === 0) return null;
return (
{lines.map((line, i) => (
{i > 0 && " "}
{line}
))}
);
}
export function QuestionsEmpty(props: QuestionHelpProps) {
return (
No questions yet…
{questionHelpLines(props).map((line, i) => (
{line}
))}
);
}
export function QuestionsClosed() {
return (
This post is no longer accepting questions!
);
}
export function QuestionsEditorNotice(props: QuestionHelpProps) {
let closed = useSubmissionsClosed(props.closesAt);
return (
No questions yet…
{AUDIENCE_SUBMITTERS[props.audience]} can submit questions. You can
answer them from here.
{props.showPending
? "Questions will be visible to readers as soon as they're submitted. You can remove any question at any time."
: "Questions will only be visible to readers once you answer them."}
{props.closesAt && (
{closed ? (
"Submissions are closed."
) : (
)}
)}
To adjust permissions, click{" "}
in the upper right
of this block.
);
}
export function QuestionComposer(props: {
asker?: InteractionAuthor | null;
draftKey: string | null;
submitLabel?: string;
autoFocus?: boolean;
onSubmit: (text: FacetedText) => Promise;
}) {
return (
props.onSubmit(docToFacetedText(doc))}
actions={
<>
{props.asker && (
)}
{props.submitLabel || DEFAULT_QUESTIONS_BUTTON_TEXT}
>
}
/>
);
}
export function QuestionComposerPreview(props: { action: React.ReactNode }) {
return (
Readers will be able to ask a question here…
);
}
export function LoggedOutQuestionComposer(props: {
linkAccount?: boolean;
redirectRoute?: string;
}) {
return (
{props.linkAccount ? "Link " : "Log in"}
}
/>{" "}
{!props.linkAccount && "with"} an Atmosphere account to ask a question!
);
}
export type QuestionsPublication = {
publicationUri: string;
publicationUrl?: string;
publicationName: string;
publicationDescription?: string;
newsletterMode: boolean;
};
export function QuestionsRestricted(props: {
audience: Exclude;
author: InteractionAuthor | null;
publication?: QuestionsPublication;
tierNames?: string[];
}) {
return (
);
}
export function QuestionEditorEmpty(
props: QuestionHelpProps & { action: React.ReactNode },
) {
return (
);
}
function ThreadEntry(props: {
author: InteractionAuthor;
showHandle?: boolean;
date?: string;
actions?: React.ReactNode;
continues?: boolean;
children: React.ReactNode;
}) {
return (
{props.date && (
·{" "}
)}
{props.children}
{props.actions && (
{props.actions}
)}
);
}
function QuestionRow(props: {
question: DocumentQuestion;
showHandle?: boolean;
answer?: React.ReactNode;
actions?: React.ReactNode;
editor?: React.ReactNode;
}) {
return (
);
}
export function QuestionAnswer(props: {
author: InteractionAuthor;
showHandle?: boolean;
date?: string;
actions?: React.ReactNode;
composing?: boolean;
children: React.ReactNode;
}) {
return (
{!props.composing && (
Answer
)}
{props.children}
);
}
export function QuestionAction(
props: React.ButtonHTMLAttributes & { primary?: boolean },
) {
let { primary, className, ...rest } = props;
return (
);
}
export function QuestionsList(
props: QuestionHelpProps & {
questions: DocumentQuestion[];
// The composer or a sign-in button
action?: React.ReactNode;
renderAnswer: RenderAnswer;
onWithdraw?: (uri: string) => void;
onEdit?: (uri: string, text: FacetedText) => Promise;
},
) {
let { onWithdraw, onEdit } = props;
let [editing, setEditing] = useState(null);
let others = props.questions.filter((q) => !q.mine);
let ordered = [
...props.questions.filter((q) => q.mine),
...others.filter((q) => q.answer),
...others.filter((q) => !q.answer),
];
return (
{props.action && (
<>
{props.action}
>
)}
{ordered.length === 0 ? (
) : (
)}
onEdit &&
editing === question.uri && (
setEditing(null)}
onSubmit={async (text) => {
if (await onEdit(question.uri, text)) setEditing(null);
}}
/>
)
}
renderControls={(question) =>
question.mine && !question.answer && editing !== question.uri
? [
onEdit && (
setEditing(question.uri)}
>
Edit
),
onWithdraw && (
onWithdraw(question.uri)}
>
Withdraw
),
!props.showPending && question.state === "pending" && (
Visible to others once answered
),
]
: []
}
/>
);
}
function QuestionEditor(props: {
question: DocumentQuestion;
onCancel: () => void;
onSubmit: (text: FacetedText) => Promise;
}) {
let [initialDoc] = useState(() =>
facetedTextToDoc(props.question.plaintext, props.question.facets ?? []),
);
return (
{
await props.onSubmit(docToFacetedText(doc));
return false;
}}
actions={
<>
Cancel
Update
>
}
/>
);
}
export function QuestionsModeration(props: {
questions: DocumentQuestion[];
header?: React.ReactNode;
empty: React.ReactNode;
busy?: boolean;
onSetState?: (uri: string, state: QuestionState) => void;
renderAnswer: RenderAnswer;
renderControl: (question: DocumentQuestion) => React.ReactNode;
}) {
let [showHidden, setShowHidden] = useState(false);
let shown = props.questions.filter((q) => q.state !== "hidden");
let ordered = [
...shown.filter((q) => !q.answer),
...shown.filter((q) => q.answer),
];
let hidden = props.questions.filter((q) => q.state === "hidden");
if (showHidden && hidden.length === 0) setShowHidden(false);
let showHandles = showHandlesFor(props.questions);
let stateButton = (
q: DocumentQuestion,
state: QuestionState,
label: string,
) =>
props.onSetState && (
props.onSetState?.(q.uri, state)}
>
{label}
);
let rows = (
questions: DocumentQuestion[],
controls: (q: DocumentQuestion) => React.ReactNode[],
) => (
[props.renderControl(q), ...controls(q)]}
/>
);
return (
{props.header}
{ordered.length === 0
? props.empty
: rows(ordered, (q) => [stateButton(q, "hidden", "Remove")])}
{hidden.length > 0 && (
setShowHidden(!showHidden)}
/>
{showHidden &&
rows(hidden, (q) => [stateButton(q, "pending", "Restore")])}
)}
);
}
// Whether to show handles, judged across everyone the whole list shows: the
// askers and whoever answered.
function showHandlesFor(questions: DocumentQuestion[]) {
return hasSeveralAuthors(
questions.flatMap((q) =>
q.answer ? [q.asker, q.answer.author] : [q.asker],
),
);
}
type RenderAnswer = (
question: DocumentQuestion,
showHandle?: boolean,
) => React.ReactNode;
function QuestionRows(props: {
questions: DocumentQuestion[];
showHandles?: boolean;
renderAnswer: RenderAnswer;
renderEditor?: (question: DocumentQuestion) => React.ReactNode;
renderControls: (question: DocumentQuestion) => React.ReactNode[];
}) {
if (props.questions.length === 0) return null;
return (
{props.questions.map((question) => {
let actions = props.renderControls(question).filter(Boolean);
return (
0 &&
actions.map((control, i) => (
{i > 0 && }
{control}
))
}
/>
);
})}
);
}