Skip to content
Merged
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
8 changes: 3 additions & 5 deletions app/[lang]/posts/[postPath]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import { getSession } from '@/lib/auth/session';
import { formatDate } from '@/lib/dashboard/format';
import { localizedPermanentRedirect } from '@/lib/i18n/request';
import { getLocale, initLingui } from '@/lib/i18n/server';
import { isGithubAvatar } from '@/lib/posts/avatar';
import { getGithubProfileUrl } from '@/lib/posts/github-profile';
import { buildPostMetadata, getSiteOrigin } from '@/lib/posts/metadata';
import { buildBlogPostingJsonLd, serializeJsonLd } from '@/lib/seo/json-ld';
import { localizePath } from '@/lib/i18n/config';
Expand Down Expand Up @@ -93,8 +91,8 @@ export default async function PostPage({ params }: PostPageProps) {
}

const isRepublished = post.updatedAt.getTime() > post.publishedAt.getTime();
// 작성자 GitHub 아이디가 있으면 아바타·이름을 GitHub 프로필 새 탭 링크로
const authorProfileUrl = getGithubProfileUrl(post.author.githubLogin);
// 글쓴이가 프로필을 보이기로 했으면 아바타·이름을 GitHub 프로필 새 탭 링크로
const authorProfileUrl = post.author.profileUrl;
// 검색 결과의 글 정보 (schema.org BlogPosting, 대표 주소는 게시할 때의 언어 기준)
const origin = getSiteOrigin();
const canonicalUrl =
Expand All @@ -119,7 +117,7 @@ export default async function PostPage({ params }: PostPageProps) {
);
const author = (
<>
{isGithubAvatar(post.author.image) && (
{post.author.image && (
<Image
src={post.author.image}
alt=""
Expand Down
14 changes: 11 additions & 3 deletions components/studio/publish-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { useLocale } from '@/hooks/use-locale';
import { useLocalizedRouter } from '@/hooks/use-localized-router';
import { publishDraft, unpublishDraft } from '@/lib/actions/posts';
import type { PostVisibility } from '@/lib/db/schema';
import type { AuthorDisplaySettings } from '@/lib/posts/author-display';
import { checkPublishable } from '@/lib/posts/post-content';
import { getPostPath } from '@/lib/posts/post-path';
import type { StudioDraft } from '@/lib/studio/queries';
Expand Down Expand Up @@ -116,7 +117,10 @@ const PublishActions = ({
setOpenDialog('publish');
};

const handlePublish = async (visibility: PostVisibility) => {
const handlePublish = async (
visibility: PostVisibility,
authorDisplay: AuthorDisplaySettings,
) => {
if (!draft || !editor || isPending) {
return;
}
Expand All @@ -141,6 +145,7 @@ const PublishActions = ({
JSON.stringify(editor.getJSON()),
visibility,
locale,
authorDisplay,
);

if (result.status === 'published') {
Expand Down Expand Up @@ -283,18 +288,21 @@ const PublishActions = ({
)}
</div>
)}
{openDialog === 'publish' && (
{openDialog === 'publish' && draft && (
<PublishDialog
isRepublish={primary === 'republish'}
initialVisibility={
publishState.post?.visibility ?? DEFAULT_VISIBILITY
}
author={draft.publishAuthor}
isPrivateRepository={repository.isPrivate}
blankCount={content.blankCount}
hasNoTitle={content.hasNoTitle}
failure={failure}
isPending={isPending}
onSubmit={(visibility) => void handlePublish(visibility)}
onSubmit={(visibility, authorDisplay) =>
void handlePublish(visibility, authorDisplay)
}
onClose={closeDialog}
/>
)}
Expand Down
171 changes: 171 additions & 0 deletions components/studio/publish-author-fields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
'use client';

import { useId } from 'react';
import Image from 'next/image';
import { Trans, useLingui } from '@lingui/react/macro';
import {
AUTHOR_NICKNAME_MAX_LENGTH,
getNicknameProblem,
normalizeAuthorNickname,
resolvePostAuthor,
type AuthorDisplayDraft,
type AuthorDisplayMode,
type AuthorIdentity,
} from '@/lib/posts/author-display';

interface PublishAuthorFieldsProps {
identity: AuthorIdentity;
value: AuthorDisplayDraft;
onChange: (value: AuthorDisplayDraft) => void;
}

/**
* 게시 창의 작성자 표시 선택
* - GitHub 이름 / GitHub 아이디(있을 때만) / 별명 중 하나 + 프로필 사진·링크 표시 여부
* - 글에 실제로 보일 모습을 아래에 미리 보여 줌
*/
const PublishAuthorFields = ({
identity,
value,
onChange,
}: PublishAuthorFieldsProps) => {
const { t } = useLingui();
const nicknameId = useId();
const problemId = useId();
const problem = getNicknameProblem(value);
const preview = resolvePostAuthor(identity, {
mode: value.mode,
nickname: normalizeAuthorNickname(value.nickname),
showProfile: value.showProfile,
});
const options: Array<{
mode: AuthorDisplayMode;
label: string;
hint?: string;
}> = [
{ mode: 'name', label: t`GitHub 이름`, hint: identity.name },
...(identity.githubLogin
? [
{
mode: 'login' as const,
label: t`GitHub 아이디`,
hint: identity.githubLogin,
},
]
: []),
{ mode: 'nickname', label: t`별명` },
];

return (
<fieldset className="flex flex-col gap-2">
<legend className="mb-2 text-[12px] leading-[normal] font-medium text-ink">
<Trans>작성자 표시</Trans>
</legend>
{options.map((option) => (
<label
key={option.mode}
className="flex min-w-0 cursor-pointer items-center gap-2.5 text-[13px] leading-[normal] text-ink"
>
<input
type="radio"
name="author-display"
value={option.mode}
checked={value.mode === option.mode}
onChange={() => onChange({ ...value, mode: option.mode })}
className="accent-brand"
/>
<span className="shrink-0 font-medium">{option.label}</span>
{option.hint && (
<span className="truncate text-[12px] text-ink-faint">
{option.hint}
</span>
)}
</label>
))}
{value.mode === 'nickname' && (
<div className="flex flex-col gap-1.5 pl-6">
<label htmlFor={nicknameId} className="sr-only">
<Trans>별명</Trans>
</label>
<input
id={nicknameId}
type="text"
value={value.nickname}
maxLength={AUTHOR_NICKNAME_MAX_LENGTH * 2}
autoComplete="off"
spellCheck={false}
onChange={(event) =>
onChange({ ...value, nickname: event.target.value })
}
placeholder={t`글에 보일 별명 (${AUTHOR_NICKNAME_MAX_LENGTH}자 이내)`}
aria-invalid={problem === 'invalid'}
aria-describedby={problem ? problemId : undefined}
className={`h-9 w-full rounded border bg-white px-3 text-[13px] leading-[normal] text-ink outline-none placeholder:text-ink-subtle ${
problem === 'invalid'
? 'border-danger'
: 'border-line focus:border-brand'
}`}
/>
{problem === 'invalid' && (
<p
id={problemId}
className="text-[12px] leading-[1.6] break-keep text-danger"
>
<Trans>
별명은 {AUTHOR_NICKNAME_MAX_LENGTH}자 이내로, 보이지 않는 문자
없이 입력해 주세요
</Trans>
</p>
)}
{problem === 'empty' && (
<p
id={problemId}
className="text-[12px] leading-[1.6] break-keep text-ink-faint"
>
<Trans>별명을 입력해야 게시할 수 있습니다</Trans>
</p>
)}
</div>
)}
<label className="mt-1 flex cursor-pointer items-center gap-2.5 text-[13px] leading-[normal] text-ink">
<input
type="checkbox"
checked={value.showProfile}
onChange={(event) =>
onChange({ ...value, showProfile: event.target.checked })
}
className="accent-brand"
/>
<Trans>GitHub 프로필 사진과 링크 보이기</Trans>
</label>
<div className="mt-1 flex flex-col gap-2 rounded border border-line bg-surface p-3">
<span className="text-[11px] leading-[normal] text-ink-faint">
<Trans>글에는 이렇게 보여요</Trans>
</span>
<span className="flex min-w-0 items-center gap-2.5 text-[13px] leading-[normal]">
{preview.image && (
<Image
src={preview.image}
alt=""
width={24}
height={24}
className="size-6 rounded-full"
/>
)}
<span className="truncate font-medium text-ink">
{problem ? t`(별명)` : preview.name}
</span>
</span>
<span className="text-[12px] leading-[1.6] break-keep text-ink-faint">
{preview.profileUrl ? (
<Trans>이름을 누르면 GitHub 프로필이 열립니다</Trans>
) : (
<Trans>GitHub 프로필로 연결되지 않습니다</Trans>
)}
</span>
</div>
</fieldset>
);
};

export default PublishAuthorFields;
43 changes: 37 additions & 6 deletions components/studio/publish-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ import { msg } from '@lingui/core/macro';
import type { MessageDescriptor } from '@lingui/core';
import { Plural, Trans, useLingui } from '@lingui/react/macro';
import DialogFrame from '@/components/shared/dialog-frame';
import PublishAuthorFields from '@/components/studio/publish-author-fields';
import {
BRAND_BUTTON_CLASS,
OUTLINE_BUTTON_CLASS,
} from '@/components/studio/studio-styles';
import type { PostVisibility } from '@/lib/db/schema';
import {
getNicknameProblem,
normalizeAuthorNickname,
toAuthorDisplayDraft,
type AuthorDisplaySettings,
} from '@/lib/posts/author-display';
import type { PublishAuthor } from '@/lib/studio/queries';

// 게시 창 안에 보이는 실패 사유 (서버 응답과 요청 실패)
export type PublishFailure =
Expand Down Expand Up @@ -52,6 +60,8 @@ interface PublishDialogProps {
isRepublish: boolean;
// 처음 선택돼 있을 공개 범위 (다시 게시면 현재 값)
initialVisibility: PostVisibility;
// 작성자 표시 미리보기용 GitHub 정보와 처음 선택돼 있을 설정
author: PublishAuthor;
// 비공개 저장소의 초안이면 코드가 공개된다는 경고를 보임
isPrivateRepository: boolean;
// 본문에 남은 빈칸 노드 수 (채웠든 아니든 하나라도 있으면 게시할 수 없음)
Expand All @@ -60,17 +70,21 @@ interface PublishDialogProps {
hasNoTitle: boolean;
failure: PublishFailure | null;
isPending: boolean;
onSubmit: (visibility: PostVisibility) => void;
onSubmit: (
visibility: PostVisibility,
authorDisplay: AuthorDisplaySettings,
) => void;
onClose: () => void;
}

/**
* 게시 확인 창: 공개 범위 선택, 비공개 저장소 경고, 게시할 수 없는 사유 표시
* - 열 때마다 새로 마운트되어 공개 범위 선택이 초기화됨
* 게시 확인 창: 공개 범위·작성자 표시 선택, 비공개 저장소 경고, 게시할 수 없는 사유 표시
* - 열 때마다 새로 마운트되어 선택이 초기화됨
*/
const PublishDialog = ({
isRepublish,
initialVisibility,
author,
isPrivateRepository,
blankCount,
hasNoTitle,
Expand All @@ -81,7 +95,12 @@ const PublishDialog = ({
}: PublishDialogProps) => {
const { t, i18n } = useLingui();
const [visibility, setVisibility] = useState(initialVisibility);
const [authorDraft, setAuthorDraft] = useState(() =>
toAuthorDisplayDraft(author.initial),
);
const hasBlanks = blankCount > 0;
const hasNicknameProblem = getNicknameProblem(authorDraft) !== null;
const cannotSubmit = hasBlanks || hasNoTitle || hasNicknameProblem;
const reason: PublishFailure | null =
failure ?? (!hasBlanks && hasNoTitle ? 'no_title' : null);

Expand All @@ -95,8 +114,15 @@ const PublishDialog = ({
onSubmit={(event) => {
event.preventDefault();

if (!isPending && !hasBlanks && !hasNoTitle) {
onSubmit(visibility);
if (!isPending && !cannotSubmit) {
onSubmit(visibility, {
mode: authorDraft.mode,
nickname:
authorDraft.mode === 'nickname'
? normalizeAuthorNickname(authorDraft.nickname)
: null,
showProfile: authorDraft.showProfile,
});
}
}}
className="flex flex-col gap-6"
Expand Down Expand Up @@ -135,6 +161,11 @@ const PublishDialog = ({
</label>
))}
</fieldset>
<PublishAuthorFields
identity={author.identity}
value={authorDraft}
onChange={setAuthorDraft}
/>
{isPrivateRepository && (
<p className="text-[12px] leading-[1.6] break-keep text-danger">
<Trans>비공개 저장소의 코드가 본문에 그대로 공개됩니다</Trans>
Expand Down Expand Up @@ -169,7 +200,7 @@ const PublishDialog = ({
</button>
<button
type="submit"
disabled={isPending || hasBlanks || hasNoTitle}
disabled={isPending || cannotSubmit}
aria-busy={isPending}
className={`h-8.5 disabled:border-disabled disabled:bg-disabled disabled:text-ink-subtle disabled:opacity-100 ${BRAND_BUTTON_CLASS}`}
>
Expand Down
5 changes: 5 additions & 0 deletions drizzle/0030_nasty_lethal_legion.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ALTER TABLE "comdraft"."post" ADD COLUMN "author_display" text DEFAULT 'name' NOT NULL;--> statement-breakpoint
ALTER TABLE "comdraft"."post" ADD COLUMN "author_nickname" text;--> statement-breakpoint
ALTER TABLE "comdraft"."post" ADD COLUMN "show_author_profile" boolean DEFAULT true NOT NULL;--> statement-breakpoint
ALTER TABLE "comdraft"."post" ADD CONSTRAINT "post_author_display_check" CHECK ("comdraft"."post"."author_display" in ('name', 'login', 'nickname'));--> statement-breakpoint
ALTER TABLE "comdraft"."post" ADD CONSTRAINT "post_author_nickname_check" CHECK (("comdraft"."post"."author_display" = 'nickname') = ("comdraft"."post"."author_nickname" is not null));
Loading