Skip to content
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

fix(tool-nuxt): add composables and fix types #78

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions space-plugins/nuxt-base/composables/useAppBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,5 +261,7 @@ export const useAppBridge = () => {
completed,
appBridgeAuth: appBridgeAuthStatus,
oauth: oauthStatus,
getSlug,
getParentHost,
};
};
3 changes: 3 additions & 0 deletions space-plugins/nuxt-base/utils/appBridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { useAppBridge } from '../composables/useAppBridge';

export type AppBridge = ReturnType<typeof useAppBridge>;
7 changes: 7 additions & 0 deletions space-plugins/nuxt-starter/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '#app' {
interface NuxtApp {
$appBridge: AppBridge;
}
}

export {};
10 changes: 3 additions & 7 deletions space-plugins/nuxt-starter/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<script setup lang="ts">
const config = useAppConfig();
const { completed, appBridgeAuth, oauth } = useAppBridge();
const appBridge = useAppBridge();
const nuxtApp = useNuxtApp();
nuxtApp.provide('appBridge', {
completed,
appBridgeAuth,
oauth,
});
nuxtApp.provide('appBridge', appBridge);
</script>

<template>
<slot v-if="!config.appBridge.enabled || completed" />
<slot v-if="!config.appBridge.enabled || appBridge.completed.value" />
Comment on lines +3 to +9
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there's a possibility to add more to the return of useAppBridge, I provide the whole appBridge instead of spreading and passing everything.

Because of that change, I had to put appBridge.completed.value in the v-if.

(Not appBridge.completed, because appBridge.completed is still a ref object there. Let me know if you don't understand why it's still not a ref object there)

</template>
9 changes: 0 additions & 9 deletions space-plugins/story-starter/starters/nuxt/index.d.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part is already moved to nuxt-base

This file was deleted.

2 changes: 1 addition & 1 deletion tool-plugins/nuxt-starter/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default defineAppConfig({
type: 'space-plugin',
type: 'tool-plugin',
appBridge: {
enabled: true,
oauth: true,
Expand Down
33 changes: 33 additions & 0 deletions tool-plugins/nuxt-starter/composables/useAutoHeight.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copied & modified from tool-plugins/nextjs-starter

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export function useAutoHeight() {
const nuxtApp = useNuxtApp();
let observer: MutationObserver | undefined;

onMounted(() => {
const observer = new MutationObserver(() => {
const slug = nuxtApp.$appBridge.getSlug();
const parentHost = nuxtApp.$appBridge.getParentHost();
window.parent.postMessage(
{
action: 'tool-changed',
tool: slug,
event: 'heightChange',
height: document.body.scrollHeight,
},
parentHost,
);
});

observer.observe(document.body, {
attributes: true,
childList: true,
subtree: true,
});
});

onUnmounted(() => {
if (observer) {
observer.disconnect();
observer = undefined;
}
});
}
38 changes: 38 additions & 0 deletions tool-plugins/nuxt-starter/composables/useStoryContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const useStoryContext = () => {
const nuxtApp = useNuxtApp();
const data = ref<StoryContext>();
const status = ref<'idle' | 'pending' | 'success'>('idle');

const listener = (event: MessageEvent<StoryContextWithAction>) => {
const { action, ...rest } = event.data;
if (action === 'get-context') {
data.value = rest;
status.value = 'success';
}
};

const refresh = () => {
const slug = nuxtApp.$appBridge.getSlug();
const parentHost = nuxtApp.$appBridge.getParentHost();
status.value = 'pending';
window.parent.postMessage(
{
action: 'tool-changed',
tool: slug,
event: 'getContext',
},
parentHost,
);
};

onMounted(() => {
window.addEventListener('message', listener);
refresh();
});

onUnmounted(() => {
window.removeEventListener('message', listener);
});

return { status, data, refresh };
};
7 changes: 7 additions & 0 deletions tool-plugins/nuxt-starter/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
declare module '#app' {
interface NuxtApp {
$appBridge: AppBridge;
}
}

export {};
12 changes: 5 additions & 7 deletions tool-plugins/nuxt-starter/layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<script setup lang="ts">
const config = useAppConfig();
const { completed, appBridgeAuth, oauth } = useAppBridge();
const appBridge = useAppBridge();
const nuxtApp = useNuxtApp();
nuxtApp.provide('appBridge', {
completed,
appBridgeAuth,
oauth,
});
nuxtApp.provide('appBridge', appBridge);

useAutoHeight();
</script>

<template>
<slot v-if="!config.appBridge.enabled || completed" />
<slot v-if="!config.appBridge.enabled || appBridge.completed.value" />
</template>
6 changes: 6 additions & 0 deletions tool-plugins/nuxt-starter/pages/index.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script setup lang="ts">
const { data } = await useFetch('/api/stories');
const alert = useAlert();
const { data: story } = useStoryContext();
</script>

<template>
Expand Down Expand Up @@ -33,6 +34,11 @@ const alert = useAlert();
</li>
</ul>
</div>

<div>
<p>Story Context</p>
<pre>{{ story }}</pre>
</div>
</div>
</div>
<BaseAlert
Expand Down
Loading