Skip to content

Commit

Permalink
fix(frontend): serverContextの型エラーを修正 (#15131)
Browse files Browse the repository at this point in the history
* fix(frontend): serverContextの型エラーを修正

* add comment
  • Loading branch information
kakkokari-gtyih authored Dec 16, 2024
1 parent 3e0fcae commit 0804092
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
5 changes: 3 additions & 2 deletions packages/frontend/src/pages/clip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ import { clipsCache } from '@/cache.js';
import { isSupportShare } from '@/scripts/navigator.js';
import { copyToClipboard } from '@/scripts/copy-to-clipboard.js';
import { genEmbedCode } from '@/scripts/get-embed-code.js';
import { getServerContext } from '@/server-context.js';
import { assertServerContext, serverContext } from '@/server-context.js';

const CTX_CLIP = getServerContext('clip');
// contextは非ログイン状態の情報しかないためログイン時は利用できない
const CTX_CLIP = $i && assertServerContext(serverContext, 'clip') ? serverContext.clip : null;

const props = defineProps<{
clipId: string,
Expand Down
6 changes: 4 additions & 2 deletions packages/frontend/src/pages/note.vue
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ import { dateString } from '@/filters/date.js';
import MkClipPreview from '@/components/MkClipPreview.vue';
import { defaultStore } from '@/store.js';
import { pleaseLogin } from '@/scripts/please-login.js';
import { getServerContext } from '@/server-context.js';
import { serverContext, assertServerContext } from '@/server-context.js';
import { $i } from '@/account.js';

const CTX_NOTE = getServerContext('note');
// contextは非ログイン状態の情報しかないためログイン時は利用できない
const CTX_NOTE = $i && assertServerContext(serverContext, 'note') ? serverContext.note : null;

const props = defineProps<{
noteId: string;
Expand Down
5 changes: 3 additions & 2 deletions packages/frontend/src/pages/user/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import { definePageMetadata } from '@/scripts/page-metadata.js';
import { i18n } from '@/i18n.js';
import { $i } from '@/account.js';
import MkHorizontalSwipe from '@/components/MkHorizontalSwipe.vue';
import { getServerContext } from '@/server-context.js';
import { serverContext, assertServerContext } from '@/server-context.js';

const XHome = defineAsyncComponent(() => import('./home.vue'));
const XTimeline = defineAsyncComponent(() => import('./index.timeline.vue'));
Expand All @@ -53,7 +53,8 @@ const XFlashs = defineAsyncComponent(() => import('./flashs.vue'));
const XGallery = defineAsyncComponent(() => import('./gallery.vue'));
const XRaw = defineAsyncComponent(() => import('./raw.vue'));

const CTX_USER = getServerContext('user');
// contextは非ログイン状態の情報しかないためログイン時は利用できない
const CTX_USER = $i && assertServerContext(serverContext, 'user') ? serverContext.user : null;

const props = withDefaults(defineProps<{
acct: string;
Expand Down
12 changes: 5 additions & 7 deletions packages/frontend/src/server-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@
* SPDX-FileCopyrightText: syuilo and misskey-project
* SPDX-License-Identifier: AGPL-3.0-only
*/

import * as Misskey from 'misskey-js';
import { $i } from '@/account.js';

const providedContextEl = document.getElementById('misskey_clientCtx');

export type ServerContext = {
clip?: Misskey.entities.Clip;
note?: Misskey.entities.Note;
user?: Misskey.entities.UserLite;
user?: Misskey.entities.UserDetailed;
} | null;

export const serverContext: ServerContext = (providedContextEl && providedContextEl.textContent) ? JSON.parse(providedContextEl.textContent) : null;

export function getServerContext<K extends keyof NonNullable<ServerContext>>(entity: K): Required<Pick<NonNullable<ServerContext>, K>> | null {
// contextは非ログイン状態の情報しかないためログイン時は利用できない
if ($i) return null;

return serverContext ? (serverContext[entity] ?? null) : null;
export function assertServerContext<K extends keyof NonNullable<ServerContext>>(ctx: ServerContext, entity: K): ctx is Required<Pick<NonNullable<ServerContext>, K>> {
if (ctx == null) return false;
return entity in ctx && ctx[entity] != null;
}

0 comments on commit 0804092

Please sign in to comment.