• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

EcrituresNumeriques / stylo / 28380938656

29 Jun 2026 02:51PM UTC coverage: 68.636% (-0.3%) from 68.902%
28380938656

push

github

web-flow
feat: refonte de l'entĂȘte principale (#2057)

936 of 1474 branches covered (63.5%)

Branch coverage included in aggregate %.

0 of 25 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

4942 of 7090 relevant lines covered (69.7%)

6.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.0
/front/src/hooks/workspace.js
1
import { useTranslation } from 'react-i18next'
2
import { useSelector } from 'react-redux'
3
import { useParams } from 'react-router'
4

5
import { executeQuery } from '../helpers/graphQL.js'
6
import useFetchData, { useMutateData } from './graphql.js'
7
import { usePreferenceItem } from './user.js'
8

9
import {
10
  create as createMutation,
11
  getWorkspaceMembers,
12
  getWorkspaces,
13
  inviteWorkspaceMember as inviteMemberMutation,
14
  leaveWorkspace as leaveMutation,
15
  removeWorkspaceMember as removeMemberMutation,
16
  updateFormMetadata as updateFormMetadataMutation,
17
} from './Workspaces.graphql'
18

19
/**
20
 * Returns the active workspace identifier
21
 *
22
 * In order of priority:
23
 * - URL
24
 * - redux store
25
 * @returns {string|undefined}
26
 */
27
export function useActiveWorkspaceId() {
28
  const { value } = usePreferenceItem('workspaceId', 'user')
×
29
  const { workspaceId } = useParams()
×
30

31
  return workspaceId ?? value
×
32
}
33

34
/**
35
 * Returns the active workspace identifier
36
 *
37
 * In order of priority:
38
 * - URL
39
 * - redux store
40
 * @returns {string|undefined}
41
 */
42
export function useActiveWorkspace() {
NEW
43
  const workspaceId = useActiveWorkspaceId()
×
NEW
44
  const { workspaces, isLoading } = useWorkspaces()
×
45

NEW
46
  return isLoading ? null : workspaces.find(({ _id }) => _id === workspaceId)
×
47
}
48

49
export function useWorkspaceMembersActions(workspaceId) {
50
  const { mutate: workspacesMutate } = useMutateData({
3✔
51
    query: getWorkspaces,
52
  })
53
  const sessionToken = useSelector((state) => state.sessionToken)
4✔
54
  const { data, mutate, error, isLoading } = useFetchData(
3✔
55
    { query: getWorkspaceMembers, variables: { workspaceId } },
56
    {
57
      revalidateOnFocus: false,
58
      revalidateOnReconnect: false,
59
    }
60
  )
61

62
  const updateMembersCount = async (result) => {
3✔
63
    await workspacesMutate(async (data) => ({
1✔
64
      workspaces: data?.workspaces?.map((w) => {
65
        if (w._id === workspaceId) {
×
66
          return {
×
67
            ...w,
68
            stats: {
69
              ...w.stats,
70
              membersCount: result.workspace.members.length,
71
            },
72
          }
73
        } else {
74
          return w
×
75
        }
76
      }),
77
    }))
78
  }
79

80
  const removeMember = async (user) => {
3✔
81
    const { _id: userId } = user
×
82
    await executeQuery({
×
83
      query: removeMemberMutation,
84
      variables: { workspaceId, userId },
85
      sessionToken,
86
    })
87
    const result = await mutate(
×
88
      async (data) => {
89
        return {
×
90
          workspace: {
91
            members: data.workspace.members.filter((m) => m._id !== userId),
×
92
          },
93
        }
94
      },
95
      { revalidate: false }
96
    )
97
    await updateMembersCount(result)
×
98
  }
99

100
  const inviteMember = async (user) => {
3✔
101
    const { _id: userId } = user
1✔
102
    await executeQuery({
1✔
103
      query: inviteMemberMutation,
104
      variables: { workspaceId, userId },
105
      sessionToken,
106
    })
107
    const result = await mutate(
1✔
108
      async (data) => {
109
        return {
1✔
110
          workspace: {
111
            members: [user, ...data.workspace.members],
112
          },
113
        }
114
      },
115
      { revalidate: false }
116
    )
117
    await updateMembersCount(result)
1✔
118
  }
119

120
  return {
3✔
121
    error,
122
    isLoading,
123
    members:
124
      data?.workspace?.members?.map((member) => ({
3✔
125
        ...member,
126
        selected: true,
127
      })) || [],
128
    inviteMember,
129
    removeMember,
130
  }
131
}
132

133
export function useWorkspaceActions() {
134
  const { mutate } = useMutateData({ query: getWorkspaces })
×
135
  const sessionToken = useSelector((state) => state.sessionToken)
×
136
  const addWorkspace = async (workspace) => {
×
137
    const result = await executeQuery({
×
138
      sessionToken,
139
      query: createMutation,
140
      variables: {
141
        data: {
142
          color: workspace.color,
143
          description: workspace.description,
144
          name: workspace.name,
145
        },
146
      },
147
    })
148
    await mutate(
×
149
      async (data) => ({
×
150
        workspaces: [result.createWorkspace, ...data.workspaces],
151
      }),
152
      { revalidate: false }
153
    )
154
  }
155

156
  const leaveWorkspace = async (workspaceId) => {
×
157
    await executeQuery({
×
158
      sessionToken,
159
      query: leaveMutation,
160
      variables: {
161
        workspaceId,
162
      },
163
    })
164
    await mutate(
×
165
      async (data) => ({
×
166
        workspaces: data.workspaces.filter((w) => w._id !== workspaceId),
×
167
      }),
168
      { revalidate: false }
169
    )
170
  }
171

172
  const updateFormMetadata = async (workspaceId, input) => {
×
173
    await executeQuery({
×
174
      sessionToken,
175
      query: updateFormMetadataMutation,
176
      variables: {
177
        workspaceId,
178
        input,
179
      },
180
    })
181
    await mutate(
×
182
      async (data) => ({
×
183
        workspaces: data.workspaces.map((w) => {
184
          if (w._id === workspaceId) {
×
185
            return {
×
186
              ...w,
187
              formMetadata: input,
188
            }
189
          } else {
190
            return w
×
191
          }
192
        }),
193
      }),
194
      { revalidate: false }
195
    )
196
  }
197

198
  return {
×
199
    addWorkspace,
200
    leaveWorkspace,
201
    updateFormMetadata,
202
  }
203
}
204

205
export function useWorkspaces() {
206
  const { data, error, isLoading } = useFetchData(
×
207
    {
208
      query: getWorkspaces,
209
    },
210
    {
211
      fallbackData: {
212
        workspaces: [],
213
      },
214
    }
215
  )
216

217
  return {
×
218
    workspaces: data?.workspaces,
219
    error,
220
    isLoading,
221
  }
222
}
223

224
export function useWorkspaceName({ workspace }) {
225
  const { t: tWorkspace } = useTranslation('workspace', { useSuspense: false })
×
226
  return workspace?.name ?? tWorkspace('myspace.name')
×
227
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc