Skip to content

Commit

Permalink
fix pdf opening bug
Browse files Browse the repository at this point in the history
  • Loading branch information
blackforestboi committed May 9, 2024
1 parent 43d8e8b commit 8e7afdf
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 42 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
5 changes: 3 additions & 2 deletions src/dashboard-refactor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1008,12 +1008,13 @@ export class DashboardContainer extends StatefulUIElement<
}}
spaceSearchSuggestions={this.state.spaceSearchSuggestions}
pageInteractionProps={{
onClick: (day, pageResultId) => async (event) =>
onClick: (day, pageResultId) => async (event) => {
this.processEvent('clickPageResult', {
day,
pageResultId: pageResultId,
synthEvent: event,
}),
})
},
onMatchingTextToggleClick: (
day,
pageResultId,
Expand Down
24 changes: 15 additions & 9 deletions src/dashboard-refactor/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4729,18 +4729,24 @@ export class DashboardLogic extends UILogic<State, Events> {
event,
previousState,
}) => {
const pageData =
previousState?.searchResults?.pageData?.byId[event.pageResultId]
const id = event.pageResultId.split('-')[1]

const pageData = previousState?.searchResults?.pageData?.byId[id]

if (!pageData) {
return
}

if (pageData?.fullPdfUrl) {
if (pageData?.fullUrl.includes('memex.cloud/')) {
// This event will be assigned to an anchor <a> el, so we need to override that for PDFs
event.synthEvent.preventDefault()

const memexCloudUrl = new URL(pageData?.fullPdfUrl!)
const pdfUrl = pageData.fullUrl
const PDFurlwithID = await this.options.searchBG.resolvePdfPageFullUrls(
pdfUrl,
)

const memexCloudUrl = new URL(PDFurlwithID.originalLocation)
const uploadId = memexCloudUrl?.searchParams.get('upload_id')

// Uploaded PDFs need to have temporary access URLs fetched
Expand All @@ -4759,7 +4765,7 @@ export class DashboardLogic extends UILogic<State, Events> {
searchResults: {
pageData: {
byId: {
[event.pageResultId]: {
[id]: {
uploadedPdfLinkLoadState: {
$set: taskState,
},
Expand Down Expand Up @@ -4807,15 +4813,15 @@ export class DashboardLogic extends UILogic<State, Events> {
this.emitMutation({ showDropArea: { $set: true } })
}

await openPDFInViewer(pageData.fullPdfUrl!, {
await openPDFInViewer(PDFurlwithID.originalLocation, {
tabsAPI: this.options.tabsAPI,
runtimeAPI: this.options.runtimeAPI,
})
}

if (pageData?.fullUrl && !pageData?.fullPdfUrl) {
window.open(pageData.fullUrl, '_blank')
}
// if (pageData?.fullUrl && !pageData?.fullUrl) {
// window.open(pageData.fullUrl, '_blank')
// }
}

dropPdfFile: EventHandler<'dropPdfFile'> = async ({ event }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,9 @@ export default class PageResultView extends PureComponent<Props> {
type={this.props.type}
normalizedUrl={this.props.normalizedUrl}
originalUrl={this.fullUrl}
onClick={this.props.onClick}
onClick={(event) => {
this.props.onClick(event)
}}
fullTitle={this.props.fullTitle}
pdfUrl={this.props.fullPdfUrl}
favIcon={this.props.favIconURI}
Expand Down
3 changes: 3 additions & 0 deletions src/dashboard-refactor/search-results/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,9 @@ export default class SearchResultsContainer extends React.Component<
isBulkSelected={this.props.selectedItems?.includes(
page.normalizedUrl,
)}
onClick={(event) => {
interactionProps.onClick(event)
}}
youtubeService={this.props.youtubeService}
getListDetailsById={this.props.getListDetailsById}
getRootElement={this.props.getRootElement}
Expand Down
57 changes: 28 additions & 29 deletions src/search/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import { isUrlATweet } from '@worldbrain/memex-common/lib/twitter-integration/ut
import { isUrlAnEventPage } from '@worldbrain/memex-common/lib/unified-search/utils'
import type Dexie from 'dexie'
import { blobToDataURL } from 'src/util/blob-utils'
import { normalizeUrl } from '@worldbrain/memex-common/lib/url-utils/normalize'

const dayMs = 1000 * 60 * 60 * 24

Expand Down Expand Up @@ -104,6 +105,7 @@ export default class SearchBackground {
unifiedSearch: this.unifiedSearch,
extendedSuggest: this.storage.suggestExtended,
delPages: this.options.pages.delPages.bind(this.options.pages),
resolvePdfPageFullUrls: this.resolvePdfPageFullUrls.bind(this),
}
}

Expand Down Expand Up @@ -619,38 +621,35 @@ export default class SearchBackground {
return lookups
}

private async resolvePdfPageFullUrls(
docs: AnnotPage[],
): Promise<AnnotPage[]> {
const toReturn: AnnotPage[] = []
for (const doc of docs) {
if (!isMemexPageAPdf(doc)) {
toReturn.push(doc)
continue
}
async resolvePdfPageFullUrls(url: string): Promise<any> {
const page = {
url: url,
}

const locators = await this.options.pages.findLocatorsByNormalizedUrl(
doc.url,
)
const mainLocator = pickBestLocator(locators, {
priority: ContentLocatorType.Remote,
})
const locators = await this.options.pages.findLocatorsByNormalizedUrl(
normalizeUrl(page.url),
)

// If this is an uploaded PDF, we need to flag it for grabbing a temporary access URL when clicked via the `upload_id` param
if (
mainLocator &&
mainLocator.locationScheme === LocationSchemeType.UploadStorage
) {
doc.fullPdfUrl =
mainLocator.originalLocation +
'?upload_id=' +
mainLocator.location
} else {
doc.fullPdfUrl = mainLocator?.originalLocation ?? undefined
}
const mainLocator = pickBestLocator(locators, {
priority: ContentLocatorType.Remote,
})

toReturn.push(doc)
let document = mainLocator

// If this is an uploaded PDF, we need to flag it for grabbing a temporary access URL when clicked via the `upload_id` param
if (
mainLocator &&
mainLocator.locationScheme === LocationSchemeType.UploadStorage
) {
document.originalLocation =
mainLocator.originalLocation +
'?upload_id=' +
mainLocator.location
} else {
document.originalLocation =
mainLocator?.originalLocation ?? undefined
}
return toReturn

return document
}
}
7 changes: 7 additions & 0 deletions src/search/background/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ export interface RemoteSearchInterface {
suggest: SearchStorage['suggest']
extendedSuggest: SearchStorage['suggestExtended']
delPages: PageIndexingBackground['delPages']
resolvePdfPageFullUrls: (
url: string,
) => Promise<{
fullUrl: string
fullPdfUrl: string
originalLocation: string
}>
}

export type UnifiedSearchParams = TermsSearchOpts & {
Expand Down

0 comments on commit 8e7afdf

Please sign in to comment.