Skip to content

Commit 84f172a

Browse files
committed
fix(social-links): updated social links and logic for it
1 parent d8ae6a7 commit 84f172a

7 files changed

Lines changed: 64 additions & 47 deletions

File tree

src/app/features/metadata/components/cedar-template-form/cedar-template-form.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { toSignal } from '@angular/core/rxjs-interop';
2323
import { ActivatedRoute } from '@angular/router';
2424

2525
import { ENVIRONMENT } from '@core/provider/environment.provider';
26+
import { SocialShareService } from '@osf/shared/services/social-share.service';
2627

2728
import { CEDAR_CONFIG, CEDAR_VIEWER_CONFIG } from '../../constants';
2829
import { CedarMetadataHelper } from '../../helpers';
@@ -62,6 +63,7 @@ export class CedarTemplateFormComponent {
6263

6364
private route = inject(ActivatedRoute);
6465
readonly environment = inject(ENVIRONMENT);
66+
private readonly socialShareService = inject(SocialShareService);
6567

6668
readonly recordId = signal<string>('');
6769
readonly downloadUrl = signal<string>('');
@@ -184,18 +186,18 @@ export class CedarTemplateFormComponent {
184186

185187
handleEmailShare(): void {
186188
const url = window.location.href;
187-
window.location.href = `mailto:?subject=${this.schemaName()}&body=${url}`;
189+
window.location.href = this.socialShareService.getEmailLink(this.schemaName(), url);
188190
}
189191

190192
handleXShare(): void {
191193
const url = window.location.href;
192-
const link = `https://x.com/intent/tweet?url=${url}&text=${this.schemaName()}&via=OSFramework`;
194+
const link = this.socialShareService.getXLink(this.schemaName(), url);
193195
window.open(link, '_blank', 'noopener,noreferrer');
194196
}
195197

196198
handleFacebookShare(): void {
197199
const url = window.location.href;
198-
const link = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
200+
const link = this.socialShareService.getFacebookLink(url);
199201
window.open(link, '_blank', 'noopener,noreferrer');
200202
}
201203
}

src/app/shared/components/files-tree/files-tree.component.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { CustomDialogService } from '@osf/shared/services/custom-dialog.service'
3737
import { DataciteService } from '@osf/shared/services/datacite/datacite.service';
3838
import { FilesService } from '@osf/shared/services/files.service';
3939
import { FilesShareEmbedService } from '@osf/shared/services/files-share-embed.service';
40-
import { ToastService } from '@osf/shared/services/toast.service';
4140
import { ViewOnlyLinkHelperService } from '@osf/shared/services/view-only-link-helper.service';
4241
import { FileModel } from '@shared/models/files/file.model';
4342
import { FileFolderModel } from '@shared/models/files/file-folder.model';
@@ -60,7 +59,6 @@ export class FilesTreeComponent {
6059
private readonly destroyRef = inject(DestroyRef);
6160
private readonly router = inject(Router);
6261
private readonly filesService = inject(FilesService);
63-
private readonly toastService = inject(ToastService);
6462
private readonly customConfirmationService = inject(CustomConfirmationService);
6563
private readonly customDialogService = inject(CustomDialogService);
6664
private readonly dataciteService = inject(DataciteService);
@@ -88,7 +86,7 @@ export class FilesTreeComponent {
8886
uploadFilesConfirmed = output<File[] | File>();
8987
setCurrentFolder = output<FileFolderModel>();
9088
setMoveDialogCurrentFolder = output<FileFolderModel>();
91-
deleteEntryAction = output<string>();
89+
deleteEntryAction = output<FileModel>();
9290
renameEntryAction = output<RenamedFileLinkModel>();
9391
loadFiles = output<FilePageLinkModel>();
9492
selectFile = output<FileModel>();
@@ -227,7 +225,7 @@ export class FilesTreeComponent {
227225
messageKey:
228226
file.kind === FileKind.Folder ? 'files.dialogs.deleteFolder.message' : 'files.dialogs.deleteFile.message',
229227
acceptLabelKey: 'common.buttons.remove',
230-
onConfirm: () => this.deleteEntryAction.emit(file.links.delete),
228+
onConfirm: () => this.deleteEntryAction.emit(file),
231229
});
232230
}
233231

@@ -390,16 +388,6 @@ export class FilesTreeComponent {
390388
}
391389

392390
private handleEmbedAction(file: FileModel, embedType?: string): void {
393-
const embedHtml = this.filesShareEmbedService.getEmbedHtml(file, embedType);
394-
395-
if (!embedHtml) {
396-
return;
397-
}
398-
399-
const copied = this.filesShareEmbedService.copyToClipboard(embedHtml);
400-
401-
if (copied) {
402-
this.toastService.showSuccess('files.detail.toast.copiedToClipboard');
403-
}
391+
this.filesShareEmbedService.copyEmbedToClipboard(file.links.render, embedType);
404392
}
405393
}

src/app/shared/components/socials-share-button/socials-share-button.component.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,7 @@ export class SocialsShareButtonComponent {
3333
? this.socialShareService.createPreprintUrl(this.resourceId(), this.resourceProvider())
3434
: this.socialShareService.createGuidUrl(this.resourceId());
3535

36-
const shareableContent: SocialShareContentModel = {
37-
id: this.resourceId(),
38-
title: this.resourceTitle(),
39-
url: resourceUrl,
40-
};
36+
const shareableContent: SocialShareContentModel = { title: this.resourceTitle(), url: resourceUrl };
4137

4238
return this.socialShareService.generateSocialActionItems(shareableContent);
4339
});

src/app/shared/constants/social-share.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export const SOCIAL_SHARE_URLS = {
22
email: 'mailto:',
3-
twitter: { preview_url: 'https://twitter.com/intent/tweet', viaHandle: 'OsfFramework' },
3+
x: { preview_url: 'https://x.com/intent/tweet', viaHandle: 'OsfFramework' },
44
facebook: 'https://www.facebook.com/sharer/sharer.php',
5+
facebookShare: 'https://www.facebook.com/dialog/share',
56
linkedIn: 'https://www.linkedin.com/sharing/share-offsite',
67
mastodon: 'https://mastodonshare.com',
78
bluesky: 'https://bsky.app/intent/compose',
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export interface SocialShareContentModel {
2-
id: string;
32
title: string;
43
url: string;
54
}
Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
11
import { Clipboard } from '@angular/cdk/clipboard';
22
import { inject, Injectable } from '@angular/core';
33

4-
import { ENVIRONMENT } from '@osf/core/provider/environment.provider';
54
import { embedDynamicJs, embedStaticHtml } from '@shared/constants/file-embed.constants';
65

76
import { FileModel } from '../models/files/file.model';
87
import { FileShareLink } from '../models/files/file-share-link.model';
98

9+
import { SocialShareService } from './social-share.service';
10+
import { ToastService } from './toast.service';
11+
1012
@Injectable({
1113
providedIn: 'root',
1214
})
1315
export class FilesShareEmbedService {
14-
private readonly environment = inject(ENVIRONMENT);
1516
private readonly clipboard = inject(Clipboard);
17+
private readonly socialShareService = inject(SocialShareService);
18+
private readonly toastService = inject(ToastService);
1619

1720
private readonly EMBED_PLACEHOLDER = 'ENCODED_URL';
1821

1922
getShareLink(file: FileModel, shareType?: string): FileShareLink | null {
20-
const url = file.links?.html;
21-
const name = file.name;
22-
23-
if (!url || !name) return null;
23+
const name = file.name ?? '';
24+
const url = file.links?.html ?? '';
2425

25-
const encodedUrl = encodeURIComponent(url);
26-
const encodedName = encodeURIComponent(name);
26+
if (!url) {
27+
return null;
28+
}
2729

2830
switch (shareType) {
2931
case 'email':
3032
return {
31-
link: `mailto:?subject=${encodedName}&body=${encodedUrl}`,
33+
link: this.socialShareService.getEmailLink(name, url),
3234
target: '_self',
3335
};
3436
case 'twitter':
3537
return {
36-
link: `https://x.com/intent/tweet?url=${encodedUrl}&text=${encodedName}&via=OSFramework`,
38+
link: this.socialShareService.getXLink(name, url),
3739
target: '_blank',
3840
};
39-
case 'facebook': {
40-
const appId = this.environment.facebookAppId;
41+
case 'facebook':
4142
return {
42-
link: `https://www.facebook.com/dialog/share?app_id=${appId}&display=popup&href=${encodedUrl}&redirect_uri=${encodedUrl}`,
43+
link: this.socialShareService.getFacebookLink(url),
4344
target: '_blank',
4445
};
45-
}
4646
default:
4747
return null;
4848
}
4949
}
5050

51-
getEmbedHtml(file: FileModel, embedType?: string): string {
51+
getEmbedHtml(url: string, embedType?: string): string {
5252
switch (embedType) {
5353
case 'dynamic':
54-
return embedDynamicJs.replace(this.EMBED_PLACEHOLDER, file.links.render);
54+
return embedDynamicJs.replace(this.EMBED_PLACEHOLDER, url);
5555
case 'static':
56-
return embedStaticHtml.replace(this.EMBED_PLACEHOLDER, file.links.render);
56+
return embedStaticHtml.replace(this.EMBED_PLACEHOLDER, url);
5757
default:
5858
return '';
5959
}
6060
}
6161

62-
copyToClipboard(value: string): boolean {
63-
return this.clipboard.copy(value);
62+
copyEmbedToClipboard(url: string, embedType?: string): boolean {
63+
const embedHtml = this.getEmbedHtml(url, embedType);
64+
65+
if (!embedHtml) {
66+
return false;
67+
}
68+
69+
const copied = this.clipboard.copy(embedHtml);
70+
71+
if (copied) {
72+
this.toastService.showSuccess('files.detail.toast.copiedToClipboard');
73+
}
74+
75+
return copied;
6476
}
6577
}

src/app/shared/services/social-share.service.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ export class SocialShareService {
1818
return this.environment.webUrl;
1919
}
2020

21+
getEmailLink(title: string, url: string): string {
22+
return this.generateEmailLink({ title, url });
23+
}
24+
25+
getXLink(title: string, url: string): string {
26+
return this.generateXLink({ title, url });
27+
}
28+
29+
getFacebookLink(url: string): string {
30+
return this.generateFacebookLink({ title: '', url });
31+
}
32+
33+
getFacebookCustomLink(url: string): string {
34+
const encodedUrl = encodeURIComponent(url);
35+
const appId = this.environment.facebookAppId;
36+
37+
return `${SOCIAL_SHARE_URLS.facebookShare}?app_id=${appId}&display=popup&href=${encodedUrl}&redirect_uri=${encodedUrl}`;
38+
}
39+
2140
generateAllSharingLinks(content: SocialShareContentModel): SocialShareLinksModel {
2241
return {
2342
email: this.generateEmailLink(content),
24-
twitter: this.generateTwitterLink(content),
43+
twitter: this.generateXLink(content),
2544
facebook: this.generateFacebookLink(content),
2645
linkedIn: this.generateLinkedInLink(content),
2746
mastodon: this.generateMastodonLink(content),
@@ -58,11 +77,11 @@ export class SocialShareService {
5877
return `${SOCIAL_SHARE_URLS.email}?subject=${subject}&body=${body}`;
5978
}
6079

61-
private generateTwitterLink(content: SocialShareContentModel): string {
80+
private generateXLink(content: SocialShareContentModel): string {
6281
const url = encodeURIComponent(content.url);
6382
const text = encodeURIComponent(content.title);
6483

65-
return `${SOCIAL_SHARE_URLS.twitter.preview_url}?url=${url}&text=${text}&via=${SOCIAL_SHARE_URLS.twitter.viaHandle}`;
84+
return `${SOCIAL_SHARE_URLS.x.preview_url}?url=${url}&text=${text}&via=${SOCIAL_SHARE_URLS.x.viaHandle}`;
6685
}
6786

6887
private generateFacebookLink(content: SocialShareContentModel): string {

0 commit comments

Comments
 (0)