Skip to content

Commit

Permalink
apply self code review
Browse files Browse the repository at this point in the history
Signed-off-by: denis-tingaikin <[email protected]>
  • Loading branch information
denis-tingaikin committed Jan 11, 2025
1 parent 564900b commit 63fd5df
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 29 deletions.
4 changes: 2 additions & 2 deletions packages/presentation/src/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,10 @@ async function uploadFileWithSignedUrl (file: File, uuid: string, uploadUrl: str
}
}

export async function getJsonOrEmpty (file: string, name: string, timeoutMs = 3000): Promise<any> {
export async function getJsonOrEmpty (file: string, name: string): Promise<any> {
try {
const fileUrl = getFileUrl(file, name)
const resp = await fetch(fileUrl, { signal: AbortSignal.timeout(timeoutMs) })
const resp = await fetch(fileUrl)
return await resp.json()
} catch {
return []
Expand Down
7 changes: 1 addition & 6 deletions packages/presentation/src/link-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,8 @@ export async function fetchLinkPreviewDetails (url: string, timeoutMs = 15000):
const response = await fetch(`${linkPreviewUrl}?q=${url}`, {
signal: AbortSignal.timeout(timeoutMs)
})
if (!response.ok) {
throw new Error(`status: ${response.status}`)
}
console.log(response)
return response.json() as LinkPreviewDetails
} catch (error) {
console.error(`An error occurced on fetching or parsing data by ${url}, error:`, error)
} catch {
return {}
}
}
15 changes: 15 additions & 0 deletions plugins/attachment-resources/src/components/AttachmentGroup.svelte
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<!-- //
// Copyright © 2025 Hardcore Engineering Inc.
//
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. You may
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and
// limitations under the License.
// -->

<script lang="ts">
import { type Attachment } from '@hcengineering/attachment'
import { type WithLookup, Ref } from '@hcengineering/core'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@
export let removable: boolean = false
export let showPreview = false
export let preview = false
export let progress = false
const dispatch = createEventDispatcher()
const maxLenght: number = 30
const maxLength: number = 30
const trimFilename = (fname: string): string =>
fname.length > maxLenght ? fname.substr(0, (maxLenght - 1) / 2) + '...' + fname.substr(-(maxLenght - 1) / 2) : fname
fname.length > maxLength ? fname.substr(0, (maxLength - 1) / 2) + '...' + fname.substr(-(maxLength - 1) / 2) : fname
$: canRemove =
removable &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
}
function getUrlKey (s: string): string {
const url = new URL(s)
console.log(url)
return longestSegment(url.host) + url.pathname
}
Expand Down Expand Up @@ -332,13 +331,11 @@
function updateLinkPreview (): void {
const hrefs = refContainer.getElementsByTagName('a')
console.log(hrefs)
const newUrls: string[] = []
for (let i = 0; i < hrefs.length; i++) {
if (hrefs[i].target !== '_blank' || !isValidUrl(hrefs[i].href)) {
continue
}
console.log(hrefs[i].href)
const key = getUrlKey(hrefs[i].href)
if (urlSet.has(key)) {
continue
Expand All @@ -361,11 +358,15 @@
async function loadLinks (urls: string[]): Promise<void> {
progress = true
for (const url of urls) {
const meta = await fetchLinkPreviewDetails(url)
if (canDisplayLinkPreview(meta) && meta.url !== undefined) {
const blob = new Blob([JSON.stringify(meta)])
const file = new File([blob], meta.url, { type: 'application/link-preview' })
void createAttachment(file)
try {
const meta = await fetchLinkPreviewDetails(url)
if (canDisplayLinkPreview(meta) && meta.url !== undefined) {
const blob = new Blob([JSON.stringify(meta)])
const file = new File([blob], meta.url, { type: 'application/link-preview' })
void createAttachment(file)
}
} catch (err: any) {
void setPlatformStatus(unknownError(err))
}
}
progress = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
})
</script>

<div class="quote">
<div class="quote content">
{#if viewModel}
<div class="content gapV-2">
<div class="gapV-2">
<div class="flex gap-1">
{#if viewModel.icon !== undefined && !useDefaultIcon}
<img
Expand Down Expand Up @@ -60,7 +60,9 @@
{viewModel.description}
{/if}
{#if viewModel.image}
<img src={viewModel.image} class="round-image" alt="link-preview" />
<a target="_blank" href={viewModel.url}>
<img src={viewModel.image} class="round-image" alt="link-preview" />
</a>
{/if}
</div>
</div>
Expand All @@ -74,9 +76,10 @@

<style lang="scss">
.round-image {
border-radius: 5px;
max-width: 15rem;
max-height: 15rem;
border: 0.5px solid;
border-radius: 7px;
max-width: 25rem;
max-height: 25rem;
}
.preview-icon {
max-width: 22px;
Expand All @@ -87,7 +90,7 @@
padding-left: 15px;
}
.content {
max-width: 15rem;
max-height: 25rem;
max-width: 35rem;
max-height: 35rem;
}
</style>
6 changes: 3 additions & 3 deletions plugins/attachment-resources/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ export function getType (
if (type.startsWith('video/')) {
return 'video'
}
if (type === 'application/pdf') {
if (type.includes('application/pdf')) {
return 'pdf'
}
if (type === 'application/json') {
if (type.includes('application/json')) {
return 'json'
}
if (type.startsWith('text/')) {
return 'text'
}
if (type === 'application/link-preview') {
if (type.includes('application/link-preview')) {
return 'link-preview'
}
return 'other'
Expand Down

0 comments on commit 63fd5df

Please sign in to comment.