Skip to content

Commit 7d64c82

Browse files
committed
♻️(frontend) get media url from config endpoint
We centralized the configuration on the backend side, it is easier to manage and we can change the configuration without having to rebuild the frontend. We now use the config endpoint to get the media url, we refacto to remove the frontend env occurences and to adapt with the new way to get the media url.
1 parent 6252227 commit 7d64c82

File tree

10 files changed

+50
-14
lines changed

10 files changed

+50
-14
lines changed

src/frontend/Dockerfile

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ ENV NEXT_PUBLIC_Y_PROVIDER_URL=${Y_PROVIDER_URL}
6767
ARG API_ORIGIN
6868
ENV NEXT_PUBLIC_API_ORIGIN=${API_ORIGIN}
6969

70-
ARG MEDIA_URL
71-
ENV NEXT_PUBLIC_MEDIA_URL=${MEDIA_URL}
72-
7370
ARG SW_DEACTIVATED
7471
ENV NEXT_PUBLIC_SW_DEACTIVATED=${SW_DEACTIVATED}
7572

src/frontend/apps/e2e/__tests__/app-impress/config.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import path from 'path';
2+
13
import { expect, test } from '@playwright/test';
24

5+
import { createDoc } from './common';
6+
37
const config = {
48
COLLABORATION_SERVER_URL: 'ws://localhost:4444',
59
ENVIRONMENT: 'development',
@@ -77,4 +81,33 @@ test.describe('Config', () => {
7781
// alt 'Gouvernement Logo' comes from the theme
7882
await expect(footer.getByAltText('Gouvernement Logo')).toBeVisible();
7983
});
84+
85+
test('it checks that media server is configured from config endpoint', async ({
86+
page,
87+
browserName,
88+
}) => {
89+
await page.goto('/');
90+
91+
await createDoc(page, 'doc-media', browserName, 1);
92+
93+
const fileChooserPromise = page.waitForEvent('filechooser');
94+
95+
await page.locator('.bn-block-outer').last().fill('/');
96+
await page.getByText('Resizable image with caption').click();
97+
await page.getByText('Upload image').click();
98+
99+
const fileChooser = await fileChooserPromise;
100+
await fileChooser.setFiles(
101+
path.join(__dirname, 'assets/logo-suite-numerique.png'),
102+
);
103+
104+
const image = page.getByRole('img', { name: 'logo-suite-numerique.png' });
105+
106+
await expect(image).toBeVisible();
107+
108+
// Check src of image
109+
expect(await image.getAttribute('src')).toMatch(
110+
/http:\/\/localhost:8083\/media\/.*\/attachments\/.*.png/,
111+
);
112+
});
80113
});

src/frontend/apps/impress/.env

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
NEXT_PUBLIC_API_ORIGIN=
22
NEXT_PUBLIC_Y_PROVIDER_URL=
3-
NEXT_PUBLIC_MEDIA_URL=
43
NEXT_PUBLIC_SW_DEACTIVATED=
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
NEXT_PUBLIC_API_ORIGIN=http://localhost:8071
22
NEXT_PUBLIC_Y_PROVIDER_URL=ws://localhost:4444
3-
NEXT_PUBLIC_MEDIA_URL=http://localhost:8083
43
NEXT_PUBLIC_SW_DEACTIVATED=true

src/frontend/apps/impress/src/core/conf.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
export const mediaUrl = () =>
2-
process.env.NEXT_PUBLIC_MEDIA_URL ||
3-
(typeof window !== 'undefined' ? window.location.origin : '');
4-
51
export const backendUrl = () =>
62
process.env.NEXT_PUBLIC_API_ORIGIN ||
73
(typeof window !== 'undefined' ? window.location.origin : '');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './useMediaUrl';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useConfig } from '../api';
2+
3+
export const useMediaUrl = () => {
4+
const { data: conf } = useConfig();
5+
6+
return (
7+
conf?.MEDIA_BASE_URL ||
8+
(typeof window !== 'undefined' ? window.location.origin : '')
9+
);
10+
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export * from './api/useConfig';
1+
export * from './api/';
22
export * from './ConfigProvider';
3+
export * from './hooks';

src/frontend/apps/impress/src/custom-next.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ declare module '*.svg?url' {
2020
namespace NodeJS {
2121
interface ProcessEnv {
2222
NEXT_PUBLIC_API_ORIGIN?: string;
23-
NEXT_PUBLIC_MEDIA_URL?: string;
2423
NEXT_PUBLIC_Y_PROVIDER_URL?: string;
2524
NEXT_PUBLIC_SW_DEACTIVATED?: string;
2625
}

src/frontend/apps/impress/src/features/docs/doc-editor/components/BlockNoteEditor.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import React, { useCallback, useEffect } from 'react';
99
import { useTranslation } from 'react-i18next';
1010

1111
import { Box, TextErrors } from '@/components';
12-
import { mediaUrl } from '@/core';
1312
import { useAuthStore } from '@/core/auth';
13+
import { useMediaUrl } from '@/core/config';
1414
import { Doc } from '@/features/docs/doc-management';
1515

1616
import { useCreateDocAttachment } from '../api/useCreateDocUpload';
@@ -92,6 +92,7 @@ export const BlockNoteEditor = ({
9292
const isVersion = doc.id !== storeId;
9393
const { userData } = useAuthStore();
9494
const { setEditor } = useEditorStore();
95+
const mediaUrl = useMediaUrl();
9596

9697
const readOnly = !doc.abilities.partial_update || isVersion;
9798
useSaveDoc(doc.id, provider.document, !readOnly);
@@ -114,9 +115,9 @@ export const BlockNoteEditor = ({
114115
body,
115116
});
116117

117-
return `${mediaUrl()}${ret.file}`;
118+
return `${mediaUrl}${ret.file}`;
118119
},
119-
[createDocAttachment, doc.id],
120+
[createDocAttachment, doc.id, mediaUrl],
120121
);
121122

122123
const editor = useCreateBlockNote(

0 commit comments

Comments
 (0)