Skip to content

Commit

Permalink
Merge pull request #106 from kloudlite/feature/update-event
Browse files Browse the repository at this point in the history
✨ Added resource update event listner hook
  • Loading branch information
abdheshnayak authored Mar 1, 2024
2 parents 4260659 + 1bb73ef commit 3b752db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/client/helpers/socket/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface ISocketResp<T = any> {
}

type IData = {
event?: string;
event?: 'subscribe' | 'unsubscribe';
id: string;
};

Expand Down
36 changes: 36 additions & 0 deletions lib/client/helpers/socket/useWatch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useEffect } from 'react';
import { ISocketResp, useSubscribe } from './context';
import { useReload } from '../reloader';

export const useSocketWatch = (
onUpdate: (v: ISocketResp<any>[]) => void,
topic: string
) => {
const { responses, subscribed } = useSubscribe(
{
for: 'resource-update',
data: {
id: topic,
respath: topic,
},
},
[]
);

useEffect(() => {
if (subscribed) {
onUpdate(responses);
}
}, [responses]);
};

export const useWatchReload = (topic: string) => {
const reloadPage = useReload();
useSocketWatch((rd) => {
console.log(rd);
if (rd.find((v) => v.id === topic)) {
console.log('reloading due to watch event', rd);
reloadPage();
}
}, topic);
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GearSix } from '@jengaicons/react';
import { Link, useParams } from '@remix-run/react';
import { Link, useOutletContext, useParams } from '@remix-run/react';
import { generateKey, titleCase } from '~/components/utils';
import ConsoleAvatar from '~/console/components/console-avatar';
import {
Expand All @@ -20,6 +20,8 @@ import {
parseUpdateOrCreatedBy,
parseUpdateOrCreatedOn,
} from '~/console/server/r-utils/common';
import { useWatchReload } from '~/root/lib/client/helpers/socket/useWatch';
import { IAccountContext } from '../_layout';

type BaseType = ExtractNodeType<IProjects>;
const RESOURCE_NAME = 'project';
Expand Down Expand Up @@ -156,6 +158,8 @@ const ListView = ({ items }: { items: BaseType[] }) => {
};

const ProjectResources = ({ items = [] }: { items: BaseType[] }) => {
const { account } = useOutletContext<IAccountContext>();
useWatchReload(`account:${parseName(account)}`);
return (
<ListGridView
listView={<ListView items={items} />}
Expand Down

0 comments on commit 3b752db

Please sign in to comment.