40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
/**
|
|
* 기기의 네이티브 공유 시트를 열고 요청을 처리했는지 반환합니다.
|
|
*
|
|
* 사용자가 공유 시트를 닫은 경우도 정상적으로 처리된 것으로 간주합니다.
|
|
*/
|
|
export async function tryNativeShare(data: ShareData): Promise<boolean> {
|
|
if (typeof navigator.share !== 'function') {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
await navigator.share(data);
|
|
return true;
|
|
} catch (error) {
|
|
if (error instanceof Error && error.name === 'AbortError') {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** API 서버가 제공하는 영상별 Open Graph 공유 페이지 URL을 만듭니다. */
|
|
export function buildVideoShareUrl(apiBaseUrl: string, videoId: number | string): string {
|
|
return `${apiBaseUrl.replace(/\/$/, '')}/video/share/${encodeURIComponent(String(videoId))}`;
|
|
}
|
|
|
|
/** 콘텐츠 종류별 공개 공유 URL. video.id 와 ssul_content.id 가 겹치므로 경로를 갈라야 한다. */
|
|
export function buildContentShareUrl(
|
|
apiBaseUrl: string,
|
|
contentId: number | string,
|
|
contentType: 'video' | 'ssul' = 'video',
|
|
): string {
|
|
if (contentType === 'ssul') {
|
|
const origin = typeof window !== 'undefined' ? window.location.origin : '';
|
|
return `${origin}/ssul/${encodeURIComponent(String(contentId))}`;
|
|
}
|
|
return buildVideoShareUrl(apiBaseUrl, contentId);
|
|
}
|