import { ISPService } from './ISPService' ; import { sp , Web , Site } from "@pnp/sp" ; import { setup as pnpSetup } from '@pnp/common' ; import { ApplicationCustomizerContext } from '@microsoft/sp-application-base' ; import { ISiteItem } from '../Modules/ISiteItem' ; export class SPService implements ISPService { private appCustomizerContext : ApplicationCustomizerContext | undefined = undefined ; constructor ( context ) { this . appCustomizerContext = context ; pnpSetup ( { spfxContext : context } ) ; } public async getMyFollowedSites ( ) { try { return sp . social . my . followed ( 4 ) . then ( myFollowedSites => { let myFollowedSitesLocal = [ ] ; if ( myFollowedSites ) { const myFollowedSitesPromises = myFollowedSites . map ( async ( msite ) => { let sLogo = undefined ; try { sLogo = await this . getSiteLogo ( msite . Uri ) ; if ( undefined !== sLogo ) { return { Id : msite . Id , Name : msite . Name , Uri : msite . Uri , SiteLogo : sLogo } ; } } catch ( error ) { } } ) ; return Promise . all ( myFollowedSitesPromises ) ; } } ) ; } catch ( err ) { } } description param siteUrl private async getSiteLogo ( siteUrl ) { try { let web = new Web ( siteUrl ) ; try { let sLogoResult = await web . select ( 'SiteLogoUrl' ) . get ( ) ; return sLogoResult . SiteLogoUrl ; } catch ( e ) { return undefined ; } } catch ( error ) { console . error ( JSON . stringify ( error ) ) ; } } description param items param sortBy param descending public SortMyFollowedSites ( items , sortBy , descending ? ) { if ( items && items . length > 0 ) { if ( sortBy && sortBy !== undefined ) { if ( descending ) { return items . sort ( ( a , b ) => { if ( a [ sortBy ] < b [ sortBy ] ) { return 1 ; } if ( a [ sortBy ] > b [ sortBy ] ) { return - 1 ; } return 0 ; } ) ; } else { return items . sort ( ( a , b ) => { if ( a [ sortBy ] < b [ sortBy ] ) { return - 1 ; } if ( a [ sortBy ] > b [ sortBy ] ) { return 1 ; } return 0 ; } ) ; } } } } }