-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
61888ba
commit c1a3c05
Showing
11 changed files
with
1,624 additions
and
1,471 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import * as React from "react"; | ||
import * as React from 'react'; | ||
|
||
import { BaseComponentContext } from "@microsoft/sp-component-base"; | ||
import { BaseComponentContext } from '@microsoft/sp-component-base'; | ||
|
||
import { ISearchImagesResult } from "../models/ISearchImagesResult"; | ||
import { ISearchResult } from "../models/ISearchResult"; | ||
import { ISearchSiteAssetsResult } from "../models/ISearchSiteAssetsResult"; | ||
import { ISearchImagesResult } from '../models/ISearchImagesResult'; | ||
import { ISearchResult } from '../models/ISearchResult'; | ||
import { ISearchSiteAssetsResult } from '../models/ISearchSiteAssetsResult'; | ||
|
||
interface IuseGraphAPI { | ||
searchImages: (query: string, from: number) => Promise<ISearchResult>; | ||
|
@@ -19,105 +19,125 @@ export const useGraphAPI = (context: BaseComponentContext): IuseGraphAPI => { | |
return await context.msGraphClientFactory.getClient("3"); | ||
}, [context]); | ||
|
||
const searchImages = React.useCallback(async (query: string, from: number = 0): Promise<ISearchResult> => { | ||
const queryImages = query.match(/\.jpg|\.png/g); | ||
if (!queryImages || !graphClient) return undefined; | ||
const searchResults = await (await graphClient).api("/search/query").post({ | ||
requests: [ | ||
{ | ||
entityTypes: ["driveItem"], | ||
query: { | ||
queryString: query, | ||
}, | ||
fields: [ | ||
"editor", | ||
"driveId", | ||
"Title", | ||
"Path", | ||
"Filename", | ||
"FileExtension", | ||
"FileType", | ||
"Created", | ||
"Author", | ||
"LastModifiedTime", | ||
"EditorOwsUser", | ||
"ModifiedBy", | ||
"LinkingUrl", | ||
"SiteTitle", | ||
"ParentLink", | ||
"DocumentPreviewMetadata", | ||
"ListID", | ||
"ListItemID", | ||
"SPSiteURL", | ||
"SiteID", | ||
"WebId", | ||
"UniqueID", | ||
"SPWebUrl", | ||
"DefaultEncodingURL", | ||
"PictureThumbnailURL", | ||
const searchImages = React.useCallback( | ||
async (query: string, from: number = 0): Promise<ISearchResult> => { | ||
if (!query || !graphClient) return undefined; | ||
try { | ||
const client = await graphClient; | ||
|
||
const searchResults = await client.api("/search/query").post({ | ||
requests: [ | ||
{ | ||
entityTypes: ["driveItem"], | ||
query: { | ||
queryString: `${query} AND -driveitem:""`, | ||
}, | ||
fields: [ | ||
"editor", | ||
"driveId", | ||
"Title", | ||
"Path", | ||
"Filename", | ||
"FileExtension", | ||
"FileType", | ||
"Created", | ||
"Author", | ||
"LastModifiedTime", | ||
"EditorOwsUser", | ||
"ModifiedBy", | ||
"LinkingUrl", | ||
"SiteTitle", | ||
"ParentLink", | ||
"DocumentPreviewMetadata", | ||
"ListID", | ||
"ListItemID", | ||
"SPSiteURL", | ||
"SiteID", | ||
"WebId", | ||
"UniqueID", | ||
"SPWebUrl", | ||
"DefaultEncodingURL", | ||
"PictureThumbnailURL", | ||
"DriveItem", | ||
"UniqueId", | ||
"BannerImageUrlOWSURLH", | ||
], | ||
from: from, | ||
size: 50, | ||
}, | ||
], | ||
from: from, | ||
size: 50, | ||
}, | ||
], | ||
}); | ||
const hasMoreResults = searchResults.value[0]?.hitsContainers[0]?.moreResultsAvailable; | ||
const total = searchResults.value[0]?.hitsContainers[0]?.total; | ||
const fields = searchResults.value[0]?.hitsContainers[0]?.hits?.map((hit: { resource: { listItem: any } }) => { | ||
return { ...hit.resource?.listItem.fields, id: hit.resource?.listItem.id }; | ||
}) as ISearchImagesResult[]; | ||
return { fields, hasMoreResults, total }; | ||
}, []); | ||
}); | ||
const hasMoreResults = searchResults.value[0]?.hitsContainers[0]?.moreResultsAvailable; | ||
const total = searchResults.value[0]?.hitsContainers[0]?.total; | ||
const fields = searchResults.value[0]?.hitsContainers[0]?.hits?.map((hit: { resource: { listItem: any } }) => { | ||
return { ...hit.resource?.listItem.fields, id: hit.resource?.listItem.id }; | ||
}) as ISearchImagesResult[]; | ||
return { fields, hasMoreResults, total }; | ||
} catch (error) { | ||
console.error("[searchImages] error:", error); | ||
throw error; | ||
} | ||
}, | ||
[graphClient] | ||
); | ||
|
||
const getDriveItemDownloadUrl = React.useCallback( | ||
async (driveId: string, itemId: string): Promise<string> => { | ||
if (!graphClient || !driveId || !itemId) return undefined; | ||
try { | ||
const driveItem = await (await graphClient) | ||
const client = await graphClient; | ||
const driveItem = await client | ||
.api(`/drives/${driveId}/items/${itemId}[email protected]`) | ||
.get(); | ||
return driveItem["@microsoft.graph.downloadUrl"]; | ||
} catch (error) { | ||
console.log("[getDriveItemDownloadUrl] error:", error); | ||
console.error("[getDriveItemDownloadUrl] error:", error); | ||
throw error; | ||
} | ||
}, | ||
[graphClient] | ||
); | ||
|
||
const getSiteAssetsLibrary = React.useCallback(async (site: string): Promise<ISearchSiteAssetsResult> => { | ||
if (!site || !graphClient) return undefined; | ||
const query = `path:${site}/SiteAssets`; | ||
const searchResults = await (await graphClient).api("/search/query").post({ | ||
requests: [ | ||
{ | ||
entityTypes: ["drive"], | ||
query: { | ||
queryString: query, | ||
}, | ||
fields: [ | ||
"editor", | ||
"driveId", | ||
"Title", | ||
"Path", | ||
"Filename", | ||
"FileExtension", | ||
"id", | ||
"name", | ||
"path", | ||
"parentReference", | ||
const getSiteAssetsLibrary = React.useCallback( | ||
async (site: string): Promise<ISearchSiteAssetsResult> => { | ||
if (!site || !graphClient) return undefined; | ||
try { | ||
const client = await graphClient; | ||
const query = `path:${site}/SiteAssets`; | ||
const searchResults = await client.api("/search/query").post({ | ||
requests: [ | ||
{ | ||
entityTypes: ["drive"], | ||
query: { | ||
queryString: query, | ||
}, | ||
fields: [ | ||
"editor", | ||
"driveId", | ||
"Title", | ||
"Path", | ||
"Filename", | ||
"FileExtension", | ||
"id", | ||
"name", | ||
"path", | ||
"parentReference", | ||
], | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
}); | ||
|
||
const fields = searchResults.value[0]?.hitsContainers[0]?.hits?.map( | ||
(hit: { resource: ISearchSiteAssetsResult }) => { | ||
return { ...hit.resource }; | ||
const fields = searchResults.value[0]?.hitsContainers[0]?.hits?.map( | ||
(hit: { resource: ISearchSiteAssetsResult }) => ({ ...hit.resource }) | ||
) as ISearchSiteAssetsResult[]; | ||
return fields[0]; | ||
} catch (error) { | ||
console.error("[getSiteAssetsLibrary] error:", error); | ||
throw error; | ||
} | ||
) as ISearchSiteAssetsResult; | ||
return fields[0]; | ||
}, []); | ||
}, | ||
[graphClient] | ||
); | ||
|
||
return { | ||
searchImages, | ||
|
Oops, something went wrong.