-
Notifications
You must be signed in to change notification settings - Fork 202
Feat: CSV Import #1767
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Feat: CSV Import #1767
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
8bd1869
feat: Import documents from a CSV.
ItzNotABug 6bca626
Merge branch 'feat-pink-v2' into 'csv-imports'.
ItzNotABug 75a3a20
update: copy and maxHeight.
ItzNotABug 7f6cb7f
Merge branch 'feat-pink-v2' into csv-imports
ItzNotABug 6b3a0d5
update: misc logic.
ItzNotABug a35a307
Merge branch 'feat-pink-v2' into 'csv-imports'.
ItzNotABug 014d079
update: realtime progress on csv imports!
ItzNotABug 92af712
fix: realtime logic update on progress.
ItzNotABug 3e147e9
misc: fixes.
ItzNotABug 6c45385
fix: selection on file upload.
ItzNotABug 645eaaa
update: design reviews, add error message.
ItzNotABug c6ca1c1
address comments: design review.
ItzNotABug ca8b328
fix: tabs state calculation.
ItzNotABug 19b637c
Merge branch 'feat-pink-v2' into 'csv-imports'.
ItzNotABug bc5a31f
bump: lockfile.
ItzNotABug a8f583c
Merge branch 'feat-pink-v2' into csv-imports
ItzNotABug e0d93a0
fix: designs on file picker; update: misc. logic here and there.
ItzNotABug 346b2c9
fix: invalid query.
ItzNotABug 6602bb8
update: consistent empty views.
ItzNotABug 2476ad5
address comments.
ItzNotABug be1e6f5
Merge branch 'feat-pink-v2' into 'csv-imports'.
ItzNotABug File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| <script lang="ts"> | ||
| import { onMount } from 'svelte'; | ||
| import { base } from '$app/paths'; | ||
| import { page } from '$app/state'; | ||
| import { sdk } from '$lib/stores/sdk'; | ||
| import { Dependencies } from '$lib/constants'; | ||
| import { goto, invalidate } from '$app/navigation'; | ||
| import { getProjectId } from '$lib/helpers/project'; | ||
| import { writable, type Writable } from 'svelte/store'; | ||
| import { addNotification } from '$lib/stores/notifications'; | ||
| import { Layout, Typography } from '@appwrite.io/pink-svelte'; | ||
| import { type Models, type Payload, Query } from '@appwrite.io/console'; | ||
|
|
||
| type ImportItem = { | ||
| status: string; | ||
| collection?: string; | ||
| }; | ||
|
|
||
| type ImportItemsMap = Map<string, ImportItem>; | ||
|
|
||
| /** | ||
| * Keeps a track of the active and ongoing csv migrations. | ||
| * | ||
| * The structure is as follows - | ||
| * `{ migrationId: { status: status, collection: collection } }` | ||
| */ | ||
| const importItems: Writable<ImportItemsMap> = writable(new Map()); | ||
|
|
||
| async function showCompletionNotification( | ||
| databaseId: string, | ||
| collectionId: string, | ||
| importData: Payload | ||
| ) { | ||
| const projectId = page.params.project; | ||
| await invalidate(Dependencies.DOCUMENTS); | ||
| const url = `${base}/project-${projectId}/databases/database-${databaseId}/collection-${collectionId}`; | ||
|
|
||
| // extract clean message from nested backend error. | ||
| const match = importData.errors.join('').match(/message: '(.*)' Message:/i); | ||
| const errorMessage = match?.[1]; | ||
|
|
||
| const type = importData.status === 'completed' ? 'success' : 'error'; | ||
| const message = | ||
| importData.status === 'completed' | ||
| ? 'CSV import finished successfully.' | ||
| : `${errorMessage}`; | ||
|
|
||
| addNotification({ | ||
| type, | ||
| message, | ||
| isHtml: true, | ||
| buttons: | ||
| collectionId === page.params.collection || type === 'error' | ||
| ? undefined | ||
| : [ | ||
| { | ||
| name: 'View documents', | ||
| method: () => goto(url) | ||
| } | ||
| ] | ||
| }); | ||
| } | ||
|
|
||
| async function updateOrAddItem(importData: Payload | Models.Migration) { | ||
| if (importData.source.toLowerCase() !== 'csv') return; | ||
|
|
||
| const status = importData.status; | ||
| const resourceId = importData.resourceId ?? ''; | ||
| const [databaseId, collectionId] = resourceId.split(':') ?? []; | ||
|
|
||
| const current = $importItems.get(importData.$id); | ||
| let collectionName = current?.collection ?? null; | ||
|
|
||
| if (!collectionName && collectionId) { | ||
| try { | ||
| const collection = await sdk.forProject.databases.getCollection( | ||
| databaseId, | ||
| collectionId | ||
| ); | ||
| collectionName = collection.name; | ||
| } catch { | ||
| collectionName = null; | ||
| } | ||
| } | ||
|
|
||
| importItems.update((items) => { | ||
| const existing = items.get(importData.$id); | ||
|
|
||
| const isDone = (s: string) => s === 'completed' || s === 'failed'; | ||
| const isInProgress = (s: string) => ['pending', 'processing', 'uploading'].includes(s); | ||
|
|
||
| const shouldSkip = | ||
| (existing && isDone(existing.status) && isInProgress(status)) || | ||
| existing?.status === status; | ||
|
|
||
| if (shouldSkip) return items; | ||
|
|
||
| const next = new Map(items); | ||
| next.set(importData.$id, { status, collection: collectionName ?? undefined }); | ||
| return next; | ||
| }); | ||
|
|
||
| if (status === 'completed' || status === 'failed') { | ||
| await showCompletionNotification(databaseId, collectionId, importData); | ||
| } | ||
| } | ||
|
|
||
| function clear() { | ||
| importItems.update((items) => { | ||
| items.clear(); | ||
| return items; | ||
| }); | ||
| } | ||
|
|
||
| function graphSize(status: string): number { | ||
| switch (status) { | ||
| case 'pending': | ||
| return 10; | ||
| case 'processing': | ||
| return 30; | ||
| case 'uploading': | ||
| return 60; | ||
| case 'completed': | ||
| case 'failed': | ||
| return 100; | ||
| default: | ||
| return 30; | ||
| } | ||
| } | ||
|
|
||
| function text(status: string, collectionName = '') { | ||
| const name = collectionName ? `<b>${collectionName}</b>` : ''; | ||
| switch (status) { | ||
| case 'completed': | ||
| case 'failed': | ||
| return `Import to ${name} ${status}`; | ||
| case 'processing': | ||
| return `Importing CSV file${name ? ` to ${name}` : ''}`; | ||
| default: | ||
| return 'Preparing CSV for import...'; | ||
| } | ||
| } | ||
|
|
||
| onMount(() => { | ||
| sdk.forProject.migrations | ||
| .list([Query.equal('source', 'CSV'), Query.equal('status', ['pending', 'processing'])]) | ||
| .then((migrations) => { | ||
| migrations.migrations.forEach(updateOrAddItem); | ||
| }); | ||
|
|
||
| return sdk.forConsole.client.subscribe('console', (response) => { | ||
| if (!response.channels.includes(`projects.${getProjectId()}`)) return; | ||
| if (response.events.includes('migrations.*')) { | ||
| updateOrAddItem(response.payload as Payload); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| $: isOpen = true; | ||
| $: showCsvImportBox = $importItems.size > 0; | ||
| </script> | ||
|
|
||
| {#if showCsvImportBox} | ||
| <Layout.Stack direction="column" gap="l" alignItems="flex-end"> | ||
| <section class="upload-box"> | ||
| <header class="upload-box-header"> | ||
| <h4 class="upload-box-title"> | ||
| <Typography.Text variant="m-500"> | ||
| Importing documents ({$importItems.size}) | ||
| </Typography.Text> | ||
| </h4> | ||
| <button | ||
| class="upload-box-button" | ||
| class:is-open={isOpen} | ||
| aria-label="toggle upload box" | ||
| on:click={() => (isOpen = !isOpen)}> | ||
| <span class="icon-cheveron-up" aria-hidden="true"></span> | ||
| </button> | ||
| <button | ||
| class="upload-box-button" | ||
| aria-label="close backup restore box" | ||
| on:click={clear}> | ||
| <span class="icon-x" aria-hidden="true"></span> | ||
| </button> | ||
| </header> | ||
|
|
||
| {#each [...$importItems.entries()] as [key, value] (key)} | ||
| <div class="upload-box-content" class:is-open={isOpen}> | ||
| <ul class="upload-box-list"> | ||
| <li class="upload-box-item"> | ||
| <section class="progress-bar u-width-full-line"> | ||
| <div | ||
| class="progress-bar-top-line u-flex u-gap-8 u-main-space-between"> | ||
| <Typography.Text> | ||
| {@html text(value.status, value.collection)} | ||
| </Typography.Text> | ||
| </div> | ||
| <div | ||
| class="progress-bar-container" | ||
| class:is-danger={value.status === 'failed'} | ||
| style="--graph-size:{graphSize(value.status)}%"> | ||
| </div> | ||
| </section> | ||
| </li> | ||
| </ul> | ||
| </div> | ||
| {/each} | ||
| </section> | ||
| </Layout.Stack> | ||
| {/if} | ||
|
|
||
| <style lang="scss"> | ||
| .upload-box-title { | ||
| font-size: 11px; | ||
| } | ||
|
|
||
| .upload-box-content { | ||
| min-width: 400px; | ||
| max-width: 100vw; | ||
| } | ||
|
|
||
| .upload-box-button { | ||
| display: flex; | ||
| align-items: center; | ||
| justify-content: center; | ||
| } | ||
|
|
||
| .progress-bar-container { | ||
| height: 4px; | ||
|
|
||
| &::before { | ||
| height: 4px; | ||
| background-color: var(--bgcolor-neutral-invert); | ||
| } | ||
|
|
||
| &.is-danger::before { | ||
| height: 4px; | ||
| background-color: var(--bgcolor-error); | ||
| } | ||
| } | ||
| </style> | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.