layout.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use client';
  2. import './style.scss';
  3. import { useState, useEffect, useCallback, type ReactNode } from 'react';
  4. import { fetchApi } from '@/lib/utils/client';
  5. import { useStudioContext } from '@/app/studio/context';
  6. import type { CrewWidgetConfigResponse, CrewWidgetConfigItem } from '@/types/response/crew/widgetConfig';
  7. import { CrewWidgetConfigContext } from './context';
  8. export default function CrewWidgetLayout({ children }: { children: ReactNode })
  9. {
  10. const { channelID } = useStudioContext();
  11. const [items, setItems] = useState<CrewWidgetConfigItem[]>([]);
  12. const [loading, setLoading] = useState(true);
  13. const [saving, setSaving] = useState(false);
  14. const fetchList = useCallback(() => {
  15. if (!channelID) {
  16. setLoading(false);
  17. return;
  18. }
  19. setLoading(true);
  20. fetchApi<CrewWidgetConfigResponse>(`/api/studio/crew/widget/config/${channelID}`)
  21. .then(res => setItems(res.data?.list ?? []))
  22. .catch(() => {})
  23. .finally(() => setLoading(false)
  24. );
  25. }, [channelID]);
  26. useEffect(() => {
  27. fetchList();
  28. }, [fetchList]);
  29. if (!channelID) {
  30. return (
  31. <div className="studio-page">
  32. <p className="studio-page__empty">채널을 먼저 연동해 주세요.</p>
  33. </div>
  34. );
  35. }
  36. return (
  37. <CrewWidgetConfigContext.Provider value={{ items, loading, saving, setSaving, fetchList }}>
  38. {children}
  39. </CrewWidgetConfigContext.Provider>
  40. );
  41. }